| 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 <memory> |
| 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" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
| 11 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "chrome/browser/banners/app_banner_manager.h" | 13 #include "chrome/browser/banners/app_banner_manager.h" |
| 14 #include "chrome/browser/banners/app_banner_metrics.h" | 14 #include "chrome/browser/banners/app_banner_metrics.h" |
| 15 #include "chrome/browser/banners/app_banner_settings_helper.h" | 15 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 16 #include "chrome/browser/installable/installable_manager.h" | 16 #include "chrome/browser/installable/installable_manager.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/render_frame_host.h" |
| 22 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 24 #include "mojo/public/cpp/bindings/binding.h" |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" | 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 26 #include "services/shell/public/cpp/interface_registry.h" |
| 27 #include "third_party/WebKit/public/platform/modules/app_banner/app_banner.mojom
.h" |
| 24 | 28 |
| 25 namespace banners { | 29 namespace banners { |
| 26 | 30 |
| 27 // All calls to RequestAppBanner should terminate in one of Stop() (not showing | 31 // All calls to RequestAppBanner should terminate in one of Stop() (not showing |
| 28 // banner) or ShowBanner(). This browser test uses this and overrides those two | 32 // banner) or ShowBanner(). This browser test uses this and overrides those two |
| 29 // methods to capture this information. | 33 // methods to capture this information. |
| 30 class AppBannerManagerTest : public AppBannerManager { | 34 class AppBannerManagerTest : public AppBannerManager { |
| 31 public: | 35 public: |
| 32 explicit AppBannerManagerTest(content::WebContents* web_contents) | 36 explicit AppBannerManagerTest(content::WebContents* web_contents) |
| 33 : AppBannerManager(web_contents) {} | 37 : AppBannerManager(web_contents) {} |
| 34 ~AppBannerManagerTest() override {} | 38 ~AppBannerManagerTest() override {} |
| 35 | 39 |
| 40 void BindHandle(mojo::ScopedMessagePipeHandle handle) { |
| 41 binding_.reset(new mojo::Binding<blink::mojom::AppBannerService>( |
| 42 this, |
| 43 mojo::MakeRequest<blink::mojom::AppBannerService>(std::move(handle)))); |
| 44 } |
| 45 |
| 36 bool will_show() { return will_show_.get() && *will_show_; } | 46 bool will_show() { return will_show_.get() && *will_show_; } |
| 37 | 47 |
| 38 bool is_active() { return AppBannerManager::is_active(); } | 48 bool is_active() { return AppBannerManager::is_active(); } |
| 39 | 49 |
| 40 // Set the page transition of each banner request. | 50 // Set the page transition of each banner request. |
| 41 void set_page_transition_(ui::PageTransition transition) { | 51 void set_page_transition_(ui::PageTransition transition) { |
| 42 last_transition_type_ = transition; | 52 last_transition_type_ = transition; |
| 43 } | 53 } |
| 44 | 54 |
| 45 using AppBannerManager::RequestAppBanner; | 55 using AppBannerManager::RequestAppBanner; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 76 | 86 |
| 77 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 87 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
| 78 const GURL& validated_url) override { | 88 const GURL& validated_url) override { |
| 79 // Do nothing else to ensure the banner pipeline doesn't start. | 89 // Do nothing else to ensure the banner pipeline doesn't start. |
| 80 validated_url_ = validated_url; | 90 validated_url_ = validated_url; |
| 81 } | 91 } |
| 82 | 92 |
| 83 private: | 93 private: |
| 84 bool IsDebugMode() const override { return false; } | 94 bool IsDebugMode() const override { return false; } |
| 85 | 95 |
| 96 std::unique_ptr<mojo::Binding<blink::mojom::AppBannerService>> binding_; |
| 97 |
| 86 base::Closure quit_closure_; | 98 base::Closure quit_closure_; |
| 87 std::unique_ptr<bool> will_show_; | 99 std::unique_ptr<bool> will_show_; |
| 88 }; | 100 }; |
| 89 | 101 |
| 90 class AppBannerManagerBrowserTest : public InProcessBrowserTest { | 102 class AppBannerManagerBrowserTest : public InProcessBrowserTest { |
| 91 public: | 103 public: |
| 92 void SetUpOnMainThread() override { | 104 void SetUpOnMainThread() override { |
| 93 AppBannerSettingsHelper::SetEngagementWeights(1, 1); | 105 AppBannerSettingsHelper::SetEngagementWeights(1, 1); |
| 94 AppBannerSettingsHelper::SetTotalEngagementToTrigger(2); | 106 AppBannerSettingsHelper::SetTotalEngagementToTrigger(2); |
| 95 ASSERT_TRUE(embedded_test_server()->Start()); | 107 ASSERT_TRUE(embedded_test_server()->Start()); |
| 96 InProcessBrowserTest::SetUpOnMainThread(); | 108 InProcessBrowserTest::SetUpOnMainThread(); |
| 97 } | 109 } |
| 98 | 110 |
| 99 void SetUpCommandLine(base::CommandLine* command_line) override { | 111 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 100 // Make sure app banners are disabled in the browser, otherwise they will | 112 // Make sure app banners are disabled in the browser, otherwise they will |
| 101 // interfere with the test. | 113 // interfere with the test. |
| 102 command_line->AppendSwitch(switches::kDisableAddToShelf); | 114 command_line->AppendSwitch(switches::kDisableAddToShelf); |
| 103 } | 115 } |
| 104 | 116 |
| 105 protected: | 117 protected: |
| 106 void RequestAppBanner(AppBannerManagerTest* manager, | 118 void RequestAppBanner(AppBannerManagerTest* manager, |
| 107 const GURL& url, | 119 content::WebContents* web_contents, |
| 108 base::RunLoop& run_loop, | 120 base::RunLoop& run_loop, |
| 109 ui::PageTransition transition, | 121 ui::PageTransition transition, |
| 110 bool expected_to_show) { | 122 bool expected_to_show) { |
| 123 // Intercept the app banner Mojo requests, since |
| 124 // AppBannerManager::BindToMojoRequest does not bind this test class. |
| 125 web_contents->GetMainFrame()->GetInterfaceRegistry()->AddInterface( |
| 126 blink::mojom::AppBannerService::Name_, |
| 127 base::Bind(&AppBannerManagerTest::BindHandle, |
| 128 base::Unretained(manager))); |
| 129 |
| 111 manager->set_page_transition_(transition); | 130 manager->set_page_transition_(transition); |
| 112 manager->RequestAppBanner(url, false, run_loop.QuitClosure()); | 131 manager->RequestAppBanner(web_contents->GetLastCommittedURL(), false, |
| 132 run_loop.QuitClosure()); |
| 113 run_loop.Run(); | 133 run_loop.Run(); |
| 114 | 134 |
| 115 EXPECT_EQ(expected_to_show, manager->will_show()); | 135 EXPECT_EQ(expected_to_show, manager->will_show()); |
| 116 ASSERT_FALSE(manager->is_active()); | 136 ASSERT_FALSE(manager->is_active()); |
| 117 | 137 |
| 118 // If showing the banner, ensure that the minutes histogram is recorded. | 138 // If showing the banner, ensure that the minutes histogram is recorded. |
| 119 histograms_.ExpectTotalCount(banners::kMinutesHistogram, | 139 histograms_.ExpectTotalCount(banners::kMinutesHistogram, |
| 120 (manager->will_show() ? 1 : 0)); | 140 (manager->will_show() ? 1 : 0)); |
| 121 } | 141 } |
| 122 | 142 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 136 std::string valid_page(url); | 156 std::string valid_page(url); |
| 137 GURL test_url = embedded_test_server()->GetURL(valid_page); | 157 GURL test_url = embedded_test_server()->GetURL(valid_page); |
| 138 content::WebContents* web_contents = | 158 content::WebContents* web_contents = |
| 139 browser()->tab_strip_model()->GetActiveWebContents(); | 159 browser()->tab_strip_model()->GetActiveWebContents(); |
| 140 std::unique_ptr<AppBannerManagerTest> manager( | 160 std::unique_ptr<AppBannerManagerTest> manager( |
| 141 new AppBannerManagerTest(web_contents)); | 161 new AppBannerManagerTest(web_contents)); |
| 142 | 162 |
| 143 for (unsigned int i = 1; i <= unshown_repetitions; ++i) { | 163 for (unsigned int i = 1; i <= unshown_repetitions; ++i) { |
| 144 ui_test_utils::NavigateToURL(browser(), test_url); | 164 ui_test_utils::NavigateToURL(browser(), test_url); |
| 145 base::RunLoop run_loop; | 165 base::RunLoop run_loop; |
| 146 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 166 RequestAppBanner(manager.get(), web_contents, run_loop, transition, |
| 147 run_loop, transition, false); | 167 false); |
| 148 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, i, i); | 168 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, i, i); |
| 149 AppBannerManager::SetTimeDeltaForTesting(i); | 169 AppBannerManager::SetTimeDeltaForTesting(i); |
| 150 } | 170 } |
| 151 | 171 |
| 152 // On the final loop, check whether the banner triggered or not as expected. | 172 // On the final loop, check whether the banner triggered or not as expected. |
| 153 ui_test_utils::NavigateToURL(browser(), test_url); | 173 ui_test_utils::NavigateToURL(browser(), test_url); |
| 154 base::RunLoop run_loop; | 174 base::RunLoop run_loop; |
| 155 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 175 RequestAppBanner(manager.get(), web_contents, run_loop, transition, |
| 156 run_loop, transition, expected_to_show); | 176 expected_to_show); |
| 157 // Navigate to ensure the InstallableStatusCodeHistogram is logged. | 177 // Navigate to ensure the InstallableStatusCodeHistogram is logged. |
| 158 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | 178 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
| 159 CheckInstallableStatusCodeHistogram(expected_code_for_histogram, 1, | 179 CheckInstallableStatusCodeHistogram(expected_code_for_histogram, 1, |
| 160 unshown_repetitions + 1); | 180 unshown_repetitions + 1); |
| 161 EXPECT_FALSE(manager->need_to_log_status()); | 181 EXPECT_FALSE(manager->need_to_log_status()); |
| 162 } | 182 } |
| 163 | 183 |
| 164 void CheckInstallableStatusCodeHistogram(InstallableStatusCode expected_code, | 184 void CheckInstallableStatusCodeHistogram(InstallableStatusCode expected_code, |
| 165 int expected_count, | 185 int expected_count, |
| 166 int total_count) { | 186 int total_count) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 content::WebContents* web_contents = | 293 content::WebContents* web_contents = |
| 274 browser()->tab_strip_model()->GetActiveWebContents(); | 294 browser()->tab_strip_model()->GetActiveWebContents(); |
| 275 | 295 |
| 276 std::unique_ptr<AppBannerManagerTest> manager( | 296 std::unique_ptr<AppBannerManagerTest> manager( |
| 277 new AppBannerManagerTest(web_contents)); | 297 new AppBannerManagerTest(web_contents)); |
| 278 | 298 |
| 279 // Add a direct nav on day 1. | 299 // Add a direct nav on day 1. |
| 280 { | 300 { |
| 281 base::RunLoop run_loop; | 301 base::RunLoop run_loop; |
| 282 ui_test_utils::NavigateToURL(browser(), test_url); | 302 ui_test_utils::NavigateToURL(browser(), test_url); |
| 283 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 303 RequestAppBanner(manager.get(), web_contents, run_loop, |
| 284 run_loop, ui::PAGE_TRANSITION_TYPED, false); | 304 ui::PAGE_TRANSITION_TYPED, false); |
| 285 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 1, 1); | 305 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 1, 1); |
| 286 EXPECT_FALSE(manager->need_to_log_status()); | 306 EXPECT_FALSE(manager->need_to_log_status()); |
| 287 } | 307 } |
| 288 | 308 |
| 289 // Add an indirect nav on day 1 which is ignored. | 309 // Add an indirect nav on day 1 which is ignored. |
| 290 { | 310 { |
| 291 base::RunLoop run_loop; | 311 base::RunLoop run_loop; |
| 292 ui_test_utils::NavigateToURL(browser(), test_url); | 312 ui_test_utils::NavigateToURL(browser(), test_url); |
| 293 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 313 RequestAppBanner(manager.get(), web_contents, run_loop, |
| 294 run_loop, ui::PAGE_TRANSITION_LINK, false); | 314 ui::PAGE_TRANSITION_LINK, false); |
| 295 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 2, 2); | 315 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 2, 2); |
| 296 EXPECT_FALSE(manager->need_to_log_status()); | 316 EXPECT_FALSE(manager->need_to_log_status()); |
| 297 AppBannerManager::SetTimeDeltaForTesting(1); | 317 AppBannerManager::SetTimeDeltaForTesting(1); |
| 298 } | 318 } |
| 299 | 319 |
| 300 // Add an indirect nav on day 2. | 320 // Add an indirect nav on day 2. |
| 301 { | 321 { |
| 302 base::RunLoop run_loop; | 322 base::RunLoop run_loop; |
| 303 ui_test_utils::NavigateToURL(browser(), test_url); | 323 ui_test_utils::NavigateToURL(browser(), test_url); |
| 304 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 324 RequestAppBanner(manager.get(), web_contents, run_loop, |
| 305 run_loop, ui::PAGE_TRANSITION_MANUAL_SUBFRAME, false); | 325 ui::PAGE_TRANSITION_MANUAL_SUBFRAME, false); |
| 306 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 3, 3); | 326 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 3, 3); |
| 307 EXPECT_FALSE(manager->need_to_log_status()); | 327 EXPECT_FALSE(manager->need_to_log_status()); |
| 308 } | 328 } |
| 309 | 329 |
| 310 // Add a direct nav on day 2 which overrides. | 330 // Add a direct nav on day 2 which overrides. |
| 311 { | 331 { |
| 312 base::RunLoop run_loop; | 332 base::RunLoop run_loop; |
| 313 ui_test_utils::NavigateToURL(browser(), test_url); | 333 ui_test_utils::NavigateToURL(browser(), test_url); |
| 314 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 334 RequestAppBanner(manager.get(), web_contents, run_loop, |
| 315 run_loop, ui::PAGE_TRANSITION_GENERATED, false); | 335 ui::PAGE_TRANSITION_GENERATED, false); |
| 316 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 4, 4); | 336 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 4, 4); |
| 317 EXPECT_FALSE(manager->need_to_log_status()); | 337 EXPECT_FALSE(manager->need_to_log_status()); |
| 318 AppBannerManager::SetTimeDeltaForTesting(2); | 338 AppBannerManager::SetTimeDeltaForTesting(2); |
| 319 } | 339 } |
| 320 | 340 |
| 321 // Add a direct nav on day 3. | 341 // Add a direct nav on day 3. |
| 322 { | 342 { |
| 323 base::RunLoop run_loop; | 343 base::RunLoop run_loop; |
| 324 ui_test_utils::NavigateToURL(browser(), test_url); | 344 ui_test_utils::NavigateToURL(browser(), test_url); |
| 325 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 345 RequestAppBanner(manager.get(), web_contents, run_loop, |
| 326 run_loop, ui::PAGE_TRANSITION_GENERATED, false); | 346 ui::PAGE_TRANSITION_GENERATED, false); |
| 327 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 5, 5); | 347 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 5, 5); |
| 328 EXPECT_FALSE(manager->need_to_log_status()); | 348 EXPECT_FALSE(manager->need_to_log_status()); |
| 329 AppBannerManager::SetTimeDeltaForTesting(3); | 349 AppBannerManager::SetTimeDeltaForTesting(3); |
| 330 } | 350 } |
| 331 | 351 |
| 332 // Add an indirect nav on day 4. | 352 // Add an indirect nav on day 4. |
| 333 { | 353 { |
| 334 base::RunLoop run_loop; | 354 base::RunLoop run_loop; |
| 335 ui_test_utils::NavigateToURL(browser(), test_url); | 355 ui_test_utils::NavigateToURL(browser(), test_url); |
| 336 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 356 RequestAppBanner(manager.get(), web_contents, run_loop, |
| 337 run_loop, ui::PAGE_TRANSITION_FORM_SUBMIT, false); | 357 ui::PAGE_TRANSITION_FORM_SUBMIT, false); |
| 338 EXPECT_FALSE(manager->need_to_log_status()); | 358 EXPECT_FALSE(manager->need_to_log_status()); |
| 339 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 6, 6); | 359 CheckInstallableStatusCodeHistogram(INSUFFICIENT_ENGAGEMENT, 6, 6); |
| 340 } | 360 } |
| 341 | 361 |
| 342 // Add a direct nav on day 4 which should trigger the banner. | 362 // Add a direct nav on day 4 which should trigger the banner. |
| 343 { | 363 { |
| 344 base::RunLoop run_loop; | 364 base::RunLoop run_loop; |
| 345 ui_test_utils::NavigateToURL(browser(), test_url); | 365 ui_test_utils::NavigateToURL(browser(), test_url); |
| 346 RequestAppBanner(manager.get(), web_contents->GetLastCommittedURL(), | 366 RequestAppBanner(manager.get(), web_contents, run_loop, |
| 347 run_loop, ui::PAGE_TRANSITION_TYPED, true); | 367 ui::PAGE_TRANSITION_TYPED, true); |
| 348 EXPECT_FALSE(manager->need_to_log_status()); | 368 EXPECT_FALSE(manager->need_to_log_status()); |
| 349 CheckInstallableStatusCodeHistogram(SHOWING_WEB_APP_BANNER, 1, 7); | 369 CheckInstallableStatusCodeHistogram(SHOWING_WEB_APP_BANNER, 1, 7); |
| 350 } | 370 } |
| 351 } | 371 } |
| 352 | 372 |
| 353 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, | 373 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, |
| 354 WebAppBannerNoTypeInManifest) { | 374 WebAppBannerNoTypeInManifest) { |
| 355 RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( | 375 RunBannerTest(GetURLOfPageWithServiceWorkerAndManifest( |
| 356 "/banners/manifest_no_type.json"), | 376 "/banners/manifest_no_type.json"), |
| 357 ui::PAGE_TRANSITION_TYPED, 1, SHOWING_WEB_APP_BANNER, true); | 377 ui::PAGE_TRANSITION_TYPED, 1, SHOWING_WEB_APP_BANNER, true); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 RunBannerTest("/banners/prompt_in_handler_test_page.html", | 415 RunBannerTest("/banners/prompt_in_handler_test_page.html", |
| 396 ui::PAGE_TRANSITION_TYPED, 1, SHOWING_WEB_APP_BANNER, true); | 416 ui::PAGE_TRANSITION_TYPED, 1, SHOWING_WEB_APP_BANNER, true); |
| 397 } | 417 } |
| 398 | 418 |
| 399 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerInIFrame) { | 419 IN_PROC_BROWSER_TEST_F(AppBannerManagerBrowserTest, WebAppBannerInIFrame) { |
| 400 RunBannerTest("/banners/iframe_test_page.html", ui::PAGE_TRANSITION_TYPED, 0, | 420 RunBannerTest("/banners/iframe_test_page.html", ui::PAGE_TRANSITION_TYPED, 0, |
| 401 NO_MANIFEST, false); | 421 NO_MANIFEST, false); |
| 402 } | 422 } |
| 403 | 423 |
| 404 } // namespace banners | 424 } // namespace banners |
| OLD | NEW |