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 "chrome/browser/ui/bookmarks/bookmark_prompt_controller.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/bookmarks/bookmark_prompt_prefs.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_list.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "chrome/test/base/test_browser_window.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/web_contents.h"
21 class BookmarkPromptControllerTest
: public BrowserWithTestWindowTest
{
23 BookmarkPromptControllerTest() : field_trial_list_(NULL
), page_id_(0) {
24 base::FieldTrialList::CreateFieldTrial("BookmarkPrompt", "Experiment");
28 int show_prompt_call_count() const {
29 return static_cast<MyTestBrowserWindow
*>(browser()->window())->
30 show_prompt_call_count();
33 void Visit(const GURL
& url
) {
34 AddTab(browser(), url
);
36 // Simulate page loaded.
38 content::WebContents
* web_contents
=
39 browser()->tab_strip_model()->GetActiveWebContents();
40 content::NotificationService::current()->Notify(
41 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
42 content::Source
<content::WebContents
>(web_contents
),
43 content::Details
<int>(&page_id_
));
45 // Wait until HistoryService::QueryURL completion.
46 static_cast<TestingProfile
*>(browser()->profile())->
47 BlockUntilHistoryProcessesPendingRequests();
51 class MyTestBrowserWindow
: public TestBrowserWindow
{
53 MyTestBrowserWindow() : show_prompt_call_count_(0) {}
54 int show_prompt_call_count() { return show_prompt_call_count_
; }
57 virtual bool IsActive() const OVERRIDE
{ return true; }
58 virtual void ShowBookmarkPrompt() OVERRIDE
{ ++show_prompt_call_count_
; }
59 int show_prompt_call_count_
;
61 DISALLOW_COPY_AND_ASSIGN(MyTestBrowserWindow
);
64 virtual void SetUp() OVERRIDE
{
65 TestingBrowserProcess::GetGlobal()->
66 SetBookmarkPromptController(new BookmarkPromptController
);
67 BrowserWithTestWindowTest::SetUp();
68 ASSERT_TRUE(static_cast<TestingProfile
*>(browser()->profile())->
69 CreateHistoryService(true, false));
70 static_cast<TestingProfile
*>(browser()->profile())->
71 BlockUntilHistoryIndexIsRefreshed();
72 // Simulate browser activation.
73 BrowserList::SetLastActive(browser());
76 virtual void TearDown() OVERRIDE
{
77 TestingBrowserProcess::GetGlobal()->
78 SetBookmarkPromptController(NULL
);
79 static_cast<TestingProfile
*>(browser()->profile())->
80 DestroyHistoryService();
81 BrowserWithTestWindowTest::TearDown();
84 virtual BrowserWindow
* CreateBrowserWindow() OVERRIDE
{
85 return new MyTestBrowserWindow
;
88 base::FieldTrialList field_trial_list_
;
91 DISALLOW_COPY_AND_ASSIGN(BookmarkPromptControllerTest
);
94 // Test for maximum prompt impression count.
95 TEST_F(BookmarkPromptControllerTest
, MaxImpressionCountTest
) {
96 BookmarkPromptPrefs
prefs(browser()->profile()->GetPrefs());
98 // Simulate we've already display bookmark prompt many times.
99 for (int i
= 0; i
< BookmarkPromptController::kMaxPromptImpressionCount
;
101 prefs
.IncrementPromptImpressionCount();
104 // Visit the URL many times to display bookmark prompt.
105 GURL
url("http://foo");
106 for (int visit_count
= 1;
107 visit_count
<= BookmarkPromptController::kVisitCountForSessionTrigger
;
112 // Although, we don't display bookmark prompt since we've already displayed
114 EXPECT_EQ(0, show_prompt_call_count());
115 EXPECT_EQ(BookmarkPromptController::kMaxPromptImpressionCount
,
116 prefs
.GetPromptImpressionCount());
119 // Test for session trigger and permanent trigger.
120 TEST_F(BookmarkPromptControllerTest
, TriggerTest
) {
121 BookmarkPromptPrefs
prefs(browser()->profile()->GetPrefs());
123 GURL
url("http://foo");
124 for (int visit_count
= 1;
125 visit_count
< BookmarkPromptController::kVisitCountForPermanentTrigger
;
128 if (visit_count
< BookmarkPromptController::kVisitCountForSessionTrigger
) {
129 EXPECT_EQ(0, show_prompt_call_count());
130 EXPECT_EQ(0, prefs
.GetPromptImpressionCount());
132 EXPECT_EQ(1, show_prompt_call_count());
133 EXPECT_EQ(1, prefs
.GetPromptImpressionCount());
139 EXPECT_EQ(2, show_prompt_call_count());
140 EXPECT_EQ(2, prefs
.GetPromptImpressionCount());