Make sure waiting period in memory and persisted file are always in sync.

Sometimes the waiting period is 0 and there's no wall-clock-wait-period
prefs file created even though scattering is enabled. It happens because
the existing code doesn't maintain the invariants properly. So when a
user-initiated update check happens, the wall-clock-wait-period prefs
file is deleted but the in-memory variable
omaha_request_params_.waiting_period is not being updated.

This CL fixes these invariants by ensuring all the scattering artifacts
are removed completely when scattering is disabled and they're properly
recomputed when scattering is enabled.

BUG=chromium-os:32924
TEST=Updated unit tests, tested on ZGB.
Change-Id: Iabd2fd744f8c1a5099c00cf4d1f952757ec3e634
Reviewed-on: https://gerrit.chromium.org/gerrit/28348
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Ready: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/update_attempter_unittest.cc b/update_attempter_unittest.cc
index 0173dea..c28c8ac 100644
--- a/update_attempter_unittest.cc
+++ b/update_attempter_unittest.cc
@@ -546,6 +546,8 @@
   attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
 
   EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
+  EXPECT_CALL(mock_system_state_, GetDevicePolicy()).WillRepeatedly(
+      Return(device_policy));
 
   EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
       .WillRepeatedly(DoAll(
@@ -591,6 +593,8 @@
   attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
 
   EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
+  EXPECT_CALL(mock_system_state_, GetDevicePolicy()).WillRepeatedly(
+      Return(device_policy));
 
   EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
       .WillRepeatedly(DoAll(
@@ -645,6 +649,7 @@
 
   LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
       << "Failed to initialize preferences.";
+  EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
   EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
 
   // make sure scatter_factor is non-zero as scattering is disabled
@@ -655,6 +660,8 @@
   attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
 
   EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
+  EXPECT_CALL(mock_system_state_, GetDevicePolicy()).WillRepeatedly(
+      Return(device_policy));
 
   EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
       .WillRepeatedly(DoAll(
@@ -667,9 +674,10 @@
   EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
 
   // Make sure scattering is disabled for manual (i.e. user initiated) update
-  // checks and none of the artifacts are present.
+  // checks and all artifacts are removed.
   EXPECT_FALSE(attempter_.omaha_request_params_.wall_clock_based_wait_enabled);
   EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
+  EXPECT_TRUE(attempter_.omaha_request_params_.waiting_period.InSeconds() == 0);
   EXPECT_FALSE(attempter_.omaha_request_params_.
                  update_check_count_wait_enabled);
   EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));