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 <vector> |
| 6 |
| 7 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 const char kTestURL[] = "http://www.google.com"; |
| 13 const char kTestPackageName[] = "test.package"; |
| 14 |
| 15 bool IsWithinDay(base::Time time1, base::Time time2) { |
| 16 return time1 - time2 < base::TimeDelta::FromDays(1) || |
| 17 time2 - time1 < base::TimeDelta::FromDays(1); |
| 18 } |
| 19 |
| 20 class AppBannerSettingsHelperTest : public ChromeRenderViewHostTestHarness {}; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 TEST_F(AppBannerSettingsHelperTest, Block) { |
| 25 GURL url(kTestURL); |
| 26 NavigateAndCommit(url); |
| 27 |
| 28 // Check that by default, showing the banner is allowed. |
| 29 EXPECT_TRUE(AppBannerSettingsHelper::IsAllowed(web_contents(), url, |
| 30 kTestPackageName)); |
| 31 |
| 32 // Block the banner and test it is no longer allowed. |
| 33 AppBannerSettingsHelper::Block(web_contents(), url, kTestPackageName); |
| 34 EXPECT_FALSE(AppBannerSettingsHelper::IsAllowed(web_contents(), url, |
| 35 kTestPackageName)); |
| 36 } |
| 37 |
| 38 TEST_F(AppBannerSettingsHelperTest, CouldShowEvents) { |
| 39 GURL url(kTestURL); |
| 40 NavigateAndCommit(url); |
| 41 |
| 42 // Check that by default, there are no events recorded. |
| 43 std::vector<base::Time> events = |
| 44 AppBannerSettingsHelper::GetCouldShowBannerEvents(web_contents(), url, |
| 45 kTestPackageName); |
| 46 EXPECT_TRUE(events.empty()); |
| 47 |
| 48 base::Time::Exploded exploded_reference_time; |
| 49 exploded_reference_time.year = 2015; |
| 50 exploded_reference_time.month = 1; |
| 51 exploded_reference_time.day_of_month = 30; |
| 52 exploded_reference_time.day_of_week = 5; |
| 53 exploded_reference_time.hour = 11; |
| 54 exploded_reference_time.minute = 0; |
| 55 exploded_reference_time.second = 0; |
| 56 exploded_reference_time.millisecond = 0; |
| 57 |
| 58 base::Time reference_time = |
| 59 base::Time::FromLocalExploded(exploded_reference_time); |
| 60 base::Time same_day = reference_time + base::TimeDelta::FromHours(2); |
| 61 base::Time three_days_prior = reference_time - base::TimeDelta::FromDays(3); |
| 62 base::Time previous_fortnight = |
| 63 reference_time - base::TimeDelta::FromDays(14); |
| 64 |
| 65 // Test adding the first date. |
| 66 AppBannerSettingsHelper::RecordCouldShowBannerEvent( |
| 67 web_contents(), url, kTestPackageName, previous_fortnight); |
| 68 |
| 69 // It should be the only date recorded. |
| 70 events = AppBannerSettingsHelper::GetCouldShowBannerEvents( |
| 71 web_contents(), url, kTestPackageName); |
| 72 EXPECT_EQ(1u, events.size()); |
| 73 EXPECT_TRUE(IsWithinDay(events[0], previous_fortnight)); |
| 74 |
| 75 // Now add the next date. |
| 76 AppBannerSettingsHelper::RecordCouldShowBannerEvent( |
| 77 web_contents(), url, kTestPackageName, three_days_prior); |
| 78 |
| 79 // Now there should be two days. |
| 80 events = AppBannerSettingsHelper::GetCouldShowBannerEvents( |
| 81 web_contents(), url, kTestPackageName); |
| 82 EXPECT_EQ(2u, events.size()); |
| 83 EXPECT_TRUE(IsWithinDay(events[0], previous_fortnight)); |
| 84 EXPECT_TRUE(IsWithinDay(events[1], three_days_prior)); |
| 85 |
| 86 // Now add the reference date. |
| 87 AppBannerSettingsHelper::RecordCouldShowBannerEvent( |
| 88 web_contents(), url, kTestPackageName, reference_time); |
| 89 |
| 90 // Now there should still be two days, but the first date should have been |
| 91 // removed. |
| 92 events = AppBannerSettingsHelper::GetCouldShowBannerEvents( |
| 93 web_contents(), url, kTestPackageName); |
| 94 EXPECT_EQ(2u, events.size()); |
| 95 EXPECT_TRUE(IsWithinDay(events[0], three_days_prior)); |
| 96 EXPECT_TRUE(IsWithinDay(events[1], reference_time)); |
| 97 |
| 98 // Now add the the other day on the reference date. |
| 99 AppBannerSettingsHelper::RecordCouldShowBannerEvent( |
| 100 web_contents(), url, kTestPackageName, same_day); |
| 101 |
| 102 // Now there should still be the same two days. |
| 103 events = AppBannerSettingsHelper::GetCouldShowBannerEvents( |
| 104 web_contents(), url, kTestPackageName); |
| 105 EXPECT_EQ(2u, events.size()); |
| 106 EXPECT_TRUE(IsWithinDay(events[0], three_days_prior)); |
| 107 EXPECT_TRUE(IsWithinDay(events[1], reference_time)); |
| 108 } |
OLD | NEW |