| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_loop.h" | 5 #include "base/compiler_specific.h" |
| 6 #include "base/strings/string16.h" | 6 #include "base/strings/string16.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search_engines/template_url.h" | 9 #include "chrome/browser/search_engines/template_url.h" |
| 11 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_factory.h" | 11 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 13 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" | 12 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" |
| 14 #include "chrome/browser/ui/search_engines/template_url_table_model.h" | 13 #include "chrome/browser/ui/search_engines/template_url_table_model.h" |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "chrome/test/base/testing_pref_service_syncable.h" | |
| 17 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/browser/notification_service.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "ui/base/models/table_model_observer.h" | 16 #include "ui/base/models/table_model_observer.h" |
| 21 | 17 |
| 22 using base::ASCIIToUTF16; | 18 using base::ASCIIToUTF16; |
| 23 | 19 |
| 24 static const base::string16 kA(ASCIIToUTF16("a")); | 20 static const base::string16 kA(ASCIIToUTF16("a")); |
| 25 static const base::string16 kA1(ASCIIToUTF16("a1")); | 21 static const base::string16 kA1(ASCIIToUTF16("a1")); |
| 26 static const base::string16 kB(ASCIIToUTF16("b")); | 22 static const base::string16 kB(ASCIIToUTF16("b")); |
| 27 static const base::string16 kB1(ASCIIToUTF16("b1")); | 23 static const base::string16 kB1(ASCIIToUTF16("b1")); |
| 28 | 24 |
| 29 // Base class for keyword editor tests. Creates a profile containing an | 25 // Base class for keyword editor tests. Creates a profile containing an |
| 30 // empty TemplateURLService. | 26 // empty TemplateURLService. |
| 31 class KeywordEditorControllerTest : public testing::Test, | 27 class KeywordEditorControllerTest : public testing::Test, |
| 32 public ui::TableModelObserver { | 28 public ui::TableModelObserver { |
| 33 public: | 29 public: |
| 34 // Initializes all of the state. | 30 KeywordEditorControllerTest() |
| 35 void Init(bool simulate_load_failure); | 31 : simulate_load_failure_(false), |
| 32 model_changed_count_(0), |
| 33 items_changed_count_(0), |
| 34 added_count_(0), |
| 35 removed_count_(0) {} |
| 36 | 36 |
| 37 virtual void SetUp() { | 37 explicit KeywordEditorControllerTest(bool simulate_load_failure) |
| 38 Init(false); | 38 : simulate_load_failure_(simulate_load_failure), |
| 39 model_changed_count_(0), |
| 40 items_changed_count_(0), |
| 41 added_count_(0), |
| 42 removed_count_(0) {} |
| 43 |
| 44 virtual void SetUp() OVERRIDE { |
| 45 util_.SetUp(); |
| 46 |
| 47 if (simulate_load_failure_) |
| 48 util_.model()->OnWebDataServiceRequestDone(0, NULL); |
| 49 else |
| 50 util_.ChangeModelToLoadState(); |
| 51 |
| 52 controller_.reset(new KeywordEditorController(util_.profile())); |
| 53 controller_->table_model()->SetObserver(this); |
| 54 } |
| 55 |
| 56 virtual void TearDown() OVERRIDE { |
| 57 controller_.reset(); |
| 58 util_.TearDown(); |
| 39 } | 59 } |
| 40 | 60 |
| 41 virtual void OnModelChanged() OVERRIDE { | 61 virtual void OnModelChanged() OVERRIDE { |
| 42 model_changed_count_++; | 62 model_changed_count_++; |
| 43 } | 63 } |
| 44 | 64 |
| 45 virtual void OnItemsChanged(int start, int length) OVERRIDE { | 65 virtual void OnItemsChanged(int start, int length) OVERRIDE { |
| 46 items_changed_count_++; | 66 items_changed_count_++; |
| 47 } | 67 } |
| 48 | 68 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 62 ASSERT_EQ(removed_count, removed_count_); | 82 ASSERT_EQ(removed_count, removed_count_); |
| 63 ClearChangeCount(); | 83 ClearChangeCount(); |
| 64 } | 84 } |
| 65 | 85 |
| 66 void ClearChangeCount() { | 86 void ClearChangeCount() { |
| 67 model_changed_count_ = items_changed_count_ = added_count_ = | 87 model_changed_count_ = items_changed_count_ = added_count_ = |
| 68 removed_count_ = 0; | 88 removed_count_ = 0; |
| 69 } | 89 } |
| 70 | 90 |
| 71 void SimulateDefaultSearchIsManaged(const std::string& url) { | 91 void SimulateDefaultSearchIsManaged(const std::string& url) { |
| 72 ASSERT_FALSE(url.empty()); | 92 util_.SetManagedDefaultSearchPreferences(true, |
| 73 TestingPrefServiceSyncable* service = profile_->GetTestingPrefService(); | 93 "managed", |
| 74 service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, | 94 "managed", |
| 75 new base::FundamentalValue(true)); | 95 url, |
| 76 service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, | 96 std::string(), |
| 77 new base::StringValue(url)); | 97 std::string(), |
| 78 service->SetManagedPref(prefs::kDefaultSearchProviderName, | 98 std::string(), |
| 79 new base::StringValue("managed")); | 99 std::string(), |
| 80 service->SetManagedPref(prefs::kDefaultSearchProviderKeyword, | 100 std::string()); |
| 81 new base::StringValue("managed")); | |
| 82 // Clear the IDs that are not specified via policy. | |
| 83 service->SetManagedPref(prefs::kDefaultSearchProviderID, | |
| 84 new base::StringValue(std::string())); | |
| 85 service->SetManagedPref(prefs::kDefaultSearchProviderPrepopulateID, | |
| 86 new base::StringValue(std::string())); | |
| 87 model_->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, | |
| 88 content::NotificationService::AllSources(), | |
| 89 content::NotificationService::NoDetails()); | |
| 90 } | 101 } |
| 91 | 102 |
| 92 TemplateURLTableModel* table_model() const { | 103 TemplateURLTableModel* table_model() { return controller_->table_model(); } |
| 93 return controller_->table_model(); | 104 KeywordEditorController* controller() { return controller_.get(); } |
| 94 } | 105 const TemplateURLServiceTestUtil* util() const { return &util_; } |
| 95 | 106 |
| 96 protected: | 107 int items_changed_count() const { return items_changed_count_; } |
| 97 base::MessageLoopForUI message_loop_; | 108 int added_count() const { return added_count_; } |
| 98 scoped_ptr<TestingProfile> profile_; | 109 int removed_count() const { return removed_count_; } |
| 110 |
| 111 private: |
| 99 scoped_ptr<KeywordEditorController> controller_; | 112 scoped_ptr<KeywordEditorController> controller_; |
| 100 TemplateURLService* model_; | 113 TemplateURLServiceTestUtil util_; |
| 114 bool simulate_load_failure_; |
| 101 | 115 |
| 102 int model_changed_count_; | 116 int model_changed_count_; |
| 103 int items_changed_count_; | 117 int items_changed_count_; |
| 104 int added_count_; | 118 int added_count_; |
| 105 int removed_count_; | 119 int removed_count_; |
| 106 }; | 120 }; |
| 107 | 121 |
| 108 void KeywordEditorControllerTest::Init(bool simulate_load_failure) { | 122 class KeywordEditorControllerNoWebDataTest |
| 109 ClearChangeCount(); | 123 : public KeywordEditorControllerTest { |
| 110 | 124 public: |
| 111 // If init is called twice, make sure that the controller is destroyed before | 125 KeywordEditorControllerNoWebDataTest() : KeywordEditorControllerTest(true) {} |
| 112 // the profile is. | 126 }; |
| 113 controller_.reset(); | |
| 114 profile_.reset(new TestingProfile()); | |
| 115 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 116 profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor); | |
| 117 | |
| 118 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get()); | |
| 119 if (simulate_load_failure) | |
| 120 model_->OnWebDataServiceRequestDone(0, NULL); | |
| 121 | |
| 122 controller_.reset(new KeywordEditorController(profile_.get())); | |
| 123 controller_->table_model()->SetObserver(this); | |
| 124 } | |
| 125 | 127 |
| 126 // Tests adding a TemplateURL. | 128 // Tests adding a TemplateURL. |
| 127 TEST_F(KeywordEditorControllerTest, Add) { | 129 TEST_F(KeywordEditorControllerTest, Add) { |
| 128 controller_->AddTemplateURL(kA, kB, "http://c"); | 130 int original_row_count = table_model()->RowCount(); |
| 131 controller()->AddTemplateURL(kA, kB, "http://c"); |
| 129 | 132 |
| 130 // Verify the observer was notified. | 133 // Verify the observer was notified. |
| 131 VerifyChangeCount(0, 0, 1, 0); | 134 VerifyChangeCount(0, 0, 1, 0); |
| 132 if (HasFatalFailure()) | 135 if (HasFatalFailure()) |
| 133 return; | 136 return; |
| 134 | 137 |
| 135 // Verify the TableModel has the new data. | 138 // Verify the TableModel has the new data. |
| 136 ASSERT_EQ(1, table_model()->RowCount()); | 139 ASSERT_EQ(original_row_count + 1, table_model()->RowCount()); |
| 137 | 140 |
| 138 // Verify the TemplateURLService has the new entry. | 141 // Verify the TemplateURLService has the new data. |
| 139 ASSERT_EQ(1U, model_->GetTemplateURLs().size()); | 142 const TemplateURL* turl = util()->model()->GetTemplateURLForKeyword(kB); |
| 140 | 143 ASSERT_TRUE(turl); |
| 141 // Verify the entry is what we added. | |
| 142 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | |
| 143 EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name()); | 144 EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name()); |
| 144 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); | 145 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); |
| 145 EXPECT_EQ("http://c", turl->url()); | 146 EXPECT_EQ("http://c", turl->url()); |
| 146 } | 147 } |
| 147 | 148 |
| 148 // Tests modifying a TemplateURL. | 149 // Tests modifying a TemplateURL. |
| 149 TEST_F(KeywordEditorControllerTest, Modify) { | 150 TEST_F(KeywordEditorControllerTest, Modify) { |
| 150 controller_->AddTemplateURL(kA, kB, "http://c"); | 151 controller()->AddTemplateURL(kA, kB, "http://c"); |
| 151 ClearChangeCount(); | 152 ClearChangeCount(); |
| 152 | 153 |
| 153 // Modify the entry. | 154 // Modify the entry. |
| 154 TemplateURL* turl = model_->GetTemplateURLs()[0]; | 155 TemplateURL* turl = util()->model()->GetTemplateURLs()[0]; |
| 155 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1"); | 156 controller()->ModifyTemplateURL(turl, kA1, kB1, "http://c1"); |
| 156 | 157 |
| 157 // Make sure it was updated appropriately. | 158 // Make sure it was updated appropriately. |
| 158 VerifyChangeCount(0, 1, 0, 0); | 159 VerifyChangeCount(0, 1, 0, 0); |
| 159 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); | 160 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); |
| 160 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); | 161 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); |
| 161 EXPECT_EQ("http://c1", turl->url()); | 162 EXPECT_EQ("http://c1", turl->url()); |
| 162 } | 163 } |
| 163 | 164 |
| 164 // Tests making a TemplateURL the default search provider. | 165 // Tests making a TemplateURL the default search provider. |
| 165 TEST_F(KeywordEditorControllerTest, MakeDefault) { | 166 TEST_F(KeywordEditorControllerTest, MakeDefault) { |
| 166 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 167 int index = controller()->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 167 ClearChangeCount(); | 168 ClearChangeCount(); |
| 168 | 169 |
| 169 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | 170 const TemplateURL* turl = util()->model()->GetTemplateURLForKeyword(kB); |
| 170 int new_default = controller_->MakeDefaultTemplateURL(0); | 171 int new_default = controller()->MakeDefaultTemplateURL(index); |
| 171 EXPECT_EQ(0, new_default); | 172 EXPECT_EQ(index, new_default); |
| 172 // Making an item the default sends a handful of changes. Which are sent isn't | 173 // Making an item the default sends a handful of changes. Which are sent isn't |
| 173 // important, what is important is 'something' is sent. | 174 // important, what is important is 'something' is sent. |
| 174 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || | 175 ASSERT_TRUE(items_changed_count() > 0 || added_count() > 0 || |
| 175 removed_count_ > 0); | 176 removed_count() > 0); |
| 176 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); | 177 ASSERT_TRUE(util()->model()->GetDefaultSearchProvider() == turl); |
| 177 | 178 |
| 178 // Making it default a second time should fail. | 179 // Making it default a second time should fail. |
| 179 new_default = controller_->MakeDefaultTemplateURL(0); | 180 new_default = controller()->MakeDefaultTemplateURL(index); |
| 180 EXPECT_EQ(-1, new_default); | 181 EXPECT_EQ(-1, new_default); |
| 181 } | 182 } |
| 182 | 183 |
| 183 // Tests that a TemplateURL can't be made the default if the default search | 184 // Tests that a TemplateURL can't be made the default if the default search |
| 184 // provider is managed via policy. | 185 // provider is managed via policy. |
| 185 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { | 186 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { |
| 186 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 187 controller()->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 187 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); | 188 controller()->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); |
| 188 ClearChangeCount(); | 189 ClearChangeCount(); |
| 189 | 190 |
| 190 const TemplateURL* turl1 = | 191 const TemplateURL* turl1 = |
| 191 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b")); | 192 util()->model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); |
| 192 ASSERT_TRUE(turl1 != NULL); | 193 ASSERT_TRUE(turl1 != NULL); |
| 193 const TemplateURL* turl2 = | 194 const TemplateURL* turl2 = |
| 194 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1")); | 195 util()->model()->GetTemplateURLForKeyword(ASCIIToUTF16("b1")); |
| 195 ASSERT_TRUE(turl2 != NULL); | 196 ASSERT_TRUE(turl2 != NULL); |
| 196 | 197 |
| 197 EXPECT_TRUE(controller_->CanMakeDefault(turl1)); | 198 EXPECT_TRUE(controller()->CanMakeDefault(turl1)); |
| 198 EXPECT_TRUE(controller_->CanMakeDefault(turl2)); | 199 EXPECT_TRUE(controller()->CanMakeDefault(turl2)); |
| 199 | 200 |
| 200 SimulateDefaultSearchIsManaged(turl2->url()); | 201 SimulateDefaultSearchIsManaged(turl2->url()); |
| 201 EXPECT_TRUE(model_->is_default_search_managed()); | 202 EXPECT_TRUE(util()->model()->is_default_search_managed()); |
| 202 | 203 |
| 203 EXPECT_FALSE(controller_->CanMakeDefault(turl1)); | 204 EXPECT_FALSE(controller()->CanMakeDefault(turl1)); |
| 204 EXPECT_FALSE(controller_->CanMakeDefault(turl2)); | 205 EXPECT_FALSE(controller()->CanMakeDefault(turl2)); |
| 205 } | 206 } |
| 206 | 207 |
| 207 // Tests that a TemplateURL can't be edited if it is the managed default search | 208 // Tests that a TemplateURL can't be edited if it is the managed default search |
| 208 // provider. | 209 // provider. |
| 209 TEST_F(KeywordEditorControllerTest, EditManagedDefault) { | 210 TEST_F(KeywordEditorControllerTest, EditManagedDefault) { |
| 210 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 211 controller()->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 211 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); | 212 controller()->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); |
| 212 ClearChangeCount(); | 213 ClearChangeCount(); |
| 213 | 214 |
| 214 const TemplateURL* turl1 = | 215 const TemplateURL* turl1 = |
| 215 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b")); | 216 util()->model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); |
| 216 ASSERT_TRUE(turl1 != NULL); | 217 ASSERT_TRUE(turl1 != NULL); |
| 217 const TemplateURL* turl2 = | 218 const TemplateURL* turl2 = |
| 218 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1")); | 219 util()->model()->GetTemplateURLForKeyword(ASCIIToUTF16("b1")); |
| 219 ASSERT_TRUE(turl2 != NULL); | 220 ASSERT_TRUE(turl2 != NULL); |
| 220 | 221 |
| 221 EXPECT_TRUE(controller_->CanEdit(turl1)); | 222 EXPECT_TRUE(controller()->CanEdit(turl1)); |
| 222 EXPECT_TRUE(controller_->CanEdit(turl2)); | 223 EXPECT_TRUE(controller()->CanEdit(turl2)); |
| 223 | 224 |
| 224 // Simulate setting a managed default. This will add another template URL to | 225 // Simulate setting a managed default. This will add another template URL to |
| 225 // the model. | 226 // the model. |
| 226 SimulateDefaultSearchIsManaged(turl2->url()); | 227 SimulateDefaultSearchIsManaged(turl2->url()); |
| 227 EXPECT_TRUE(model_->is_default_search_managed()); | 228 EXPECT_TRUE(util()->model()->is_default_search_managed()); |
| 228 EXPECT_TRUE(controller_->CanEdit(turl1)); | 229 EXPECT_TRUE(controller()->CanEdit(turl1)); |
| 229 EXPECT_TRUE(controller_->CanEdit(turl2)); | 230 EXPECT_TRUE(controller()->CanEdit(turl2)); |
| 230 EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider())); | 231 EXPECT_FALSE( |
| 232 controller()->CanEdit(util()->model()->GetDefaultSearchProvider())); |
| 231 } | 233 } |
| 232 | 234 |
| 233 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) { | 235 TEST_F(KeywordEditorControllerNoWebDataTest, MakeDefaultNoWebData) { |
| 234 // Simulate a failure to load Web Data. | 236 int index = controller()->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 235 Init(true); | |
| 236 | |
| 237 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | |
| 238 ClearChangeCount(); | 237 ClearChangeCount(); |
| 239 | 238 |
| 240 // This should not result in a crash. | 239 // This should not result in a crash. |
| 241 int new_default = controller_->MakeDefaultTemplateURL(0); | 240 int new_default = controller()->MakeDefaultTemplateURL(index); |
| 242 EXPECT_EQ(0, new_default); | 241 EXPECT_EQ(index, new_default); |
| 243 } | 242 } |
| 244 | 243 |
| 245 // Mutates the TemplateURLService and make sure table model is updating | 244 // Mutates the TemplateURLService and make sure table model is updating |
| 246 // appropriately. | 245 // appropriately. |
| 247 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) { | 246 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) { |
| 247 int original_row_count = table_model()->RowCount(); |
| 248 |
| 248 TemplateURLData data; | 249 TemplateURLData data; |
| 249 data.short_name = ASCIIToUTF16("b"); | 250 data.short_name = ASCIIToUTF16("b"); |
| 250 data.SetKeyword(ASCIIToUTF16("a")); | 251 data.SetKeyword(ASCIIToUTF16("a")); |
| 251 TemplateURL* turl = new TemplateURL(profile_.get(), data); | 252 TemplateURL* turl = new TemplateURL(util()->profile(), data); |
| 252 model_->Add(turl); | 253 util()->model()->Add(turl); |
| 253 | 254 |
| 254 // Table model should have updated. | 255 // Table model should have updated. |
| 255 VerifyChangeCount(1, 0, 0, 0); | 256 VerifyChangeCount(1, 0, 0, 0); |
| 256 | 257 |
| 257 // And should contain the newly added TemplateURL. | 258 // And should contain the newly added TemplateURL. |
| 258 ASSERT_EQ(1, table_model()->RowCount()); | 259 ASSERT_EQ(original_row_count + 1, table_model()->RowCount()); |
| 259 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); | 260 ASSERT_GE(table_model()->IndexOfTemplateURL(turl), 0); |
| 260 } | 261 } |
| OLD | NEW |