This source file includes following definitions.
- Increment
- TEST
- TEST
- TEST
- TEST
- TEST
#include "chrome/browser/metrics/variations/variations_request_scheduler_mobile.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/testing_pref_service.h"
#include "chrome/common/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_variations {
namespace {
void Increment(int *n) {
(*n)++;
}
}
TEST(VariationsRequestSchedulerMobileTest, StartNoRun) {
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime,
base::Time::Now().ToInternalValue());
int executed = 0;
const base::Closure task = base::Bind(&Increment, &executed);
VariationsRequestSchedulerMobile scheduler(task, &prefs);
scheduler.Start();
EXPECT_EQ(0, executed);
}
TEST(VariationsRequestSchedulerMobileTest, StartRun) {
TestingPrefServiceSimple prefs;
base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24);
prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime,
old.ToInternalValue());
int executed = 0;
const base::Closure task = base::Bind(&Increment, &executed);
VariationsRequestSchedulerMobile scheduler(task, &prefs);
scheduler.Start();
EXPECT_EQ(1, executed);
}
TEST(VariationsRequestSchedulerMobileTest, OnAppEnterForegroundNoRun) {
base::MessageLoopForUI message_loop_;
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime,
base::Time::Now().ToInternalValue());
int executed = 0;
const base::Closure task = base::Bind(&Increment, &executed);
VariationsRequestSchedulerMobile scheduler(task, &prefs);
EXPECT_FALSE(scheduler.schedule_fetch_timer_.IsRunning());
scheduler.OnAppEnterForeground();
EXPECT_TRUE(scheduler.schedule_fetch_timer_.IsRunning());
scheduler.schedule_fetch_timer_.user_task().Run();
EXPECT_EQ(0, executed);
}
TEST(VariationsRequestSchedulerMobileTest, OnAppEnterForegroundRun) {
base::MessageLoopForUI message_loop_;
TestingPrefServiceSimple prefs;
base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24);
prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime,
old.ToInternalValue());
int executed = 0;
const base::Closure task = base::Bind(&Increment, &executed);
VariationsRequestSchedulerMobile scheduler(task, &prefs);
EXPECT_FALSE(scheduler.schedule_fetch_timer_.IsRunning());
scheduler.OnAppEnterForeground();
EXPECT_TRUE(scheduler.schedule_fetch_timer_.IsRunning());
scheduler.schedule_fetch_timer_.user_task().Run();
EXPECT_EQ(1, executed);
}
TEST(VariationsRequestSchedulerMobileTest, OnAppEnterForegroundOnStartup) {
base::MessageLoopForUI message_loop_;
TestingPrefServiceSimple prefs;
base::Time old = base::Time::Now() - base::TimeDelta::FromHours(24);
prefs.registry()->RegisterInt64Pref(prefs::kVariationsLastFetchTime,
old.ToInternalValue());
int executed = 0;
const base::Closure task = base::Bind(&Increment, &executed);
VariationsRequestSchedulerMobile scheduler(task, &prefs);
scheduler.Start();
EXPECT_EQ(1, executed);
scheduler.OnAppEnterForeground();
EXPECT_FALSE(scheduler.schedule_fetch_timer_.IsRunning());
EXPECT_EQ(1, executed);
const base::Time last_fetch_time = base::Time::FromInternalValue(
prefs.GetInt64(prefs::kVariationsLastFetchTime));
prefs.SetInt64(
prefs::kVariationsLastFetchTime,
(last_fetch_time - base::TimeDelta::FromHours(24)).ToInternalValue());
scheduler.last_request_time_ -= base::TimeDelta::FromHours(24);
scheduler.OnAppEnterForeground();
EXPECT_TRUE(scheduler.schedule_fetch_timer_.IsRunning());
scheduler.schedule_fetch_timer_.user_task().Run();
EXPECT_EQ(2, executed);
}
}