variations_request_scheduler_mobile_unittest.cc 4.9 KB

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