1 // Copyright 2015 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/banners/app_banner_data_fetcher.h"
7 #include "base/command_line.h"
8 #include "base/location.h"
9 #include "base/run_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chrome/browser/banners/app_banner_data_fetcher_desktop.h"
14 #include "chrome/browser/banners/app_banner_settings_helper.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/test/test_navigation_observer.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h"
26 class TestObserver
: public AppBannerDataFetcher::Observer
{
28 TestObserver(AppBannerDataFetcher
* fetcher
, base::Closure quit_closure
)
30 quit_closure_(quit_closure
) {
31 fetcher_
->AddObserverForTesting(this);
34 virtual ~TestObserver() {
36 fetcher_
->RemoveObserverForTesting(this);
39 void OnDecidedWhetherToShow(AppBannerDataFetcher
* fetcher
,
40 bool will_show
) override
{
41 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, quit_closure_
);
42 ASSERT_FALSE(will_show_
.get());
43 will_show_
.reset(new bool(will_show
));
46 void OnFetcherDestroyed(AppBannerDataFetcher
* fetcher
) override
{
47 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, quit_closure_
);
51 bool will_show() { return will_show_
.get() && *will_show_
; }
54 AppBannerDataFetcher
* fetcher_
;
55 base::Closure quit_closure_
;
56 scoped_ptr
<bool> will_show_
;
59 class AppBannerDataFetcherBrowserTest
: public InProcessBrowserTest
,
60 public AppBannerDataFetcher::Delegate
{
62 AppBannerDataFetcherBrowserTest() : weak_factory_(this) {
65 void SetUpOnMainThread() override
{
66 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
67 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
68 InProcessBrowserTest::SetUpOnMainThread();
71 bool HandleNonWebApp(const std::string
& platform
,
73 const std::string
& id
) override
{
74 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, quit_closure_
);
75 non_web_platform_
= platform
;
79 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
80 command_line
->AppendSwitch(
81 switches::kEnableExperimentalWebPlatformFeatures
);
82 // Make sure app banners are disabled in the browser, otherwise they will
83 // interfere with the test.
84 command_line
->AppendSwitch(switches::kDisableAddToShelf
);
88 void RunFetcher(const GURL
& url
,
89 const std::string
& expected_non_web_platform
,
90 ui::PageTransition transition
,
91 bool expected_to_show
) {
92 content::WebContents
* web_contents
=
93 browser()->tab_strip_model()->GetActiveWebContents();
94 scoped_refptr
<AppBannerDataFetcherDesktop
> fetcher(
95 new AppBannerDataFetcherDesktop(web_contents
,
96 weak_factory_
.GetWeakPtr(), 128));
98 base::RunLoop run_loop
;
99 quit_closure_
= run_loop
.QuitClosure();
100 scoped_ptr
<TestObserver
> observer(new TestObserver(fetcher
.get(),
101 run_loop
.QuitClosure()));
102 fetcher
->Start(url
, transition
);
105 EXPECT_EQ(expected_non_web_platform
, non_web_platform_
);
106 EXPECT_EQ(expected_to_show
, observer
->will_show());
107 ASSERT_FALSE(fetcher
->is_active());
110 void LoadURLAndWaitForServiceWorker(const GURL
& url
) {
111 content::WebContents
* web_contents
=
112 browser()->tab_strip_model()->GetActiveWebContents();
113 content::TestNavigationObserver
observer(web_contents
, 2);
114 ui_test_utils::NavigateToURL(browser(), url
);
116 EXPECT_EQ("sw_activated", observer
.last_navigation_url().ref());
119 void RunBannerTest(const std::string
& manifest_page
,
120 ui::PageTransition transition
,
121 unsigned int unshown_repetitions
,
123 std::string
valid_page(manifest_page
);
124 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
125 content::WebContents
* web_contents
=
126 browser()->tab_strip_model()->GetActiveWebContents();
128 for (unsigned int i
= 0; i
< unshown_repetitions
; ++i
) {
129 LoadURLAndWaitForServiceWorker(test_url
);
130 RunFetcher(web_contents
->GetURL(), std::string(), transition
, false);
131 AppBannerDataFetcher::SetTimeDeltaForTesting(i
+1);
134 // On the final loop, check whether the banner triggered or not as expected.
135 LoadURLAndWaitForServiceWorker(test_url
);
136 RunFetcher(web_contents
->GetURL(), std::string(), transition
, expectation
);
140 std::string non_web_platform_
;
141 base::Closure quit_closure_
;
142 base::WeakPtrFactory
<AppBannerDataFetcherBrowserTest
> weak_factory_
;
145 // http://crbug.com/517307
147 #define MAYBE_WebAppBannerCreatedDirect \
148 DISABLED_WebAppBannerCreatedDirect
150 #define MAYBE_WebAppBannerCreatedDirect WebAppBannerCreatedDirect
152 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
153 MAYBE_WebAppBannerCreatedDirect
) {
154 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED
,
158 // http://crbug.com/517307
160 #define MAYBE_WebAppBannerCreatedDirectSingle \
161 DISABLED_WebAppBannerCreatedDirectSingle
163 #define MAYBE_WebAppBannerCreatedDirectSingle WebAppBannerCreatedDirectSingle
165 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
166 MAYBE_WebAppBannerCreatedDirectSingle
) {
167 AppBannerSettingsHelper::SetEngagementWeights(2, 1);
168 RunBannerTest("/banners/manifest_test_page.html",
169 ui::PAGE_TRANSITION_GENERATED
, 0, true);
172 // http://crbug.com/517307
174 #define MAYBE_WebAppBannerCreatedDirectMultiple \
175 DISABLED_WebAppBannerCreatedDirectMultiple
177 #define MAYBE_WebAppBannerCreatedDirectMultiple \
178 WebAppBannerCreatedDirectMultiple
180 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
181 MAYBE_WebAppBannerCreatedDirectMultiple
) {
182 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1);
183 RunBannerTest("/banners/manifest_test_page.html",
184 ui::PAGE_TRANSITION_GENERATED
, 3, true);
187 // http://crbug.com/517307
189 #define MAYBE_WebAppBannerCreatedIndirect DISABLED_WebAppBannerCreatedIndirect
191 #define MAYBE_WebAppBannerCreatedIndirect WebAppBannerCreatedIndirect
193 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
194 MAYBE_WebAppBannerCreatedIndirect
) {
195 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK
,
199 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
200 WebAppBannerCreatedIndirectSingle
) {
201 AppBannerSettingsHelper::SetEngagementWeights(1, 3);
202 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_RELOAD
,
206 // http://crbug.com/517307
208 #define MAYBE_WebAppBannerCreatedIndirectMultiple \
209 DISABLED_WebAppBannerCreatedIndirectMultiple
211 #define MAYBE_WebAppBannerCreatedIndirectMultiple \
212 WebAppBannerCreatedIndirectMultiple
214 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
215 MAYBE_WebAppBannerCreatedIndirectMultiple
) {
216 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
217 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK
,
221 // http://crbug.com/517307
223 #define MAYBE_WebAppBannerCreatedVarious DISABLED_WebAppBannerCreatedVarious
225 #define MAYBE_WebAppBannerCreatedVarious WebAppBannerCreatedVarious
227 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
228 MAYBE_WebAppBannerCreatedVarious
) {
229 AppBannerSettingsHelper::SetEngagementWeights(0.5, 0.25);
231 std::string
valid_page("/banners/manifest_test_page.html");
232 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
233 content::WebContents
* web_contents
=
234 browser()->tab_strip_model()->GetActiveWebContents();
236 // Add a direct nav on day 1.
237 LoadURLAndWaitForServiceWorker(test_url
);
238 RunFetcher(web_contents
->GetURL(), std::string(), ui::PAGE_TRANSITION_TYPED
,
241 // Add an indirect nav on day 1 which is ignored.
242 LoadURLAndWaitForServiceWorker(test_url
);
243 RunFetcher(web_contents
->GetURL(), std::string(), ui::PAGE_TRANSITION_LINK
,
245 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
247 // Add an indirect nav on day 2.
248 LoadURLAndWaitForServiceWorker(test_url
);
249 RunFetcher(web_contents
->GetURL(), std::string(),
250 ui::PAGE_TRANSITION_MANUAL_SUBFRAME
, false);
252 // Add a direct nav on day 2 which overrides.
253 LoadURLAndWaitForServiceWorker(test_url
);
254 RunFetcher(web_contents
->GetURL(), std::string(),
255 ui::PAGE_TRANSITION_GENERATED
, false);
256 AppBannerDataFetcher::SetTimeDeltaForTesting(2);
258 // Add a direct nav on day 3.
259 LoadURLAndWaitForServiceWorker(test_url
);
260 RunFetcher(web_contents
->GetURL(), std::string(),
261 ui::PAGE_TRANSITION_GENERATED
, false);
262 AppBannerDataFetcher::SetTimeDeltaForTesting(3);
264 // Add an indirect nav on day 4.
265 LoadURLAndWaitForServiceWorker(test_url
);
266 RunFetcher(web_contents
->GetURL(), std::string(),
267 ui::PAGE_TRANSITION_FORM_SUBMIT
, false);
269 // Add a direct nav on day 4 which should trigger the banner.
270 LoadURLAndWaitForServiceWorker(test_url
);
271 RunFetcher(web_contents
->GetURL(), std::string(),
272 ui::PAGE_TRANSITION_TYPED
, true);
275 // http://crbug.com/517307
277 #define MAYBE_WebAppBannerNoTypeInManifest DISABLED_WebAppBannerNoTypeInManifest
279 #define MAYBE_WebAppBannerNoTypeInManifest WebAppBannerNoTypeInManifest
281 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
282 MAYBE_WebAppBannerNoTypeInManifest
) {
283 RunBannerTest("/banners/manifest_no_type_test_page.html",
284 ui::PAGE_TRANSITION_TYPED
, 1, true);
287 // http://crbug.com/517307
289 #define MAYBE_WebAppBannerNoTypeInManifestCapsExtension DISABLED_WebAppBannerNoTypeInManifestCapsExtension
291 #define MAYBE_WebAppBannerNoTypeInManifestCapsExtension WebAppBannerNoTypeInManifestCapsExtension
293 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
294 MAYBE_WebAppBannerNoTypeInManifestCapsExtension
) {
295 RunBannerTest("/banners/manifest_no_type_caps_test_page.html",
296 ui::PAGE_TRANSITION_TYPED
, 1, true);
299 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, PlayAppManifest
) {
300 std::string
valid_page("/banners/play_app_test_page.html");
301 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
302 content::WebContents
* web_contents
=
303 browser()->tab_strip_model()->GetActiveWebContents();
305 // Native banners do not require the SW, so we can just load the URL.
306 ui_test_utils::NavigateToURL(browser(), test_url
);
307 std::string
play_platform("play");
308 RunFetcher(web_contents
->GetURL(), play_platform
, ui::PAGE_TRANSITION_TYPED
,
311 // The logic to get the details for a play app banner are only on android
312 // builds, so this test does not check that the banner is shown.
315 // http://crbug.com/517307
317 #define MAYBE_NoManifest DISABLED_NoManifest
319 #define MAYBE_NoManifest NoManifest
321 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, MAYBE_NoManifest
) {
322 RunBannerTest("/banners/no_manifest_test_page.html",
323 ui::PAGE_TRANSITION_TYPED
, 1, false);
326 // http://crbug.com/517307
328 #define MAYBE_CancelBannerDirect DISABLED_CancelBannerDirect
330 #define MAYBE_CancelBannerDirect CancelBannerDirect
332 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
333 MAYBE_CancelBannerDirect
) {
334 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED
, 1,
338 // http://crbug.com/517307
340 #define MAYBE_CancelBannerIndirect DISABLED_CancelBannerIndirect
342 #define MAYBE_CancelBannerIndirect CancelBannerIndirect
344 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
345 MAYBE_CancelBannerIndirect
) {
346 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
347 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED
, 3,
351 // http://crbug.com/517307
353 #define MAYBE_PromptBanner DISABLED_PromptBanner
355 #define MAYBE_PromptBanner PromptBanner
357 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, MAYBE_PromptBanner
) {
358 RunBannerTest("/banners/prompt_test_page.html", ui::PAGE_TRANSITION_TYPED
, 1,
362 // http://crbug.com/517307
364 #define MAYBE_PromptBannerInHandler DISABLED_PromptBannerInHandler
366 #define MAYBE_PromptBannerInHandler PromptBannerInHandler
368 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
369 MAYBE_PromptBannerInHandler
) {
370 RunBannerTest("/banners/prompt_in_handler_test_page.html",
371 ui::PAGE_TRANSITION_TYPED
, 1, true);
374 // http://crbug.com/517307
376 #define MAYBE_WebAppBannerInIFrame DISABLED_WebAppBannerInIFrame
378 #define MAYBE_WebAppBannerInIFrame WebAppBannerInIFrame
381 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
382 MAYBE_WebAppBannerInIFrame
) {
383 RunBannerTest("/banners/iframe_test_page.html", ui::PAGE_TRANSITION_TYPED
, 1,
387 } // namespace banners