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

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

Issue 2778983005: Allow the app banner installability check to run on page load. (Closed)
Patch Set: Address comments Created 3 years, 8 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/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/test/histogram_tester.h" 11 #include "base/test/histogram_tester.h"
12 #include "base/test/scoped_feature_list.h"
12 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
13 #include "chrome/browser/banners/app_banner_manager.h" 14 #include "chrome/browser/banners/app_banner_manager.h"
14 #include "chrome/browser/banners/app_banner_metrics.h" 15 #include "chrome/browser/banners/app_banner_metrics.h"
15 #include "chrome/browser/banners/app_banner_settings_helper.h" 16 #include "chrome/browser/banners/app_banner_settings_helper.h"
17 #include "chrome/browser/engagement/site_engagement_score.h"
16 #include "chrome/browser/engagement/site_engagement_service.h" 18 #include "chrome/browser/engagement/site_engagement_service.h"
17 #include "chrome/browser/installable/installable_logging.h" 19 #include "chrome/browser/installable/installable_logging.h"
18 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" 22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/common/chrome_features.h"
21 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/base/in_process_browser_test.h" 25 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/ui_test_utils.h" 26 #include "chrome/test/base/ui_test_utils.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h" 27 #include "net/test/embedded_test_server/embedded_test_server.h"
25 28
26 namespace banners { 29 namespace banners {
27 30
28 // Browser tests for web app banners. 31 // Browser tests for web app banners.
29 // NOTE: this test relies on service workers; failures and flakiness may be due 32 // NOTE: this test relies on service workers; failures and flakiness may be due
30 // to changes in SW code. 33 // to changes in SW code.
31 class AppBannerManagerTest : public AppBannerManager { 34 class AppBannerManagerTest : public AppBannerManager {
32 public: 35 public:
33 explicit AppBannerManagerTest(content::WebContents* web_contents) 36 explicit AppBannerManagerTest(content::WebContents* web_contents)
34 : AppBannerManager(web_contents) {} 37 : AppBannerManager(web_contents) {}
35 38
36 ~AppBannerManagerTest() override {} 39 ~AppBannerManagerTest() override {}
37 40
41 void RequestAppBanner(const GURL& validated_url,
42 bool is_debug_mode) override {
43 // Filter out about:blank navigations - we use these in testing to force
44 // Stop() to be called.
45 if (validated_url == GURL("about:blank"))
46 return;
47
48 AppBannerManager::RequestAppBanner(validated_url, is_debug_mode);
49 }
50
38 bool will_show() { return will_show_.get() && *will_show_; } 51 bool will_show() { return will_show_.get() && *will_show_; }
39 52
40 void clear_will_show() { will_show_.reset(); } 53 void clear_will_show() { will_show_.reset(); }
41 54
42 bool is_active() { return AppBannerManager::is_active(); } 55 bool is_active() { return AppBannerManager::is_active(); }
43 56
57 bool is_complete() { return AppBannerManager::is_complete(); }
58
59 bool is_pending_engagement() {
60 return AppBannerManager::is_pending_engagement();
61 }
62
44 bool need_to_log_status() { return need_to_log_status_; } 63 bool need_to_log_status() { return need_to_log_status_; }
45 64
46 void Prepare(base::Closure quit_closure) { 65 void Prepare(base::Closure quit_closure) {
47 quit_closure_ = quit_closure; 66 quit_closure_ = quit_closure;
48 } 67 }
49 68
50 protected: 69 protected:
51 // All calls to RequestAppBanner should terminate in one of Stop() (not 70 // All calls to RequestAppBanner should terminate in one of Stop() (not
52 // showing banner) or ShowBanner(). Override those two methods to capture test 71 // showing banner), UpdateState(State::PENDING_ENGAGEMENT) (waiting for
53 // status. 72 // sufficient engagement), or ShowBanner(). Override these methods to capture
73 // test status.
54 void Stop() override { 74 void Stop() override {
55 AppBannerManager::Stop(); 75 AppBannerManager::Stop();
56 ASSERT_FALSE(will_show_.get()); 76 ASSERT_FALSE(will_show_.get());
57 will_show_.reset(new bool(false)); 77 will_show_.reset(new bool(false));
58 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); 78 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
59 } 79 }
60 80
61 void ShowBanner() override { 81 void ShowBanner() override {
62 // Fake the call to ReportStatus here - this is usually called in 82 // Fake the call to ReportStatus here - this is usually called in
63 // platform-specific code which is not exposed here. 83 // platform-specific code which is not exposed here.
64 ReportStatus(nullptr, SHOWING_WEB_APP_BANNER); 84 ReportStatus(nullptr, SHOWING_WEB_APP_BANNER);
65 RecordDidShowBanner("AppBanner.WebApp.Shown"); 85 RecordDidShowBanner("AppBanner.WebApp.Shown");
66 86
67 ASSERT_FALSE(will_show_.get()); 87 ASSERT_FALSE(will_show_.get());
68 will_show_.reset(new bool(true)); 88 will_show_.reset(new bool(true));
69 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); 89 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
70 } 90 }
71 91
92 void UpdateState(AppBannerManager::State state) override {
93 AppBannerManager::UpdateState(state);
94
95 if (state == AppBannerManager::State::PENDING_ENGAGEMENT)
96 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
97 }
98
72 private: 99 private:
73 bool IsDebugMode() const override { return false; } 100 bool IsDebugMode() const override { return false; }
74 101
75 base::Closure quit_closure_; 102 base::Closure quit_closure_;
76 std::unique_ptr<bool> will_show_; 103 std::unique_ptr<bool> will_show_;
77 104
78 DISALLOW_COPY_AND_ASSIGN(AppBannerManagerTest); 105 DISALLOW_COPY_AND_ASSIGN(AppBannerManagerTest);
79 }; 106 };
80 107
81 class AppBannerManagerBrowserTest : public InProcessBrowserTest { 108 class AppBannerManagerBrowserTest : public InProcessBrowserTest {
82 public: 109 public:
83 void SetUpOnMainThread() override { 110 void SetUpOnMainThread() override {
84 AppBannerSettingsHelper::SetTotalEngagementToTrigger(10); 111 AppBannerSettingsHelper::SetTotalEngagementToTrigger(10);
112 SiteEngagementScore::SetParamValuesForTesting();
85 ASSERT_TRUE(embedded_test_server()->Start()); 113 ASSERT_TRUE(embedded_test_server()->Start());
86 InProcessBrowserTest::SetUpOnMainThread(); 114 InProcessBrowserTest::SetUpOnMainThread();
87 } 115 }
88 116
89 void SetUpCommandLine(base::CommandLine* command_line) override { 117 void SetUpCommandLine(base::CommandLine* command_line) override {
90 // Make sure app banners are disabled in the browser, otherwise they will 118 // Make sure app banners are disabled in the browser, otherwise they will
91 // interfere with the test. 119 // interfere with the test.
92 command_line->AppendSwitch(switches::kDisableAddToShelf); 120 command_line->AppendSwitch(switches::kDisableAddToShelf);
93 } 121 }
94 122
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 histograms.ExpectTotalCount(banners::kMinutesHistogram, 0); 170 histograms.ExpectTotalCount(banners::kMinutesHistogram, 0);
143 histograms.ExpectTotalCount(banners::kInstallableStatusCodeHistogram, 171 histograms.ExpectTotalCount(banners::kInstallableStatusCodeHistogram,
144 0); 172 0);
145 } 173 }
146 service->ResetBaseScoreForURL(test_url, engagement); 174 service->ResetBaseScoreForURL(test_url, engagement);
147 ++iterations; 175 ++iterations;
148 } 176 }
149 177
150 // On the final loop, we expect the banner pipeline to trigger - the 178 // On the final loop, we expect the banner pipeline to trigger - the
151 // navigation should generate the final engagement to show the banner. Spin 179 // navigation should generate the final engagement to show the banner. Spin
152 // the run loop, which should be quit by either Stop() or ShowBanner(). 180 // the run loop and wait for the manager to finish.
153 base::RunLoop run_loop; 181 base::RunLoop run_loop;
154 manager->clear_will_show(); 182 manager->clear_will_show();
155 manager->Prepare(run_loop.QuitClosure()); 183 manager->Prepare(run_loop.QuitClosure());
156 ui_test_utils::NavigateToURL(browser, test_url); 184 ui_test_utils::NavigateToURL(browser, test_url);
157 run_loop.Run(); 185 run_loop.Run();
158 186
159 EXPECT_EQ(expected_to_show, manager->will_show()); 187 EXPECT_EQ(expected_to_show, manager->will_show());
160 EXPECT_FALSE(manager->is_active()); 188 EXPECT_FALSE(manager->is_active());
161 189
162 // Navigate to ensure the InstallableStatusCodeHistogram is logged.
163 ui_test_utils::NavigateToURL(browser, GURL("about:blank"));
164
165 // If in incognito, ensure that nothing is recorded. 190 // If in incognito, ensure that nothing is recorded.
166 // If showing the banner, ensure that the minutes histogram is recorded. 191 // If showing the banner, ensure that the minutes histogram is recorded.
167 if (browser->profile()->IsOffTheRecord()) { 192 if (browser->profile()->IsOffTheRecord()) {
168 histograms.ExpectTotalCount(banners::kMinutesHistogram, 0); 193 histograms.ExpectTotalCount(banners::kMinutesHistogram, 0);
169 histograms.ExpectTotalCount(banners::kInstallableStatusCodeHistogram, 0); 194 histograms.ExpectTotalCount(banners::kInstallableStatusCodeHistogram, 0);
170 } else { 195 } else {
171 histograms.ExpectTotalCount(banners::kMinutesHistogram, 196 histograms.ExpectTotalCount(banners::kMinutesHistogram,
172 (manager->will_show() ? 1 : 0)); 197 (manager->will_show() ? 1 : 0));
173 histograms.ExpectUniqueSample(banners::kInstallableStatusCodeHistogram, 198 histograms.ExpectUniqueSample(banners::kInstallableStatusCodeHistogram,
174 expected_code_for_histogram, 1); 199 expected_code_for_histogram, 1);
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 Browser* incognito_browser = 347 Browser* incognito_browser =
323 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank")); 348 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
324 std::unique_ptr<AppBannerManagerTest> manager( 349 std::unique_ptr<AppBannerManagerTest> manager(
325 CreateAppBannerManager(incognito_browser)); 350 CreateAppBannerManager(incognito_browser));
326 std::vector<double> engagement_scores{10}; 351 std::vector<double> engagement_scores{10};
327 RunBannerTest(incognito_browser, manager.get(), 352 RunBannerTest(incognito_browser, manager.get(),
328 "/banners/manifest_test_page.html", engagement_scores, 353 "/banners/manifest_test_page.html", engagement_scores,
329 IN_INCOGNITO, false); 354 IN_INCOGNITO, false);
330 } 355 }
331 356
357 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest,
358 CheckOnLoadWithSufficientEngagement) {
359 base::test::ScopedFeatureList feature_list;
360 feature_list.InitAndEnableFeature(
361 features::kCheckInstallabilityForBannerOnLoad);
362 std::unique_ptr<AppBannerManagerTest> manager(
363 CreateAppBannerManager(browser()));
364 std::vector<double> engagement_scores{10};
365 RunBannerTest(browser(), manager.get(), "/banners/manifest_test_page.html",
366 engagement_scores, SHOWING_WEB_APP_BANNER, true);
367 }
368
369 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest,
370 CheckOnLoadWithSufficientEngagementCancelDirect) {
371 base::test::ScopedFeatureList feature_list;
372 feature_list.InitAndEnableFeature(
373 features::kCheckInstallabilityForBannerOnLoad);
374 std::unique_ptr<AppBannerManagerTest> manager(
375 CreateAppBannerManager(browser()));
376 std::vector<double> engagement_scores{10};
377 RunBannerTest(browser(), manager.get(), "/banners/cancel_test_page.html",
378 engagement_scores, RENDERER_CANCELLED, false);
379 }
380
381 IN_PROC_BROWSER_TEST_F(
382 AppBannerManagerBrowserTest,
383 CheckOnLoadWithSufficientEngagementCancelBannerAfterPromptInHandler) {
384 base::test::ScopedFeatureList feature_list;
385 feature_list.InitAndEnableFeature(
386 features::kCheckInstallabilityForBannerOnLoad);
387 std::unique_ptr<AppBannerManagerTest> manager(
388 CreateAppBannerManager(browser()));
389 std::vector<double> engagement_scores{10};
390 RunBannerTest(browser(), manager.get(),
391 "/banners/prompt_in_handler_test_page.html", engagement_scores,
392 SHOWING_WEB_APP_BANNER, true);
393 std::string cancel_test_page_url =
394 GetURLOfPageWithManifest("/banners/cancel_test_page.html?manifest=",
395 "/banners/manifest_different_start_url.json");
396 RunBannerTest(browser(), manager.get(), cancel_test_page_url,
397 engagement_scores, RENDERER_CANCELLED, false);
398 }
399
400 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest,
401 CheckOnLoadWithoutSufficientEngagement) {
402 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1);
403 SiteEngagementService* service =
404 SiteEngagementService::Get(browser()->profile());
405 base::test::ScopedFeatureList feature_list;
406 feature_list.InitAndEnableFeature(
407 features::kCheckInstallabilityForBannerOnLoad);
408 std::unique_ptr<AppBannerManagerTest> manager(
409 CreateAppBannerManager(browser()));
410
411 base::HistogramTester histograms;
412 GURL test_url =
413 embedded_test_server()->GetURL("/banners/manifest_test_page.html");
414 service->ResetBaseScoreForURL(test_url, 0);
415
416 // First run through: expect the manager to end up stopped in the pending
417 // state, without showing a banner.
418 {
419 base::RunLoop run_loop;
420 manager->clear_will_show();
421 manager->Prepare(run_loop.QuitClosure());
422 ui_test_utils::NavigateToURL(browser(), test_url);
423 run_loop.Run();
424 }
425
426 EXPECT_FALSE(manager->will_show());
427 EXPECT_FALSE(manager->is_active());
428 EXPECT_TRUE(manager->is_pending_engagement());
429 EXPECT_TRUE(manager->need_to_log_status());
430
431 // Trigger an engagement increase that signals observers and expect the banner
432 // to be shown.
433 {
434 base::RunLoop run_loop;
435 manager->clear_will_show();
436 manager->Prepare(run_loop.QuitClosure());
437 service->HandleNavigation(
438 browser()->tab_strip_model()->GetActiveWebContents(),
439 ui::PageTransition::PAGE_TRANSITION_TYPED);
440 run_loop.Run();
441 }
442
443 EXPECT_TRUE(manager->will_show());
444 EXPECT_FALSE(manager->is_active());
445 EXPECT_FALSE(manager->need_to_log_status());
446 EXPECT_TRUE(manager->is_complete());
447
448 histograms.ExpectTotalCount(banners::kMinutesHistogram, 1);
449 histograms.ExpectUniqueSample(banners::kInstallableStatusCodeHistogram,
450 SHOWING_WEB_APP_BANNER, 1);
451 }
452
453 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, CheckOnLoadThenNavigate) {
454 base::test::ScopedFeatureList feature_list;
455 feature_list.InitAndEnableFeature(
456 features::kCheckInstallabilityForBannerOnLoad);
457 std::unique_ptr<AppBannerManagerTest> manager(
458 CreateAppBannerManager(browser()));
459
460 base::HistogramTester histograms;
461 GURL test_url =
462 embedded_test_server()->GetURL("/banners/manifest_test_page.html");
463
464 // First run through: expect the manager to end up stopped in the pending
465 // state, without showing a banner.
466 {
467 base::RunLoop run_loop;
468 manager->clear_will_show();
469 manager->Prepare(run_loop.QuitClosure());
470 ui_test_utils::NavigateToURL(browser(), test_url);
471 run_loop.Run();
472 }
473
474 EXPECT_FALSE(manager->will_show());
475 EXPECT_FALSE(manager->is_active());
476 EXPECT_TRUE(manager->is_pending_engagement());
477 EXPECT_TRUE(manager->need_to_log_status());
478
479 // Navigate and expect Stop() to be called.
480 {
481 base::RunLoop run_loop;
482 manager->clear_will_show();
483 manager->Prepare(run_loop.QuitClosure());
484 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
485 run_loop.Run();
486 }
487
488 EXPECT_FALSE(manager->will_show());
489 EXPECT_FALSE(manager->is_active());
490 EXPECT_FALSE(manager->need_to_log_status());
491
492 histograms.ExpectTotalCount(banners::kMinutesHistogram, 0);
493 histograms.ExpectUniqueSample(banners::kInstallableStatusCodeHistogram,
494 INSUFFICIENT_ENGAGEMENT, 1);
495 }
496
332 } // namespace banners 497 } // namespace banners
OLDNEW
« no previous file with comments | « chrome/browser/banners/app_banner_manager.cc ('k') | chrome/browser/engagement/site_engagement_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698