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 "components/variations/variations_request_scheduler_mobile.h"
6 
7 #include "base/bind.h"
8 #include "base/test/task_environment.h"
9 #include "components/prefs/pref_registry_simple.h"
10 #include "components/prefs/testing_pref_service.h"
11 #include "components/variations/pref_names.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace variations {
15 
16 namespace {
17 
18 // Simple method used to verify a Callback has been triggered.
Increment(int * n)19 void Increment(int* n) {
20   (*n)++;
21 }
22 
23 }  // namespace
24 
TEST(VariationsRequestSchedulerMobileTest,StartNoRun)25 TEST(VariationsRequestSchedulerMobileTest, StartNoRun) {
26   TestingPrefServiceSimple prefs;
27   // Initialize to as if it was just fetched. This means it should not run.
28   prefs.registry()->RegisterTimePref(prefs::kVariationsLastFetchTime,
29                                      base::Time::Now());
30   int executed = 0;
31   const base::RepeatingClosure task =
32       base::BindRepeating(&Increment, &executed);
33   VariationsRequestSchedulerMobile scheduler(task, &prefs);
34   scheduler.Start();
35   // We expect it the task to not have triggered.
36   EXPECT_EQ(0, executed);
37 }
38 
TEST(VariationsRequestSchedulerMobileTest,StartRun)39 TEST(VariationsRequestSchedulerMobileTest, StartRun) {
40   TestingPrefServiceSimple prefs;
41   // Verify it doesn't take more than a day.
42   base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24);
43   prefs.registry()->RegisterTimePref(prefs::kVariationsLastFetchTime, old);
44   int executed = 0;
45   const base::RepeatingClosure task =
46       base::BindRepeating(&Increment, &executed);
47   VariationsRequestSchedulerMobile scheduler(task, &prefs);
48   scheduler.Start();
49   // We expect the task to have triggered.
50   EXPECT_EQ(1, executed);
51 }
52 
TEST(VariationsRequestSchedulerMobileTest,OnAppEnterForegroundNoRun)53 TEST(VariationsRequestSchedulerMobileTest, OnAppEnterForegroundNoRun) {
54   base::test::SingleThreadTaskEnvironment task_environment;
55 
56   TestingPrefServiceSimple prefs;
57 
58   // Initialize to as if it was just fetched. This means it should not run.
59   prefs.registry()->RegisterTimePref(prefs::kVariationsLastFetchTime,
60                                      base::Time::Now());
61   int executed = 0;
62   const base::RepeatingClosure task =
63       base::BindRepeating(&Increment, &executed);
64   VariationsRequestSchedulerMobile scheduler(task, &prefs);
65 
66   // Verify timer not running.
67   EXPECT_FALSE(scheduler.schedule_fetch_timer_.IsRunning());
68   scheduler.OnAppEnterForeground();
69 
70   // Timer now running.
71   EXPECT_TRUE(scheduler.schedule_fetch_timer_.IsRunning());
72 
73   // Force execution of the task on this timer to verify that the correct task
74   // was added to the timer.
75   scheduler.schedule_fetch_timer_.FireNow();
76 
77   // The task should not execute because the seed was fetched too recently.
78   EXPECT_EQ(0, executed);
79 }
80 
TEST(VariationsRequestSchedulerMobileTest,OnAppEnterForegroundRun)81 TEST(VariationsRequestSchedulerMobileTest, OnAppEnterForegroundRun) {
82   base::test::SingleThreadTaskEnvironment task_environment;
83 
84   TestingPrefServiceSimple prefs;
85 
86   base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24);
87   prefs.registry()->RegisterTimePref(prefs::kVariationsLastFetchTime, old);
88   int executed = 0;
89   const base::RepeatingClosure task =
90       base::BindRepeating(&Increment, &executed);
91   VariationsRequestSchedulerMobile scheduler(task, &prefs);
92 
93   // Verify timer not running.
94   EXPECT_FALSE(scheduler.schedule_fetch_timer_.IsRunning());
95   scheduler.OnAppEnterForeground();
96 
97   // Timer now running.
98   EXPECT_TRUE(scheduler.schedule_fetch_timer_.IsRunning());
99 
100   // Force execution of the task on this timer to verify that the correct task
101   // was added to the timer - this will verify that the right task is running.
102   scheduler.schedule_fetch_timer_.FireNow();
103 
104   // We expect the input task to have triggered.
105   EXPECT_EQ(1, executed);
106 }
107 
TEST(VariationsRequestSchedulerMobileTest,OnAppEnterForegroundOnStartup)108 TEST(VariationsRequestSchedulerMobileTest, OnAppEnterForegroundOnStartup) {
109   base::test::SingleThreadTaskEnvironment task_environment;
110 
111   TestingPrefServiceSimple prefs;
112 
113   base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24);
114   prefs.registry()->RegisterTimePref(prefs::kVariationsLastFetchTime, old);
115   int executed = 0;
116   const base::RepeatingClosure task =
117       base::BindRepeating(&Increment, &executed);
118   VariationsRequestSchedulerMobile scheduler(task, &prefs);
119 
120   scheduler.Start();
121 
122   // We expect the task to have triggered.
123   EXPECT_EQ(1, executed);
124 
125   scheduler.OnAppEnterForeground();
126 
127   EXPECT_FALSE(scheduler.schedule_fetch_timer_.IsRunning());
128   // We expect the input task to not have triggered again, so executed stays
129   // at 1.
130   EXPECT_EQ(1, executed);
131 
132   // Simulate letting time pass.
133   const base::Time last_fetch_time =
134       prefs.GetTime(prefs::kVariationsLastFetchTime);
135   const base::Time one_day_earlier =
136       last_fetch_time - base::TimeDelta::FromHours(24);
137   prefs.SetTime(prefs::kVariationsLastFetchTime, one_day_earlier);
138   scheduler.last_request_time_ -= base::TimeDelta::FromHours(24);
139 
140   scheduler.OnAppEnterForeground();
141   EXPECT_TRUE(scheduler.schedule_fetch_timer_.IsRunning());
142   scheduler.schedule_fetch_timer_.FireNow();
143   // This time it should execute the task.
144   EXPECT_EQ(2, executed);
145 }
146 
147 }  // namespace variations
148