Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / banners / app_banner_data_fetcher_browsertest.cc
blob3b5979f1e6d9a14da02a75764225790f22e33d7f
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"
24 namespace banners {
26 class TestObserver : public AppBannerDataFetcher::Observer {
27 public:
28 TestObserver(AppBannerDataFetcher* fetcher, base::Closure quit_closure)
29 : fetcher_(fetcher),
30 quit_closure_(quit_closure) {
31 fetcher_->AddObserverForTesting(this);
34 virtual ~TestObserver() {
35 if (fetcher_)
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_);
48 fetcher_ = nullptr;
51 bool will_show() { return will_show_.get() && *will_show_; }
53 private:
54 AppBannerDataFetcher* fetcher_;
55 base::Closure quit_closure_;
56 scoped_ptr<bool> will_show_;
59 class AppBannerDataFetcherBrowserTest : public InProcessBrowserTest,
60 public AppBannerDataFetcher::Delegate {
61 public:
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,
73 const GURL& url,
74 const std::string& id) override {
75 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
76 non_web_platform_ = platform;
77 return false;
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);
88 protected:
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(), 128));
99 base::RunLoop run_loop;
100 quit_closure_ = run_loop.QuitClosure();
101 scoped_ptr<TestObserver> observer(new TestObserver(fetcher.get(),
102 run_loop.QuitClosure()));
103 fetcher->Start(url, transition);
104 run_loop.Run();
106 EXPECT_EQ(expected_non_web_platform, non_web_platform_);
107 EXPECT_EQ(expected_to_show, observer->will_show());
108 ASSERT_FALSE(fetcher->is_active());
111 void LoadURLAndWaitForServiceWorker(const GURL& url) {
112 content::WebContents* web_contents =
113 browser()->tab_strip_model()->GetActiveWebContents();
114 content::TestNavigationObserver observer(web_contents, 2);
115 ui_test_utils::NavigateToURL(browser(), url);
116 observer.Wait();
117 EXPECT_EQ("sw_activated", observer.last_navigation_url().ref());
120 void RunBannerTest(const std::string& manifest_page,
121 ui::PageTransition transition,
122 unsigned int unshown_repetitions,
123 bool expectation) {
124 std::string valid_page(manifest_page);
125 GURL test_url = embedded_test_server()->GetURL(valid_page);
126 content::WebContents* web_contents =
127 browser()->tab_strip_model()->GetActiveWebContents();
129 for (unsigned int i = 0; i < unshown_repetitions; ++i) {
130 LoadURLAndWaitForServiceWorker(test_url);
131 RunFetcher(web_contents->GetURL(), std::string(), transition, false);
132 AppBannerDataFetcher::SetTimeDeltaForTesting(i+1);
135 // On the final loop, check whether the banner triggered or not as expected.
136 LoadURLAndWaitForServiceWorker(test_url);
137 RunFetcher(web_contents->GetURL(), std::string(), transition, expectation);
140 private:
141 std::string non_web_platform_;
142 base::Closure quit_closure_;
143 base::WeakPtrFactory<AppBannerDataFetcherBrowserTest> weak_factory_;
146 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
147 WebAppBannerCreatedDirect) {
148 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED,
149 1, true);
152 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
153 WebAppBannerCreatedDirectLargerTotal) {
154 AppBannerSettingsHelper::SetTotalEngagementToTrigger(4);
155 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED,
156 3, true);
159 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
160 WebAppBannerCreatedDirectSmallerTotal) {
161 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1);
162 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED,
163 0, true);
166 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
167 WebAppBannerCreatedDirectSingle) {
168 AppBannerSettingsHelper::SetEngagementWeights(2, 1);
169 RunBannerTest("/banners/manifest_test_page.html",
170 ui::PAGE_TRANSITION_GENERATED, 0, true);
173 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
174 WebAppBannerCreatedDirectMultiple) {
175 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1);
176 RunBannerTest("/banners/manifest_test_page.html",
177 ui::PAGE_TRANSITION_GENERATED, 3, true);
180 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
181 WebAppBannerCreatedDirectMultipleLargerTotal) {
182 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1);
183 AppBannerSettingsHelper::SetTotalEngagementToTrigger(3);
184 RunBannerTest("/banners/manifest_test_page.html",
185 ui::PAGE_TRANSITION_GENERATED, 5, true);
188 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
189 WebAppBannerCreatedDirectMultipleSmallerTotal) {
190 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1);
191 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1);
192 RunBannerTest("/banners/manifest_test_page.html",
193 ui::PAGE_TRANSITION_GENERATED, 1, true);
196 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
197 WebAppBannerCreatedIndirect) {
198 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK,
199 1, true);
202 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
203 WebAppBannerCreatedIndirectLargerTotal) {
204 AppBannerSettingsHelper::SetTotalEngagementToTrigger(5);
205 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK,
206 4, true);
209 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
210 WebAppBannerCreatedIndirectSmallerTotal) {
211 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1);
212 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK,
213 0, true);
216 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
217 WebAppBannerCreatedIndirectSingle) {
218 AppBannerSettingsHelper::SetEngagementWeights(1, 3);
219 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_RELOAD,
220 0, true);
223 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
224 WebAppBannerCreatedIndirectMultiple) {
225 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
226 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK,
227 3, true);
230 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
231 WebAppBannerCreatedIndirectMultipleLargerTotal) {
232 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
233 AppBannerSettingsHelper::SetTotalEngagementToTrigger(4);
234 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK,
235 7, true);
238 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
239 WebAppBannerCreatedVarious) {
240 AppBannerSettingsHelper::SetEngagementWeights(0.5, 0.25);
242 std::string valid_page("/banners/manifest_test_page.html");
243 GURL test_url = embedded_test_server()->GetURL(valid_page);
244 content::WebContents* web_contents =
245 browser()->tab_strip_model()->GetActiveWebContents();
247 // Add a direct nav on day 1.
248 LoadURLAndWaitForServiceWorker(test_url);
249 RunFetcher(web_contents->GetURL(), std::string(), ui::PAGE_TRANSITION_TYPED,
250 false);
252 // Add an indirect nav on day 1 which is ignored.
253 LoadURLAndWaitForServiceWorker(test_url);
254 RunFetcher(web_contents->GetURL(), std::string(), ui::PAGE_TRANSITION_LINK,
255 false);
256 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
258 // Add an indirect nav on day 2.
259 LoadURLAndWaitForServiceWorker(test_url);
260 RunFetcher(web_contents->GetURL(), std::string(),
261 ui::PAGE_TRANSITION_MANUAL_SUBFRAME, false);
263 // Add a direct nav on day 2 which overrides.
264 LoadURLAndWaitForServiceWorker(test_url);
265 RunFetcher(web_contents->GetURL(), std::string(),
266 ui::PAGE_TRANSITION_GENERATED, false);
267 AppBannerDataFetcher::SetTimeDeltaForTesting(2);
269 // Add a direct nav on day 3.
270 LoadURLAndWaitForServiceWorker(test_url);
271 RunFetcher(web_contents->GetURL(), std::string(),
272 ui::PAGE_TRANSITION_GENERATED, false);
273 AppBannerDataFetcher::SetTimeDeltaForTesting(3);
275 // Add an indirect nav on day 4.
276 LoadURLAndWaitForServiceWorker(test_url);
277 RunFetcher(web_contents->GetURL(), std::string(),
278 ui::PAGE_TRANSITION_FORM_SUBMIT, false);
280 // Add a direct nav on day 4 which should trigger the banner.
281 LoadURLAndWaitForServiceWorker(test_url);
282 RunFetcher(web_contents->GetURL(), std::string(),
283 ui::PAGE_TRANSITION_TYPED, true);
286 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
287 WebAppBannerNoTypeInManifest) {
288 RunBannerTest("/banners/manifest_no_type_test_page.html",
289 ui::PAGE_TRANSITION_TYPED, 1, true);
292 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest,
293 WebAppBannerNoTypeInManifestCapsExtension) {
294 RunBannerTest("/banners/manifest_no_type_caps_test_page.html",
295 ui::PAGE_TRANSITION_TYPED, 1, true);
298 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, PlayAppManifest) {
299 std::string valid_page("/banners/play_app_test_page.html");
300 GURL test_url = embedded_test_server()->GetURL(valid_page);
301 content::WebContents* web_contents =
302 browser()->tab_strip_model()->GetActiveWebContents();
304 // Native banners do not require the SW, so we can just load the URL.
305 ui_test_utils::NavigateToURL(browser(), test_url);
306 std::string play_platform("play");
307 RunFetcher(web_contents->GetURL(), play_platform, ui::PAGE_TRANSITION_TYPED,
308 false);
310 // The logic to get the details for a play app banner are only on android
311 // builds, so this test does not check that the banner is shown.
314 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, NoManifest) {
315 RunBannerTest("/banners/no_manifest_test_page.html",
316 ui::PAGE_TRANSITION_TYPED, 1, false);
319 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, CancelBannerDirect) {
320 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED, 1,
321 false);
324 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, CancelBannerIndirect) {
325 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5);
326 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED, 3,
327 false);
330 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, PromptBanner) {
331 RunBannerTest("/banners/prompt_test_page.html", ui::PAGE_TRANSITION_TYPED, 1,
332 true);
335 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, PromptBannerInHandler) {
336 RunBannerTest("/banners/prompt_in_handler_test_page.html",
337 ui::PAGE_TRANSITION_TYPED, 1, true);
340 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, WebAppBannerInIFrame) {
341 RunBannerTest("/banners/iframe_test_page.html", ui::PAGE_TRANSITION_TYPED, 1,
342 false);
345 } // namespace banners