Allow the minimum time between banner trigger visits to be varied via field trials.
[chromium-blink-merge.git] / chrome / browser / banners / app_banner_settings_helper_unittest.cc
blob82550f15b6700a5acae9ad0e4de4880c14904c90
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 <vector>
7 #include "chrome/browser/banners/app_banner_settings_helper.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "ui/base/page_transition_types.h"
11 namespace {
13 const char kTestURL[] = "https://www.google.com";
14 const char kSameOriginTestURL[] = "https://www.google.com/foo.html";
15 const char kTestPackageName[] = "test.package";
17 base::Time GetReferenceTime() {
18 base::Time::Exploded exploded_reference_time;
19 exploded_reference_time.year = 2015;
20 exploded_reference_time.month = 1;
21 exploded_reference_time.day_of_month = 30;
22 exploded_reference_time.day_of_week = 5;
23 exploded_reference_time.hour = 11;
24 exploded_reference_time.minute = 0;
25 exploded_reference_time.second = 0;
26 exploded_reference_time.millisecond = 0;
28 return base::Time::FromLocalExploded(exploded_reference_time);
31 bool IsWithinDay(base::Time time1, base::Time time2) {
32 return time1 - time2 < base::TimeDelta::FromDays(1) ||
33 time2 - time1 < base::TimeDelta::FromDays(1);
36 bool IsWithinHour(base::Time time1, base::Time time2) {
37 return time1 - time2 < base::TimeDelta::FromHours(1) ||
38 time2 - time1 < base::TimeDelta::FromHours(1);
41 class AppBannerSettingsHelperTest : public ChromeRenderViewHostTestHarness {};
43 } // namespace
45 TEST_F(AppBannerSettingsHelperTest, BucketTimeToResolutionInvalid) {
46 base::Time reference_time = GetReferenceTime();
48 // Test null, 1 day, and greater than 1 day cases.
49 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 0),
50 reference_time.LocalMidnight());
51 EXPECT_EQ(
52 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 1440),
53 reference_time.LocalMidnight());
54 EXPECT_EQ(
55 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 2880),
56 reference_time.LocalMidnight());
58 // Test number of minutes in 1 day + 1.
59 EXPECT_EQ(
60 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 1441),
61 reference_time.LocalMidnight());
63 // Test minutes which are not divisible by 1440 (minutes in a day).
64 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 7),
65 reference_time.LocalMidnight());
66 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 13),
67 reference_time.LocalMidnight());
68 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 21),
69 reference_time.LocalMidnight());
70 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 35),
71 reference_time.LocalMidnight());
72 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 42),
73 reference_time.LocalMidnight());
74 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 50),
75 reference_time.LocalMidnight());
76 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 59),
77 reference_time.LocalMidnight());
80 TEST_F(AppBannerSettingsHelperTest, BucketTimeToResolutionValid) {
81 // 11:00
82 base::Time reference_time = GetReferenceTime();
83 // 13:44
84 base::Time same_day_later =
85 reference_time + base::TimeDelta::FromMinutes(164);
86 // 10:18
87 base::Time same_day_earlier =
88 reference_time - base::TimeDelta::FromMinutes(42);
89 base::Time midnight = reference_time.LocalMidnight();
90 base::Time bucketed_hour = midnight + base::TimeDelta::FromHours(11);
91 base::Time bucketed_hour_later = midnight + base::TimeDelta::FromHours(13);
92 base::Time bucketed_hour_earlier = midnight + base::TimeDelta::FromHours(10);
94 // Resolution of 1 minute: 11:00, 13:44, 10:18.
95 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 1),
96 bucketed_hour);
97 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 1),
98 bucketed_hour_later + base::TimeDelta::FromMinutes(44));
99 EXPECT_EQ(
100 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 1),
101 bucketed_hour_earlier + base::TimeDelta::FromMinutes(18));
103 // Resolution of 3 minutes: 11:00, 13:43, 10:18.
104 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 3),
105 bucketed_hour);
106 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 3),
107 bucketed_hour_later + base::TimeDelta::FromMinutes(42));
108 EXPECT_EQ(
109 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 3),
110 bucketed_hour_earlier + base::TimeDelta::FromMinutes(18));
112 // Resolution of 10 minutes: 11:00, 13:40, 10:10.
113 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 10),
114 bucketed_hour);
115 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 10),
116 bucketed_hour_later + base::TimeDelta::FromMinutes(40));
117 EXPECT_EQ(
118 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 10),
119 bucketed_hour_earlier + base::TimeDelta::FromMinutes(10));
121 // Resolution of 20 minutes: 11:00, 13:40, 10:00.
122 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 20),
123 bucketed_hour);
124 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 20),
125 bucketed_hour_later + base::TimeDelta::FromMinutes(40));
126 EXPECT_EQ(
127 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 20),
128 bucketed_hour_earlier);
130 // Resolution of 60 minutes: 11:00, 13:00, 10:00.
131 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 60),
132 bucketed_hour);
133 EXPECT_EQ(AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 60),
134 bucketed_hour_later);
135 EXPECT_EQ(
136 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 60),
137 bucketed_hour_earlier);
139 // Resolution of 120 minutes: 10:00, 12:00, 10:00.
140 EXPECT_EQ(
141 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 120),
142 bucketed_hour_earlier);
143 EXPECT_EQ(
144 AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 120),
145 bucketed_hour_later - base::TimeDelta::FromHours(1));
146 EXPECT_EQ(
147 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 120),
148 bucketed_hour_earlier);
150 // Resolution of 180 minutes: 9:00, 12:00, 9:00.
151 EXPECT_EQ(
152 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 180),
153 bucketed_hour_earlier - base::TimeDelta::FromHours(1));
154 EXPECT_EQ(
155 AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 180),
156 bucketed_hour_later - base::TimeDelta::FromHours(1));
157 EXPECT_EQ(
158 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 180),
159 bucketed_hour_earlier - base::TimeDelta::FromHours(1));
161 // Resolution of 240 minutes: 8:00, 12:00, 8:00.
162 EXPECT_EQ(
163 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 240),
164 midnight + base::TimeDelta::FromHours(8));
165 EXPECT_EQ(
166 AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 240),
167 midnight + base::TimeDelta::FromHours(12));
168 EXPECT_EQ(
169 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 240),
170 midnight + base::TimeDelta::FromHours(8));
172 // Resolution of 360 minutes: 6:00, 12:00, 6:00
173 EXPECT_EQ(
174 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 360),
175 midnight + base::TimeDelta::FromHours(6));
176 EXPECT_EQ(
177 AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 360),
178 midnight + base::TimeDelta::FromHours(12));
179 EXPECT_EQ(
180 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 360),
181 midnight + base::TimeDelta::FromHours(6));
183 // Resolution of 720 minutes: 0:00, 12:00, 0:00
184 EXPECT_EQ(
185 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 720),
186 midnight);
187 EXPECT_EQ(
188 AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 720),
189 midnight + base::TimeDelta::FromHours(12));
190 EXPECT_EQ(
191 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 720),
192 midnight);
194 // Resolution of 1440 minutes: 0:00, 0:00, 0:00
195 EXPECT_EQ(
196 AppBannerSettingsHelper::BucketTimeToResolution(reference_time, 1440),
197 midnight);
198 EXPECT_EQ(
199 AppBannerSettingsHelper::BucketTimeToResolution(same_day_later, 1440),
200 midnight);
201 EXPECT_EQ(
202 AppBannerSettingsHelper::BucketTimeToResolution(same_day_earlier, 1440),
203 midnight);
206 TEST_F(AppBannerSettingsHelperTest, CouldShowEvents) {
207 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
208 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(1440);
209 GURL url(kTestURL);
210 NavigateAndCommit(url);
212 // Check that by default, there are no events recorded.
213 std::vector<AppBannerSettingsHelper::BannerEvent> events =
214 AppBannerSettingsHelper::GetCouldShowBannerEvents(web_contents(), url,
215 kTestPackageName);
216 EXPECT_TRUE(events.empty());
218 base::Time reference_time = GetReferenceTime();
219 base::Time same_day = reference_time + base::TimeDelta::FromHours(2);
220 base::Time three_days_prior = reference_time - base::TimeDelta::FromDays(3);
221 base::Time previous_fortnight =
222 reference_time - base::TimeDelta::FromDays(14);
224 // Test adding the first date.
225 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
226 web_contents(), url, kTestPackageName, previous_fortnight,
227 ui::PAGE_TRANSITION_TYPED);
229 // It should be the only date recorded.
230 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
231 web_contents(), url, kTestPackageName);
232 EXPECT_EQ(1u, events.size());
233 EXPECT_TRUE(IsWithinDay(events[0].time, previous_fortnight));
234 EXPECT_EQ(events[0].engagement, 1);
236 // Now add the next date.
237 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
238 web_contents(), url, kTestPackageName, three_days_prior,
239 ui::PAGE_TRANSITION_GENERATED);
241 // Now there should be two events.
242 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
243 web_contents(), url, kTestPackageName);
244 EXPECT_EQ(2u, events.size());
245 EXPECT_TRUE(IsWithinDay(events[0].time, previous_fortnight));
246 EXPECT_TRUE(IsWithinDay(events[1].time, three_days_prior));
247 EXPECT_EQ(events[0].engagement, 1);
248 EXPECT_EQ(events[1].engagement, 1);
250 // Now add the reference date.
251 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
252 web_contents(), url, kTestPackageName, reference_time,
253 ui::PAGE_TRANSITION_LINK);
255 // Now there should still be two events, but the first date should have been
256 // removed.
257 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
258 web_contents(), url, kTestPackageName);
259 EXPECT_EQ(2u, events.size());
260 EXPECT_TRUE(IsWithinDay(events[0].time, three_days_prior));
261 EXPECT_TRUE(IsWithinDay(events[1].time, reference_time));
262 EXPECT_EQ(events[0].engagement, 1);
263 EXPECT_EQ(events[1].engagement, 1);
265 // Now add the the other date on the reference day.
266 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
267 web_contents(), url, kTestPackageName, same_day,
268 ui::PAGE_TRANSITION_RELOAD);
270 // Now there should still be the same two dates.
271 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
272 web_contents(), url, kTestPackageName);
273 EXPECT_EQ(2u, events.size());
274 EXPECT_TRUE(IsWithinDay(events[0].time, three_days_prior));
275 EXPECT_TRUE(IsWithinDay(events[1].time, reference_time));
276 EXPECT_EQ(events[0].engagement, 1);
277 EXPECT_EQ(events[1].engagement, 1);
280 TEST_F(AppBannerSettingsHelperTest, CouldShowEventsDifferentResolution) {
281 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
282 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(20);
283 GURL url(kTestURL);
284 NavigateAndCommit(url);
286 // Check that by default, there are no events recorded.
287 std::vector<AppBannerSettingsHelper::BannerEvent> events =
288 AppBannerSettingsHelper::GetCouldShowBannerEvents(web_contents(), url,
289 kTestPackageName);
290 EXPECT_TRUE(events.empty());
292 base::Time reference_time = GetReferenceTime();
293 base::Time same_day_ignored_i =
294 reference_time + base::TimeDelta::FromMinutes(10);
295 base::Time same_day_counted_i =
296 reference_time + base::TimeDelta::FromMinutes(20);
297 base::Time same_day_counted_ii =
298 reference_time + base::TimeDelta::FromMinutes(45);
299 base::Time same_day_ignored_ii =
300 reference_time + base::TimeDelta::FromMinutes(59);
302 // Add the reference date.
303 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
304 web_contents(), url, kTestPackageName, reference_time,
305 ui::PAGE_TRANSITION_LINK);
307 // There should be one event recorded
308 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
309 web_contents(), url, kTestPackageName);
310 EXPECT_EQ(1u, events.size());
311 EXPECT_TRUE(IsWithinDay(events[0].time, reference_time));
312 EXPECT_EQ(events[0].engagement, 1);
314 // Now add the the ignored date on the reference day.
315 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
316 web_contents(), url, kTestPackageName, same_day_ignored_i,
317 ui::PAGE_TRANSITION_RELOAD);
319 // Now there should still one event.
320 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
321 web_contents(), url, kTestPackageName);
322 EXPECT_EQ(1u, events.size());
323 EXPECT_TRUE(IsWithinDay(events[0].time, reference_time));
324 EXPECT_EQ(events[0].engagement, 1);
326 // Now add the the first counted date on the reference day.
327 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
328 web_contents(), url, kTestPackageName, same_day_counted_i,
329 ui::PAGE_TRANSITION_TYPED);
331 // Now there should be two events.
332 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
333 web_contents(), url, kTestPackageName);
334 EXPECT_EQ(2u, events.size());
335 EXPECT_TRUE(IsWithinDay(events[0].time, reference_time));
336 EXPECT_TRUE(IsWithinDay(events[1].time, same_day_counted_i));
337 EXPECT_EQ(events[0].engagement, 1);
338 EXPECT_EQ(events[1].engagement, 1);
340 // Now add the the second counted date on the reference day.
341 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
342 web_contents(), url, kTestPackageName, same_day_counted_ii,
343 ui::PAGE_TRANSITION_GENERATED);
345 // Now there should be three events.
346 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
347 web_contents(), url, kTestPackageName);
348 EXPECT_EQ(3u, events.size());
349 EXPECT_TRUE(IsWithinDay(events[0].time, reference_time));
350 EXPECT_TRUE(IsWithinDay(events[1].time, same_day_counted_i));
351 EXPECT_TRUE(IsWithinDay(events[2].time, same_day_counted_ii));
352 EXPECT_EQ(events[0].engagement, 1);
353 EXPECT_EQ(events[1].engagement, 1);
354 EXPECT_EQ(events[2].engagement, 1);
356 // Now add the the second ignored date on the reference day.
357 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
358 web_contents(), url, kTestPackageName, same_day_ignored_ii,
359 ui::PAGE_TRANSITION_LINK);
361 // Now there should still be three events.
362 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
363 web_contents(), url, kTestPackageName);
364 EXPECT_EQ(3u, events.size());
365 EXPECT_TRUE(IsWithinDay(events[0].time, reference_time));
366 EXPECT_TRUE(IsWithinDay(events[1].time, same_day_counted_i));
367 EXPECT_TRUE(IsWithinDay(events[2].time, same_day_counted_ii));
368 EXPECT_EQ(events[0].engagement, 1);
369 EXPECT_EQ(events[1].engagement, 1);
370 EXPECT_EQ(events[2].engagement, 1);
373 TEST_F(AppBannerSettingsHelperTest, SingleEvents) {
374 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
375 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(1440);
376 GURL url(kTestURL);
377 NavigateAndCommit(url);
379 base::Time reference_time = GetReferenceTime();
380 base::Time other_time = reference_time - base::TimeDelta::FromDays(3);
381 for (int event = AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW;
382 event < AppBannerSettingsHelper::APP_BANNER_EVENT_NUM_EVENTS; ++event) {
383 // Check that by default, there is no event.
384 base::Time event_time = AppBannerSettingsHelper::GetSingleBannerEvent(
385 web_contents(), url, kTestPackageName,
386 AppBannerSettingsHelper::AppBannerEvent(event));
387 EXPECT_TRUE(event_time.is_null());
389 // Check that a time can be recorded.
390 AppBannerSettingsHelper::RecordBannerEvent(
391 web_contents(), url, kTestPackageName,
392 AppBannerSettingsHelper::AppBannerEvent(event), reference_time);
394 event_time = AppBannerSettingsHelper::GetSingleBannerEvent(
395 web_contents(), url, kTestPackageName,
396 AppBannerSettingsHelper::AppBannerEvent(event));
397 EXPECT_EQ(reference_time, event_time);
399 // Check that another time can be recorded.
400 AppBannerSettingsHelper::RecordBannerEvent(
401 web_contents(), url, kTestPackageName,
402 AppBannerSettingsHelper::AppBannerEvent(event), other_time);
404 event_time = AppBannerSettingsHelper::GetSingleBannerEvent(
405 web_contents(), url, kTestPackageName,
406 AppBannerSettingsHelper::AppBannerEvent(event));
407 EXPECT_EQ(other_time, event_time);
411 TEST_F(AppBannerSettingsHelperTest, CouldShowEventReplacedWithHigherWeight) {
412 // Set direct engagement to be worth 4 and indirect to be worth 2.
413 AppBannerSettingsHelper::SetEngagementWeights(4, 2);
414 GURL url(kTestURL);
415 NavigateAndCommit(url);
417 base::Time reference_time = GetReferenceTime();
418 base::Time later_same_day = reference_time + base::TimeDelta::FromHours(2);
419 base::Time later_again_same_day =
420 reference_time + base::TimeDelta::FromHours(6);
421 base::Time next_day = reference_time + base::TimeDelta::FromDays(1);
422 base::Time later_next_day = next_day + base::TimeDelta::FromHours(3);
424 // Ensure there are no events recorded by default.
425 std::vector<AppBannerSettingsHelper::BannerEvent> events =
426 AppBannerSettingsHelper::GetCouldShowBannerEvents(web_contents(), url,
427 kTestPackageName);
428 EXPECT_TRUE(events.empty());
430 // Record an indirect engagement type.
431 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
432 web_contents(), url, kTestPackageName, reference_time,
433 ui::PAGE_TRANSITION_LINK);
435 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
436 web_contents(), url, kTestPackageName);
438 EXPECT_EQ(1u, events.size());
439 EXPECT_TRUE(IsWithinHour(events[0].time, reference_time));
440 EXPECT_EQ(2, events[0].engagement);
442 // Record a direct engagement type. This should override the previous value.
443 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
444 web_contents(), url, kTestPackageName,
445 later_same_day, ui::PAGE_TRANSITION_TYPED);
447 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
448 web_contents(), url, kTestPackageName);
450 EXPECT_EQ(1u, events.size());
451 EXPECT_TRUE(IsWithinHour(events[0].time, later_same_day));
452 EXPECT_EQ(4, events[0].engagement);
454 // Record an indirect engagement type. This should be ignored.
455 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
456 web_contents(), url, kTestPackageName,
457 later_again_same_day, ui::PAGE_TRANSITION_RELOAD);
459 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
460 web_contents(), url, kTestPackageName);
462 EXPECT_EQ(1u, events.size());
463 EXPECT_TRUE(IsWithinHour(events[0].time, later_same_day));
464 EXPECT_EQ(4, events[0].engagement);
466 // Record an indirect engagement type one day later. This should appear.
467 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
468 web_contents(), url, kTestPackageName, next_day,
469 ui::PAGE_TRANSITION_AUTO_BOOKMARK);
471 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
472 web_contents(), url, kTestPackageName);
474 EXPECT_EQ(2u, events.size());
475 EXPECT_TRUE(IsWithinHour(events[0].time, later_same_day));
476 EXPECT_EQ(4, events[0].engagement);
477 EXPECT_TRUE(IsWithinHour(events[1].time, next_day));
478 EXPECT_EQ(2, events[1].engagement);
480 // Record a direct engagement type later on the next day. This should override
481 // the previous value.
482 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
483 web_contents(), url, kTestPackageName, later_next_day,
484 ui::PAGE_TRANSITION_GENERATED);
486 events = AppBannerSettingsHelper::GetCouldShowBannerEvents(
487 web_contents(), url, kTestPackageName);
489 EXPECT_EQ(2u, events.size());
490 EXPECT_TRUE(IsWithinHour(events[0].time, later_same_day));
491 EXPECT_EQ(4, events[0].engagement);
492 EXPECT_TRUE(IsWithinHour(events[1].time, later_next_day));
493 EXPECT_EQ(4, events[1].engagement);
496 TEST_F(AppBannerSettingsHelperTest, IndirectEngagementWithLowerWeight) {
497 AppBannerSettingsHelper::SetEngagementWeights(2, 0.5);
498 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(1440);
499 GURL url(kTestURL);
500 NavigateAndCommit(url);
502 base::Time reference_time = GetReferenceTime();
503 base::Time second_day = reference_time + base::TimeDelta::FromDays(1);
504 base::Time third_day = reference_time + base::TimeDelta::FromDays(2);
505 base::Time fourth_day = reference_time + base::TimeDelta::FromDays(3);
507 // By default the banner should not be shown.
508 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
509 web_contents(), url, kTestPackageName, reference_time));
511 // It should take four indirect visits with a weight of 0.5 to trigger the
512 // banner.
513 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
514 web_contents(), url, kTestPackageName, reference_time,
515 ui::PAGE_TRANSITION_LINK);
516 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
517 web_contents(), url, kTestPackageName, reference_time));
519 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
520 web_contents(), url, kTestPackageName, second_day,
521 ui::PAGE_TRANSITION_LINK);
522 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
523 web_contents(), url, kTestPackageName, second_day));
525 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
526 web_contents(), url, kTestPackageName, third_day,
527 ui::PAGE_TRANSITION_FORM_SUBMIT);
528 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
529 web_contents(), url, kTestPackageName, third_day));
531 // Visit the site again; now it should be shown.
532 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
533 web_contents(), url, kTestPackageName, fourth_day,
534 ui::PAGE_TRANSITION_MANUAL_SUBFRAME);
535 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
536 web_contents(), url, kTestPackageName, fourth_day));
539 TEST_F(AppBannerSettingsHelperTest, DirectEngagementWithHigherWeight) {
540 AppBannerSettingsHelper::SetEngagementWeights(2, 0.5);
541 GURL url(kTestURL);
542 NavigateAndCommit(url);
544 base::Time reference_time = GetReferenceTime();
546 // By default the banner should not be shown.
547 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
548 web_contents(), url, kTestPackageName, reference_time));
550 // It should take one direct visit with a weight of 2 to trigger the banner.
551 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
552 web_contents(), url, kTestPackageName, reference_time,
553 ui::PAGE_TRANSITION_TYPED);
554 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
555 web_contents(), url, kTestPackageName, reference_time));
558 TEST_F(AppBannerSettingsHelperTest, ShouldShowFromEngagement) {
559 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
560 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(1440);
561 GURL url(kTestURL);
562 NavigateAndCommit(url);
564 base::Time reference_time = GetReferenceTime();
565 base::Time one_day_ago = reference_time - base::TimeDelta::FromDays(1);
566 base::Time one_year_ago = reference_time - base::TimeDelta::FromDays(366);
568 // By default the banner should not be shown.
569 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
570 web_contents(), url, kTestPackageName, reference_time));
572 // Visit the site once, it still should not be shown.
573 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
574 web_contents(), url, kTestPackageName, one_year_ago,
575 ui::PAGE_TRANSITION_TYPED);
576 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
577 web_contents(), url, kTestPackageName, reference_time));
579 // Visit the site again after a long delay, it still should not be shown.
580 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
581 web_contents(), url, kTestPackageName, one_day_ago,
582 ui::PAGE_TRANSITION_TYPED);
583 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
584 web_contents(), url, kTestPackageName, reference_time));
586 // Visit the site again; now it should be shown.
587 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
588 web_contents(), url, kTestPackageName, reference_time,
589 ui::PAGE_TRANSITION_TYPED);
590 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
591 web_contents(), url, kTestPackageName, reference_time));
594 TEST_F(AppBannerSettingsHelperTest, ShouldNotShowAfterBlocking) {
595 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
596 GURL url(kTestURL);
597 NavigateAndCommit(url);
599 base::Time reference_time = GetReferenceTime();
600 base::Time one_day_ago = reference_time - base::TimeDelta::FromDays(1);
601 base::Time two_months_ago = reference_time - base::TimeDelta::FromDays(60);
602 base::Time one_year_ago = reference_time - base::TimeDelta::FromDays(366);
604 // By default the banner should not be shown.
605 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
606 web_contents(), url, kTestPackageName, reference_time));
608 // Record events such that the banner should show.
609 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
610 web_contents(), url, kTestPackageName, one_day_ago,
611 ui::PAGE_TRANSITION_TYPED);
612 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
613 web_contents(), url, kTestPackageName, reference_time,
614 ui::PAGE_TRANSITION_TYPED);
615 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
616 web_contents(), url, kTestPackageName, reference_time));
618 // Block the site a long time ago. It should still be shown.
619 AppBannerSettingsHelper::RecordBannerEvent(
620 web_contents(), url, kTestPackageName,
621 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, one_year_ago);
622 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
623 web_contents(), url, kTestPackageName, reference_time));
625 // Block the site more recently. Now it should not be shown.
626 AppBannerSettingsHelper::RecordBannerEvent(
627 web_contents(), url, kTestPackageName,
628 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, two_months_ago);
629 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
630 web_contents(), url, kTestPackageName, reference_time));
633 TEST_F(AppBannerSettingsHelperTest, ShouldNotShowAfterShowing) {
634 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
635 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(1440);
636 GURL url(kTestURL);
637 NavigateAndCommit(url);
639 base::Time reference_time = GetReferenceTime();
640 base::Time one_day_ago = reference_time - base::TimeDelta::FromDays(1);
641 base::Time three_weeks_ago = reference_time - base::TimeDelta::FromDays(21);
642 base::Time one_year_ago = reference_time - base::TimeDelta::FromDays(366);
644 // By default the banner should not be shown.
645 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
646 web_contents(), url, kTestPackageName, reference_time));
648 // Record events such that the banner should show.
649 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
650 web_contents(), url, kTestPackageName, one_day_ago,
651 ui::PAGE_TRANSITION_TYPED);
652 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
653 web_contents(), url, kTestPackageName, reference_time,
654 ui::PAGE_TRANSITION_TYPED);
655 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
656 web_contents(), url, kTestPackageName, reference_time));
658 // Show the banner a long time ago. It should still be shown.
659 AppBannerSettingsHelper::RecordBannerEvent(
660 web_contents(), url, kTestPackageName,
661 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW, one_year_ago);
662 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
663 web_contents(), url, kTestPackageName, reference_time));
665 // Show the site more recently. Now it should not be shown.
666 AppBannerSettingsHelper::RecordBannerEvent(
667 web_contents(), url, kTestPackageName,
668 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW, three_weeks_ago);
669 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
670 web_contents(), url, kTestPackageName, reference_time));
673 TEST_F(AppBannerSettingsHelperTest, ShouldNotShowAfterAdding) {
674 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
675 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(1440);
676 GURL url(kTestURL);
677 NavigateAndCommit(url);
679 base::Time reference_time = GetReferenceTime();
680 base::Time one_day_ago = reference_time - base::TimeDelta::FromDays(1);
681 base::Time one_year_ago = reference_time - base::TimeDelta::FromDays(366);
683 // By default the banner should not be shown.
684 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
685 web_contents(), url, kTestPackageName, reference_time));
687 // Record events such that the banner should show.
688 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
689 web_contents(), url, kTestPackageName, one_day_ago,
690 ui::PAGE_TRANSITION_TYPED);
691 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
692 web_contents(), url, kTestPackageName, reference_time,
693 ui::PAGE_TRANSITION_TYPED);
694 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
695 web_contents(), url, kTestPackageName, reference_time));
697 // Add the site a long time ago. It should not be shown.
698 AppBannerSettingsHelper::RecordBannerEvent(
699 web_contents(), url, kTestPackageName,
700 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
701 one_year_ago);
702 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
703 web_contents(), url, kTestPackageName, reference_time));
706 TEST_F(AppBannerSettingsHelperTest, OperatesOnOrigins) {
707 AppBannerSettingsHelper::SetEngagementWeights(1, 1);
708 GURL url(kTestURL);
709 NavigateAndCommit(url);
711 base::Time reference_time = GetReferenceTime();
712 base::Time one_day_ago = reference_time - base::TimeDelta::FromDays(1);
714 // By default the banner should not be shown.
715 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
716 web_contents(), url, kTestPackageName, reference_time));
718 // Record events such that the banner should show.
719 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
720 web_contents(), url, kTestPackageName, one_day_ago,
721 ui::PAGE_TRANSITION_TYPED);
722 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
723 web_contents(), url, kTestPackageName, reference_time,
724 ui::PAGE_TRANSITION_TYPED);
726 // Navigate to another page on the same origin.
727 url = GURL(kSameOriginTestURL);
728 NavigateAndCommit(url);
730 // The banner should show as settings are per-origin.
731 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
732 web_contents(), url, kTestPackageName, reference_time));