1 // Copyright (c) 2010 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/instant/promo_counter.h" 6 #include "chrome/test/testing_profile.h" 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 typedef testing::Test PromoCounterTest; 10 11 // Makes sure ShouldShow returns false after the max number of days. 12 TEST_F(PromoCounterTest, MaxTimeElapsed) { 13 TestingProfile profile; 14 PromoCounter::RegisterUserPrefs(profile.GetPrefs(), "test"); 15 16 base::Time test_time(base::Time::Now()); 17 PromoCounter counter(&profile, "test", "test", 2, 2); 18 ASSERT_TRUE(counter.ShouldShow(test_time)); 19 ASSERT_TRUE(counter.ShouldShow(test_time + base::TimeDelta::FromHours(2))); 20 ASSERT_FALSE(counter.ShouldShow(test_time + base::TimeDelta::FromDays(4))); 21 } 22 23 // Makes sure ShouldShow returns false after max number of sessions encountered. 24 TEST_F(PromoCounterTest, MaxSessionsLapsed) { 25 TestingProfile profile; 26 PromoCounter::RegisterUserPrefs(profile.GetPrefs(), "test"); 27 28 base::Time test_time(base::Time::Now()); 29 { 30 PromoCounter counter(&profile, "test", "test", 2, 2); 31 ASSERT_TRUE(counter.ShouldShow(test_time)); 32 } 33 34 { 35 PromoCounter counter(&profile, "test", "test", 2, 2); 36 ASSERT_TRUE(counter.ShouldShow(test_time)); 37 } 38 39 { 40 PromoCounter counter(&profile, "test", "test", 2, 2); 41 ASSERT_FALSE(counter.ShouldShow(test_time)); 42 } 43 } 44 45 // Makes sure invoking Hide make ShouldShow return false. 46 TEST_F(PromoCounterTest, Hide) { 47 TestingProfile profile; 48 PromoCounter::RegisterUserPrefs(profile.GetPrefs(), "test"); 49 50 base::Time test_time(base::Time::Now()); 51 { 52 PromoCounter counter(&profile, "test", "test", 2, 2); 53 counter.Hide(); 54 ASSERT_FALSE(counter.ShouldShow(test_time)); 55 } 56 57 // Recreate to make sure pref was correctly written. 58 { 59 PromoCounter counter(&profile, "test", "test", 2, 2); 60 ASSERT_FALSE(counter.ShouldShow(test_time)); 61 } 62 } 63 64 // Same as Hide, but invokes ShouldShow first. 65 TEST_F(PromoCounterTest, Hide2) { 66 TestingProfile profile; 67 PromoCounter::RegisterUserPrefs(profile.GetPrefs(), "test"); 68 69 base::Time test_time(base::Time::Now()); 70 { 71 PromoCounter counter(&profile, "test", "test", 2, 2); 72 ASSERT_TRUE(counter.ShouldShow(test_time)); 73 counter.Hide(); 74 ASSERT_FALSE(counter.ShouldShow(test_time)); 75 } 76 77 { 78 PromoCounter counter(&profile, "test", "test", 2, 2); 79 ASSERT_FALSE(counter.ShouldShow(test_time)); 80 } 81 } 82