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 AppBannerSettingsHelper::SetTotalEngagementToTrigger(2);
68 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
69 InProcessBrowserTest::SetUpOnMainThread();
72 bool HandleNonWebApp(const std::string
& platform
,
74 const std::string
& id
) override
{
75 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, quit_closure_
);
76 non_web_platform_
= platform
;
80 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
81 command_line
->AppendSwitch(
82 switches::kEnableExperimentalWebPlatformFeatures
);
83 // Make sure app banners are disabled in the browser, otherwise they will
84 // interfere with the test.
85 command_line
->AppendSwitch(switches::kDisableAddToShelf
);
89 void RunFetcher(const GURL
& url
,
90 const std::string
& expected_non_web_platform
,
91 ui::PageTransition transition
,
92 bool expected_to_show
) {
93 content::WebContents
* web_contents
=
94 browser()->tab_strip_model()->GetActiveWebContents();
95 scoped_refptr
<AppBannerDataFetcherDesktop
> fetcher(
96 new AppBannerDataFetcherDesktop(web_contents
,
97 weak_factory_
.GetWeakPtr(),
100 base::RunLoop run_loop
;
101 quit_closure_
= run_loop
.QuitClosure();
102 scoped_ptr
<TestObserver
> observer(new TestObserver(fetcher
.get(),
103 run_loop
.QuitClosure()));
104 fetcher
->Start(url
, transition
);
107 EXPECT_EQ(expected_non_web_platform
, non_web_platform_
);
108 EXPECT_EQ(expected_to_show
, observer
->will_show());
109 ASSERT_FALSE(fetcher
->is_active());
112 void LoadURLAndWaitForServiceWorker(const GURL
& url
) {
113 content::WebContents
* web_contents
=
114 browser()->tab_strip_model()->GetActiveWebContents();
115 content::TestNavigationObserver
observer(web_contents
, 2);
116 ui_test_utils::NavigateToURL(browser(), url
);
118 EXPECT_EQ("sw_activated", observer
.last_navigation_url().ref());
121 void RunBannerTest(const std::string
& manifest_page
,
122 ui::PageTransition transition
,
123 unsigned int unshown_repetitions
,
125 std::string
valid_page(manifest_page
);
126 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
127 content::WebContents
* web_contents
=
128 browser()->tab_strip_model()->GetActiveWebContents();
130 for (unsigned int i
= 0; i
< unshown_repetitions
; ++i
) {
131 LoadURLAndWaitForServiceWorker(test_url
);
132 RunFetcher(web_contents
->GetURL(), std::string(), transition
, false);
133 AppBannerDataFetcher::SetTimeDeltaForTesting(i
+1);
136 // On the final loop, check whether the banner triggered or not as expected.
137 LoadURLAndWaitForServiceWorker(test_url
);
138 RunFetcher(web_contents
->GetURL(), std::string(), transition
, expectation
);
142 std::string non_web_platform_
;
143 base::Closure quit_closure_
;
144 base::WeakPtrFactory
<AppBannerDataFetcherBrowserTest
> weak_factory_
;
147 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
148 WebAppBannerCreatedDirect
) {
149 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED
,
153 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
154 WebAppBannerCreatedDirectLargerTotal
) {
155 AppBannerSettingsHelper::SetTotalEngagementToTrigger(4);
156 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED
,
160 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
161 WebAppBannerCreatedDirectSmallerTotal
) {
162 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1);
163 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED
,
167 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
168 WebAppBannerCreatedDirectSingle
) {
169 AppBannerSettingsHelper::SetEngagementWeights(2, 1);
170 RunBannerTest("/banners/manifest_test_page.html",
171 ui::PAGE_TRANSITION_GENERATED
, 0, true);
174 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
175 WebAppBannerCreatedDirectMultiple
) {
176 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1);
177 RunBannerTest("/banners/manifest_test_page.html",
178 ui::PAGE_TRANSITION_GENERATED
, 3, true);
181 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
182 WebAppBannerCreatedDirectMultipleLargerTotal
) {
183 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1);
184 AppBannerSettingsHelper::SetTotalEngagementToTrigger(3);
185 RunBannerTest("/banners/manifest_test_page.html",
186 ui::PAGE_TRANSITION_GENERATED
, 5, true);
189 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
190 WebAppBannerCreatedDirectMultipleSmallerTotal
) {
191 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1);
192 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1);
193 RunBannerTest("/banners/manifest_test_page.html",
194 ui::PAGE_TRANSITION_GENERATED
, 1, true);
197 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
198 WebAppBannerCreatedIndirect
) {
199 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK
,
203 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
204 WebAppBannerCreatedIndirectLargerTotal
) {
205 AppBannerSettingsHelper::SetTotalEngagementToTrigger(5);
206 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK
,
210 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
211 WebAppBannerCreatedIndirectSmallerTotal
) {
212 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1);
213 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK
,
217 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
218 WebAppBannerCreatedIndirectSingle
) {
219 AppBannerSettingsHelper::SetEngagementWeights(1, 3);
220 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_RELOAD
,
224 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
225 WebAppBannerCreatedIndirectMultiple
) {
226 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
227 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK
,
231 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
232 WebAppBannerCreatedIndirectMultipleLargerTotal
) {
233 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
234 AppBannerSettingsHelper::SetTotalEngagementToTrigger(4);
235 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK
,
239 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
240 WebAppBannerCreatedVarious
) {
241 AppBannerSettingsHelper::SetEngagementWeights(0.5, 0.25);
243 std::string
valid_page("/banners/manifest_test_page.html");
244 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
245 content::WebContents
* web_contents
=
246 browser()->tab_strip_model()->GetActiveWebContents();
248 // Add a direct nav on day 1.
249 LoadURLAndWaitForServiceWorker(test_url
);
250 RunFetcher(web_contents
->GetURL(), std::string(), ui::PAGE_TRANSITION_TYPED
,
253 // Add an indirect nav on day 1 which is ignored.
254 LoadURLAndWaitForServiceWorker(test_url
);
255 RunFetcher(web_contents
->GetURL(), std::string(), ui::PAGE_TRANSITION_LINK
,
257 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
259 // Add an indirect nav on day 2.
260 LoadURLAndWaitForServiceWorker(test_url
);
261 RunFetcher(web_contents
->GetURL(), std::string(),
262 ui::PAGE_TRANSITION_MANUAL_SUBFRAME
, false);
264 // Add a direct nav on day 2 which overrides.
265 LoadURLAndWaitForServiceWorker(test_url
);
266 RunFetcher(web_contents
->GetURL(), std::string(),
267 ui::PAGE_TRANSITION_GENERATED
, false);
268 AppBannerDataFetcher::SetTimeDeltaForTesting(2);
270 // Add a direct nav on day 3.
271 LoadURLAndWaitForServiceWorker(test_url
);
272 RunFetcher(web_contents
->GetURL(), std::string(),
273 ui::PAGE_TRANSITION_GENERATED
, false);
274 AppBannerDataFetcher::SetTimeDeltaForTesting(3);
276 // Add an indirect nav on day 4.
277 LoadURLAndWaitForServiceWorker(test_url
);
278 RunFetcher(web_contents
->GetURL(), std::string(),
279 ui::PAGE_TRANSITION_FORM_SUBMIT
, false);
281 // Add a direct nav on day 4 which should trigger the banner.
282 LoadURLAndWaitForServiceWorker(test_url
);
283 RunFetcher(web_contents
->GetURL(), std::string(),
284 ui::PAGE_TRANSITION_TYPED
, true);
287 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
288 WebAppBannerNoTypeInManifest
) {
289 RunBannerTest("/banners/manifest_no_type_test_page.html",
290 ui::PAGE_TRANSITION_TYPED
, 1, true);
293 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
,
294 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 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, NoManifest
) {
316 RunBannerTest("/banners/no_manifest_test_page.html",
317 ui::PAGE_TRANSITION_TYPED
, 1, false);
320 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, CancelBannerDirect
) {
321 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED
, 1,
325 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, CancelBannerIndirect
) {
326 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
327 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED
, 3,
331 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, PromptBanner
) {
332 RunBannerTest("/banners/prompt_test_page.html", ui::PAGE_TRANSITION_TYPED
, 1,
336 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, PromptBannerInHandler
) {
337 RunBannerTest("/banners/prompt_in_handler_test_page.html",
338 ui::PAGE_TRANSITION_TYPED
, 1, true);
341 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, WebAppBannerInIFrame
) {
342 RunBannerTest("/banners/iframe_test_page.html", ui::PAGE_TRANSITION_TYPED
, 1,
346 } // namespace banners