OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/metrics/variations/variations_request_scheduler_mobile.
h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/prefs/pref_registry_simple.h" |
| 10 #include "base/prefs/testing_pref_service.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace chrome_variations { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Simple method used to verify a Callback has been triggered. |
| 19 void Increment(int *n) { |
| 20 (*n)++; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 TEST(VariationsRequestSchedulerMobileTest, StartNoRun) { |
| 26 TestingPrefServiceSimple prefs; |
| 27 // Initialize to as if it was just fetched. |
| 28 prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime, |
| 29 base::Time::Now().ToInternalValue()); |
| 30 int executed = 0; |
| 31 const base::Closure task = base::Bind(&Increment, &executed); |
| 32 VariationsRequestSchedulerMobile scheduler(task, &prefs); |
| 33 scheduler.Start(); |
| 34 // We expect it the task to not have triggered. |
| 35 EXPECT_EQ(0, executed); |
| 36 } |
| 37 |
| 38 TEST(VariationsRequestSchedulerMobileTest, StartRun) { |
| 39 TestingPrefServiceSimple prefs; |
| 40 // Verify it doesn't take more than a day. |
| 41 base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24); |
| 42 prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime, |
| 43 old.ToInternalValue()); |
| 44 int executed = 0; |
| 45 const base::Closure task = base::Bind(&Increment, &executed); |
| 46 VariationsRequestSchedulerMobile scheduler(task, &prefs); |
| 47 scheduler.Start(); |
| 48 // We expect the task to have triggered. |
| 49 EXPECT_EQ(1, executed); |
| 50 } |
| 51 |
| 52 TEST(VariationsRequestSchedulerMobileTest, ScheduleFetch) { |
| 53 base::MessageLoopForUI message_loop_; |
| 54 |
| 55 TestingPrefServiceSimple prefs; |
| 56 |
| 57 base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24); |
| 58 prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime, |
| 59 old.ToInternalValue()); |
| 60 int executed = 0; |
| 61 const base::Closure task = base::Bind(&Increment, &executed); |
| 62 VariationsRequestSchedulerMobile scheduler(task, &prefs); |
| 63 |
| 64 // Verify timer not running. |
| 65 EXPECT_FALSE(scheduler.schedule_fetch_timer_.IsRunning()); |
| 66 scheduler.ScheduleFetch(); |
| 67 |
| 68 // Timer now running. |
| 69 EXPECT_TRUE(scheduler.schedule_fetch_timer_.IsRunning()); |
| 70 |
| 71 // Force execution of the task on this timer. |
| 72 scheduler.schedule_fetch_timer_.user_task().Run(); |
| 73 |
| 74 // We expect the input task to have triggered. |
| 75 EXPECT_EQ(1, executed); |
| 76 } |
| 77 |
| 78 } // namespace chrome_variations |
OLD | NEW |