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/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/test/test_navigation_observer.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
24 class TestObserver
: public AppBannerDataFetcher::Observer
{
26 TestObserver(AppBannerDataFetcher
* fetcher
, base::Closure quit_closure
)
28 quit_closure_(quit_closure
) {
29 fetcher_
->AddObserverForTesting(this);
32 virtual ~TestObserver() {
34 fetcher_
->RemoveObserverForTesting(this);
37 void OnDecidedWhetherToShow(AppBannerDataFetcher
* fetcher
,
38 bool will_show
) override
{
39 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, quit_closure_
);
40 ASSERT_FALSE(will_show_
.get());
41 will_show_
.reset(new bool(will_show
));
44 void OnFetcherDestroyed(AppBannerDataFetcher
* fetcher
) override
{
45 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, quit_closure_
);
49 bool will_show() { return will_show_
.get() && *will_show_
; }
52 AppBannerDataFetcher
* fetcher_
;
53 base::Closure quit_closure_
;
54 scoped_ptr
<bool> will_show_
;
57 class AppBannerDataFetcherBrowserTest
: public InProcessBrowserTest
,
58 public AppBannerDataFetcher::Delegate
{
60 AppBannerDataFetcherBrowserTest() : weak_factory_(this) {
63 void SetUpOnMainThread() override
{
64 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
65 InProcessBrowserTest::SetUpOnMainThread();
68 bool HandleNonWebApp(const std::string
& platform
,
70 const std::string
& id
) override
{
71 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, quit_closure_
);
72 non_web_platform_
= platform
;
76 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
77 command_line
->AppendSwitch(
78 switches::kEnableExperimentalWebPlatformFeatures
);
82 void RunFetcher(const GURL
& url
,
83 const std::string
& expected_non_web_platform
,
84 bool expected_to_show
) {
85 content::WebContents
* web_contents
=
86 browser()->tab_strip_model()->GetActiveWebContents();
87 scoped_refptr
<AppBannerDataFetcherDesktop
> fetcher(
88 new AppBannerDataFetcherDesktop(web_contents
,
89 weak_factory_
.GetWeakPtr(), 128));
91 base::RunLoop run_loop
;
92 quit_closure_
= run_loop
.QuitClosure();
93 scoped_ptr
<TestObserver
> observer(new TestObserver(fetcher
.get(),
94 run_loop
.QuitClosure()));
98 EXPECT_EQ(expected_non_web_platform
, non_web_platform_
);
99 EXPECT_EQ(expected_to_show
, observer
->will_show());
100 ASSERT_FALSE(fetcher
->is_active());
103 void LoadURLAndWaitForServiceWorker(const GURL
& url
) {
104 content::WebContents
* web_contents
=
105 browser()->tab_strip_model()->GetActiveWebContents();
106 content::TestNavigationObserver
observer(web_contents
, 2);
107 ui_test_utils::NavigateToURL(browser(), url
);
109 EXPECT_EQ("sw_activated", observer
.last_navigation_url().ref());
113 std::string non_web_platform_
;
114 base::Closure quit_closure_
;
115 base::WeakPtrFactory
<AppBannerDataFetcherBrowserTest
> weak_factory_
;
118 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, WebAppBannerCreated
) {
119 std::string
valid_page("/banners/manifest_test_page.html");
120 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
121 content::WebContents
* web_contents
=
122 browser()->tab_strip_model()->GetActiveWebContents();
124 LoadURLAndWaitForServiceWorker(test_url
);
125 RunFetcher(web_contents
->GetURL(), std::string(), false);
127 // Advance by a day, then visit the page again to trigger the banner.
128 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
129 LoadURLAndWaitForServiceWorker(test_url
);
130 RunFetcher(web_contents
->GetURL(), std::string(), true);
133 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, PlayAppManifest
) {
134 std::string
valid_page("/banners/play_app_test_page.html");
135 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
136 content::WebContents
* web_contents
=
137 browser()->tab_strip_model()->GetActiveWebContents();
139 // Native banners do not require the SW, so we can just load the URL.
140 ui_test_utils::NavigateToURL(browser(), test_url
);
141 std::string
play_platform("play");
142 RunFetcher(web_contents
->GetURL(), play_platform
, false);
144 // The logic to get the details for a play app banner are only on android
145 // builds, so this test does not check that the banner is shown.
148 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, NoManifest
) {
149 std::string
valid_page("/banners/no_manifest_test_page.html");
150 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
151 content::WebContents
* web_contents
=
152 browser()->tab_strip_model()->GetActiveWebContents();
154 LoadURLAndWaitForServiceWorker(test_url
);
155 RunFetcher(web_contents
->GetURL(), std::string(), false);
157 // Advance by a day, then visit the page again. Still shouldn't see a banner.
158 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
159 LoadURLAndWaitForServiceWorker(test_url
);
160 RunFetcher(web_contents
->GetURL(), std::string(), false);
163 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, CancelBanner
) {
164 std::string
valid_page("/banners/cancel_test_page.html");
165 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
166 content::WebContents
* web_contents
=
167 browser()->tab_strip_model()->GetActiveWebContents();
169 LoadURLAndWaitForServiceWorker(test_url
);
170 RunFetcher(web_contents
->GetURL(), std::string(), false);
172 // Advance by a day, then visit the page again. Still shouldn't see a banner.
173 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
174 LoadURLAndWaitForServiceWorker(test_url
);
175 RunFetcher(web_contents
->GetURL(), std::string(), false);
178 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest
, WebAppBannerInIFrame
) {
179 std::string
valid_page("/banners/iframe_test_page.html");
180 GURL test_url
= embedded_test_server()->GetURL(valid_page
);
181 content::WebContents
* web_contents
=
182 browser()->tab_strip_model()->GetActiveWebContents();
184 LoadURLAndWaitForServiceWorker(test_url
);
185 RunFetcher(web_contents
->GetURL(), std::string(), false);
187 // Advance by a day, then visit the page again to trigger the banner.
188 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
189 LoadURLAndWaitForServiceWorker(test_url
);
190 RunFetcher(web_contents
->GetURL(), std::string(), false);
193 } // namespace banners