| OLD | NEW | 
|---|
| 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 "chrome/browser/banners/app_banner_manager.h" | 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/single_thread_task_runner.h" |  | 
| 10 #include "base/task_runner.h" |  | 
| 11 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" | 
| 12 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" | 
| 13 #include "chrome/browser/banners/app_banner_manager.h" | 11 #include "chrome/browser/banners/app_banner_manager.h" | 
| 14 #include "chrome/browser/banners/app_banner_metrics.h" | 12 #include "chrome/browser/banners/app_banner_metrics.h" | 
| 15 #include "chrome/browser/banners/app_banner_settings_helper.h" | 13 #include "chrome/browser/banners/app_banner_settings_helper.h" | 
| 16 #include "chrome/browser/installable/installable_manager.h" | 14 #include "chrome/browser/engagement/site_engagement_service.h" | 
|  | 15 #include "chrome/browser/installable/installable_logging.h" | 
| 17 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" | 
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 
| 19 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" | 
| 20 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" | 
| 21 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" | 
| 22 #include "content/public/common/content_switches.h" |  | 
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 
| 24 | 22 | 
| 25 namespace banners { | 23 namespace banners { | 
| 26 | 24 | 
| 27 // All calls to RequestAppBanner should terminate in one of Stop() (not showing | 25 // Browser tests for web app banners. | 
| 28 // banner) or ShowBanner(). This browser test uses this and overrides those two | 26 // NOTE: this test relies on service workers; failures and flakiness may be due | 
| 29 // methods to capture this information. | 27 // to changes in SW code. | 
| 30 class AppBannerManagerTest : public AppBannerManager { | 28 class AppBannerManagerTest : public AppBannerManager { | 
| 31  public: | 29  public: | 
| 32   explicit AppBannerManagerTest(content::WebContents* web_contents) | 30   explicit AppBannerManagerTest(content::WebContents* web_contents) | 
| 33       : AppBannerManager(web_contents) {} | 31       : AppBannerManager(web_contents) {} | 
|  | 32 | 
| 34   ~AppBannerManagerTest() override {} | 33   ~AppBannerManagerTest() override {} | 
| 35 | 34 | 
| 36   bool will_show() { return will_show_.get() && *will_show_; } | 35   bool will_show() { return will_show_.get() && *will_show_; } | 
| 37 | 36 | 
| 38   bool is_active() { return AppBannerManager::is_active(); } | 37   bool is_active() { return AppBannerManager::is_active(); } | 
| 39 | 38 | 
| 40   // Set the page transition of each banner request. | 39   bool need_to_log_status() { return need_to_log_status_; } | 
| 41   void set_page_transition_(ui::PageTransition transition) { | 40 | 
| 42     last_transition_type_ = transition; | 41   void Prepare(base::Closure quit_closure) { | 
|  | 42     will_show_.reset(nullptr); | 
|  | 43     quit_closure_ = quit_closure; | 
| 43   } | 44   } | 
| 44 | 45 | 
| 45   using AppBannerManager::RequestAppBanner; |  | 
| 46   void RequestAppBanner(const GURL& validated_url, |  | 
| 47                         bool is_debug_mode, |  | 
| 48                         base::Closure quit_closure) { |  | 
| 49     will_show_.reset(nullptr); |  | 
| 50     quit_closure_ = quit_closure; |  | 
| 51     AppBannerManager::RequestAppBanner(validated_url, is_debug_mode); |  | 
| 52   } |  | 
| 53 |  | 
| 54   bool need_to_log_status() { return need_to_log_status_; } |  | 
| 55 |  | 
| 56  protected: | 46  protected: | 
|  | 47   // All calls to RequestAppBanner should terminate in one of Stop() (not | 
|  | 48   // showing banner) or ShowBanner(). Override those two methods to capture test | 
|  | 49   // status. | 
| 57   void Stop() override { | 50   void Stop() override { | 
| 58     AppBannerManager::Stop(); | 51     AppBannerManager::Stop(); | 
| 59     ASSERT_FALSE(will_show_.get()); | 52     ASSERT_FALSE(will_show_.get()); | 
| 60     will_show_.reset(new bool(false)); | 53     will_show_.reset(new bool(false)); | 
| 61     base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | 54     base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | 
| 62   } | 55   } | 
| 63 | 56 | 
| 64   void ShowBanner() override { | 57   void ShowBanner() override { | 
| 65     // Fake the call to ReportStatus here - this is usually called in | 58     // Fake the call to ReportStatus here - this is usually called in | 
| 66     // platform-specific code which is not exposed here. | 59     // platform-specific code which is not exposed here. | 
| 67     ReportStatus(nullptr, SHOWING_WEB_APP_BANNER); | 60     ReportStatus(nullptr, SHOWING_WEB_APP_BANNER); | 
|  | 61     RecordDidShowBanner("AppBanner.WebApp.Shown"); | 
|  | 62 | 
| 68     ASSERT_FALSE(will_show_.get()); | 63     ASSERT_FALSE(will_show_.get()); | 
| 69     will_show_.reset(new bool(true)); | 64     will_show_.reset(new bool(true)); | 
| 70     base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | 65     base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | 
| 71   } | 66   } | 
| 72 | 67 | 
| 73   void DidStartNavigation(content::NavigationHandle* handle) override { |  | 
| 74     // Do nothing to ensure we never observe the site engagement service. |  | 
| 75   } |  | 
| 76 |  | 
| 77   void DidFinishLoad(content::RenderFrameHost* render_frame_host, |  | 
| 78                      const GURL& validated_url) override { |  | 
| 79     // Do nothing else to ensure the banner pipeline doesn't start. |  | 
| 80     validated_url_ = validated_url; |  | 
| 81   } |  | 
| 82 |  | 
| 83  private: | 68  private: | 
| 84   bool IsDebugMode() const override { return false; } | 69   bool IsDebugMode() const override { return false; } | 
| 85 | 70 | 
| 86   base::Closure quit_closure_; | 71   base::Closure quit_closure_; | 
| 87   std::unique_ptr<bool> will_show_; | 72   std::unique_ptr<bool> will_show_; | 
| 88 }; | 73 }; | 
| 89 | 74 | 
| 90 class AppBannerManagerBrowserTest : public InProcessBrowserTest { | 75 class AppBannerManagerBrowserTest : public InProcessBrowserTest { | 
| 91  public: | 76  public: | 
| 92   void SetUpOnMainThread() override { | 77   void SetUpOnMainThread() override { | 
| 93     AppBannerSettingsHelper::SetEngagementWeights(1, 1); | 78     AppBannerSettingsHelper::SetTotalEngagementToTrigger(10); | 
| 94     AppBannerSettingsHelper::SetTotalEngagementToTrigger(2); |  | 
| 95     ASSERT_TRUE(embedded_test_server()->Start()); | 79     ASSERT_TRUE(embedded_test_server()->Start()); | 
| 96     InProcessBrowserTest::SetUpOnMainThread(); | 80     InProcessBrowserTest::SetUpOnMainThread(); | 
| 97   } | 81   } | 
| 98 | 82 | 
| 99   void SetUpCommandLine(base::CommandLine* command_line) override { | 83   void SetUpCommandLine(base::CommandLine* command_line) override { | 
| 100     // Make sure app banners are disabled in the browser, otherwise they will | 84     // Make sure app banners are disabled in the browser, otherwise they will | 
| 101     // interfere with the test. | 85     // interfere with the test. | 
| 102     command_line->AppendSwitch(switches::kDisableAddToShelf); | 86     command_line->AppendSwitch(switches::kDisableAddToShelf); | 
| 103   } | 87   } | 
| 104 | 88 | 
| 105  protected: | 89  protected: | 
| 106   void RequestAppBanner(AppBannerManagerTest* manager, |  | 
| 107                         const GURL& url, |  | 
| 108                         base::RunLoop& run_loop, |  | 
| 109                         ui::PageTransition transition, |  | 
| 110                         bool expected_to_show) { |  | 
| 111     manager->set_page_transition_(transition); |  | 
| 112     manager->RequestAppBanner(url, false, run_loop.QuitClosure()); |  | 
| 113     run_loop.Run(); |  | 
| 114 |  | 
| 115     EXPECT_EQ(expected_to_show, manager->will_show()); |  | 
| 116     ASSERT_FALSE(manager->is_active()); |  | 
| 117 |  | 
| 118     // If showing the banner, ensure that the minutes histogram is recorded. |  | 
| 119     histograms_.ExpectTotalCount(banners::kMinutesHistogram, |  | 
| 120                                 (manager->will_show() ? 1 : 0)); |  | 
| 121   } |  | 
| 122 |  | 
| 123   // Returns a test server URL to a page controlled by a service worker with | 90   // Returns a test server URL to a page controlled by a service worker with | 
| 124   // |manifest_url| injected as the manifest tag. | 91   // |manifest_url| injected as the manifest tag. | 
| 125   std::string GetURLOfPageWithServiceWorkerAndManifest( | 92   std::string GetURLOfPageWithServiceWorkerAndManifest( | 
| 126       const std::string& manifest_url) { | 93       const std::string& manifest_url) { | 
| 127     return "/banners/manifest_test_page.html?manifest=" + | 94     return "/banners/manifest_test_page.html?manifest=" + | 
| 128            embedded_test_server()->GetURL(manifest_url).spec(); | 95            embedded_test_server()->GetURL(manifest_url).spec(); | 
| 129   } | 96   } | 
| 130 | 97 | 
| 131   void RunBannerTest(const std::string& url, | 98   void RunBannerTest(const std::string& url, | 
| 132                      ui::PageTransition transition, | 99                      const std::vector<double>& engagement_scores, | 
| 133                      unsigned int unshown_repetitions, |  | 
| 134                      InstallableStatusCode expected_code_for_histogram, | 100                      InstallableStatusCode expected_code_for_histogram, | 
| 135                      bool expected_to_show) { | 101                      bool expected_to_show) { | 
| 136     std::string valid_page(url); | 102     base::HistogramTester histograms; | 
| 137     GURL test_url = embedded_test_server()->GetURL(valid_page); | 103     GURL test_url = embedded_test_server()->GetURL(url); | 
| 138     content::WebContents* web_contents = | 104     content::WebContents* web_contents = | 
| 139         browser()->tab_strip_model()->GetActiveWebContents(); | 105         browser()->tab_strip_model()->GetActiveWebContents(); | 
| 140     std::unique_ptr<AppBannerManagerTest> manager( | 106     std::unique_ptr<AppBannerManagerTest> manager( | 
| 141         new AppBannerManagerTest(web_contents)); | 107         new AppBannerManagerTest(web_contents)); | 
| 142 | 108 | 
| 143     for (unsigned int i = 1; i <= unshown_repetitions; ++i) { | 109     // Loop through the vector of engagement scores. We only expect the banner | 
| 144       ui_test_utils::NavigateToURL(browser(), test_url); | 110     // pipeline to trigger on the last one; otherwise, nothing is expected to | 
| 145       base::RunLoop run_loop; | 111     // happen. | 
| 146       RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 112     int iterations = 0; | 
| 147                        run_loop, transition, false); | 113     SiteEngagementService* service = | 
| 148       CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, i, i); | 114         SiteEngagementService::Get(browser()->profile()); | 
| 149       AppBannerManager::SetTimeDeltaForTesting(i); | 115     for (double engagement : engagement_scores) { | 
|  | 116       if (iterations > 0) { | 
|  | 117         ui_test_utils::NavigateToURL(browser(), test_url); | 
|  | 118 | 
|  | 119         EXPECT_EQ(false, manager->will_show()); | 
|  | 120         EXPECT_FALSE(manager->is_active()); | 
|  | 121 | 
|  | 122         histograms.ExpectTotalCount(banners::kMinutesHistogram, 0); | 
|  | 123         histograms.ExpectTotalCount(banners::kInstallableStatusCodeHistogram, | 
|  | 124                                      0); | 
|  | 125       } | 
|  | 126       service->ResetScoreForURL(test_url, engagement); | 
|  | 127       ++iterations; | 
| 150     } | 128     } | 
| 151 | 129 | 
| 152     // On the final loop, check whether the banner triggered or not as expected. | 130     // On the final loop, we expect the banner pipeline to trigger - the | 
|  | 131     // navigation should generate the final engagement to show the banner. Spin | 
|  | 132     // the run loop, which should be quit by either Stop() or ShowBanner(). | 
|  | 133     base::RunLoop run_loop; | 
|  | 134     manager->Prepare(run_loop.QuitClosure()); | 
| 153     ui_test_utils::NavigateToURL(browser(), test_url); | 135     ui_test_utils::NavigateToURL(browser(), test_url); | 
| 154     base::RunLoop run_loop; | 136     run_loop.Run(); | 
| 155     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 137 | 
| 156                      run_loop, transition, expected_to_show); | 138     EXPECT_EQ(expected_to_show, manager->will_show()); | 
|  | 139     EXPECT_FALSE(manager->is_active()); | 
|  | 140 | 
| 157     // Navigate to ensure the InstallableStatusCodeHistogram is logged. | 141     // Navigate to ensure the InstallableStatusCodeHistogram is logged. | 
| 158     ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | 142     ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | 
| 159     CheckInstallableStatusCodeHistogram(expected_code_for_histogram, 1, | 143 | 
| 160                                         unshown_repetitions + 1); | 144     // If showing the banner, ensure that the minutes histogram is recorded. | 
|  | 145     histograms.ExpectTotalCount(banners::kMinutesHistogram, | 
|  | 146                                  (manager->will_show() ? 1 : 0)); | 
|  | 147     histograms.ExpectUniqueSample(banners::kInstallableStatusCodeHistogram, | 
|  | 148                                    expected_code_for_histogram, 1); | 
| 161     EXPECT_FALSE(manager->need_to_log_status()); | 149     EXPECT_FALSE(manager->need_to_log_status()); | 
| 162   } | 150   } | 
|  | 151 }; | 
| 163 | 152 | 
| 164   void CheckInstallableStatusCodeHistogram(InstallableStatusCode expected_code, | 153 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerCreated) { | 
| 165                                            int expected_count, | 154   std::vector<double> engagement_scores{0, 10}; | 
| 166                                            int total_count) { | 155   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
| 167     histograms_.ExpectBucketCount(banners::kInstallableStatusCodeHistogram, |  | 
| 168                                   expected_code, expected_count); |  | 
| 169     histograms_.ExpectTotalCount(banners::kInstallableStatusCodeHistogram, |  | 
| 170                                  total_count); |  | 
| 171   } |  | 
| 172 |  | 
| 173  private: |  | 
| 174   base::HistogramTester histograms_; |  | 
| 175 }; |  | 
| 176 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerCreatedDirect) { |  | 
| 177   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED, |  | 
| 178                 1, SHOWING_WEB_APP_BANNER, true); |  | 
| 179 } |  | 
| 180 |  | 
| 181 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 182                        WebAppBannerCreatedDirectLargerTotal) { |  | 
| 183   AppBannerSettingsHelper::SetTotalEngagementToTrigger(4); |  | 
| 184   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED, |  | 
| 185                 3, SHOWING_WEB_APP_BANNER, true); |  | 
| 186 } |  | 
| 187 |  | 
| 188 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 189                        WebAppBannerCreatedDirectSmallerTotal) { |  | 
| 190   AppBannerSettingsHelper::SetTotalEngagementToTrigger(1); |  | 
| 191   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED, |  | 
| 192                 0, SHOWING_WEB_APP_BANNER, true); |  | 
| 193 } |  | 
| 194 |  | 
| 195 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 196                        WebAppBannerCreatedDirectSingle) { |  | 
| 197   AppBannerSettingsHelper::SetEngagementWeights(2, 1); |  | 
| 198   RunBannerTest("/banners/manifest_test_page.html", |  | 
| 199                 ui::PAGE_TRANSITION_GENERATED, 0, SHOWING_WEB_APP_BANNER, true); |  | 
| 200 } |  | 
| 201 |  | 
| 202 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 203                        WebAppBannerCreatedDirectMultiple) { |  | 
| 204   AppBannerSettingsHelper::SetEngagementWeights(0.5, 1); |  | 
| 205   RunBannerTest("/banners/manifest_test_page.html", |  | 
| 206                 ui::PAGE_TRANSITION_GENERATED, 3, SHOWING_WEB_APP_BANNER, true); |  | 
| 207 } |  | 
| 208 |  | 
| 209 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 210                        WebAppBannerCreatedDirectMultipleLargerTotal) { |  | 
| 211   AppBannerSettingsHelper::SetEngagementWeights(0.5, 1); |  | 
| 212   AppBannerSettingsHelper::SetTotalEngagementToTrigger(3); |  | 
| 213   RunBannerTest("/banners/manifest_test_page.html", |  | 
| 214                 ui::PAGE_TRANSITION_GENERATED, 5, SHOWING_WEB_APP_BANNER, true); |  | 
| 215 } |  | 
| 216 |  | 
| 217 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 218                        WebAppBannerCreatedDirectMultipleSmallerTotal) { |  | 
| 219   AppBannerSettingsHelper::SetEngagementWeights(0.5, 1); |  | 
| 220   AppBannerSettingsHelper::SetTotalEngagementToTrigger(1); |  | 
| 221   RunBannerTest("/banners/manifest_test_page.html", |  | 
| 222                 ui::PAGE_TRANSITION_GENERATED, 1, SHOWING_WEB_APP_BANNER, true); |  | 
| 223 } |  | 
| 224 |  | 
| 225 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 226                        WebAppBannerCreatedIndirect) { |  | 
| 227   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, 1, |  | 
| 228                 SHOWING_WEB_APP_BANNER, true); |  | 
| 229 } |  | 
| 230 |  | 
| 231 // Flaky http://crbug.com/660798 |  | 
| 232 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 233                        DISABLED_WebAppBannerCreatedIndirectLargerTotal) { |  | 
| 234   AppBannerSettingsHelper::SetTotalEngagementToTrigger(5); |  | 
| 235   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, 4, |  | 
| 236                 SHOWING_WEB_APP_BANNER, true); | 156                 SHOWING_WEB_APP_BANNER, true); | 
| 237 } | 157 } | 
| 238 | 158 | 
| 239 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 159 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 
| 240                        WebAppBannerCreatedIndirectSmallerTotal) { | 160                        WebAppBannerCreatedImmediately) { | 
| 241   AppBannerSettingsHelper::SetTotalEngagementToTrigger(1); | 161   std::vector<double> engagement_scores{10}; | 
| 242   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, 0, | 162   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
| 243                 SHOWING_WEB_APP_BANNER, true); | 163                 SHOWING_WEB_APP_BANNER, true); | 
| 244 } | 164 } | 
| 245 | 165 | 
| 246 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 166 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 
| 247                        WebAppBannerCreatedIndirectSingle) { | 167                        WebAppBannerCreatedAfterSeveralVisits) { | 
| 248   AppBannerSettingsHelper::SetEngagementWeights(1, 3); | 168   std::vector<double> engagement_scores{0, 1, 2, 3, 4, 5, 10}; | 
| 249   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_RELOAD, | 169   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
| 250                 0, SHOWING_WEB_APP_BANNER, true); |  | 
| 251 } |  | 
| 252 |  | 
| 253 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 254                        WebAppBannerCreatedIndirectMultiple) { |  | 
| 255   AppBannerSettingsHelper::SetEngagementWeights(1, 0.5); |  | 
| 256   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, 3, |  | 
| 257                 SHOWING_WEB_APP_BANNER, true); | 170                 SHOWING_WEB_APP_BANNER, true); | 
| 258 } | 171 } | 
| 259 | 172 | 
| 260 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 173 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 
| 261                        WebAppBannerCreatedIndirectMultipleLargerTotal) { | 174                        WebAppBannerNotSeenAfterShowing) { | 
| 262   AppBannerSettingsHelper::SetEngagementWeights(1, 0.5); | 175   std::vector<double> engagement_scores{0, 10}; | 
| 263   AppBannerSettingsHelper::SetTotalEngagementToTrigger(4); | 176   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
| 264   RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, 7, | 177                 SHOWING_WEB_APP_BANNER, true); | 
|  | 178 | 
|  | 179   AppBannerManager::SetTimeDeltaForTesting(1); | 
|  | 180   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
|  | 181                 PREVIOUSLY_IGNORED, false); | 
|  | 182 | 
|  | 183   AppBannerManager::SetTimeDeltaForTesting(13); | 
|  | 184   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
|  | 185                 PREVIOUSLY_IGNORED, false); | 
|  | 186 | 
|  | 187   AppBannerManager::SetTimeDeltaForTesting(14); | 
|  | 188   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
|  | 189                 SHOWING_WEB_APP_BANNER, true); | 
|  | 190 | 
|  | 191   AppBannerSettingsHelper::SetDaysAfterDismissAndIgnoreToTrigger(90, 2); | 
|  | 192 | 
|  | 193   AppBannerManager::SetTimeDeltaForTesting(16); | 
|  | 194   RunBannerTest("/banners/manifest_test_page.html", engagement_scores, | 
| 265                 SHOWING_WEB_APP_BANNER, true); | 195                 SHOWING_WEB_APP_BANNER, true); | 
| 266 } | 196 } | 
| 267 | 197 | 
| 268 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 198 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 
| 269                        WebAppBannerCreatedVarious) { |  | 
| 270   AppBannerSettingsHelper::SetEngagementWeights(0.5, 0.25); |  | 
| 271 |  | 
| 272   std::string valid_page("/banners/manifest_test_page.html"); |  | 
| 273   GURL test_url = embedded_test_server()->GetURL(valid_page); |  | 
| 274   content::WebContents* web_contents = |  | 
| 275       browser()->tab_strip_model()->GetActiveWebContents(); |  | 
| 276 |  | 
| 277   std::unique_ptr<AppBannerManagerTest> manager( |  | 
| 278       new AppBannerManagerTest(web_contents)); |  | 
| 279 |  | 
| 280   // Add a direct nav on day 1. |  | 
| 281   { |  | 
| 282     base::RunLoop run_loop; |  | 
| 283     ui_test_utils::NavigateToURL(browser(), test_url); |  | 
| 284     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), |  | 
| 285                      run_loop, ui::PAGE_TRANSITION_TYPED, false); |  | 
| 286     CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 1, 1); |  | 
| 287     EXPECT_FALSE(manager->need_to_log_status()); |  | 
| 288   } |  | 
| 289 |  | 
| 290   // Add an indirect nav on day 1 which is ignored. |  | 
| 291   { |  | 
| 292     base::RunLoop run_loop; |  | 
| 293     ui_test_utils::NavigateToURL(browser(), test_url); |  | 
| 294     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), |  | 
| 295                      run_loop, ui::PAGE_TRANSITION_LINK, false); |  | 
| 296     CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 2, 2); |  | 
| 297     EXPECT_FALSE(manager->need_to_log_status()); |  | 
| 298     AppBannerManager::SetTimeDeltaForTesting(1); |  | 
| 299   } |  | 
| 300 |  | 
| 301   // Add an indirect nav on day 2. |  | 
| 302   { |  | 
| 303     base::RunLoop run_loop; |  | 
| 304     ui_test_utils::NavigateToURL(browser(), test_url); |  | 
| 305     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), |  | 
| 306                      run_loop, ui::PAGE_TRANSITION_MANUAL_SUBFRAME, false); |  | 
| 307     CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 3, 3); |  | 
| 308     EXPECT_FALSE(manager->need_to_log_status()); |  | 
| 309   } |  | 
| 310 |  | 
| 311   // Add a direct nav on day 2 which overrides. |  | 
| 312   { |  | 
| 313     base::RunLoop run_loop; |  | 
| 314     ui_test_utils::NavigateToURL(browser(), test_url); |  | 
| 315     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), |  | 
| 316                      run_loop, ui::PAGE_TRANSITION_GENERATED, false); |  | 
| 317     CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 4, 4); |  | 
| 318     EXPECT_FALSE(manager->need_to_log_status()); |  | 
| 319     AppBannerManager::SetTimeDeltaForTesting(2); |  | 
| 320   } |  | 
| 321 |  | 
| 322   // Add a direct nav on day 3. |  | 
| 323   { |  | 
| 324     base::RunLoop run_loop; |  | 
| 325     ui_test_utils::NavigateToURL(browser(), test_url); |  | 
| 326     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), |  | 
| 327                      run_loop, ui::PAGE_TRANSITION_GENERATED, false); |  | 
| 328     CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 5, 5); |  | 
| 329     EXPECT_FALSE(manager->need_to_log_status()); |  | 
| 330     AppBannerManager::SetTimeDeltaForTesting(3); |  | 
| 331   } |  | 
| 332 |  | 
| 333   // Add an indirect nav on day 4. |  | 
| 334   { |  | 
| 335     base::RunLoop run_loop; |  | 
| 336     ui_test_utils::NavigateToURL(browser(), test_url); |  | 
| 337     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), |  | 
| 338                      run_loop, ui::PAGE_TRANSITION_FORM_SUBMIT, false); |  | 
| 339     EXPECT_FALSE(manager->need_to_log_status()); |  | 
| 340     CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 6, 6); |  | 
| 341   } |  | 
| 342 |  | 
| 343   // Add a direct nav on day 4 which should trigger the banner. |  | 
| 344   { |  | 
| 345     base::RunLoop run_loop; |  | 
| 346     ui_test_utils::NavigateToURL(browser(), test_url); |  | 
| 347     RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), |  | 
| 348                      run_loop, ui::PAGE_TRANSITION_TYPED, true); |  | 
| 349     EXPECT_FALSE(manager->need_to_log_status()); |  | 
| 350     CheckInstallableStatusCodeHistogram(SHOWING_WEB_APP_BANNER, 1, 7); |  | 
| 351   } |  | 
| 352 } |  | 
| 353 |  | 
| 354 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |  | 
| 355                        WebAppBannerNoTypeInManifest) { | 199                        WebAppBannerNoTypeInManifest) { | 
|  | 200   std::vector<double> engagement_scores{0, 10}; | 
| 356   RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( | 201   RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( | 
| 357                     "/banners/manifest_no_type.json"), | 202                     "/banners/manifest_no_type.json"), | 
| 358                 ui::PAGE_TRANSITION_TYPED, 1, SHOWING_WEB_APP_BANNER, true); | 203                 engagement_scores, SHOWING_WEB_APP_BANNER, true); | 
| 359 } | 204 } | 
| 360 | 205 | 
| 361 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 206 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 
| 362                        WebAppBannerNoTypeInManifestCapsExtension) { | 207                        WebAppBannerNoTypeInManifestCapsExtension) { | 
|  | 208   std::vector<double> engagement_scores{0, 10}; | 
| 363   RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( | 209   RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( | 
| 364                     "/banners/manifest_no_type_caps.json"), | 210                     "/banners/manifest_no_type_caps.json"), | 
| 365                 ui::PAGE_TRANSITION_TYPED, 1, SHOWING_WEB_APP_BANNER, true); | 211                 engagement_scores, SHOWING_WEB_APP_BANNER, true); | 
| 366 } | 212 } | 
| 367 | 213 | 
| 368 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, NoManifest) { | 214 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, NoManifest) { | 
| 369   RunBannerTest("/banners/no_manifest_test_page.html", | 215   std::vector<double> engagement_scores{10}; | 
| 370                 ui::PAGE_TRANSITION_TYPED, 0, NO_MANIFEST, false); | 216   RunBannerTest("/banners/no_manifest_test_page.html", engagement_scores, | 
|  | 217                 NO_MANIFEST, false); | 
| 371 } | 218 } | 
| 372 | 219 | 
| 373 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, MissingManifest) { | 220 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, MissingManifest) { | 
|  | 221   std::vector<double> engagement_scores{10}; | 
| 374   RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( | 222   RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( | 
| 375                     "/banners/manifest_missing.json"), | 223                     "/banners/manifest_missing.json"), | 
| 376                 ui::PAGE_TRANSITION_TYPED, 0, MANIFEST_EMPTY, false); | 224                 engagement_scores, MANIFEST_EMPTY, false); | 
| 377 } | 225 } | 
| 378 | 226 | 
| 379 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, CancelBannerDirect) { | 227 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, CancelBannerDirect) { | 
| 380   RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED, 1, | 228   std::vector<double> engagement_scores{10}; | 
| 381                 RENDERER_CANCELLED, false); | 229   RunBannerTest("/banners/cancel_test_page.html", engagement_scores, | 
| 382 } |  | 
| 383 |  | 
| 384 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, CancelBannerIndirect) { |  | 
| 385   AppBannerSettingsHelper::SetEngagementWeights(1, 0.5); |  | 
| 386   RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_LINK, 3, |  | 
| 387                 RENDERER_CANCELLED, false); | 230                 RENDERER_CANCELLED, false); | 
| 388 } | 231 } | 
| 389 | 232 | 
| 390 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, PromptBanner) { | 233 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, PromptBanner) { | 
| 391   RunBannerTest("/banners/prompt_test_page.html", ui::PAGE_TRANSITION_TYPED, 1, | 234   std::vector<double> engagement_scores{0, 5, 10}; | 
|  | 235   RunBannerTest("/banners/prompt_test_page.html", engagement_scores, | 
| 392                 SHOWING_WEB_APP_BANNER, true); | 236                 SHOWING_WEB_APP_BANNER, true); | 
| 393 } | 237 } | 
| 394 | 238 | 
| 395 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, PromptBannerInHandler) { | 239 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, PromptBannerInHandler) { | 
| 396   RunBannerTest("/banners/prompt_in_handler_test_page.html", | 240   std::vector<double> engagement_scores{0, 2, 5, 10}; | 
| 397                 ui::PAGE_TRANSITION_TYPED, 1, SHOWING_WEB_APP_BANNER, true); | 241   RunBannerTest("/banners/prompt_in_handler_test_page.html", engagement_scores, | 
|  | 242                 SHOWING_WEB_APP_BANNER, true); | 
| 398 } | 243 } | 
| 399 | 244 | 
| 400 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerInIFrame) { | 245 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerInIFrame) { | 
| 401   RunBannerTest("/banners/iframe_test_page.html", ui::PAGE_TRANSITION_TYPED, 0, | 246   std::vector<double> engagement_scores{10}; | 
|  | 247   RunBannerTest("/banners/iframe_test_page.html", engagement_scores, | 
| 402                 NO_MANIFEST, false); | 248                 NO_MANIFEST, false); | 
| 403 } | 249 } | 
| 404 | 250 | 
| 405 }  // namespace banners | 251 }  // namespace banners | 
| OLD | NEW | 
|---|