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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search_engines/template_url.h"
11 #include "chrome/browser/search_engines/template_url_service.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h"
14 #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"
18 #include "content/public/browser/notification_service.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/base/models/table_model_observer.h"
22 static const string16 kA(ASCIIToUTF16("a"));
23 static const string16 kA1(ASCIIToUTF16("a1"));
24 static const string16 kB(ASCIIToUTF16("b"));
25 static const string16 kB1(ASCIIToUTF16("b1"));
27 // Base class for keyword editor tests. Creates a profile containing an
28 // empty TemplateURLService.
29 class KeywordEditorControllerTest : public testing::Test,
30 public ui::TableModelObserver {
32 // Initializes all of the state.
33 void Init(bool simulate_load_failure);
35 virtual void SetUp() {
39 virtual void OnModelChanged() OVERRIDE {
40 model_changed_count_++;
43 virtual void OnItemsChanged(int start, int length) OVERRIDE {
44 items_changed_count_++;
47 virtual void OnItemsAdded(int start, int length) OVERRIDE {
51 virtual void OnItemsRemoved(int start, int length) OVERRIDE {
55 void VerifyChangeCount(int model_changed_count, int item_changed_count,
56 int added_count, int removed_count) {
57 ASSERT_EQ(model_changed_count, model_changed_count_);
58 ASSERT_EQ(item_changed_count, items_changed_count_);
59 ASSERT_EQ(added_count, added_count_);
60 ASSERT_EQ(removed_count, removed_count_);
64 void ClearChangeCount() {
65 model_changed_count_ = items_changed_count_ = added_count_ =
69 void SimulateDefaultSearchIsManaged(const std::string& url) {
70 ASSERT_FALSE(url.empty());
71 TestingPrefServiceSyncable* service = profile_->GetTestingPrefService();
72 service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
73 new base::FundamentalValue(true));
74 service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
75 new base::StringValue(url));
76 service->SetManagedPref(prefs::kDefaultSearchProviderName,
77 new base::StringValue("managed"));
78 // Clear the IDs that are not specified via policy.
79 service->SetManagedPref(prefs::kDefaultSearchProviderID,
80 new base::StringValue(std::string()));
81 service->SetManagedPref(prefs::kDefaultSearchProviderPrepopulateID,
82 new base::StringValue(std::string()));
83 model_->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
84 content::NotificationService::AllSources(),
85 content::NotificationService::NoDetails());
88 TemplateURLTableModel* table_model() const {
89 return controller_->table_model();
93 base::MessageLoopForUI message_loop_;
94 scoped_ptr<TestingProfile> profile_;
95 scoped_ptr<KeywordEditorController> controller_;
96 TemplateURLService* model_;
98 int model_changed_count_;
99 int items_changed_count_;
104 void KeywordEditorControllerTest::Init(bool simulate_load_failure) {
107 // If init is called twice, make sure that the controller is destroyed before
110 profile_.reset(new TestingProfile());
111 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
112 profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor);
114 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get());
115 if (simulate_load_failure)
116 model_->OnWebDataServiceRequestDone(0, NULL);
118 controller_.reset(new KeywordEditorController(profile_.get()));
119 controller_->table_model()->SetObserver(this);
122 // Tests adding a TemplateURL.
123 TEST_F(KeywordEditorControllerTest, Add) {
124 controller_->AddTemplateURL(kA, kB, "http://c");
126 // Verify the observer was notified.
127 VerifyChangeCount(0, 0, 1, 0);
128 if (HasFatalFailure())
131 // Verify the TableModel has the new data.
132 ASSERT_EQ(1, table_model()->RowCount());
134 // Verify the TemplateURLService has the new entry.
135 ASSERT_EQ(1U, model_->GetTemplateURLs().size());
137 // Verify the entry is what we added.
138 const TemplateURL* turl = model_->GetTemplateURLs()[0];
139 EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name());
140 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword());
141 EXPECT_EQ("http://c", turl->url());
144 // Tests modifying a TemplateURL.
145 TEST_F(KeywordEditorControllerTest, Modify) {
146 controller_->AddTemplateURL(kA, kB, "http://c");
150 TemplateURL* turl = model_->GetTemplateURLs()[0];
151 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1");
153 // Make sure it was updated appropriately.
154 VerifyChangeCount(0, 1, 0, 0);
155 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name());
156 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword());
157 EXPECT_EQ("http://c1", turl->url());
160 // Tests making a TemplateURL the default search provider.
161 TEST_F(KeywordEditorControllerTest, MakeDefault) {
162 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
165 const TemplateURL* turl = model_->GetTemplateURLs()[0];
166 int new_default = controller_->MakeDefaultTemplateURL(0);
167 EXPECT_EQ(0, new_default);
168 // Making an item the default sends a handful of changes. Which are sent isn't
169 // important, what is important is 'something' is sent.
170 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 ||
172 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl);
174 // Making it default a second time should fail.
175 new_default = controller_->MakeDefaultTemplateURL(0);
176 EXPECT_EQ(-1, new_default);
179 // Tests that a TemplateURL can't be made the default if the default search
180 // provider is managed via policy.
181 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) {
182 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
183 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}");
186 const TemplateURL* turl1 =
187 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
188 ASSERT_TRUE(turl1 != NULL);
189 const TemplateURL* turl2 =
190 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1"));
191 ASSERT_TRUE(turl2 != NULL);
193 EXPECT_TRUE(controller_->CanMakeDefault(turl1));
194 EXPECT_TRUE(controller_->CanMakeDefault(turl2));
196 SimulateDefaultSearchIsManaged(turl2->url());
197 EXPECT_TRUE(model_->is_default_search_managed());
199 EXPECT_FALSE(controller_->CanMakeDefault(turl1));
200 EXPECT_FALSE(controller_->CanMakeDefault(turl2));
203 // Tests that a TemplateURL can't be edited if it is the managed default search
205 TEST_F(KeywordEditorControllerTest, EditManagedDefault) {
206 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
207 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}");
210 const TemplateURL* turl1 =
211 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
212 ASSERT_TRUE(turl1 != NULL);
213 const TemplateURL* turl2 =
214 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1"));
215 ASSERT_TRUE(turl2 != NULL);
217 EXPECT_TRUE(controller_->CanEdit(turl1));
218 EXPECT_TRUE(controller_->CanEdit(turl2));
220 // Simulate setting a managed default. This will add another template URL to
222 SimulateDefaultSearchIsManaged(turl2->url());
223 EXPECT_TRUE(model_->is_default_search_managed());
224 EXPECT_TRUE(controller_->CanEdit(turl1));
225 EXPECT_TRUE(controller_->CanEdit(turl2));
226 EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider()));
229 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) {
230 // Simulate a failure to load Web Data.
233 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
236 // This should not result in a crash.
237 int new_default = controller_->MakeDefaultTemplateURL(0);
238 EXPECT_EQ(0, new_default);
241 // Mutates the TemplateURLService and make sure table model is updating
243 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) {
244 TemplateURLData data;
245 data.short_name = ASCIIToUTF16("b");
246 data.SetKeyword(ASCIIToUTF16("a"));
247 TemplateURL* turl = new TemplateURL(profile_.get(), data);
250 // Table model should have updated.
251 VerifyChangeCount(1, 0, 0, 0);
253 // And should contain the newly added TemplateURL.
254 ASSERT_EQ(1, table_model()->RowCount());
255 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl));