Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / banners / app_banner_data_fetcher_browsertest.cc
blob0d119c6fb33c9d56caf54a29658ae69ca94f36e9
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/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/task_runner.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/test/test_navigation_observer.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
19 namespace banners {
21 class TestObserver : public AppBannerDataFetcher::Observer {
22 public:
23 TestObserver(AppBannerDataFetcher* fetcher, base::Closure quit_closure)
24 : fetcher_(fetcher),
25 quit_closure_(quit_closure) {
26 fetcher_->AddObserverForTesting(this);
29 virtual ~TestObserver() {
30 if (fetcher_)
31 fetcher_->RemoveObserverForTesting(this);
34 void OnDecidedWhetherToShow(AppBannerDataFetcher* fetcher,
35 bool will_show) override {
36 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
37 ASSERT_FALSE(will_show_.get());
38 will_show_.reset(new bool(will_show));
41 void OnFetcherDestroyed(AppBannerDataFetcher* fetcher) override {
42 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
43 fetcher_ = nullptr;
46 bool will_show() { return will_show_.get() && *will_show_; }
48 private:
49 AppBannerDataFetcher* fetcher_;
50 base::Closure quit_closure_;
51 scoped_ptr<bool> will_show_;
54 class AppBannerDataFetcherBrowserTest : public InProcessBrowserTest,
55 public AppBannerDataFetcher::Delegate {
56 public:
57 AppBannerDataFetcherBrowserTest() : manifest_was_invalid_(false),
58 weak_factory_(this) {
61 void SetUpOnMainThread() override {
62 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
63 InProcessBrowserTest::SetUpOnMainThread();
66 bool OnInvalidManifest(AppBannerDataFetcher* fetcher) override {
67 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
68 manifest_was_invalid_ = true;
69 return false;
72 void SetUpCommandLine(base::CommandLine* command_line) override {
73 command_line->AppendSwitch(
74 switches::kEnableExperimentalWebPlatformFeatures);
77 protected:
78 void RunFetcher(const GURL& url,
79 bool expected_manifest_valid,
80 bool expected_to_show) {
81 content::WebContents* web_contents =
82 browser()->tab_strip_model()->GetActiveWebContents();
83 scoped_refptr<AppBannerDataFetcher> fetcher(
84 new AppBannerDataFetcher(web_contents, weak_factory_.GetWeakPtr(),
85 128));
87 base::RunLoop run_loop;
88 quit_closure_ = run_loop.QuitClosure();
89 scoped_ptr<TestObserver> observer(new TestObserver(fetcher.get(),
90 run_loop.QuitClosure()));
91 fetcher->Start(url);
92 run_loop.Run();
94 EXPECT_EQ(expected_manifest_valid, !manifest_was_invalid_);
95 EXPECT_EQ(expected_to_show, observer->will_show());
96 ASSERT_FALSE(fetcher->is_active());
99 void LoadURLAndWaitForServiceWorker(const GURL& url) {
100 content::WebContents* web_contents =
101 browser()->tab_strip_model()->GetActiveWebContents();
102 content::TestNavigationObserver observer(web_contents, 2);
103 ui_test_utils::NavigateToURL(browser(), url);
104 observer.Wait();
105 EXPECT_EQ("sw_activated", observer.last_navigation_url().ref());
108 private:
109 bool manifest_was_invalid_;
110 base::Closure quit_closure_;
111 base::WeakPtrFactory<AppBannerDataFetcherBrowserTest> weak_factory_;
114 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, WebAppBannerCreated) {
115 std::string valid_page = "/banners/manifest_test_page.html";
116 GURL test_url = embedded_test_server()->GetURL(valid_page);
117 content::WebContents* web_contents =
118 browser()->tab_strip_model()->GetActiveWebContents();
120 LoadURLAndWaitForServiceWorker(test_url);
121 RunFetcher(web_contents->GetURL(), true, false);
123 // Advance by a day, then visit the page again to trigger the banner.
124 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
125 LoadURLAndWaitForServiceWorker(test_url);
126 RunFetcher(web_contents->GetURL(), true, true);
129 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, NoManifest) {
130 std::string valid_page = "/banners/no_manifest_test_page.html";
131 GURL test_url = embedded_test_server()->GetURL(valid_page);
132 content::WebContents* web_contents =
133 browser()->tab_strip_model()->GetActiveWebContents();
135 LoadURLAndWaitForServiceWorker(test_url);
136 RunFetcher(web_contents->GetURL(), false, false);
138 // Advance by a day, then visit the page again. Still shouldn't see a banner.
139 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
140 LoadURLAndWaitForServiceWorker(test_url);
141 RunFetcher(web_contents->GetURL(), false, false);
144 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, CancelBanner) {
145 std::string valid_page = "/banners/cancel_test_page.html";
146 GURL test_url = embedded_test_server()->GetURL(valid_page);
147 content::WebContents* web_contents =
148 browser()->tab_strip_model()->GetActiveWebContents();
150 LoadURLAndWaitForServiceWorker(test_url);
151 RunFetcher(web_contents->GetURL(), true, false);
153 // Advance by a day, then visit the page again. Still shouldn't see a banner.
154 AppBannerDataFetcher::SetTimeDeltaForTesting(1);
155 LoadURLAndWaitForServiceWorker(test_url);
156 RunFetcher(web_contents->GetURL(), true, false);
159 } // namespace banners