| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file |
| 4 |
| 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/browser/protector/composite_settings_change.h" |
| 7 #include "chrome/browser/protector/mock_setting_change.h" |
| 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "grit/generated_resources.h" |
| 11 #include "ui/base/l10n/l10n_util.h" |
| 12 |
| 13 using ::testing::NiceMock; |
| 14 using ::testing::Return; |
| 15 |
| 16 namespace protector { |
| 17 |
| 18 namespace { |
| 19 |
| 20 string16 kTitle1 = ASCIIToUTF16("A"); |
| 21 string16 kTitle2 = ASCIIToUTF16("B"); |
| 22 string16 kTitle3 = ASCIIToUTF16("C"); |
| 23 |
| 24 }; |
| 25 |
| 26 class CompositeSettingsChangeTest : public testing::Test { |
| 27 protected: |
| 28 string16 GetApplyButtonText(const string16& apply_text) { |
| 29 return l10n_util::GetStringFUTF16(IDS_CHANGE_SETTING, apply_text); |
| 30 } |
| 31 |
| 32 TestingProfile profile_; |
| 33 }; |
| 34 |
| 35 // Verify that MergeWith works and Apply/Discard/Timeout call same functions |
| 36 // of all merged changes. |
| 37 TEST_F(CompositeSettingsChangeTest, MergeWith) { |
| 38 // These instances will be owned by CompositeSettingsChange. |
| 39 MockSettingChange* change1 = new NiceMock<MockSettingChange>; |
| 40 MockSettingChange* change2 = new NiceMock<MockSettingChange>; |
| 41 MockSettingChange* change3 = new NiceMock<MockSettingChange>; |
| 42 |
| 43 EXPECT_CALL(*change1, MockInit(&profile_)).WillOnce(Return(true)); |
| 44 ASSERT_TRUE(change1->Init(&profile_)); |
| 45 EXPECT_CALL(*change2, MockInit(&profile_)).WillOnce(Return(true)); |
| 46 ASSERT_TRUE(change2->Init(&profile_)); |
| 47 EXPECT_CALL(*change3, MockInit(&profile_)).WillOnce(Return(true)); |
| 48 ASSERT_TRUE(change3->Init(&profile_)); |
| 49 |
| 50 // Compose 1st and 2nd changes together. |
| 51 scoped_ptr<CompositeSettingsChange> composite_change( |
| 52 change1->MergeWith(change2)); |
| 53 |
| 54 EXPECT_TRUE(composite_change->Contains(change1)); |
| 55 EXPECT_TRUE(composite_change->Contains(change2)); |
| 56 EXPECT_FALSE(composite_change->Contains(change3)); |
| 57 EXPECT_TRUE(composite_change->Contains(composite_change.get())); |
| 58 |
| 59 // Add third change. |
| 60 EXPECT_EQ(composite_change.get(), composite_change->MergeWith(change3)); |
| 61 |
| 62 EXPECT_TRUE(composite_change->Contains(change1)); |
| 63 EXPECT_TRUE(composite_change->Contains(change2)); |
| 64 EXPECT_TRUE(composite_change->Contains(change3)); |
| 65 EXPECT_TRUE(composite_change->Contains(composite_change.get())); |
| 66 |
| 67 // Apply/Discard/Timeout calls should be proxied to inner change instances: |
| 68 EXPECT_CALL(*change1, Apply(NULL)); |
| 69 EXPECT_CALL(*change2, Apply(NULL)); |
| 70 EXPECT_CALL(*change3, Apply(NULL)); |
| 71 composite_change->Apply(NULL); |
| 72 |
| 73 EXPECT_CALL(*change1, Discard(NULL)); |
| 74 EXPECT_CALL(*change2, Discard(NULL)); |
| 75 EXPECT_CALL(*change3, Discard(NULL)); |
| 76 composite_change->Discard(NULL); |
| 77 |
| 78 EXPECT_CALL(*change1, Timeout()); |
| 79 EXPECT_CALL(*change2, Timeout()); |
| 80 EXPECT_CALL(*change3, Timeout()); |
| 81 composite_change->Timeout(); |
| 82 } |
| 83 |
| 84 TEST_F(CompositeSettingsChangeTest, ApplyButtonText) { |
| 85 // These instances will be owned by CompositeSettingsChange. |
| 86 MockSettingChange* change1 = new NiceMock<MockSettingChange>; |
| 87 MockSettingChange* change2 = new NiceMock<MockSettingChange>; |
| 88 MockSettingChange* change3 = new NiceMock<MockSettingChange>; |
| 89 |
| 90 EXPECT_CALL(*change1, MockInit(&profile_)).WillOnce(Return(true)); |
| 91 ASSERT_TRUE(change1->Init(&profile_)); |
| 92 EXPECT_CALL(*change2, MockInit(&profile_)).WillOnce(Return(true)); |
| 93 ASSERT_TRUE(change2->Init(&profile_)); |
| 94 EXPECT_CALL(*change3, MockInit(&profile_)).WillOnce(Return(true)); |
| 95 ASSERT_TRUE(change3->Init(&profile_)); |
| 96 |
| 97 // |change1| has higher priority. |
| 98 EXPECT_CALL(*change1, GetApplyDisplayName()).WillOnce( |
| 99 Return(BaseSettingChange::DisplayName(10, kTitle1))); |
| 100 EXPECT_CALL(*change2, GetApplyDisplayName()).WillOnce( |
| 101 Return(BaseSettingChange::DisplayName(5, kTitle2))); |
| 102 |
| 103 scoped_ptr<CompositeSettingsChange> composite_change( |
| 104 change1->MergeWith(change2)); |
| 105 |
| 106 EXPECT_EQ(GetApplyButtonText(kTitle1), |
| 107 composite_change->GetApplyButtonText()); |
| 108 |
| 109 // |change3| has the highest priority now. |
| 110 EXPECT_CALL(*change3, GetApplyDisplayName()).WillOnce( |
| 111 Return(BaseSettingChange::DisplayName(15, kTitle3))); |
| 112 EXPECT_EQ(composite_change.get(), composite_change->MergeWith(change3)); |
| 113 |
| 114 EXPECT_EQ(GetApplyButtonText(kTitle3), |
| 115 composite_change->GetApplyButtonText()); |
| 116 |
| 117 EXPECT_CALL(*change1, GetApplyKeyword()).WillOnce(Return("K1")); |
| 118 EXPECT_EQ("K1", composite_change->GetApplyKeyword()); |
| 119 } |
| 120 |
| 121 } // namespace protector |
| OLD | NEW |