| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/banners/app_banner_data_fetcher.h" | 5 #include "chrome/browser/banners/app_banner_data_fetcher.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 AppBannerDataFetcher* fetcher_; | 49 AppBannerDataFetcher* fetcher_; |
| 50 base::Closure quit_closure_; | 50 base::Closure quit_closure_; |
| 51 scoped_ptr<bool> will_show_; | 51 scoped_ptr<bool> will_show_; |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 class AppBannerDataFetcherBrowserTest : public InProcessBrowserTest, | 54 class AppBannerDataFetcherBrowserTest : public InProcessBrowserTest, |
| 55 public AppBannerDataFetcher::Delegate { | 55 public AppBannerDataFetcher::Delegate { |
| 56 public: | 56 public: |
| 57 AppBannerDataFetcherBrowserTest() : manifest_was_invalid_(false), | 57 AppBannerDataFetcherBrowserTest() : weak_factory_(this) { |
| 58 weak_factory_(this) { | |
| 59 } | 58 } |
| 60 | 59 |
| 61 void SetUpOnMainThread() override { | 60 void SetUpOnMainThread() override { |
| 62 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 61 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 63 InProcessBrowserTest::SetUpOnMainThread(); | 62 InProcessBrowserTest::SetUpOnMainThread(); |
| 64 } | 63 } |
| 65 | 64 |
| 66 bool OnInvalidManifest(AppBannerDataFetcher* fetcher) override { | 65 bool HandleNonWebApp(const std::string& platform, |
| 66 const GURL& url, |
| 67 const std::string& id) override { |
| 67 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); | 68 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); |
| 68 manifest_was_invalid_ = true; | 69 non_web_platform_ = platform; |
| 69 return false; | 70 return false; |
| 70 } | 71 } |
| 71 | 72 |
| 72 void SetUpCommandLine(base::CommandLine* command_line) override { | 73 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 73 command_line->AppendSwitch( | 74 command_line->AppendSwitch( |
| 74 switches::kEnableExperimentalWebPlatformFeatures); | 75 switches::kEnableExperimentalWebPlatformFeatures); |
| 75 } | 76 } |
| 76 | 77 |
| 77 protected: | 78 protected: |
| 78 void RunFetcher(const GURL& url, | 79 void RunFetcher(const GURL& url, |
| 79 bool expected_manifest_valid, | 80 const std::string& expected_non_web_platform, |
| 80 bool expected_to_show) { | 81 bool expected_to_show) { |
| 81 content::WebContents* web_contents = | 82 content::WebContents* web_contents = |
| 82 browser()->tab_strip_model()->GetActiveWebContents(); | 83 browser()->tab_strip_model()->GetActiveWebContents(); |
| 83 scoped_refptr<AppBannerDataFetcher> fetcher( | 84 scoped_refptr<AppBannerDataFetcher> fetcher( |
| 84 new AppBannerDataFetcher(web_contents, weak_factory_.GetWeakPtr(), | 85 new AppBannerDataFetcher(web_contents, weak_factory_.GetWeakPtr(), |
| 85 128)); | 86 128)); |
| 86 | 87 |
| 87 base::RunLoop run_loop; | 88 base::RunLoop run_loop; |
| 88 quit_closure_ = run_loop.QuitClosure(); | 89 quit_closure_ = run_loop.QuitClosure(); |
| 89 scoped_ptr<TestObserver> observer(new TestObserver(fetcher.get(), | 90 scoped_ptr<TestObserver> observer(new TestObserver(fetcher.get(), |
| 90 run_loop.QuitClosure())); | 91 run_loop.QuitClosure())); |
| 91 fetcher->Start(url); | 92 fetcher->Start(url); |
| 92 run_loop.Run(); | 93 run_loop.Run(); |
| 93 | 94 |
| 94 EXPECT_EQ(expected_manifest_valid, !manifest_was_invalid_); | 95 EXPECT_EQ(expected_non_web_platform, non_web_platform_); |
| 95 EXPECT_EQ(expected_to_show, observer->will_show()); | 96 EXPECT_EQ(expected_to_show, observer->will_show()); |
| 96 ASSERT_FALSE(fetcher->is_active()); | 97 ASSERT_FALSE(fetcher->is_active()); |
| 97 } | 98 } |
| 98 | 99 |
| 99 void LoadURLAndWaitForServiceWorker(const GURL& url) { | 100 void LoadURLAndWaitForServiceWorker(const GURL& url) { |
| 100 content::WebContents* web_contents = | 101 content::WebContents* web_contents = |
| 101 browser()->tab_strip_model()->GetActiveWebContents(); | 102 browser()->tab_strip_model()->GetActiveWebContents(); |
| 102 content::TestNavigationObserver observer(web_contents, 2); | 103 content::TestNavigationObserver observer(web_contents, 2); |
| 103 ui_test_utils::NavigateToURL(browser(), url); | 104 ui_test_utils::NavigateToURL(browser(), url); |
| 104 observer.Wait(); | 105 observer.Wait(); |
| 105 EXPECT_EQ("sw_activated", observer.last_navigation_url().ref()); | 106 EXPECT_EQ("sw_activated", observer.last_navigation_url().ref()); |
| 106 } | 107 } |
| 107 | 108 |
| 108 private: | 109 private: |
| 109 bool manifest_was_invalid_; | 110 std::string non_web_platform_; |
| 110 base::Closure quit_closure_; | 111 base::Closure quit_closure_; |
| 111 base::WeakPtrFactory<AppBannerDataFetcherBrowserTest> weak_factory_; | 112 base::WeakPtrFactory<AppBannerDataFetcherBrowserTest> weak_factory_; |
| 112 }; | 113 }; |
| 113 | 114 |
| 114 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, WebAppBannerCreated) { | 115 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, WebAppBannerCreated) { |
| 115 std::string valid_page = "/banners/manifest_test_page.html"; | 116 std::string valid_page("/banners/manifest_test_page.html"); |
| 116 GURL test_url = embedded_test_server()->GetURL(valid_page); | 117 GURL test_url = embedded_test_server()->GetURL(valid_page); |
| 117 content::WebContents* web_contents = | 118 content::WebContents* web_contents = |
| 118 browser()->tab_strip_model()->GetActiveWebContents(); | 119 browser()->tab_strip_model()->GetActiveWebContents(); |
| 119 | 120 |
| 120 LoadURLAndWaitForServiceWorker(test_url); | 121 LoadURLAndWaitForServiceWorker(test_url); |
| 121 RunFetcher(web_contents->GetURL(), true, false); | 122 RunFetcher(web_contents->GetURL(), std::string(), false); |
| 122 | 123 |
| 123 // Advance by a day, then visit the page again to trigger the banner. | 124 // Advance by a day, then visit the page again to trigger the banner. |
| 124 AppBannerDataFetcher::SetTimeDeltaForTesting(1); | 125 AppBannerDataFetcher::SetTimeDeltaForTesting(1); |
| 125 LoadURLAndWaitForServiceWorker(test_url); | 126 LoadURLAndWaitForServiceWorker(test_url); |
| 126 RunFetcher(web_contents->GetURL(), true, true); | 127 RunFetcher(web_contents->GetURL(), std::string(), true); |
| 128 } |
| 129 |
| 130 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, PlayAppManifest) { |
| 131 std::string valid_page("/banners/play_app_test_page.html"); |
| 132 GURL test_url = embedded_test_server()->GetURL(valid_page); |
| 133 content::WebContents* web_contents = |
| 134 browser()->tab_strip_model()->GetActiveWebContents(); |
| 135 |
| 136 // Native banners do not require the SW, so we can just load the URL. |
| 137 ui_test_utils::NavigateToURL(browser(), test_url); |
| 138 std::string play_platform("play"); |
| 139 RunFetcher(web_contents->GetURL(), play_platform, false); |
| 140 |
| 141 // The logic to get the details for a play app banner are only on android |
| 142 // builds, so this test does not check that the banner is shown. |
| 127 } | 143 } |
| 128 | 144 |
| 129 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, NoManifest) { | 145 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, NoManifest) { |
| 130 std::string valid_page = "/banners/no_manifest_test_page.html"; | 146 std::string valid_page("/banners/no_manifest_test_page.html"); |
| 131 GURL test_url = embedded_test_server()->GetURL(valid_page); | 147 GURL test_url = embedded_test_server()->GetURL(valid_page); |
| 132 content::WebContents* web_contents = | 148 content::WebContents* web_contents = |
| 133 browser()->tab_strip_model()->GetActiveWebContents(); | 149 browser()->tab_strip_model()->GetActiveWebContents(); |
| 134 | 150 |
| 135 LoadURLAndWaitForServiceWorker(test_url); | 151 LoadURLAndWaitForServiceWorker(test_url); |
| 136 RunFetcher(web_contents->GetURL(), false, false); | 152 RunFetcher(web_contents->GetURL(), std::string(), false); |
| 137 | 153 |
| 138 // Advance by a day, then visit the page again. Still shouldn't see a banner. | 154 // Advance by a day, then visit the page again. Still shouldn't see a banner. |
| 139 AppBannerDataFetcher::SetTimeDeltaForTesting(1); | 155 AppBannerDataFetcher::SetTimeDeltaForTesting(1); |
| 140 LoadURLAndWaitForServiceWorker(test_url); | 156 LoadURLAndWaitForServiceWorker(test_url); |
| 141 RunFetcher(web_contents->GetURL(), false, false); | 157 RunFetcher(web_contents->GetURL(), std::string(), false); |
| 142 } | 158 } |
| 143 | 159 |
| 144 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, CancelBanner) { | 160 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, CancelBanner) { |
| 145 std::string valid_page = "/banners/cancel_test_page.html"; | 161 std::string valid_page("/banners/cancel_test_page.html"); |
| 146 GURL test_url = embedded_test_server()->GetURL(valid_page); | 162 GURL test_url = embedded_test_server()->GetURL(valid_page); |
| 147 content::WebContents* web_contents = | 163 content::WebContents* web_contents = |
| 148 browser()->tab_strip_model()->GetActiveWebContents(); | 164 browser()->tab_strip_model()->GetActiveWebContents(); |
| 149 | 165 |
| 150 LoadURLAndWaitForServiceWorker(test_url); | 166 LoadURLAndWaitForServiceWorker(test_url); |
| 151 RunFetcher(web_contents->GetURL(), true, false); | 167 RunFetcher(web_contents->GetURL(), std::string(), false); |
| 152 | 168 |
| 153 // Advance by a day, then visit the page again. Still shouldn't see a banner. | 169 // Advance by a day, then visit the page again. Still shouldn't see a banner. |
| 154 AppBannerDataFetcher::SetTimeDeltaForTesting(1); | 170 AppBannerDataFetcher::SetTimeDeltaForTesting(1); |
| 155 LoadURLAndWaitForServiceWorker(test_url); | 171 LoadURLAndWaitForServiceWorker(test_url); |
| 156 RunFetcher(web_contents->GetURL(), true, false); | 172 RunFetcher(web_contents->GetURL(), std::string(), false); |
| 157 } | 173 } |
| 158 | 174 |
| 159 } // namespace banners | 175 } // namespace banners |
| OLD | NEW |