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