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

Side by Side Diff: chrome/browser/banners/app_banner_manager_browsertest.cc

Issue 2677853002: [Webapps]: Clear AppBannerManager::page_requested_prompt_ at start of banner flow (Closed)
Patch Set: Merge branch 'install_banner' into install_banner2 Created 3 years, 10 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <vector> 5 #include <vector>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/test/histogram_tester.h" 9 #include "base/test/histogram_tester.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
11 #include "chrome/browser/banners/app_banner_manager.h" 11 #include "chrome/browser/banners/app_banner_manager.h"
12 #include "chrome/browser/banners/app_banner_metrics.h" 12 #include "chrome/browser/banners/app_banner_metrics.h"
13 #include "chrome/browser/banners/app_banner_settings_helper.h" 13 #include "chrome/browser/banners/app_banner_settings_helper.h"
14 #include "chrome/browser/engagement/site_engagement_service.h" 14 #include "chrome/browser/engagement/site_engagement_service.h"
15 #include "chrome/browser/installable/installable_logging.h" 15 #include "chrome/browser/installable/installable_logging.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/test/base/in_process_browser_test.h" 20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h" 21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/web_contents_user_data.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h" 23 #include "net/test/embedded_test_server/embedded_test_server.h"
23 24
24 namespace banners { 25 namespace banners {
25 26
26 // Browser tests for web app banners. 27 // Browser tests for web app banners.
27 // NOTE: this test relies on service workers; failures and flakiness may be due 28 // NOTE: this test relies on service workers; failures and flakiness may be due
28 // to changes in SW code. 29 // to changes in SW code.
29 class AppBannerManagerTest : public AppBannerManager { 30 class AppBannerManagerTest
31 : public AppBannerManager,
32 public content::WebContentsUserData<AppBannerManagerTest> {
dominickn 2017/02/06 05:22:01 Not sure if it's necessary to have this full WebCo
pkotwicz 2017/02/09 00:57:37 Passing in AppBannerManagerTest to RunBannerTest()
dominickn 2017/02/09 04:49:04 I'm not really convinced of the closer to how prod
30 public: 33 public:
31 explicit AppBannerManagerTest(content::WebContents* web_contents) 34 explicit AppBannerManagerTest(content::WebContents* web_contents)
32 : AppBannerManager(web_contents) {} 35 : AppBannerManager(web_contents) {}
33 36
34 ~AppBannerManagerTest() override {} 37 ~AppBannerManagerTest() override {}
35 38
36 bool will_show() { return will_show_.get() && *will_show_; } 39 bool will_show() { return will_show_.get() && *will_show_; }
37 40
41 void clear_will_show() { will_show_.reset(); }
42
38 bool is_active() { return AppBannerManager::is_active(); } 43 bool is_active() { return AppBannerManager::is_active(); }
39 44
40 bool need_to_log_status() { return need_to_log_status_; } 45 bool need_to_log_status() { return need_to_log_status_; }
41 46
42 void Prepare(base::Closure quit_closure) { 47 void Prepare(base::Closure quit_closure) {
43 will_show_.reset(nullptr);
44 quit_closure_ = quit_closure; 48 quit_closure_ = quit_closure;
45 } 49 }
46 50
47 protected: 51 protected:
48 // All calls to RequestAppBanner should terminate in one of Stop() (not 52 // All calls to RequestAppBanner should terminate in one of Stop() (not
49 // showing banner) or ShowBanner(). Override those two methods to capture test 53 // showing banner) or ShowBanner(). Override those two methods to capture test
50 // status. 54 // status.
51 void Stop() override { 55 void Stop() override {
52 AppBannerManager::Stop(); 56 AppBannerManager::Stop();
53 ASSERT_FALSE(will_show_.get()); 57 ASSERT_FALSE(will_show_.get());
54 will_show_.reset(new bool(false)); 58 will_show_.reset(new bool(false));
55 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); 59 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
56 } 60 }
57 61
58 void ShowBanner() override { 62 void ShowBanner() override {
59 // Fake the call to ReportStatus here - this is usually called in 63 // Fake the call to ReportStatus here - this is usually called in
60 // platform-specific code which is not exposed here. 64 // platform-specific code which is not exposed here.
61 ReportStatus(nullptr, SHOWING_WEB_APP_BANNER); 65 ReportStatus(nullptr, SHOWING_WEB_APP_BANNER);
62 RecordDidShowBanner("AppBanner.WebApp.Shown"); 66 RecordDidShowBanner("AppBanner.WebApp.Shown");
63 67
64 ASSERT_FALSE(will_show_.get()); 68 ASSERT_FALSE(will_show_.get());
65 will_show_.reset(new bool(true)); 69 will_show_.reset(new bool(true));
66 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); 70 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
67 } 71 }
68 72
73 void WebContentsDestroyed() override {
74 will_show_.reset(nullptr);
75 AppBannerManager::WebContentsDestroyed();
dominickn 2017/02/06 05:22:00 WebContentsDestroyed() calls Stop(), which sets wi
pkotwicz 2017/02/09 00:57:37 AppBannerManager::WebContentsDestroyed() always ca
dominickn 2017/02/09 04:49:04 I'm not a huge fan of this (this is partially why
76 }
77
69 private: 78 private:
79 friend class content::WebContentsUserData<AppBannerManagerTest>;
80
70 bool IsDebugMode() const override { return false; } 81 bool IsDebugMode() const override { return false; }
71 82
72 base::Closure quit_closure_; 83 base::Closure quit_closure_;
73 std::unique_ptr<bool> will_show_; 84 std::unique_ptr<bool> will_show_;
85
86 DISALLOW_COPY_AND_ASSIGN(AppBannerManagerTest);
74 }; 87 };
75 88
89 } // namespace banners
90
91 DEFINE_WEB_CONTENTS_USER_DATA_KEY(banners::AppBannerManagerTest);
92
93 namespace banners {
94
76 class AppBannerManagerBrowserTest : public InProcessBrowserTest { 95 class AppBannerManagerBrowserTest : public InProcessBrowserTest {
77 public: 96 public:
78 void SetUpOnMainThread() override { 97 void SetUpOnMainThread() override {
79 AppBannerSettingsHelper::SetTotalEngagementToTrigger(10); 98 AppBannerSettingsHelper::SetTotalEngagementToTrigger(10);
80 ASSERT_TRUE(embedded_test_server()->Start()); 99 ASSERT_TRUE(embedded_test_server()->Start());
81 InProcessBrowserTest::SetUpOnMainThread(); 100 InProcessBrowserTest::SetUpOnMainThread();
82 } 101 }
83 102
84 void SetUpCommandLine(base::CommandLine* command_line) override { 103 void SetUpCommandLine(base::CommandLine* command_line) override {
85 // Make sure app banners are disabled in the browser, otherwise they will 104 // Make sure app banners are disabled in the browser, otherwise they will
86 // interfere with the test. 105 // interfere with the test.
87 command_line->AppendSwitch(switches::kDisableAddToShelf); 106 command_line->AppendSwitch(switches::kDisableAddToShelf);
88 } 107 }
89 108
90 protected: 109 protected:
110 // Returns a test server URL to page |page_url| with |manifest_url| injected
111 // as the manifest tag.
112 std::string GetURLOfPageWithManifest(const std::string& page_url,
113 const std::string& manifest_url) {
114 return page_url + embedded_test_server()->GetURL(manifest_url).spec();
115 }
116
91 // Returns a test server URL to a page controlled by a service worker with 117 // Returns a test server URL to a page controlled by a service worker with
92 // |manifest_url| injected as the manifest tag. 118 // |manifest_url| injected as the manifest tag.
93 std::string GetURLOfPageWithServiceWorkerAndManifest( 119 std::string GetURLOfPageWithServiceWorkerAndManifest(
94 const std::string& manifest_url) { 120 const std::string& manifest_url) {
95 return "/banners/manifest_test_page.html?manifest=" + 121 return GetURLOfPageWithManifest(
96 embedded_test_server()->GetURL(manifest_url).spec(); 122 "/banners/manifest_test_page.html?manifest=", manifest_url);
97 } 123 }
98 124
99 void RunBannerTest(Browser* browser, 125 void RunBannerTest(Browser* browser,
100 const std::string& url, 126 const std::string& url,
101 const std::vector<double>& engagement_scores, 127 const std::vector<double>& engagement_scores,
102 InstallableStatusCode expected_code_for_histogram, 128 InstallableStatusCode expected_code_for_histogram,
103 bool expected_to_show) { 129 bool expected_to_show) {
104 base::HistogramTester histograms; 130 base::HistogramTester histograms;
105 GURL test_url = embedded_test_server()->GetURL(url); 131 GURL test_url = embedded_test_server()->GetURL(url);
106 content::WebContents* web_contents = 132 content::WebContents* web_contents =
107 browser->tab_strip_model()->GetActiveWebContents(); 133 browser->tab_strip_model()->GetActiveWebContents();
108 std::unique_ptr<AppBannerManagerTest> manager( 134
109 new AppBannerManagerTest(web_contents)); 135 // Create manager if one does not already exist.
136 AppBannerManagerTest::CreateForWebContents(web_contents);
137 AppBannerManagerTest* manager =
138 AppBannerManagerTest::FromWebContents(web_contents);
139 manager->clear_will_show();
110 140
111 // Loop through the vector of engagement scores. We only expect the banner 141 // Loop through the vector of engagement scores. We only expect the banner
112 // pipeline to trigger on the last one; otherwise, nothing is expected to 142 // pipeline to trigger on the last one; otherwise, nothing is expected to
113 // happen. 143 // happen.
114 int iterations = 0; 144 int iterations = 0;
115 SiteEngagementService* service = 145 SiteEngagementService* service =
116 SiteEngagementService::Get(browser->profile()); 146 SiteEngagementService::Get(browser->profile());
117 for (double engagement : engagement_scores) { 147 for (double engagement : engagement_scores) {
118 if (iterations > 0) { 148 if (iterations > 0) {
119 ui_test_utils::NavigateToURL(browser, test_url); 149 ui_test_utils::NavigateToURL(browser, test_url);
120 150
121 EXPECT_EQ(false, manager->will_show()); 151 EXPECT_EQ(false, manager->will_show());
122 EXPECT_FALSE(manager->is_active()); 152 EXPECT_FALSE(manager->is_active());
123 153
124 histograms.ExpectTotalCount(banners::kMinutesHistogram, 0); 154 histograms.ExpectTotalCount(banners::kMinutesHistogram, 0);
125 histograms.ExpectTotalCount(banners::kInstallableStatusCodeHistogram, 155 histograms.ExpectTotalCount(banners::kInstallableStatusCodeHistogram,
126 0); 156 0);
127 } 157 }
128 service->ResetScoreForURL(test_url, engagement); 158 service->ResetScoreForURL(test_url, engagement);
129 ++iterations; 159 ++iterations;
130 } 160 }
131 161
132 // On the final loop, we expect the banner pipeline to trigger - the 162 // On the final loop, we expect the banner pipeline to trigger - the
133 // navigation should generate the final engagement to show the banner. Spin 163 // navigation should generate the final engagement to show the banner. Spin
134 // the run loop, which should be quit by either Stop() or ShowBanner(). 164 // the run loop, which should be quit by either Stop() or ShowBanner().
135 base::RunLoop run_loop; 165 base::RunLoop run_loop;
166 manager->clear_will_show();
136 manager->Prepare(run_loop.QuitClosure()); 167 manager->Prepare(run_loop.QuitClosure());
137 ui_test_utils::NavigateToURL(browser, test_url); 168 ui_test_utils::NavigateToURL(browser, test_url);
138 run_loop.Run(); 169 run_loop.Run();
139 170
140 EXPECT_EQ(expected_to_show, manager->will_show()); 171 EXPECT_EQ(expected_to_show, manager->will_show());
141 EXPECT_FALSE(manager->is_active()); 172 EXPECT_FALSE(manager->is_active());
142 173
143 // Navigate to ensure the InstallableStatusCodeHistogram is logged. 174 // Navigate to ensure the InstallableStatusCodeHistogram is logged.
144 ui_test_utils::NavigateToURL(browser, GURL("about:blank")); 175 ui_test_utils::NavigateToURL(browser, GURL("about:blank"));
145 176
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 RunBannerTest(browser(), "/banners/prompt_test_page.html", engagement_scores, 274 RunBannerTest(browser(), "/banners/prompt_test_page.html", engagement_scores,
244 SHOWING_WEB_APP_BANNER, true); 275 SHOWING_WEB_APP_BANNER, true);
245 } 276 }
246 277
247 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, PromptBannerInHandler) { 278 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, PromptBannerInHandler) {
248 std::vector<double> engagement_scores{0, 2, 5, 10}; 279 std::vector<double> engagement_scores{0, 2, 5, 10};
249 RunBannerTest(browser(), "/banners/prompt_in_handler_test_page.html", 280 RunBannerTest(browser(), "/banners/prompt_in_handler_test_page.html",
250 engagement_scores, SHOWING_WEB_APP_BANNER, true); 281 engagement_scores, SHOWING_WEB_APP_BANNER, true);
251 } 282 }
252 283
284 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest,
285 CancelBannerAfterPromptInHandler) {
286 std::vector<double> engagement_scores{10};
287 RunBannerTest(browser(), "/banners/prompt_in_handler_test_page.html",
288 engagement_scores, SHOWING_WEB_APP_BANNER, true);
289 std::string cancel_test_page_url = GetURLOfPageWithManifest(
290 "/banners/cancel_test_page.html?manifest=",
291 "/banners/manifest_different_start_url.json");
292 RunBannerTest(browser(), cancel_test_page_url, engagement_scores,
293 RENDERER_CANCELLED, false);
294 }
295
253 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerInIFrame) { 296 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerInIFrame) {
254 std::vector<double> engagement_scores{10}; 297 std::vector<double> engagement_scores{10};
255 RunBannerTest(browser(), "/banners/iframe_test_page.html", engagement_scores, 298 RunBannerTest(browser(), "/banners/iframe_test_page.html", engagement_scores,
256 NO_MANIFEST, false); 299 NO_MANIFEST, false);
257 } 300 }
258 301
259 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, DoesNotShowInIncognito) { 302 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, DoesNotShowInIncognito) {
260 std::vector<double> engagement_scores{10}; 303 std::vector<double> engagement_scores{10};
261 Browser* incognito_browser = 304 Browser* incognito_browser =
262 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank")); 305 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
263 RunBannerTest(incognito_browser, "/banners/manifest_test_page.html", 306 RunBannerTest(incognito_browser, "/banners/manifest_test_page.html",
264 engagement_scores, IN_INCOGNITO, false); 307 engagement_scores, IN_INCOGNITO, false);
265 } 308 }
266 309
267 } // namespace banners 310 } // namespace banners
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698