| 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/message_loop/message_loop.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 "base/values.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/search_engines/default_search_manager.h" |
| 10 #include "chrome/browser/search_engines/template_url.h" | 12 #include "chrome/browser/search_engines/template_url.h" |
| 11 #include "chrome/browser/search_engines/template_url_service.h" | 13 #include "chrome/browser/search_engines/template_url_service.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 13 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" | 15 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" |
| 14 #include "chrome/browser/ui/search_engines/template_url_table_model.h" | 16 #include "chrome/browser/ui/search_engines/template_url_table_model.h" |
| 15 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_pref_service_syncable.h" | 18 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 } | 66 } |
| 65 | 67 |
| 66 void ClearChangeCount() { | 68 void ClearChangeCount() { |
| 67 model_changed_count_ = items_changed_count_ = added_count_ = | 69 model_changed_count_ = items_changed_count_ = added_count_ = |
| 68 removed_count_ = 0; | 70 removed_count_ = 0; |
| 69 } | 71 } |
| 70 | 72 |
| 71 void SimulateDefaultSearchIsManaged(const std::string& url) { | 73 void SimulateDefaultSearchIsManaged(const std::string& url) { |
| 72 ASSERT_FALSE(url.empty()); | 74 ASSERT_FALSE(url.empty()); |
| 73 TestingPrefServiceSyncable* service = profile_->GetTestingPrefService(); | 75 TestingPrefServiceSyncable* service = profile_->GetTestingPrefService(); |
| 74 service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, | 76 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 75 new base::FundamentalValue(true)); | 77 value->Set(DefaultSearchManager::kShortName, |
| 76 service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, | 78 base::Value::CreateStringValue("managed")); |
| 77 new base::StringValue(url)); | 79 value->Set(DefaultSearchManager::kKeyword, |
| 78 service->SetManagedPref(prefs::kDefaultSearchProviderName, | 80 base::Value::CreateStringValue("managed")); |
| 79 new base::StringValue("managed")); | 81 value->Set(DefaultSearchManager::kURL, |
| 80 service->SetManagedPref(prefs::kDefaultSearchProviderKeyword, | 82 base::Value::CreateStringValue(url)); |
| 81 new base::StringValue("managed")); | 83 service->SetManagedPref( |
| 82 // Clear the IDs that are not specified via policy. | 84 DefaultSearchManager::kDefaultSearchProviderDataPrefName, |
| 83 service->SetManagedPref(prefs::kDefaultSearchProviderID, | 85 value.release()); |
| 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 } | 86 } |
| 91 | 87 |
| 92 TemplateURLTableModel* table_model() const { | 88 TemplateURLTableModel* table_model() const { |
| 93 return controller_->table_model(); | 89 return controller_->table_model(); |
| 94 } | 90 } |
| 95 | 91 |
| 96 protected: | 92 protected: |
| 97 base::MessageLoopForUI message_loop_; | 93 base::MessageLoopForUI message_loop_; |
| 98 scoped_ptr<TestingProfile> profile_; | 94 scoped_ptr<TestingProfile> profile_; |
| 99 scoped_ptr<KeywordEditorController> controller_; | 95 scoped_ptr<KeywordEditorController> controller_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 118 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get()); | 114 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get()); |
| 119 if (simulate_load_failure) | 115 if (simulate_load_failure) |
| 120 model_->OnWebDataServiceRequestDone(0, NULL); | 116 model_->OnWebDataServiceRequestDone(0, NULL); |
| 121 | 117 |
| 122 controller_.reset(new KeywordEditorController(profile_.get())); | 118 controller_.reset(new KeywordEditorController(profile_.get())); |
| 123 controller_->table_model()->SetObserver(this); | 119 controller_->table_model()->SetObserver(this); |
| 124 } | 120 } |
| 125 | 121 |
| 126 // Tests adding a TemplateURL. | 122 // Tests adding a TemplateURL. |
| 127 TEST_F(KeywordEditorControllerTest, Add) { | 123 TEST_F(KeywordEditorControllerTest, Add) { |
| 124 int original_row_count = table_model()->RowCount(); |
| 128 controller_->AddTemplateURL(kA, kB, "http://c"); | 125 controller_->AddTemplateURL(kA, kB, "http://c"); |
| 129 | 126 |
| 130 // Verify the observer was notified. | 127 // Verify the observer was notified. |
| 131 VerifyChangeCount(0, 0, 1, 0); | 128 VerifyChangeCount(0, 0, 1, 0); |
| 132 if (HasFatalFailure()) | 129 if (HasFatalFailure()) |
| 133 return; | 130 return; |
| 134 | 131 |
| 135 // Verify the TableModel has the new data. | 132 // Verify the TableModel has the new data. |
| 136 ASSERT_EQ(1, table_model()->RowCount()); | 133 ASSERT_EQ(original_row_count + 1, table_model()->RowCount()); |
| 137 | 134 |
| 138 // Verify the TemplateURLService has the new entry. | 135 // Verify the TemplateURLService has the new data. |
| 139 ASSERT_EQ(1U, model_->GetTemplateURLs().size()); | 136 const TemplateURL* turl = model_->GetTemplateURLForKeyword(kB); |
| 140 | 137 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()); | 138 EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name()); |
| 144 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); | 139 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); |
| 145 EXPECT_EQ("http://c", turl->url()); | 140 EXPECT_EQ("http://c", turl->url()); |
| 146 } | 141 } |
| 147 | 142 |
| 148 // Tests modifying a TemplateURL. | 143 // Tests modifying a TemplateURL. |
| 149 TEST_F(KeywordEditorControllerTest, Modify) { | 144 TEST_F(KeywordEditorControllerTest, Modify) { |
| 150 controller_->AddTemplateURL(kA, kB, "http://c"); | 145 controller_->AddTemplateURL(kA, kB, "http://c"); |
| 151 ClearChangeCount(); | 146 ClearChangeCount(); |
| 152 | 147 |
| 153 // Modify the entry. | 148 // Modify the entry. |
| 154 TemplateURL* turl = model_->GetTemplateURLs()[0]; | 149 TemplateURL* turl = model_->GetTemplateURLs()[0]; |
| 155 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1"); | 150 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1"); |
| 156 | 151 |
| 157 // Make sure it was updated appropriately. | 152 // Make sure it was updated appropriately. |
| 158 VerifyChangeCount(0, 1, 0, 0); | 153 VerifyChangeCount(0, 1, 0, 0); |
| 159 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); | 154 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); |
| 160 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); | 155 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); |
| 161 EXPECT_EQ("http://c1", turl->url()); | 156 EXPECT_EQ("http://c1", turl->url()); |
| 162 } | 157 } |
| 163 | 158 |
| 164 // Tests making a TemplateURL the default search provider. | 159 // Tests making a TemplateURL the default search provider. |
| 165 TEST_F(KeywordEditorControllerTest, MakeDefault) { | 160 TEST_F(KeywordEditorControllerTest, MakeDefault) { |
| 166 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 161 int index = controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 167 ClearChangeCount(); | 162 ClearChangeCount(); |
| 168 | 163 |
| 169 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | 164 const TemplateURL* turl = model_->GetTemplateURLForKeyword(kB); |
| 170 int new_default = controller_->MakeDefaultTemplateURL(0); | 165 int new_default = controller_->MakeDefaultTemplateURL(index); |
| 171 EXPECT_EQ(0, new_default); | 166 EXPECT_EQ(index, new_default); |
| 172 // Making an item the default sends a handful of changes. Which are sent isn't | 167 // Making an item the default sends a handful of changes. Which are sent isn't |
| 173 // important, what is important is 'something' is sent. | 168 // important, what is important is 'something' is sent. |
| 174 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || | 169 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || |
| 175 removed_count_ > 0); | 170 removed_count_ > 0); |
| 176 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); | 171 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); |
| 177 | 172 |
| 178 // Making it default a second time should fail. | 173 // Making it default a second time should fail. |
| 179 new_default = controller_->MakeDefaultTemplateURL(0); | 174 new_default = controller_->MakeDefaultTemplateURL(index); |
| 180 EXPECT_EQ(-1, new_default); | 175 EXPECT_EQ(-1, new_default); |
| 181 } | 176 } |
| 182 | 177 |
| 183 // Tests that a TemplateURL can't be made the default if the default search | 178 // Tests that a TemplateURL can't be made the default if the default search |
| 184 // provider is managed via policy. | 179 // provider is managed via policy. |
| 185 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { | 180 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { |
| 186 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 181 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 187 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); | 182 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); |
| 188 ClearChangeCount(); | 183 ClearChangeCount(); |
| 189 | 184 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 EXPECT_TRUE(model_->is_default_search_managed()); | 222 EXPECT_TRUE(model_->is_default_search_managed()); |
| 228 EXPECT_TRUE(controller_->CanEdit(turl1)); | 223 EXPECT_TRUE(controller_->CanEdit(turl1)); |
| 229 EXPECT_TRUE(controller_->CanEdit(turl2)); | 224 EXPECT_TRUE(controller_->CanEdit(turl2)); |
| 230 EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider())); | 225 EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider())); |
| 231 } | 226 } |
| 232 | 227 |
| 233 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) { | 228 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) { |
| 234 // Simulate a failure to load Web Data. | 229 // Simulate a failure to load Web Data. |
| 235 Init(true); | 230 Init(true); |
| 236 | 231 |
| 237 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 232 int index = controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 238 ClearChangeCount(); | 233 ClearChangeCount(); |
| 239 | 234 |
| 240 // This should not result in a crash. | 235 // This should not result in a crash. |
| 241 int new_default = controller_->MakeDefaultTemplateURL(0); | 236 int new_default = controller_->MakeDefaultTemplateURL(index); |
| 242 EXPECT_EQ(0, new_default); | 237 EXPECT_EQ(index, new_default); |
| 243 } | 238 } |
| 244 | 239 |
| 245 // Mutates the TemplateURLService and make sure table model is updating | 240 // Mutates the TemplateURLService and make sure table model is updating |
| 246 // appropriately. | 241 // appropriately. |
| 247 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) { | 242 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) { |
| 243 int original_row_count = table_model()->RowCount(); |
| 244 |
| 248 TemplateURLData data; | 245 TemplateURLData data; |
| 249 data.short_name = ASCIIToUTF16("b"); | 246 data.short_name = ASCIIToUTF16("b"); |
| 250 data.SetKeyword(ASCIIToUTF16("a")); | 247 data.SetKeyword(ASCIIToUTF16("a")); |
| 251 TemplateURL* turl = new TemplateURL(profile_.get(), data); | 248 TemplateURL* turl = new TemplateURL(profile_.get(), data); |
| 252 model_->Add(turl); | 249 model_->Add(turl); |
| 253 | 250 |
| 254 // Table model should have updated. | 251 // Table model should have updated. |
| 255 VerifyChangeCount(1, 0, 0, 0); | 252 VerifyChangeCount(1, 0, 0, 0); |
| 256 | 253 |
| 257 // And should contain the newly added TemplateURL. | 254 // And should contain the newly added TemplateURL. |
| 258 ASSERT_EQ(1, table_model()->RowCount()); | 255 ASSERT_EQ(original_row_count + 1, table_model()->RowCount()); |
| 259 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); | 256 ASSERT_GE(table_model()->IndexOfTemplateURL(turl), 0); |
| 260 } | 257 } |
| OLD | NEW |