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.
7 #include "chrome/browser/banners/app_banner_settings_helper.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 const char kTestURL
[] = "https://www.google.com";
13 const char kSameOriginTestURL
[] = "https://www.google.com/foo.html";
14 const char kTestPackageName
[] = "test.package";
16 base::Time
GetReferenceTime() {
17 base::Time::Exploded exploded_reference_time
;
18 exploded_reference_time
.year
= 2015;
19 exploded_reference_time
.month
= 1;
20 exploded_reference_time
.day_of_month
= 30;
21 exploded_reference_time
.day_of_week
= 5;
22 exploded_reference_time
.hour
= 11;
23 exploded_reference_time
.minute
= 0;
24 exploded_reference_time
.second
= 0;
25 exploded_reference_time
.millisecond
= 0;
27 return base::Time::FromLocalExploded(exploded_reference_time
);
30 bool IsWithinDay(base::Time time1
, base::Time time2
) {
31 return time1
- time2
< base::TimeDelta::FromDays(1) ||
32 time2
- time1
< base::TimeDelta::FromDays(1);
35 class AppBannerSettingsHelperTest
: public ChromeRenderViewHostTestHarness
{};
39 TEST_F(AppBannerSettingsHelperTest
, CouldShowEvents
) {
41 NavigateAndCommit(url
);
43 // Check that by default, there are no events recorded.
44 std::vector
<base::Time
> events
=
45 AppBannerSettingsHelper::GetCouldShowBannerEvents(web_contents(), url
,
47 EXPECT_TRUE(events
.empty());
49 base::Time reference_time
= GetReferenceTime();
50 base::Time same_day
= reference_time
+ base::TimeDelta::FromHours(2);
51 base::Time three_days_prior
= reference_time
- base::TimeDelta::FromDays(3);
52 base::Time previous_fortnight
=
53 reference_time
- base::TimeDelta::FromDays(14);
55 // Test adding the first date.
56 AppBannerSettingsHelper::RecordBannerEvent(
57 web_contents(), url
, kTestPackageName
,
58 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, previous_fortnight
);
60 // It should be the only date recorded.
61 events
= AppBannerSettingsHelper::GetCouldShowBannerEvents(
62 web_contents(), url
, kTestPackageName
);
63 EXPECT_EQ(1u, events
.size());
64 EXPECT_TRUE(IsWithinDay(events
[0], previous_fortnight
));
66 // Now add the next date.
67 AppBannerSettingsHelper::RecordBannerEvent(
68 web_contents(), url
, kTestPackageName
,
69 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, three_days_prior
);
71 // Now there should be two days.
72 events
= AppBannerSettingsHelper::GetCouldShowBannerEvents(
73 web_contents(), url
, kTestPackageName
);
74 EXPECT_EQ(2u, events
.size());
75 EXPECT_TRUE(IsWithinDay(events
[0], previous_fortnight
));
76 EXPECT_TRUE(IsWithinDay(events
[1], three_days_prior
));
78 // Now add the reference date.
79 AppBannerSettingsHelper::RecordBannerEvent(
80 web_contents(), url
, kTestPackageName
,
81 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, reference_time
);
83 // Now there should still be two days, but the first date should have been
85 events
= AppBannerSettingsHelper::GetCouldShowBannerEvents(
86 web_contents(), url
, kTestPackageName
);
87 EXPECT_EQ(2u, events
.size());
88 EXPECT_TRUE(IsWithinDay(events
[0], three_days_prior
));
89 EXPECT_TRUE(IsWithinDay(events
[1], reference_time
));
91 // Now add the the other day on the reference date.
92 AppBannerSettingsHelper::RecordBannerEvent(
93 web_contents(), url
, kTestPackageName
,
94 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, same_day
);
96 // Now there should still be the same two days.
97 events
= AppBannerSettingsHelper::GetCouldShowBannerEvents(
98 web_contents(), url
, kTestPackageName
);
99 EXPECT_EQ(2u, events
.size());
100 EXPECT_TRUE(IsWithinDay(events
[0], three_days_prior
));
101 EXPECT_TRUE(IsWithinDay(events
[1], reference_time
));
104 TEST_F(AppBannerSettingsHelperTest
, SingleEvents
) {
106 NavigateAndCommit(url
);
108 base::Time reference_time
= GetReferenceTime();
109 base::Time other_time
= reference_time
- base::TimeDelta::FromDays(3);
110 for (int event
= AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW
;
111 event
< AppBannerSettingsHelper::APP_BANNER_EVENT_NUM_EVENTS
; ++event
) {
112 // Check that by default, there is no event.
113 base::Time event_time
= AppBannerSettingsHelper::GetSingleBannerEvent(
114 web_contents(), url
, kTestPackageName
,
115 AppBannerSettingsHelper::AppBannerEvent(event
));
116 EXPECT_TRUE(event_time
.is_null());
118 // Check that a time can be recorded.
119 AppBannerSettingsHelper::RecordBannerEvent(
120 web_contents(), url
, kTestPackageName
,
121 AppBannerSettingsHelper::AppBannerEvent(event
), reference_time
);
123 event_time
= AppBannerSettingsHelper::GetSingleBannerEvent(
124 web_contents(), url
, kTestPackageName
,
125 AppBannerSettingsHelper::AppBannerEvent(event
));
126 EXPECT_EQ(reference_time
, event_time
);
128 // Check that another time can be recorded.
129 AppBannerSettingsHelper::RecordBannerEvent(
130 web_contents(), url
, kTestPackageName
,
131 AppBannerSettingsHelper::AppBannerEvent(event
), other_time
);
133 event_time
= AppBannerSettingsHelper::GetSingleBannerEvent(
134 web_contents(), url
, kTestPackageName
,
135 AppBannerSettingsHelper::AppBannerEvent(event
));
136 EXPECT_EQ(other_time
, event_time
);
140 TEST_F(AppBannerSettingsHelperTest
, ShouldShowFromEngagement
) {
142 NavigateAndCommit(url
);
144 base::Time reference_time
= GetReferenceTime();
145 base::Time one_day_ago
= reference_time
- base::TimeDelta::FromDays(1);
146 base::Time one_year_ago
= reference_time
- base::TimeDelta::FromDays(366);
148 // By default the banner should not be shown.
149 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
150 web_contents(), url
, kTestPackageName
, reference_time
));
152 // Visit the site once, it still should not be shown.
153 AppBannerSettingsHelper::RecordBannerEvent(
154 web_contents(), url
, kTestPackageName
,
155 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, one_year_ago
);
156 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
157 web_contents(), url
, kTestPackageName
, reference_time
));
159 // Visit the site again after a long delay, it still should not be shown.
160 AppBannerSettingsHelper::RecordBannerEvent(
161 web_contents(), url
, kTestPackageName
,
162 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, one_day_ago
);
163 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
164 web_contents(), url
, kTestPackageName
, reference_time
));
166 // Visit the site again; now it should be shown.
167 AppBannerSettingsHelper::RecordBannerEvent(
168 web_contents(), url
, kTestPackageName
,
169 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, reference_time
);
170 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
171 web_contents(), url
, kTestPackageName
, reference_time
));
174 TEST_F(AppBannerSettingsHelperTest
, ShouldNotShowAfterBlocking
) {
176 NavigateAndCommit(url
);
178 base::Time reference_time
= GetReferenceTime();
179 base::Time one_day_ago
= reference_time
- base::TimeDelta::FromDays(1);
180 base::Time two_months_ago
= reference_time
- base::TimeDelta::FromDays(60);
181 base::Time one_year_ago
= reference_time
- base::TimeDelta::FromDays(366);
183 // By default the banner should not be shown.
184 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
185 web_contents(), url
, kTestPackageName
, reference_time
));
187 // Record events such that the banner should show.
188 AppBannerSettingsHelper::RecordBannerEvent(
189 web_contents(), url
, kTestPackageName
,
190 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, one_day_ago
);
191 AppBannerSettingsHelper::RecordBannerEvent(
192 web_contents(), url
, kTestPackageName
,
193 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, reference_time
);
194 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
195 web_contents(), url
, kTestPackageName
, reference_time
));
197 // Block the site a long time ago. It should still be shown.
198 AppBannerSettingsHelper::RecordBannerEvent(
199 web_contents(), url
, kTestPackageName
,
200 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK
, one_year_ago
);
201 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
202 web_contents(), url
, kTestPackageName
, reference_time
));
204 // Block the site more recently. Now it should not be shown.
205 AppBannerSettingsHelper::RecordBannerEvent(
206 web_contents(), url
, kTestPackageName
,
207 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK
, two_months_ago
);
208 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
209 web_contents(), url
, kTestPackageName
, reference_time
));
212 TEST_F(AppBannerSettingsHelperTest
, ShouldNotShowAfterShowing
) {
214 NavigateAndCommit(url
);
216 base::Time reference_time
= GetReferenceTime();
217 base::Time one_day_ago
= reference_time
- base::TimeDelta::FromDays(1);
218 base::Time three_weeks_ago
= reference_time
- base::TimeDelta::FromDays(21);
219 base::Time one_year_ago
= reference_time
- base::TimeDelta::FromDays(366);
221 // By default the banner should not be shown.
222 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
223 web_contents(), url
, kTestPackageName
, reference_time
));
225 // Record events such that the banner should show.
226 AppBannerSettingsHelper::RecordBannerEvent(
227 web_contents(), url
, kTestPackageName
,
228 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, one_day_ago
);
229 AppBannerSettingsHelper::RecordBannerEvent(
230 web_contents(), url
, kTestPackageName
,
231 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, reference_time
);
232 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
233 web_contents(), url
, kTestPackageName
, reference_time
));
235 // Show the banner a long time ago. It should still be shown.
236 AppBannerSettingsHelper::RecordBannerEvent(
237 web_contents(), url
, kTestPackageName
,
238 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW
, one_year_ago
);
239 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
240 web_contents(), url
, kTestPackageName
, reference_time
));
242 // Show the site more recently. Now it should not be shown.
243 AppBannerSettingsHelper::RecordBannerEvent(
244 web_contents(), url
, kTestPackageName
,
245 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW
, three_weeks_ago
);
246 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
247 web_contents(), url
, kTestPackageName
, reference_time
));
250 TEST_F(AppBannerSettingsHelperTest
, ShouldNotShowAfterAdding
) {
252 NavigateAndCommit(url
);
254 base::Time reference_time
= GetReferenceTime();
255 base::Time one_day_ago
= reference_time
- base::TimeDelta::FromDays(1);
256 base::Time one_year_ago
= reference_time
- base::TimeDelta::FromDays(366);
258 // By default the banner should not be shown.
259 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
260 web_contents(), url
, kTestPackageName
, reference_time
));
262 // Record events such that the banner should show.
263 AppBannerSettingsHelper::RecordBannerEvent(
264 web_contents(), url
, kTestPackageName
,
265 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, one_day_ago
);
266 AppBannerSettingsHelper::RecordBannerEvent(
267 web_contents(), url
, kTestPackageName
,
268 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, reference_time
);
269 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
270 web_contents(), url
, kTestPackageName
, reference_time
));
272 // Add the site a long time ago. It should not be shown.
273 AppBannerSettingsHelper::RecordBannerEvent(
274 web_contents(), url
, kTestPackageName
,
275 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN
,
277 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
278 web_contents(), url
, kTestPackageName
, reference_time
));
281 TEST_F(AppBannerSettingsHelperTest
, OperatesOnOrigins
) {
283 NavigateAndCommit(url
);
285 base::Time reference_time
= GetReferenceTime();
286 base::Time one_day_ago
= reference_time
- base::TimeDelta::FromDays(1);
288 // By default the banner should not be shown.
289 EXPECT_FALSE(AppBannerSettingsHelper::ShouldShowBanner(
290 web_contents(), url
, kTestPackageName
, reference_time
));
292 // Record events such that the banner should show.
293 AppBannerSettingsHelper::RecordBannerEvent(
294 web_contents(), url
, kTestPackageName
,
295 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, one_day_ago
);
296 AppBannerSettingsHelper::RecordBannerEvent(
297 web_contents(), url
, kTestPackageName
,
298 AppBannerSettingsHelper::APP_BANNER_EVENT_COULD_SHOW
, reference_time
);
300 // Navigate to another page on the same origin.
301 url
= GURL(kSameOriginTestURL
);
302 NavigateAndCommit(url
);
304 // The banner should show as settings are per-origin.
305 EXPECT_TRUE(AppBannerSettingsHelper::ShouldShowBanner(
306 web_contents(), url
, kTestPackageName
, reference_time
));