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

Side by Side Diff: chrome/browser/protector/protector_service_browsertest.cc

Issue 11493003: Remove the protector service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix implicit ExtensionSystem -> TemplateURLService dependency Created 8 years 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
(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/command_line.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/protector/mock_setting_change.h"
10 #include "chrome/browser/protector/protector_service.h"
11 #include "chrome/browser/protector/protector_service_factory.h"
12 #include "chrome/browser/protector/settings_change_global_error.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/global_error/global_error.h"
15 #include "chrome/browser/ui/global_error/global_error_bubble_view_base.h"
16 #include "chrome/browser/ui/global_error/global_error_service.h"
17 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/ui_test_utils.h"
21
22 using ::testing::InvokeWithoutArgs;
23 using ::testing::NiceMock;
24 using ::testing::Return;
25
26 namespace protector {
27
28 class ProtectorServiceTest : public InProcessBrowserTest {
29 public:
30 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
31 // Make sure protector is enabled.
32 command_line->AppendSwitch(switches::kProtector);
33 }
34
35 virtual void SetUpOnMainThread() OVERRIDE {
36 protector_service_ =
37 ProtectorServiceFactory::GetForProfile(browser()->profile());
38 // ProtectService will own this change instance.
39 mock_change_ = new NiceMock<MockSettingChange>();
40 }
41
42 protected:
43 GlobalError* GetGlobalError(BaseSettingChange* change) {
44 for (ProtectorService::Items::iterator item =
45 protector_service_->items_.begin();
46 item != protector_service_->items_.end(); item++) {
47 if (item->change.get() == change)
48 return item->error.get();
49 }
50 return NULL;
51 }
52
53 // Checks that |protector_service_| has an error instance corresponding to
54 // |change| and that GlobalErrorService knows about it.
55 bool IsGlobalErrorActive(BaseSettingChange* change) {
56 GlobalError* error = GetGlobalError(change);
57 if (!error)
58 return false;
59 if (!GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
60 GetGlobalErrorByMenuItemCommandID(error->MenuItemCommandID())) {
61 return false;
62 }
63 return protector_service_->IsShowingChange();
64 }
65
66 ProtectorService* protector_service_;
67 MockSettingChange* mock_change_;
68 };
69
70 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ChangeInitError) {
71 // Init fails and causes the change to be ignored.
72 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
73 WillOnce(Return(false));
74 protector_service_->ShowChange(mock_change_);
75 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
76 content::RunAllPendingInMessageLoop();
77 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
78 EXPECT_FALSE(protector_service_->GetLastChange());
79 }
80
81 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDismiss) {
82 // Show the change and immediately dismiss it.
83 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
84 WillOnce(Return(true));
85 protector_service_->ShowChange(mock_change_);
86 content::RunAllPendingInMessageLoop();
87 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
88 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
89 protector_service_->DismissChange(mock_change_);
90 content::RunAllPendingInMessageLoop();
91 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
92 EXPECT_FALSE(protector_service_->GetLastChange());
93 }
94
95 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApply) {
96 // Show the change and apply it.
97 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
98 WillOnce(Return(true));
99 protector_service_->ShowChange(mock_change_);
100 content::RunAllPendingInMessageLoop();
101 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
102 EXPECT_CALL(*mock_change_, Apply(browser()));
103 protector_service_->ApplyChange(mock_change_, browser());
104 content::RunAllPendingInMessageLoop();
105 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
106 }
107
108 // ProtectorServiceTest.ShowAndApplyManually is timing out frequently on Win
109 // bots. http://crbug.com/130590
110 #if defined(OS_WIN)
111 #define MAYBE_ShowAndApplyManually DISABLED_ShowAndApplyManually
112 #else
113 #define MAYBE_ShowAndApplyManually ShowAndApplyManually
114 #endif
115
116 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, MAYBE_ShowAndApplyManually) {
117 // Show the change and apply it, mimicking a button click.
118 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
119 WillOnce(Return(true));
120 protector_service_->ShowChange(mock_change_);
121 content::RunAllPendingInMessageLoop();
122 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
123 EXPECT_CALL(*mock_change_, Apply(browser()));
124 // Pressing Cancel applies the change.
125 GlobalError* error = GetGlobalError(mock_change_);
126 ASSERT_TRUE(error);
127 error->BubbleViewCancelButtonPressed(browser());
128 error->GetBubbleView()->CloseBubbleView();
129 content::RunAllPendingInMessageLoop();
130 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
131 }
132
133 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscard) {
134 // Show the change and discard it.
135 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
136 WillOnce(Return(true));
137 protector_service_->ShowChange(mock_change_);
138 content::RunAllPendingInMessageLoop();
139 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
140 EXPECT_CALL(*mock_change_, Discard(browser()));
141 protector_service_->DiscardChange(mock_change_, browser());
142 content::RunAllPendingInMessageLoop();
143 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
144 }
145
146 #if defined(OS_WIN)
147 // http://crbug.com/141916
148 #define MAYBE_ShowAndDiscardManually FLAKY_ShowAndDiscardManually
149 #else
150 #define MAYBE_ShowAndDiscardManually ShowAndDiscardManually
151 #endif
152 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, MAYBE_ShowAndDiscardManually) {
153 // Show the change and discard it, mimicking a button click.
154 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
155 WillOnce(Return(true));
156 protector_service_->ShowChange(mock_change_);
157 content::RunAllPendingInMessageLoop();
158 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
159 EXPECT_CALL(*mock_change_, Discard(browser()));
160 // Pressing Apply discards the change.
161 GlobalError* error = GetGlobalError(mock_change_);
162 ASSERT_TRUE(error);
163 error->BubbleViewAcceptButtonPressed(browser());
164 error->GetBubbleView()->CloseBubbleView();
165 content::RunAllPendingInMessageLoop();
166 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
167 }
168
169 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, BubbleClosedInsideApply) {
170 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
171 WillOnce(Return(true));
172 protector_service_->ShowChange(mock_change_);
173 content::RunAllPendingInMessageLoop();
174 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
175
176 GlobalError* error = GetGlobalError(mock_change_);
177 ASSERT_TRUE(error);
178 GlobalErrorBubbleViewBase* bubble_view = error->GetBubbleView();
179 ASSERT_TRUE(bubble_view);
180 EXPECT_CALL(*mock_change_, Apply(browser())).WillOnce(InvokeWithoutArgs(
181 bubble_view, &GlobalErrorBubbleViewBase::CloseBubbleView));
182 // Pressing Cancel applies the change.
183 error->BubbleViewCancelButtonPressed(browser());
184 content::RunAllPendingInMessageLoop();
185 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
186 }
187
188 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowMultipleChangesAndApply) {
189 // Show the first change.
190 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
191 WillOnce(Return(true));
192 protector_service_->ShowChange(mock_change_);
193 content::RunAllPendingInMessageLoop();
194 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
195 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
196
197 // ProtectService will own this change instance as well.
198 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
199 // Show the second change.
200 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
201 WillOnce(Return(true));
202 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(false));
203 protector_service_->ShowChange(mock_change2);
204 content::RunAllPendingInMessageLoop();
205 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
206 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
207 EXPECT_EQ(mock_change2, protector_service_->GetLastChange());
208
209 // Apply the first change, the second should still be active.
210 EXPECT_CALL(*mock_change_, Apply(browser()));
211 protector_service_->ApplyChange(mock_change_, browser());
212 content::RunAllPendingInMessageLoop();
213 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
214 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
215 EXPECT_EQ(mock_change2, protector_service_->GetLastChange());
216
217 // Finally apply the second change.
218 EXPECT_CALL(*mock_change2, Apply(browser()));
219 protector_service_->ApplyChange(mock_change2, browser());
220 content::RunAllPendingInMessageLoop();
221 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
222 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
223 EXPECT_FALSE(protector_service_->GetLastChange());
224 }
225
226 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
227 ShowMultipleChangesDismissAndApply) {
228 // Show the first change.
229 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
230 WillOnce(Return(true));
231 protector_service_->ShowChange(mock_change_);
232 content::RunAllPendingInMessageLoop();
233 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
234
235 // ProtectService will own this change instance as well.
236 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
237 // Show the second change.
238 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
239 WillOnce(Return(true));
240 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(false));
241 protector_service_->ShowChange(mock_change2);
242 content::RunAllPendingInMessageLoop();
243 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
244 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
245
246 // Dismiss the first change, the second should still be active.
247 protector_service_->DismissChange(mock_change_);
248 content::RunAllPendingInMessageLoop();
249 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
250 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
251
252 // Finally apply the second change.
253 EXPECT_CALL(*mock_change2, Apply(browser()));
254 protector_service_->ApplyChange(mock_change2, browser());
255 content::RunAllPendingInMessageLoop();
256 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
257 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
258 }
259
260 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
261 ShowMultipleChangesAndApplyManually) {
262 // Show the first change.
263 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
264 WillOnce(Return(true));
265 protector_service_->ShowChange(mock_change_);
266 content::RunAllPendingInMessageLoop();
267 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
268
269 // The first bubble view has been displayed.
270 GlobalError* error = GetGlobalError(mock_change_);
271 ASSERT_TRUE(error);
272 ASSERT_TRUE(error->HasShownBubbleView());
273
274 // ProtectService will own this change instance as well.
275 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
276 // Show the second change.
277 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
278 WillOnce(Return(true));
279 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(false));
280 protector_service_->ShowChange(mock_change2);
281 content::RunAllPendingInMessageLoop();
282 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
283 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
284
285 // The second bubble view hasn't been displayed because the first is still
286 // shown.
287 GlobalError* error2 = GetGlobalError(mock_change2);
288 ASSERT_TRUE(error2);
289 EXPECT_FALSE(error2->HasShownBubbleView());
290
291 // Apply the first change, mimicking a button click; the second should still
292 // be active.
293 EXPECT_CALL(*mock_change_, Apply(browser()));
294 error->BubbleViewCancelButtonPressed(browser());
295 error->GetBubbleView()->CloseBubbleView();
296 content::RunAllPendingInMessageLoop();
297 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
298 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
299
300 // Now the second bubble view should be shown.
301 ASSERT_TRUE(error2->HasShownBubbleView());
302
303 // Finally apply the second change.
304 EXPECT_CALL(*mock_change2, Apply(browser()));
305 error2->BubbleViewCancelButtonPressed(browser());
306 error2->GetBubbleView()->CloseBubbleView();
307 content::RunAllPendingInMessageLoop();
308 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
309 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
310 }
311
312 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
313 ShowMultipleChangesAndApplyManuallyBeforeOther) {
314 // Show the first change.
315 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
316 WillOnce(Return(true));
317 protector_service_->ShowChange(mock_change_);
318 content::RunAllPendingInMessageLoop();
319 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
320
321 // The first bubble view has been displayed.
322 GlobalError* error = GetGlobalError(mock_change_);
323 ASSERT_TRUE(error);
324 ASSERT_TRUE(error->HasShownBubbleView());
325
326 // Apply the first change, mimicking a button click.
327 EXPECT_CALL(*mock_change_, Apply(browser()));
328 error->BubbleViewCancelButtonPressed(browser());
329 error->GetBubbleView()->CloseBubbleView();
330 content::RunAllPendingInMessageLoop();
331 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
332
333 // ProtectService will own this change instance as well.
334 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
335 // Show the second change.
336 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
337 WillOnce(Return(true));
338 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(false));
339 protector_service_->ShowChange(mock_change2);
340 content::RunAllPendingInMessageLoop();
341 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
342
343 // The second bubble view has been displayed.
344 GlobalError* error2 = GetGlobalError(mock_change2);
345 ASSERT_TRUE(error2);
346 ASSERT_TRUE(error2->HasShownBubbleView());
347
348 // Finally apply the second change.
349 EXPECT_CALL(*mock_change2, Apply(browser()));
350 error2->BubbleViewCancelButtonPressed(browser());
351 error2->GetBubbleView()->CloseBubbleView();
352 content::RunAllPendingInMessageLoop();
353 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
354 }
355
356 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowMultipleDifferentURLs) {
357 GURL url1("http://example.com/");
358 GURL url2("http://example.net/");
359
360 // Show the first change with some non-empty URL.
361 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
362 WillOnce(Return(true));
363 EXPECT_CALL(*mock_change_, GetNewSettingURL()).WillRepeatedly(Return(url1));
364 EXPECT_CALL(*mock_change_, CanBeMerged()).WillRepeatedly(Return(true));
365 protector_service_->ShowChange(mock_change_);
366 content::RunAllPendingInMessageLoop();
367 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
368 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
369
370 // ProtectService will own this change instance as well.
371 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
372 // Show the second change with another non-empty URL.
373 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
374 WillOnce(Return(true));
375 EXPECT_CALL(*mock_change2, GetNewSettingURL()).WillRepeatedly(Return(url2));
376 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(true));
377 protector_service_->ShowChange(mock_change2);
378 content::RunAllPendingInMessageLoop();
379
380 // Both changes are shown separately, not composited.
381 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
382 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
383 EXPECT_EQ(mock_change2, protector_service_->GetLastChange());
384
385 protector_service_->DismissChange(mock_change_);
386 protector_service_->DismissChange(mock_change2);
387 content::RunAllPendingInMessageLoop();
388 EXPECT_FALSE(protector_service_->GetLastChange());
389 }
390
391 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowCompositeAndDismiss) {
392 GURL url1("http://example.com/");
393
394 // Show the first change.
395 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
396 WillOnce(Return(true));
397 EXPECT_CALL(*mock_change_, GetNewSettingURL()).WillRepeatedly(Return(url1));
398 EXPECT_CALL(*mock_change_, CanBeMerged()).WillRepeatedly(Return(true));
399 protector_service_->ShowChange(mock_change_);
400 content::RunAllPendingInMessageLoop();
401 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
402 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
403
404 // The first bubble view has been displayed.
405 GlobalError* error = GetGlobalError(mock_change_);
406 ASSERT_TRUE(error);
407 EXPECT_TRUE(error->HasShownBubbleView());
408
409 // ProtectService will own this change instance as well.
410 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
411 // Show the second change.
412 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
413 WillOnce(Return(true));
414 EXPECT_CALL(*mock_change2, GetNewSettingURL()).WillRepeatedly(Return(url1));
415 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(true));
416 protector_service_->ShowChange(mock_change2);
417 content::RunAllPendingInMessageLoop();
418
419 // Now ProtectorService should be showing a single composite change.
420 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
421 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
422
423 BaseSettingChange* composite_change = protector_service_->GetLastChange();
424 ASSERT_TRUE(composite_change);
425 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
426
427 // The second (composite) bubble view has been displayed.
428 GlobalError* error2 = GetGlobalError(composite_change);
429 ASSERT_TRUE(error2);
430 EXPECT_TRUE(error2->HasShownBubbleView());
431
432 protector_service_->DismissChange(composite_change);
433 content::RunAllPendingInMessageLoop();
434 EXPECT_FALSE(IsGlobalErrorActive(composite_change));
435 EXPECT_FALSE(protector_service_->GetLastChange());
436
437 // Show the third change.
438 MockSettingChange* mock_change3 = new NiceMock<MockSettingChange>();
439 EXPECT_CALL(*mock_change3, MockInit(browser()->profile())).
440 WillOnce(Return(true));
441 EXPECT_CALL(*mock_change3, GetNewSettingURL()).WillRepeatedly(Return(url1));
442 EXPECT_CALL(*mock_change3, CanBeMerged()).WillRepeatedly(Return(true));
443 protector_service_->ShowChange(mock_change3);
444 content::RunAllPendingInMessageLoop();
445
446 // The third change should not be composed with the previous.
447 EXPECT_TRUE(IsGlobalErrorActive(mock_change3));
448 EXPECT_EQ(mock_change3, protector_service_->GetLastChange());
449
450 protector_service_->DismissChange(mock_change3);
451 content::RunAllPendingInMessageLoop();
452 EXPECT_FALSE(IsGlobalErrorActive(mock_change3));
453 EXPECT_FALSE(protector_service_->GetLastChange());
454 }
455
456 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowCompositeAndOther) {
457 GURL url1("http://example.com/");
458 GURL url2("http://example.net/");
459
460 // Show the first change.
461 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
462 WillOnce(Return(true));
463 EXPECT_CALL(*mock_change_, GetNewSettingURL()).WillRepeatedly(Return(url1));
464 EXPECT_CALL(*mock_change_, CanBeMerged()).WillRepeatedly(Return(true));
465 protector_service_->ShowChange(mock_change_);
466 content::RunAllPendingInMessageLoop();
467 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
468 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
469
470 // ProtectService will own this change instance as well.
471 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
472 // Show the second change.
473 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
474 WillOnce(Return(true));
475 EXPECT_CALL(*mock_change2, GetNewSettingURL()).WillRepeatedly(Return(url1));
476 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(true));
477 protector_service_->ShowChange(mock_change2);
478 content::RunAllPendingInMessageLoop();
479
480 // Now ProtectorService should be showing a single composite change.
481 BaseSettingChange* composite_change = protector_service_->GetLastChange();
482 ASSERT_TRUE(composite_change);
483 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
484
485 // Show the third change, with the same URL as 1st and 2nd.
486 MockSettingChange* mock_change3 = new NiceMock<MockSettingChange>();
487 EXPECT_CALL(*mock_change3, MockInit(browser()->profile())).
488 WillOnce(Return(true));
489 EXPECT_CALL(*mock_change3, GetNewSettingURL()).WillRepeatedly(Return(url1));
490 EXPECT_CALL(*mock_change3, CanBeMerged()).WillRepeatedly(Return(true));
491 protector_service_->ShowChange(mock_change3);
492 content::RunAllPendingInMessageLoop();
493
494 // The third change should be composed with the previous.
495 EXPECT_FALSE(IsGlobalErrorActive(mock_change3));
496 EXPECT_EQ(composite_change, protector_service_->GetLastChange());
497 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
498
499 // Show the 4th change, now with a different URL.
500 MockSettingChange* mock_change4 = new NiceMock<MockSettingChange>();
501 EXPECT_CALL(*mock_change4, MockInit(browser()->profile())).
502 WillOnce(Return(true));
503 EXPECT_CALL(*mock_change4, GetNewSettingURL()).WillRepeatedly(Return(url2));
504 EXPECT_CALL(*mock_change4, CanBeMerged()).WillRepeatedly(Return(true));
505 protector_service_->ShowChange(mock_change4);
506 content::RunAllPendingInMessageLoop();
507
508 // The 4th change is shown independently.
509 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
510 EXPECT_TRUE(IsGlobalErrorActive(mock_change4));
511 EXPECT_EQ(mock_change4, protector_service_->GetLastChange());
512
513 protector_service_->DismissChange(composite_change);
514 protector_service_->DismissChange(mock_change4);
515 content::RunAllPendingInMessageLoop();
516 EXPECT_FALSE(IsGlobalErrorActive(composite_change));
517 EXPECT_FALSE(IsGlobalErrorActive(mock_change4));
518 EXPECT_FALSE(protector_service_->GetLastChange());
519 }
520
521 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowCompositeAndDismissSingle) {
522 GURL url1("http://example.com/");
523 GURL url2("http://example.net/");
524
525 // Show the first change.
526 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
527 WillOnce(Return(true));
528 EXPECT_CALL(*mock_change_, GetNewSettingURL()).WillRepeatedly(Return(url1));
529 EXPECT_CALL(*mock_change_, CanBeMerged()).WillRepeatedly(Return(true));
530 protector_service_->ShowChange(mock_change_);
531 content::RunAllPendingInMessageLoop();
532 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
533 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
534
535 // ProtectService will own this change instance as well.
536 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
537 // Show the second change.
538 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
539 WillOnce(Return(true));
540 EXPECT_CALL(*mock_change2, GetNewSettingURL()).WillRepeatedly(Return(url1));
541 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(true));
542 protector_service_->ShowChange(mock_change2);
543 content::RunAllPendingInMessageLoop();
544
545 // Now ProtectorService should be showing a single composite change.
546 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
547 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
548
549 BaseSettingChange* composite_change = protector_service_->GetLastChange();
550 ASSERT_TRUE(composite_change);
551 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
552
553 // Show the third change with a different URL.
554 MockSettingChange* mock_change3 = new NiceMock<MockSettingChange>();
555 EXPECT_CALL(*mock_change3, MockInit(browser()->profile())).
556 WillOnce(Return(true));
557 EXPECT_CALL(*mock_change3, GetNewSettingURL()).WillRepeatedly(Return(url2));
558 EXPECT_CALL(*mock_change3, CanBeMerged()).WillRepeatedly(Return(true));
559 protector_service_->ShowChange(mock_change3);
560 content::RunAllPendingInMessageLoop();
561
562 // The third change should not be composed with the previous.
563 EXPECT_TRUE(IsGlobalErrorActive(mock_change3));
564 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
565 EXPECT_EQ(mock_change3, protector_service_->GetLastChange());
566
567 // Now dismiss the first change.
568 protector_service_->DismissChange(mock_change_);
569 content::RunAllPendingInMessageLoop();
570
571 // This should effectively dismiss the whole composite change.
572 EXPECT_FALSE(IsGlobalErrorActive(composite_change));
573 EXPECT_TRUE(IsGlobalErrorActive(mock_change3));
574 EXPECT_EQ(mock_change3, protector_service_->GetLastChange());
575
576 protector_service_->DismissChange(mock_change3);
577 content::RunAllPendingInMessageLoop();
578 EXPECT_FALSE(IsGlobalErrorActive(mock_change3));
579 EXPECT_FALSE(protector_service_->GetLastChange());
580 }
581
582 // Verifies that changes with different URLs but same domain are merged.
583 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, SameDomainDifferentURLs) {
584 GURL url1("http://www.example.com/abc");
585 GURL url2("http://example.com/def");
586
587 // Show the first change with some non-empty URL.
588 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
589 WillOnce(Return(true));
590 EXPECT_CALL(*mock_change_, GetNewSettingURL()).WillRepeatedly(Return(url1));
591 EXPECT_CALL(*mock_change_, CanBeMerged()).WillRepeatedly(Return(true));
592 protector_service_->ShowChange(mock_change_);
593 content::RunAllPendingInMessageLoop();
594 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
595 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
596
597 // ProtectService will own this change instance as well.
598 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
599 // Show the second change with another non-empty URL having same domain.
600 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
601 WillOnce(Return(true));
602 EXPECT_CALL(*mock_change2, GetNewSettingURL()).WillRepeatedly(Return(url2));
603 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(true));
604 protector_service_->ShowChange(mock_change2);
605 content::RunAllPendingInMessageLoop();
606
607 // Changes should be merged.
608 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
609 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
610
611 BaseSettingChange* composite_change = protector_service_->GetLastChange();
612 ASSERT_TRUE(composite_change);
613 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
614
615 protector_service_->DismissChange(composite_change);
616 content::RunAllPendingInMessageLoop();
617 EXPECT_FALSE(IsGlobalErrorActive(composite_change));
618 EXPECT_FALSE(protector_service_->GetLastChange());
619 }
620
621 // Verifies that changes with different Google URLs are merged.
622 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, DifferentGoogleDomains) {
623 GURL url1("http://www.google.com/search?q=");
624 GURL url2("http://google.ru/search?q=");
625
626 // Show the first change with some non-empty URL.
627 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
628 WillOnce(Return(true));
629 EXPECT_CALL(*mock_change_, GetNewSettingURL()).WillRepeatedly(Return(url1));
630 EXPECT_CALL(*mock_change_, CanBeMerged()).WillRepeatedly(Return(true));
631 protector_service_->ShowChange(mock_change_);
632 content::RunAllPendingInMessageLoop();
633 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
634 EXPECT_EQ(mock_change_, protector_service_->GetLastChange());
635
636 // ProtectService will own this change instance as well.
637 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
638 // Show the second change with another non-empty URL having same domain.
639 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
640 WillOnce(Return(true));
641 EXPECT_CALL(*mock_change2, GetNewSettingURL()).WillRepeatedly(Return(url2));
642 EXPECT_CALL(*mock_change2, CanBeMerged()).WillRepeatedly(Return(true));
643 protector_service_->ShowChange(mock_change2);
644 content::RunAllPendingInMessageLoop();
645
646 // Changes should be merged.
647 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
648 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
649
650 BaseSettingChange* composite_change = protector_service_->GetLastChange();
651 ASSERT_TRUE(composite_change);
652 EXPECT_TRUE(IsGlobalErrorActive(composite_change));
653
654 protector_service_->DismissChange(composite_change);
655 content::RunAllPendingInMessageLoop();
656 EXPECT_FALSE(IsGlobalErrorActive(composite_change));
657 EXPECT_FALSE(protector_service_->GetLastChange());
658 }
659
660 // TODO(ivankr): Timeout test.
661
662 } // namespace protector
OLDNEW
« no previous file with comments | « chrome/browser/protector/protector_service.cc ('k') | chrome/browser/protector/protector_service_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698