Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc

Issue 265393007: Test fixes and clean up in preparation for moving DSE to Prefs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Eriks test fixes Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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() const {
Peter Kasting 2014/05/06 19:49:00 Nit: While here: const functions should not return
Cait (Slow) 2014/05/06 21:47:47 Done.
93 return controller_->table_model(); 104 return controller_->table_model();
94 } 105 }
95 106
96 protected: 107 protected:
Peter Kasting 2014/05/06 19:49:00 Nit: While you're cleaning up this file anyway, ma
Cait (Slow) 2014/05/06 21:47:47 Done.
97 base::MessageLoopForUI message_loop_;
98 scoped_ptr<TestingProfile> profile_;
99 scoped_ptr<KeywordEditorController> controller_; 108 scoped_ptr<KeywordEditorController> controller_;
100 TemplateURLService* model_; 109 TemplateURLServiceTestUtil util_;
110 bool simulate_load_failure_;
101 111
102 int model_changed_count_; 112 int model_changed_count_;
103 int items_changed_count_; 113 int items_changed_count_;
104 int added_count_; 114 int added_count_;
105 int removed_count_; 115 int removed_count_;
106 }; 116 };
107 117
108 void KeywordEditorControllerTest::Init(bool simulate_load_failure) { 118 class KeywordEditorControllerNoWebDataTest
109 ClearChangeCount(); 119 : public KeywordEditorControllerTest {
110 120 public:
111 // If init is called twice, make sure that the controller is destroyed before 121 KeywordEditorControllerNoWebDataTest() : KeywordEditorControllerTest(true) {}
112 // the profile is. 122 };
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 123
126 // Tests adding a TemplateURL. 124 // Tests adding a TemplateURL.
127 TEST_F(KeywordEditorControllerTest, Add) { 125 TEST_F(KeywordEditorControllerTest, Add) {
126 int original_row_count = table_model()->RowCount();
128 controller_->AddTemplateURL(kA, kB, "http://c"); 127 controller_->AddTemplateURL(kA, kB, "http://c");
129 128
130 // Verify the observer was notified. 129 // Verify the observer was notified.
131 VerifyChangeCount(0, 0, 1, 0); 130 VerifyChangeCount(0, 0, 1, 0);
132 if (HasFatalFailure()) 131 if (HasFatalFailure())
133 return; 132 return;
134 133
135 // Verify the TableModel has the new data. 134 // Verify the TableModel has the new data.
136 ASSERT_EQ(1, table_model()->RowCount()); 135 ASSERT_EQ(original_row_count + 1, table_model()->RowCount());
137 136
138 // Verify the TemplateURLService has the new entry. 137 // Verify the TemplateURLService has the new data.
139 ASSERT_EQ(1U, model_->GetTemplateURLs().size()); 138 const TemplateURL* turl = util_.model()->GetTemplateURLForKeyword(kB);
140 139 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()); 140 EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name());
144 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); 141 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword());
145 EXPECT_EQ("http://c", turl->url()); 142 EXPECT_EQ("http://c", turl->url());
146 } 143 }
147 144
148 // Tests modifying a TemplateURL. 145 // Tests modifying a TemplateURL.
149 TEST_F(KeywordEditorControllerTest, Modify) { 146 TEST_F(KeywordEditorControllerTest, Modify) {
150 controller_->AddTemplateURL(kA, kB, "http://c"); 147 controller_->AddTemplateURL(kA, kB, "http://c");
151 ClearChangeCount(); 148 ClearChangeCount();
152 149
153 // Modify the entry. 150 // Modify the entry.
154 TemplateURL* turl = model_->GetTemplateURLs()[0]; 151 TemplateURL* turl = util_.model()->GetTemplateURLs()[0];
155 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1"); 152 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1");
156 153
157 // Make sure it was updated appropriately. 154 // Make sure it was updated appropriately.
158 VerifyChangeCount(0, 1, 0, 0); 155 VerifyChangeCount(0, 1, 0, 0);
159 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); 156 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name());
160 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); 157 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword());
161 EXPECT_EQ("http://c1", turl->url()); 158 EXPECT_EQ("http://c1", turl->url());
162 } 159 }
163 160
164 // Tests making a TemplateURL the default search provider. 161 // Tests making a TemplateURL the default search provider.
165 TEST_F(KeywordEditorControllerTest, MakeDefault) { 162 TEST_F(KeywordEditorControllerTest, MakeDefault) {
166 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); 163 int index = controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
167 ClearChangeCount(); 164 ClearChangeCount();
168 165
169 const TemplateURL* turl = model_->GetTemplateURLs()[0]; 166 const TemplateURL* turl = util_.model()->GetTemplateURLForKeyword(kB);
170 int new_default = controller_->MakeDefaultTemplateURL(0); 167 int new_default = controller_->MakeDefaultTemplateURL(index);
171 EXPECT_EQ(0, new_default); 168 EXPECT_EQ(index, new_default);
172 // Making an item the default sends a handful of changes. Which are sent isn't 169 // Making an item the default sends a handful of changes. Which are sent isn't
173 // important, what is important is 'something' is sent. 170 // important, what is important is 'something' is sent.
174 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || 171 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 ||
175 removed_count_ > 0); 172 removed_count_ > 0);
176 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); 173 ASSERT_TRUE(util_.model()->GetDefaultSearchProvider() == turl);
177 174
178 // Making it default a second time should fail. 175 // Making it default a second time should fail.
179 new_default = controller_->MakeDefaultTemplateURL(0); 176 new_default = controller_->MakeDefaultTemplateURL(index);
180 EXPECT_EQ(-1, new_default); 177 EXPECT_EQ(-1, new_default);
181 } 178 }
182 179
183 // Tests that a TemplateURL can't be made the default if the default search 180 // Tests that a TemplateURL can't be made the default if the default search
184 // provider is managed via policy. 181 // provider is managed via policy.
185 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { 182 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) {
186 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); 183 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
187 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); 184 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}");
188 ClearChangeCount(); 185 ClearChangeCount();
189 186
190 const TemplateURL* turl1 = 187 const TemplateURL* turl1 =
191 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b")); 188 util_.model()->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
192 ASSERT_TRUE(turl1 != NULL); 189 ASSERT_TRUE(turl1 != NULL);
193 const TemplateURL* turl2 = 190 const TemplateURL* turl2 =
194 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1")); 191 util_.model()->GetTemplateURLForKeyword(ASCIIToUTF16("b1"));
195 ASSERT_TRUE(turl2 != NULL); 192 ASSERT_TRUE(turl2 != NULL);
196 193
197 EXPECT_TRUE(controller_->CanMakeDefault(turl1)); 194 EXPECT_TRUE(controller_->CanMakeDefault(turl1));
198 EXPECT_TRUE(controller_->CanMakeDefault(turl2)); 195 EXPECT_TRUE(controller_->CanMakeDefault(turl2));
199 196
200 SimulateDefaultSearchIsManaged(turl2->url()); 197 SimulateDefaultSearchIsManaged(turl2->url());
201 EXPECT_TRUE(model_->is_default_search_managed()); 198 EXPECT_TRUE(util_.model()->is_default_search_managed());
202 199
203 EXPECT_FALSE(controller_->CanMakeDefault(turl1)); 200 EXPECT_FALSE(controller_->CanMakeDefault(turl1));
204 EXPECT_FALSE(controller_->CanMakeDefault(turl2)); 201 EXPECT_FALSE(controller_->CanMakeDefault(turl2));
205 } 202 }
206 203
207 // Tests that a TemplateURL can't be edited if it is the managed default search 204 // Tests that a TemplateURL can't be edited if it is the managed default search
208 // provider. 205 // provider.
209 TEST_F(KeywordEditorControllerTest, EditManagedDefault) { 206 TEST_F(KeywordEditorControllerTest, EditManagedDefault) {
210 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); 207 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
211 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); 208 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}");
212 ClearChangeCount(); 209 ClearChangeCount();
213 210
214 const TemplateURL* turl1 = 211 const TemplateURL* turl1 =
215 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b")); 212 util_.model()->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
216 ASSERT_TRUE(turl1 != NULL); 213 ASSERT_TRUE(turl1 != NULL);
217 const TemplateURL* turl2 = 214 const TemplateURL* turl2 =
218 model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1")); 215 util_.model()->GetTemplateURLForKeyword(ASCIIToUTF16("b1"));
219 ASSERT_TRUE(turl2 != NULL); 216 ASSERT_TRUE(turl2 != NULL);
220 217
221 EXPECT_TRUE(controller_->CanEdit(turl1)); 218 EXPECT_TRUE(controller_->CanEdit(turl1));
222 EXPECT_TRUE(controller_->CanEdit(turl2)); 219 EXPECT_TRUE(controller_->CanEdit(turl2));
223 220
224 // Simulate setting a managed default. This will add another template URL to 221 // Simulate setting a managed default. This will add another template URL to
225 // the model. 222 // the model.
226 SimulateDefaultSearchIsManaged(turl2->url()); 223 SimulateDefaultSearchIsManaged(turl2->url());
227 EXPECT_TRUE(model_->is_default_search_managed()); 224 EXPECT_TRUE(util_.model()->is_default_search_managed());
228 EXPECT_TRUE(controller_->CanEdit(turl1)); 225 EXPECT_TRUE(controller_->CanEdit(turl1));
229 EXPECT_TRUE(controller_->CanEdit(turl2)); 226 EXPECT_TRUE(controller_->CanEdit(turl2));
230 EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider())); 227 EXPECT_FALSE(controller_->CanEdit(util_.model()->GetDefaultSearchProvider()));
231 } 228 }
232 229
233 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) { 230 TEST_F(KeywordEditorControllerNoWebDataTest, MakeDefaultNoWebData) {
234 // Simulate a failure to load Web Data. 231 int index = controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
235 Init(true);
236
237 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
238 ClearChangeCount(); 232 ClearChangeCount();
239 233
240 // This should not result in a crash. 234 // This should not result in a crash.
241 int new_default = controller_->MakeDefaultTemplateURL(0); 235 int new_default = controller_->MakeDefaultTemplateURL(index);
242 EXPECT_EQ(0, new_default); 236 EXPECT_EQ(index, new_default);
243 } 237 }
244 238
245 // Mutates the TemplateURLService and make sure table model is updating 239 // Mutates the TemplateURLService and make sure table model is updating
246 // appropriately. 240 // appropriately.
247 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) { 241 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) {
242 int original_row_count = table_model()->RowCount();
243
248 TemplateURLData data; 244 TemplateURLData data;
249 data.short_name = ASCIIToUTF16("b"); 245 data.short_name = ASCIIToUTF16("b");
250 data.SetKeyword(ASCIIToUTF16("a")); 246 data.SetKeyword(ASCIIToUTF16("a"));
251 TemplateURL* turl = new TemplateURL(profile_.get(), data); 247 TemplateURL* turl = new TemplateURL(util_.profile(), data);
252 model_->Add(turl); 248 util_.model()->Add(turl);
253 249
254 // Table model should have updated. 250 // Table model should have updated.
255 VerifyChangeCount(1, 0, 0, 0); 251 VerifyChangeCount(1, 0, 0, 0);
256 252
257 // And should contain the newly added TemplateURL. 253 // And should contain the newly added TemplateURL.
258 ASSERT_EQ(1, table_model()->RowCount()); 254 ASSERT_EQ(original_row_count + 1, table_model()->RowCount());
259 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); 255 ASSERT_GE(table_model()->IndexOfTemplateURL(turl), 0);
260 } 256 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698