1a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// found in the LICENSE file.
4a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
5a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include <string>
6a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
7a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/basictypes.h"
858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/bind.h"
9a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/memory/ref_counted.h"
10a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
1158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/message_loop/message_loop.h"
12a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/metrics/histogram.h"
13a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/metrics/histogram_samples.h"
14a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/metrics/sample_map.h"
15a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/metrics/statistics_recorder.h"
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/run_loop.h"
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "base/test/simple_test_clock.h"
18a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/test/test_simple_task_runner.h"
19a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/time/time.h"
20a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/values.h"
21a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/browser/invalidation/fake_invalidation_service.h"
22a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/policy/cloud/user_cloud_policy_invalidator.h"
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation_util.h"
25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/cloud_policy_constants.h"
26a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/cloud_policy_core.h"
27a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
28a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/enterprise_metrics.h"
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_types.h"
32a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "policy/policy_constants.h"
33a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
34a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
35a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace em = enterprise_management;
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
38a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)namespace policy {
39a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class CloudPolicyInvalidatorTest : public testing::Test {
41a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) protected:
42a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Policy objects which can be used in tests.
43a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  enum PolicyObject {
44a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    POLICY_OBJECT_NONE,
45a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    POLICY_OBJECT_A,
46a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    POLICY_OBJECT_B
47a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  };
48a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
49a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  CloudPolicyInvalidatorTest();
50a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
51a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  virtual void TearDown() OVERRIDE;
52a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
53a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Starts the invalidator which will be tested.
5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // |initialize| determines if the invalidator should be initialized.
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // |start_refresh_scheduler| determines if the refresh scheduler should start.
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // |highest_handled_invalidation_version| is the highest invalidation version
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // that was handled already before this invalidator was created.
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void StartInvalidator(bool initialize,
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                        bool start_refresh_scheduler,
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                        int64 highest_handled_invalidation_version);
61a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void StartInvalidator() {
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    StartInvalidator(true, /* initialize */
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                     true, /* start_refresh_scheduler */
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                     0     /* highest_handled_invalidation_version */);
65a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
66a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Calls Initialize on the invalidator.
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void InitializeInvalidator();
6958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Calls Shutdown on the invalidator. Test must call DestroyInvalidator
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // afterwards to prevent Shutdown from being called twice.
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void ShutdownInvalidator();
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Destroys the invalidator.
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void DestroyInvalidator();
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Connects the cloud policy core.
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void ConnectCore();
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Starts the refresh scheduler.
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void StartRefreshScheduler();
8258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Disconnects the cloud policy core.
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void DisconnectCore();
8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
86a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Simulates storing a new policy to the policy store.
87a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // |object| determines which policy object the store will report the
88a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // invalidator should register for. May be POLICY_OBJECT_NONE for no object.
89a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // |invalidation_version| determines what invalidation the store will report.
9058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // |policy_changed| determines whether a policy value different from the
9158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // current value will be stored.
92c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |time| determines the timestamp the store will report.
93a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void StorePolicy(
94a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      PolicyObject object,
95a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      int64 invalidation_version,
96a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      bool policy_changed,
97c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      const base::Time& time);
98a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void StorePolicy(
99a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      PolicyObject object,
100a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      int64 invalidation_version,
101a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      bool policy_changed) {
102c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    StorePolicy(object,
103c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                invalidation_version,
104c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                policy_changed,
105c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                Now() - base::TimeDelta::FromMinutes(5));
106a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
107a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void StorePolicy(PolicyObject object, int64 invalidation_version) {
108a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    StorePolicy(object, invalidation_version, false);
109a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
110a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void StorePolicy(PolicyObject object) {
111a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    StorePolicy(object, 0);
112a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
113a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
114a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Disables the invalidation service. It is enabled by default.
115a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void DisableInvalidationService();
116a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
117a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Enables the invalidation service. It is enabled by default.
118a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void EnableInvalidationService();
119a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Causes the invalidation service to fire an invalidation.
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation FireInvalidation(
122a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      PolicyObject object,
123a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      int64 version,
124a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      const std::string& payload);
125a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
126a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Causes the invalidation service to fire an invalidation with unknown
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // version.
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation FireUnknownVersionInvalidation(PolicyObject object);
129a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
130a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Checks the expected value of the currently set invalidation info.
131a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool CheckInvalidationInfo(int64 version, const std::string& payload);
132a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Checks that the policy was not refreshed due to an invalidation.
13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool CheckPolicyNotRefreshed();
135a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Checks that the policy was refreshed due to an invalidation within an
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // appropriate timeframe depending on whether the invalidation had unknown
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // version.
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool CheckPolicyRefreshed();
14058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool CheckPolicyRefreshedWithUnknownVersion();
141a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool IsUnsent(const syncer::Invalidation& invalidation);
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
14458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Returns the invalidations enabled state set by the invalidator on the
14558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // refresh scheduler.
14658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool InvalidationsEnabled();
147a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
148a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Determines if the invalidation with the given ack handle has been
149a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // acknowledged.
150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation);
151a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
15258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Determines if the invalidator has registered for an object with the
15358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // invalidation service.
15458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool IsInvalidatorRegistered();
15558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Returns the highest invalidation version that was handled already according
1576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // to the |invalidator_|.
1586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  int64 GetHighestHandledInvalidationVersion() const;
159c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
160c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Advance the test clock.
161c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void AdvanceClock(base::TimeDelta delta);
162c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
163c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Get the current time on the test clock.
164c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::Time Now();
165c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
166c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Translate a version number into an appropriate invalidation version (which
167c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // is based on the current time).
168c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  int64 V(int version);
169c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
170c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Get an invalidation version for the given time.
171c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  int64 GetVersion(base::Time time);
172a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Get the policy type that the |invalidator_| is responsible for.
1746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual em::DeviceRegisterRequest::Type GetPolicyType() const;
1756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
176a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) private:
17758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Checks that the policy was refreshed due to an invalidation with the given
17858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // base delay.
17958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool CheckPolicyRefreshed(base::TimeDelta delay);
18058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
181d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Checks that the policy was refreshed the given number of times.
182d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool CheckPolicyRefreshCount(int count);
183d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
184a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Returns the object id of the given policy object.
185a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
186a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
187d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::MessageLoop loop_;
188d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
189a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Objects the invalidator depends on.
190a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  invalidation::FakeInvalidationService invalidation_service_;
191a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  MockCloudPolicyStore store_;
19258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CloudPolicyCore core_;
193d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  MockCloudPolicyClient* client_;
194a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
195c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::SimpleTestClock* clock_;
196a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
197a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // The invalidator which will be tested.
198a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_ptr<CloudPolicyInvalidator> invalidator_;
199a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
200a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Object ids for the test policy objects.
201a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  invalidation::ObjectId object_id_a_;
202a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  invalidation::ObjectId object_id_b_;
203a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
204a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Fake policy values which are alternated to cause the store to report a
205a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // changed policy.
206a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const char* policy_value_a_;
207a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const char* policy_value_b_;
208a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
209a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // The currently used policy value.
210a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const char* policy_value_cur_;
211a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)};
212a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
213a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
21458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                               std::string()),
216d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            &store_,
217d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            loop_.message_loop_proxy()),
218d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      client_(NULL),
21958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      task_runner_(new base::TestSimpleTaskRunner()),
220c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      clock_(new base::SimpleTestClock()),
221a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      object_id_a_(135, "asdf"),
222a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      object_id_b_(246, "zxcv"),
223a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      policy_value_a_("asdf"),
224a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      policy_value_b_("zxcv"),
225c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      policy_value_cur_(policy_value_a_) {
226c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  clock_->SetNow(
227c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(987654321));
228c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
229a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
230a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void CloudPolicyInvalidatorTest::TearDown() {
231a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (invalidator_)
232a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    invalidator_->Shutdown();
23358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  core_.Disconnect();
234a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
235a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
23658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void CloudPolicyInvalidatorTest::StartInvalidator(
23758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    bool initialize,
2386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool start_refresh_scheduler,
2396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    int64 highest_handled_invalidation_version) {
240c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  invalidator_.reset(new CloudPolicyInvalidator(
2416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      GetPolicyType(),
242c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      &core_,
243c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      task_runner_,
2446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_ptr<base::Clock>(clock_),
2456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      highest_handled_invalidation_version));
24658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (start_refresh_scheduler) {
24758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ConnectCore();
24858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    StartRefreshScheduler();
24958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
250a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (initialize)
25158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    InitializeInvalidator();
25258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
25358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
25458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void CloudPolicyInvalidatorTest::InitializeInvalidator() {
25558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  invalidator_->Initialize(&invalidation_service_);
25658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
25758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
25858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void CloudPolicyInvalidatorTest::ShutdownInvalidator() {
25958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  invalidator_->Shutdown();
26058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
26158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
26258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void CloudPolicyInvalidatorTest::DestroyInvalidator() {
26358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  invalidator_.reset();
26458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void CloudPolicyInvalidatorTest::ConnectCore() {
267d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  client_ = new MockCloudPolicyClient();
268d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  client_->SetDMToken("dm");
269d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  core_.Connect(scoped_ptr<CloudPolicyClient>(client_));
27058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
27158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
27258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void CloudPolicyInvalidatorTest::StartRefreshScheduler() {
27358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  core_.StartRefreshScheduler();
27458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
27558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
27658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void CloudPolicyInvalidatorTest::DisconnectCore() {
277d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  client_ = NULL;
27858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  core_.Disconnect();
279a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
280a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
281a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void CloudPolicyInvalidatorTest::StorePolicy(
282a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    PolicyObject object,
283a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    int64 invalidation_version,
284a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    bool policy_changed,
285c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::Time& time) {
2866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  em::PolicyData* data = new em::PolicyData();
287a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (object != POLICY_OBJECT_NONE) {
288a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    data->set_invalidation_source(GetPolicyObjectId(object).source());
289a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    data->set_invalidation_name(GetPolicyObjectId(object).name());
290a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
291c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  data->set_timestamp((time - base::Time::UnixEpoch()).InMilliseconds());
292a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Swap the policy value if a policy change is desired.
293a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (policy_changed)
294a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    policy_value_cur_ = policy_value_cur_ == policy_value_a_ ?
295a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        policy_value_b_ : policy_value_a_;
296a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  data->set_policy_value(policy_value_cur_);
297a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  store_.invalidation_version_ = invalidation_version;
298a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  store_.policy_.reset(data);
299a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::DictionaryValue policies;
300a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policies.SetInteger(
301a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      key::kMaxInvalidationFetchDelay,
302a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      CloudPolicyInvalidator::kMaxFetchDelayMin);
303a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  store_.policy_map_.LoadFrom(
304a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      &policies,
305a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      POLICY_LEVEL_MANDATORY,
306a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      POLICY_SCOPE_MACHINE);
307a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  store_.NotifyStoreLoaded();
308a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
309a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
310a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void CloudPolicyInvalidatorTest::DisableInvalidationService() {
311a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  invalidation_service_.SetInvalidatorState(
312a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      syncer::TRANSIENT_INVALIDATION_ERROR);
313a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
314a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
315a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void CloudPolicyInvalidatorTest::EnableInvalidationService() {
316a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  invalidation_service_.SetInvalidatorState(syncer::INVALIDATIONS_ENABLED);
317a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
318a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
319f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)syncer::Invalidation CloudPolicyInvalidatorTest::FireInvalidation(
320a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    PolicyObject object,
321a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    int64 version,
322a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const std::string& payload) {
3234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  syncer::Invalidation invalidation = syncer::Invalidation::Init(
324a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      GetPolicyObjectId(object),
325a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      version,
326a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      payload);
3274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  invalidation_service_.EmitInvalidationForTest(invalidation);
328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return invalidation;
329a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
330a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
331f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)syncer::Invalidation CloudPolicyInvalidatorTest::FireUnknownVersionInvalidation(
332a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    PolicyObject object) {
333f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation invalidation = syncer::Invalidation::InitUnknownVersion(
334f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPolicyObjectId(object));
3354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  invalidation_service_.EmitInvalidationForTest(invalidation);
336f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return invalidation;
337a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
338a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
339a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)bool CloudPolicyInvalidatorTest::CheckInvalidationInfo(
340a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    int64 version,
341a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const std::string& payload) {
34258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  MockCloudPolicyClient* client =
34358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      static_cast<MockCloudPolicyClient*>(core_.client());
34458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return version == client->invalidation_version_ &&
34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      payload == client->invalidation_payload_;
346a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
347a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
34858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() {
349d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return CheckPolicyRefreshCount(0);
350a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
351a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
35258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed() {
35358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return CheckPolicyRefreshed(base::TimeDelta());
354a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
355a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
356f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool CloudPolicyInvalidatorTest::IsUnsent(
357f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const syncer::Invalidation& invalidation) {
358f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return invalidation_service_.GetMockAckHandler()->IsUnsent(invalidation);
359f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
360f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
36158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool CloudPolicyInvalidatorTest::CheckPolicyRefreshedWithUnknownVersion() {
36258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return CheckPolicyRefreshed(base::TimeDelta::FromMinutes(
36358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        CloudPolicyInvalidator::kMissingPayloadDelay));
364a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
365a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
36658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool CloudPolicyInvalidatorTest::InvalidationsEnabled() {
36758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return core_.refresh_scheduler()->invalidations_available();
368a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
369a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
370a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged(
371f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const syncer::Invalidation& invalidation) {
372f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The acknowledgement task is run through a WeakHandle that posts back to our
373f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // own thread.  We need to run any posted tasks before we can check
374f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // acknowledgement status.
375f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  loop_.RunUntilIdle();
376f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
377f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsUnsent(invalidation));
378f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return !invalidation_service_.GetMockAckHandler()->IsUnacked(invalidation);
379a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
380a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
38158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() {
38258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return !invalidation_service_.invalidator_registrar()
38358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      .GetRegisteredIds(invalidator_.get()).empty();
38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
38558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)int64 CloudPolicyInvalidatorTest::GetHighestHandledInvalidationVersion() const {
3876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return invalidator_->highest_handled_invalidation_version();
388c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
389c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
390c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) {
391c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  clock_->Advance(delta);
392c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
393c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
394c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochbase::Time CloudPolicyInvalidatorTest::Now() {
395c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return clock_->Now();
396c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
397c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
398c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochint64 CloudPolicyInvalidatorTest::V(int version) {
399c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return GetVersion(Now()) + version;
400c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
401c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
402c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochint64 CloudPolicyInvalidatorTest::GetVersion(base::Time time) {
403c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return (time - base::Time::UnixEpoch()).InMicroseconds();
404a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
405a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
4066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)em::DeviceRegisterRequest::Type
4076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)CloudPolicyInvalidatorTest::GetPolicyType() const {
4086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return UserCloudPolicyInvalidator::GetPolicyType();
4096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
4106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
41158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) {
41258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds(
41358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      CloudPolicyInvalidator::kMaxFetchDelayMin);
414a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
41558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (task_runner_->GetPendingTasks().empty())
41658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return false;
41758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
41858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_GE(actual_delay, delay);
41958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_LE(actual_delay, max_delay);
420a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
421d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return CheckPolicyRefreshCount(1);
422d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
423d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
424d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)bool CloudPolicyInvalidatorTest::CheckPolicyRefreshCount(int count) {
425d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (!client_) {
426d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    task_runner_->RunUntilIdle();
427d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return count == 0;
428d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
429d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
430d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Clear any non-invalidation refreshes which may be pending.
431d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_CALL(*client_, FetchPolicy()).Times(testing::AnyNumber());
432d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
433d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  testing::Mock::VerifyAndClearExpectations(client_);
434d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
435d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Run the invalidator tasks then check for invalidation refreshes.
436d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_CALL(*client_, FetchPolicy()).Times(count);
43758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  task_runner_->RunUntilIdle();
438d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
439d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return testing::Mock::VerifyAndClearExpectations(client_);
440a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
441a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
442a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId(
443a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    PolicyObject object) const {
444a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B);
445a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_;
446a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
447a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
448a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, Uninitialized) {
44958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // No invalidations should be processed if the invalidator is not initialized.
4506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StartInvalidator(false, /* initialize */
4516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   true,  /* start_refresh_scheduler */
4526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   0      /* highest_handled_invalidation_version*/);
45358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
45458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(IsInvalidatorRegistered());
455f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
45658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
4576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
45858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
45958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
46058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) {
46158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // No invalidations should be processed if the refresh scheduler is not
46258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // started.
4636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StartInvalidator(true,  /* initialize */
4646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   false, /* start_refresh_scheduler */
4656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   0      /* highest_handled_invalidation_version*/);
466a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
46758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(IsInvalidatorRegistered());
468f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
46958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
4706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
47158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
47258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
47358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, DisconnectCoreThenInitialize) {
47458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // No invalidations should be processed if the core is disconnected before
47558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // initialization.
4766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StartInvalidator(false, /* initialize */
4776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   true,  /* start_refresh_scheduler */
4786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   0      /* highest_handled_invalidation_version*/);
47958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DisconnectCore();
48058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  InitializeInvalidator();
48158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
48258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(IsInvalidatorRegistered());
483f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
48458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
4856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
48658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
48758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
48858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, InitializeThenStartRefreshScheduler) {
48958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Make sure registration occurs and invalidations are processed when
49058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Initialize is called before starting the refresh scheduler.
49158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Note that the reverse case (start refresh scheduler then initialize) is
49258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // the default behavior for the test fixture, so will be tested in most other
49358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // tests.
4946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StartInvalidator(true,  /* initialize */
4956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   false, /* start_refresh_scheduler */
4966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   0      /* highest_handled_invalidation_version*/);
49758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ConnectCore();
49858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StartRefreshScheduler();
49958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
50058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(IsInvalidatorRegistered());
5014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FireUnknownVersionInvalidation(POLICY_OBJECT_A);
50258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
5036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
504a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
505a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
506a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) {
507a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // No registration when store is not loaded.
508a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
50958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(IsInvalidatorRegistered());
51058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
511f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
512f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
51358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
514a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
515a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // No registration when store is loaded with no invalidation object id.
516a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_NONE);
51758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(IsInvalidatorRegistered());
51858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
519f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
520f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
52158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
522a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
523a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Check registration when store is loaded for object A.
524a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
52558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(IsInvalidatorRegistered());
52658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
5274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FireUnknownVersionInvalidation(POLICY_OBJECT_A);
52858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
529f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
53058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
5316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
532a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
533a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
534a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) {
535a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Register for object A.
536a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
537a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
53858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(IsInvalidatorRegistered());
53958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
5404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FireUnknownVersionInvalidation(POLICY_OBJECT_A);
54158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
542f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
54358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
544f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
545a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
546a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Check re-registration for object B. Make sure the pending invalidation for
547a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // object A is acknowledged without making the callback.
548a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_B);
54958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(IsInvalidatorRegistered());
55058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
551f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv));
55258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
553a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
554a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure future invalidations for object A are ignored and for object B
555a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // are processed.
556f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
55758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
5584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FireUnknownVersionInvalidation(POLICY_OBJECT_B);
55958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
5606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
561a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
562a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
563a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) {
564a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Register for object A.
565a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
566a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
56758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(IsInvalidatorRegistered());
56858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
5694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FireUnknownVersionInvalidation(POLICY_OBJECT_A);
57058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
571a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
572a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Check unregistration when store is loaded with no invalidation object id.
573f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
574f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsInvalidationAcknowledged(inv));
575a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_NONE);
57658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(IsInvalidatorRegistered());
577f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv));
57858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
579f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
580f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
58158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
582a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
583a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Check re-registration for object B.
584a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_B);
58558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(IsInvalidatorRegistered());
58658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
5874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FireUnknownVersionInvalidation(POLICY_OBJECT_B);
58858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
5896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
590a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
591a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
592a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) {
593a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Register and fire invalidation
594a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
595a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
59658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
597f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation inv =
598c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      FireInvalidation(POLICY_OBJECT_A, V(12), "test_payload");
599a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
600a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure client info is set as soon as the invalidation is received.
601c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(CheckInvalidationInfo(V(12), "test_payload"));
60258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshed());
603a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
604a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure invalidation is not acknowledged until the store is loaded.
605f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsInvalidationAcknowledged(inv));
6066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
607c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(CheckInvalidationInfo(V(12), "test_payload"));
608c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  StorePolicy(POLICY_OBJECT_A, V(12));
609f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv));
610a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
6116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(12), GetHighestHandledInvalidationVersion());
612a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
613a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
614a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) {
615a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Register and fire invalidation with unknown version.
616a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
617a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
618f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
619a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
620a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure client info is not set until after the invalidation callback is
621a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // made.
622a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
62358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
624a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
625a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
626a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure invalidation is not acknowledged until the store is loaded.
627f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsInvalidationAcknowledged(inv));
628a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, -1);
629f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv));
630a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
6316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
632a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
633a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
634a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) {
635a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Generate multiple invalidations.
636a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
637a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
638c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  syncer::Invalidation inv1 = FireInvalidation(POLICY_OBJECT_A, V(1), "test1");
639c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(CheckInvalidationInfo(V(1), "test1"));
640c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  syncer::Invalidation inv2 = FireInvalidation(POLICY_OBJECT_A, V(2), "test2");
641c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(CheckInvalidationInfo(V(2), "test2"));
642c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  syncer::Invalidation inv3 = FireInvalidation(POLICY_OBJECT_A, V(3), "test3");
643c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(CheckInvalidationInfo(V(3), "test3"));
644a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
645a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure the replaced invalidations are acknowledged.
646f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
647f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
648a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
64958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Make sure the policy is refreshed once.
65058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshed());
651a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
652a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure that the last invalidation is only acknowledged after the store
653a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // is loaded with the latest version.
6546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
655c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  StorePolicy(POLICY_OBJECT_A, V(1));
656f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
6576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(1), GetHighestHandledInvalidationVersion());
658c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  StorePolicy(POLICY_OBJECT_A, V(2));
659f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
6606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
661c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  StorePolicy(POLICY_OBJECT_A, V(3));
662f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
6636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(3), GetHighestHandledInvalidationVersion());
664a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
665a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
666a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest,
667a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)       HandleMultipleInvalidationsWithUnknownVersion) {
668a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Validate that multiple invalidations with unknown version each generate
669a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // unique invalidation version numbers.
670a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
671a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
672f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation inv1 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
673a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
67458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
675a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
676f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation inv2 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
677a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
67858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
679a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(-2, std::string()));
680f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation inv3 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
681a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
68258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
683a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(-3, std::string()));
684a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
685a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure the replaced invalidations are acknowledged.
686f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
687f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
688a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
689a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make sure that the last invalidation is only acknowledged after the store
690a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // is loaded with the last unknown version.
691a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, -1);
692f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
693a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, -2);
694f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
695a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, -3);
696f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
6976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
6986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
6996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest,
7016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)       InitialHighestHandledInvalidationVersionNonZero) {
7026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
7036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StartInvalidator(true, /* initialize */
7046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   true, /* start_refresh_scheduler */
7056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   V(2)  /* highest_handled_invalidation_version*/);
7066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Check that an invalidation whose version is lower than the highest handled
7086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // so far is acknowledged but ignored otherwise.
7096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  syncer::Invalidation inv1 = FireInvalidation(POLICY_OBJECT_A, V(1), "test1");
7106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
7116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
7126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
7136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
7146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Check that an invalidation with an unknown version is handled.
7166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
7176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
7186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
7196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, -1);
7206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv));
7216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
7226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Check that an invalidation whose version matches the highest handled so far
7246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // is acknowledged but ignored otherwise.
7256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  syncer::Invalidation inv2 = FireInvalidation(POLICY_OBJECT_A, V(2), "test2");
7266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
7276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
7286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
7296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
7306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Check that an invalidation whose version is higher than the highest handled
7326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // so far is handled, causing a policy refresh.
7336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  syncer::Invalidation inv3 = FireInvalidation(POLICY_OBJECT_A, V(3), "test3");
7346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshed());
7356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(CheckInvalidationInfo(V(3), "test3"));
7366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, V(3));
7376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
7386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(3), GetHighestHandledInvalidationVersion());
739a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
740a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
74158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) {
742a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Generate an invalidation.
743a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
744a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
745c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(3), "test");
746a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
74758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Ensure that the policy is not refreshed and the invalidation is
748a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // acknowledged if the store is loaded with the latest version before the
74958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // refresh can occur.
7506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
751c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  StorePolicy(POLICY_OBJECT_A, V(3));
752f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsInvalidationAcknowledged(inv));
75358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
7546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(V(3), GetHighestHandledInvalidationVersion());
75558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
75658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
75758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) {
75858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Generate an invalidation.
75958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
76058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StartInvalidator();
761c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(3), "test");
76258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
76358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Ensure that the policy refresh is not made after the invalidator is shut
76458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // down.
76558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ShutdownInvalidator();
76658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
7676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
76858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DestroyInvalidator();
769a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
770a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
771a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, StateChanged) {
77258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Test invalidation service state changes while not registered.
773a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
774a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DisableInvalidationService();
775a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EnableInvalidationService();
77658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
777a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
77858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Test invalidation service state changes while registered.
779a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
78058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
781a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DisableInvalidationService();
78258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
783a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DisableInvalidationService();
78458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
785a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EnableInvalidationService();
78658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
787a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EnableInvalidationService();
78858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
789a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
79058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Test registration changes with invalidation service enabled.
791a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_NONE);
79258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
793a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_NONE);
79458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
795a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
79658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
797a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
79858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
799a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
80058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Test registration changes with invalidation service disabled.
801a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DisableInvalidationService();
80258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
803a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_NONE);
804a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
80558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
8066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
80758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
80858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
80958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CloudPolicyInvalidatorTest, Disconnect) {
81058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Generate an invalidation.
81158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
81258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StartInvalidator();
813c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(1), "test");
81458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
81558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
81658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Ensure that the policy is not refreshed after disconnecting the core, but
81758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // a call to indicate that invalidations are disabled is made.
81858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DisconnectCore();
81958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
82058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
82158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Ensure that invalidation service events do not cause refreshes while the
82258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // invalidator is stopped.
823c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(2), "test")));
82458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
82558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DisableInvalidationService();
82658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EnableInvalidationService();
82758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
82858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Connect and disconnect without starting the refresh scheduler.
82958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ConnectCore();
830c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(3), "test")));
83158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
83258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DisconnectCore();
833c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(4), "test")));
83458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
83558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
83658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Ensure that the invalidator returns to normal after reconnecting.
83758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ConnectCore();
83858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  StartRefreshScheduler();
83958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyNotRefreshed());
84058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(InvalidationsEnabled());
841c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  FireInvalidation(POLICY_OBJECT_A, V(5), "test");
842c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_TRUE(CheckInvalidationInfo(V(5), "test"));
84358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(CheckPolicyRefreshed());
84458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DisableInvalidationService();
84558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(InvalidationsEnabled());
8466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
847a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
848a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
8496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class CloudPolicyInvalidatorUserTypedTest
8506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    : public CloudPolicyInvalidatorTest,
8516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      public testing::WithParamInterface<em::DeviceRegisterRequest::Type>  {
8526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) protected:
8536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  CloudPolicyInvalidatorUserTypedTest();
8546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual ~CloudPolicyInvalidatorUserTypedTest();
8556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // CloudPolicyInvalidatorTest:
8576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void SetUp() OVERRIDE;
8586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Get the current count for the given metric.
8606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::HistogramBase::Count GetCount(MetricPolicyRefresh metric);
8616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::HistogramBase::Count GetInvalidationCount(PolicyInvalidationType type);
8626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) private:
8646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // CloudPolicyInvalidatorTest:
8656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual em::DeviceRegisterRequest::Type GetPolicyType() const OVERRIDE;
8666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Get histogram samples for the given histogram.
8686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<base::HistogramSamples> GetHistogramSamples(
8696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const std::string& name) const;
8706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Stores starting histogram counts for kMetricPolicyRefresh.
8726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<base::HistogramSamples> refresh_samples_;
8736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Stores starting histogram counts for kMetricPolicyInvalidations.
8756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<base::HistogramSamples> invalidations_samples_;
8766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidatorUserTypedTest);
8786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)};
8796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)CloudPolicyInvalidatorUserTypedTest::CloudPolicyInvalidatorUserTypedTest() {
8816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
8826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)CloudPolicyInvalidatorUserTypedTest::~CloudPolicyInvalidatorUserTypedTest() {
8846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
8856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CloudPolicyInvalidatorUserTypedTest::SetUp() {
8876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::StatisticsRecorder::Initialize();
8886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  refresh_samples_ = GetHistogramSamples(
8896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
8906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          kMetricDevicePolicyRefresh : kMetricUserPolicyRefresh);
8916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  invalidations_samples_ = GetHistogramSamples(
8926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
8936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          kMetricDevicePolicyInvalidations : kMetricUserPolicyInvalidations);
8946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
8956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)base::HistogramBase::Count CloudPolicyInvalidatorUserTypedTest::GetCount(
8976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    MetricPolicyRefresh metric) {
8986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return GetHistogramSamples(
8996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
9006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          kMetricDevicePolicyRefresh : kMetricUserPolicyRefresh)->
9016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)              GetCount(metric) - refresh_samples_->GetCount(metric);
9026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
9036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)base::HistogramBase::Count
9056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)CloudPolicyInvalidatorUserTypedTest::GetInvalidationCount(
9066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    PolicyInvalidationType type) {
9076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return GetHistogramSamples(
9086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
9096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          kMetricDevicePolicyInvalidations : kMetricUserPolicyInvalidations)->
9106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          GetCount(type) - invalidations_samples_->GetCount(type);
9116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
9126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)em::DeviceRegisterRequest::Type
9146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)CloudPolicyInvalidatorUserTypedTest::GetPolicyType() const {
9156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return GetParam();
9166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
9176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)scoped_ptr<base::HistogramSamples>
9196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)CloudPolicyInvalidatorUserTypedTest::GetHistogramSamples(
9206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const std::string& name) const {
9216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::HistogramBase* histogram =
9226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::StatisticsRecorder::FindHistogram(name);
9236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!histogram)
9246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return scoped_ptr<base::HistogramSamples>(new base::SampleMap());
9256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return histogram->SnapshotSamples();
9266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
9276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsUnregistered) {
929a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Store loads occurring before invalidation registration are not counted.
930a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
931a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */);
932a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */);
933a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED));
934a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
935a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
936a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
937a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
9386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
939a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
940a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
9416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsNoInvalidations) {
942a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Store loads occurring while registered should be differentiated depending
943a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // on whether the invalidation service was enabled or not.
944a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A);
945a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StartInvalidator();
946c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
947c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Initially, invalidations have not been enabled past the grace period, so
948c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // invalidations are OFF.
949a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
950a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
951c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
952c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
953c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // If the clock advances less than the grace period, invalidations are OFF.
954c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  AdvanceClock(base::TimeDelta::FromSeconds(1));
955a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
956a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
957c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
958c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
959c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // After the grace period elapses, invalidations are ON.
960c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  AdvanceClock(base::TimeDelta::FromSeconds(
961c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      CloudPolicyInvalidator::kInvalidationGracePeriod));
962a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
963a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
964a36e5920737c6adbddd3e43b760e5de8431db6e0