OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/banners/app_banner_data_fetcher.h" | |
6 | |
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/test/histogram_tester.h" | |
13 #include "base/threading/thread_task_runner_handle.h" | |
14 #include "chrome/browser/banners/app_banner_data_fetcher_desktop.h" | |
15 #include "chrome/browser/banners/app_banner_metrics.h" | |
16 #include "chrome/browser/banners/app_banner_settings_helper.h" | |
17 #include "chrome/browser/ui/browser.h" | |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
19 #include "chrome/common/chrome_switches.h" | |
20 #include "chrome/test/base/in_process_browser_test.h" | |
21 #include "chrome/test/base/ui_test_utils.h" | |
22 #include "content/public/common/content_switches.h" | |
23 #include "content/public/test/test_navigation_observer.h" | |
24 #include "net/test/embedded_test_server/embedded_test_server.h" | |
25 | |
26 namespace banners { | |
27 | |
28 class TestObserver : public AppBannerDataFetcher::Observer { | |
29 public: | |
30 TestObserver(AppBannerDataFetcher* fetcher, base::Closure quit_closure) | |
31 : fetcher_(fetcher), | |
32 quit_closure_(quit_closure) { | |
33 fetcher_->AddObserverForTesting(this); | |
34 } | |
35 | |
36 virtual ~TestObserver() { | |
37 if (fetcher_) | |
38 fetcher_->RemoveObserverForTesting(this); | |
39 } | |
40 | |
41 void OnDecidedWhetherToShow(AppBannerDataFetcher* fetcher, | |
42 bool will_show) override { | |
43 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | |
44 ASSERT_FALSE(will_show_.get()); | |
45 will_show_.reset(new bool(will_show)); | |
46 } | |
47 | |
48 void OnFetcherDestroyed(AppBannerDataFetcher* fetcher) override { | |
49 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | |
50 fetcher_ = nullptr; | |
51 } | |
52 | |
53 bool will_show() { return will_show_.get() && *will_show_; } | |
54 | |
55 private: | |
56 AppBannerDataFetcher* fetcher_; | |
57 base::Closure quit_closure_; | |
58 std::unique_ptr<bool> will_show_; | |
59 }; | |
60 | |
61 class AppBannerDataFetcherBrowserTest : public InProcessBrowserTest, | |
62 public AppBannerDataFetcher::Delegate { | |
63 public: | |
64 AppBannerDataFetcherBrowserTest() : weak_factory_(this) { | |
65 } | |
66 | |
67 void SetUpOnMainThread() override { | |
68 AppBannerSettingsHelper::SetEngagementWeights(1, 1); | |
69 AppBannerSettingsHelper::SetTotalEngagementToTrigger(2); | |
70 ASSERT_TRUE(embedded_test_server()->Start()); | |
71 InProcessBrowserTest::SetUpOnMainThread(); | |
72 } | |
73 | |
74 bool HandleNonWebApp(const std::string& platform, | |
75 const GURL& url, | |
76 const std::string& id, | |
77 bool is_debug_mode) override { | |
78 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | |
79 non_web_platform_ = platform; | |
80 return false; | |
81 } | |
82 | |
83 void SetUpCommandLine(base::CommandLine* command_line) override { | |
84 command_line->AppendSwitch( | |
85 switches::kEnableExperimentalWebPlatformFeatures); | |
86 // Make sure app banners are disabled in the browser, otherwise they will | |
87 // interfere with the test. | |
88 command_line->AppendSwitch(switches::kDisableAddToShelf); | |
89 } | |
90 | |
91 protected: | |
92 void RunFetcher(const GURL& url, | |
93 const std::string& expected_non_web_platform, | |
94 ui::PageTransition transition, | |
95 bool expected_to_show) { | |
96 content::WebContents* web_contents = | |
97 browser()->tab_strip_model()->GetActiveWebContents(); | |
98 scoped_refptr<AppBannerDataFetcherDesktop> fetcher( | |
99 new AppBannerDataFetcherDesktop( | |
100 web_contents, weak_factory_.GetWeakPtr(), 128, 128, false)); | |
101 | |
102 base::HistogramTester histograms; | |
103 base::RunLoop run_loop; | |
104 quit_closure_ = run_loop.QuitClosure(); | |
105 std::unique_ptr<TestObserver> observer( | |
106 new TestObserver(fetcher.get(), run_loop.QuitClosure())); | |
107 fetcher->Start(url, transition); | |
108 run_loop.Run(); | |
109 | |
110 EXPECT_EQ(expected_non_web_platform, non_web_platform_); | |
111 EXPECT_EQ(expected_to_show, observer->will_show()); | |
112 ASSERT_FALSE(fetcher->is_active()); | |
113 | |
114 // If showing the banner, ensure that the minutes histogram is recorded. | |
115 histograms.ExpectTotalCount(banners::kMinutesHistogram, | |
116 (observer->will_show() ? 1 : 0)); | |
117 } | |
118 | |
119 void RunBannerTest(const std::string& manifest_page, | |
120 ui::PageTransition transition, | |
121 unsigned int unshown_repetitions, | |
122 bool expectation) { | |
123 std::string valid_page(manifest_page); | |
124 GURL test_url = embedded_test_server()->GetURL(valid_page); | |
125 content::WebContents* web_contents = | |
126 browser()->tab_strip_model()->GetActiveWebContents(); | |
127 | |
128 for (unsigned int i = 0; i < unshown_repetitions; ++i) { | |
129 ui_test_utils::NavigateToURL(browser(), test_url); | |
130 RunFetcher(web_contents->GetURL(), std::string(), transition, false); | |
131 AppBannerDataFetcher::SetTimeDeltaForTesting(i+1); | |
132 } | |
133 | |
134 // On the final loop, check whether the banner triggered or not as expected. | |
135 ui_test_utils::NavigateToURL(browser(), test_url); | |
136 RunFetcher(web_contents->GetURL(), std::string(), transition, expectation); | |
137 } | |
138 | |
139 private: | |
140 std::string non_web_platform_; | |
141 base::Closure quit_closure_; | |
142 base::WeakPtrFactory<AppBannerDataFetcherBrowserTest> weak_factory_; | |
143 }; | |
144 | |
145 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
146 WebAppBannerCreatedDirect) { | |
147 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED, | |
148 1, true); | |
149 } | |
150 | |
151 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
152 WebAppBannerCreatedDirectLargerTotal) { | |
153 AppBannerSettingsHelper::SetTotalEngagementToTrigger(4); | |
154 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED, | |
155 3, true); | |
156 } | |
157 | |
158 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
159 WebAppBannerCreatedDirectSmallerTotal) { | |
160 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1); | |
161 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_TYPED, | |
162 0, true); | |
163 } | |
164 | |
165 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
166 WebAppBannerCreatedDirectSingle) { | |
167 AppBannerSettingsHelper::SetEngagementWeights(2, 1); | |
168 RunBannerTest("/banners/manifest_test_page.html", | |
169 ui::PAGE_TRANSITION_GENERATED, 0, true); | |
170 } | |
171 | |
172 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
173 WebAppBannerCreatedDirectMultiple) { | |
174 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1); | |
175 RunBannerTest("/banners/manifest_test_page.html", | |
176 ui::PAGE_TRANSITION_GENERATED, 3, true); | |
177 } | |
178 | |
179 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
180 WebAppBannerCreatedDirectMultipleLargerTotal) { | |
181 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1); | |
182 AppBannerSettingsHelper::SetTotalEngagementToTrigger(3); | |
183 RunBannerTest("/banners/manifest_test_page.html", | |
184 ui::PAGE_TRANSITION_GENERATED, 5, true); | |
185 } | |
186 | |
187 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
188 WebAppBannerCreatedDirectMultipleSmallerTotal) { | |
189 AppBannerSettingsHelper::SetEngagementWeights(0.5, 1); | |
190 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1); | |
191 RunBannerTest("/banners/manifest_test_page.html", | |
192 ui::PAGE_TRANSITION_GENERATED, 1, true); | |
193 } | |
194 | |
195 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
196 WebAppBannerCreatedIndirect) { | |
197 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, | |
198 1, true); | |
199 } | |
200 | |
201 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
202 WebAppBannerCreatedIndirectLargerTotal) { | |
203 AppBannerSettingsHelper::SetTotalEngagementToTrigger(5); | |
204 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, | |
205 4, true); | |
206 } | |
207 | |
208 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
209 WebAppBannerCreatedIndirectSmallerTotal) { | |
210 AppBannerSettingsHelper::SetTotalEngagementToTrigger(1); | |
211 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, | |
212 0, true); | |
213 } | |
214 | |
215 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
216 WebAppBannerCreatedIndirectSingle) { | |
217 AppBannerSettingsHelper::SetEngagementWeights(1, 3); | |
218 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_RELOAD, | |
219 0, true); | |
220 } | |
221 | |
222 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
223 WebAppBannerCreatedIndirectMultiple) { | |
224 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5); | |
225 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, | |
226 3, true); | |
227 } | |
228 | |
229 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
230 WebAppBannerCreatedIndirectMultipleLargerTotal) { | |
231 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5); | |
232 AppBannerSettingsHelper::SetTotalEngagementToTrigger(4); | |
233 RunBannerTest("/banners/manifest_test_page.html", ui::PAGE_TRANSITION_LINK, | |
234 7, true); | |
235 } | |
236 | |
237 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
238 WebAppBannerCreatedVarious) { | |
239 AppBannerSettingsHelper::SetEngagementWeights(0.5, 0.25); | |
240 | |
241 std::string valid_page("/banners/manifest_test_page.html"); | |
242 GURL test_url = embedded_test_server()->GetURL(valid_page); | |
243 content::WebContents* web_contents = | |
244 browser()->tab_strip_model()->GetActiveWebContents(); | |
245 | |
246 // Add a direct nav on day 1. | |
247 ui_test_utils::NavigateToURL(browser(), test_url); | |
248 RunFetcher(web_contents->GetURL(), std::string(), ui::PAGE_TRANSITION_TYPED, | |
249 false); | |
250 | |
251 // Add an indirect nav on day 1 which is ignored. | |
252 ui_test_utils::NavigateToURL(browser(), test_url); | |
253 RunFetcher(web_contents->GetURL(), std::string(), ui::PAGE_TRANSITION_LINK, | |
254 false); | |
255 AppBannerDataFetcher::SetTimeDeltaForTesting(1); | |
256 | |
257 // Add an indirect nav on day 2. | |
258 ui_test_utils::NavigateToURL(browser(), test_url); | |
259 RunFetcher(web_contents->GetURL(), std::string(), | |
260 ui::PAGE_TRANSITION_MANUAL_SUBFRAME, false); | |
261 | |
262 // Add a direct nav on day 2 which overrides. | |
263 ui_test_utils::NavigateToURL(browser(), test_url); | |
264 RunFetcher(web_contents->GetURL(), std::string(), | |
265 ui::PAGE_TRANSITION_GENERATED, false); | |
266 AppBannerDataFetcher::SetTimeDeltaForTesting(2); | |
267 | |
268 // Add a direct nav on day 3. | |
269 ui_test_utils::NavigateToURL(browser(), test_url); | |
270 RunFetcher(web_contents->GetURL(), std::string(), | |
271 ui::PAGE_TRANSITION_GENERATED, false); | |
272 AppBannerDataFetcher::SetTimeDeltaForTesting(3); | |
273 | |
274 // Add an indirect nav on day 4. | |
275 ui_test_utils::NavigateToURL(browser(), test_url); | |
276 RunFetcher(web_contents->GetURL(), std::string(), | |
277 ui::PAGE_TRANSITION_FORM_SUBMIT, false); | |
278 | |
279 // Add a direct nav on day 4 which should trigger the banner. | |
280 ui_test_utils::NavigateToURL(browser(), test_url); | |
281 RunFetcher(web_contents->GetURL(), std::string(), | |
282 ui::PAGE_TRANSITION_TYPED, true); | |
283 } | |
284 | |
285 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
286 WebAppBannerNoTypeInManifest) { | |
287 RunBannerTest("/banners/manifest_no_type_test_page.html", | |
288 ui::PAGE_TRANSITION_TYPED, 1, true); | |
289 } | |
290 | |
291 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, | |
292 WebAppBannerNoTypeInManifestCapsExtension) { | |
293 RunBannerTest("/banners/manifest_no_type_caps_test_page.html", | |
294 ui::PAGE_TRANSITION_TYPED, 1, true); | |
295 } | |
296 | |
297 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, PlayAppManifest) { | |
298 std::string valid_page("/banners/play_app_test_page.html"); | |
299 GURL test_url = embedded_test_server()->GetURL(valid_page); | |
300 content::WebContents* web_contents = | |
301 browser()->tab_strip_model()->GetActiveWebContents(); | |
302 | |
303 // Native banners do not require the SW, so we can just load the URL. | |
304 ui_test_utils::NavigateToURL(browser(), test_url); | |
305 std::string play_platform("play"); | |
306 RunFetcher(web_contents->GetURL(), play_platform, ui::PAGE_TRANSITION_TYPED, | |
307 false); | |
308 | |
309 // The logic to get the details for a play app banner are only on android | |
310 // builds, so this test does not check that the banner is shown. | |
311 } | |
312 | |
313 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, NoManifest) { | |
314 RunBannerTest("/banners/no_manifest_test_page.html", | |
315 ui::PAGE_TRANSITION_TYPED, 1, false); | |
316 } | |
317 | |
318 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, MissingManifest) { | |
319 RunBannerTest("/banners/manifest_bad_link.html", | |
320 ui::PAGE_TRANSITION_TYPED, 1, false); | |
321 } | |
322 | |
323 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, CancelBannerDirect) { | |
324 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED, 1, | |
325 false); | |
326 } | |
327 | |
328 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, CancelBannerIndirect) { | |
329 AppBannerSettingsHelper::SetEngagementWeights(1, 0.5); | |
330 RunBannerTest("/banners/cancel_test_page.html", ui::PAGE_TRANSITION_TYPED, 3, | |
331 false); | |
332 } | |
333 | |
334 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, PromptBanner) { | |
335 RunBannerTest("/banners/prompt_test_page.html", ui::PAGE_TRANSITION_TYPED, 1, | |
336 true); | |
337 } | |
338 | |
339 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, PromptBannerInHandler) { | |
340 RunBannerTest("/banners/prompt_in_handler_test_page.html", | |
341 ui::PAGE_TRANSITION_TYPED, 1, true); | |
342 } | |
343 | |
344 IN_PROC_BROWSER_TEST_F(AppBannerDataFetcherBrowserTest, WebAppBannerInIFrame) { | |
345 RunBannerTest("/banners/iframe_test_page.html", ui::PAGE_TRANSITION_TYPED, 1, | |
346 false); | |
347 } | |
348 | |
349 } // namespace banners | |
OLD | NEW |