asynchronous_policy_loader_unittest.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/policy/asynchronous_policy_loader.h"
6#include "chrome/browser/policy/asynchronous_policy_provider.h"
7#include "chrome/browser/policy/asynchronous_policy_test_base.h"
8#include "chrome/browser/policy/mock_configuration_policy_provider.h"
9#include "testing/gmock/include/gmock/gmock.h"
10
11using ::testing::_;
12using ::testing::InSequence;
13using ::testing::Return;
14
15namespace policy {
16
17class MockConfigurationPolicyObserver
18    : public ConfigurationPolicyProvider::Observer {
19 public:
20  MOCK_METHOD0(OnUpdatePolicy, void());
21  void OnProviderGoingAway() {}
22};
23
24class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase {
25 public:
26  AsynchronousPolicyLoaderTest() {}
27  virtual ~AsynchronousPolicyLoaderTest() {}
28
29  virtual void SetUp() {
30    AsynchronousPolicyTestBase::SetUp();
31    mock_provider_.reset(new MockConfigurationPolicyProvider());
32  }
33
34 protected:
35  scoped_ptr<MockConfigurationPolicyProvider> mock_provider_;
36
37 private:
38  DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoaderTest);
39};
40
41ACTION(CreateTestDictionary) {
42  return new DictionaryValue();
43}
44
45ACTION_P(CreateSequencedTestDictionary, number) {
46  DictionaryValue* test_dictionary = new DictionaryValue();
47  test_dictionary->SetInteger("id", ++(*number));
48  return test_dictionary;
49}
50
51ACTION(RescheduleImmediatePolicyReload) {
52  *arg1 = base::TimeDelta();
53  return false;
54}
55
56TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) {
57  DictionaryValue* template_dict(new DictionaryValue());
58  EXPECT_CALL(*delegate_, Load()).WillOnce(Return(template_dict));
59  scoped_refptr<AsynchronousPolicyLoader> loader =
60      new AsynchronousPolicyLoader(delegate_.release(), 10);
61  loader->Init();
62  const DictionaryValue* loaded_dict(loader->policy());
63  EXPECT_TRUE(loaded_dict->Equals(template_dict));
64}
65
66// Verify that the fallback policy requests are made.
67TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) {
68  int dictionary_number = 0;
69  InSequence s;
70  EXPECT_CALL(*delegate_, Load()).WillOnce(
71      CreateSequencedTestDictionary(&dictionary_number));
72  EXPECT_CALL(*delegate_, Load()).WillOnce(
73      CreateSequencedTestDictionary(&dictionary_number));
74  scoped_refptr<AsynchronousPolicyLoader> loader =
75      new AsynchronousPolicyLoader(delegate_.release(), 10);
76  loader->Init();
77  loop_.RunAllPending();
78  loader->Reload();
79  loop_.RunAllPending();
80
81  const DictionaryValue* loaded_dict(loader->policy());
82  int loaded_number;
83  EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number));
84  EXPECT_EQ(dictionary_number, loaded_number);
85}
86
87// Ensure that calling stop on the loader stops subsequent reloads from
88// happening.
89TEST_F(AsynchronousPolicyLoaderTest, Stop) {
90  ON_CALL(*delegate_, Load()).WillByDefault(CreateTestDictionary());
91  EXPECT_CALL(*delegate_, Load()).Times(1);
92  scoped_refptr<AsynchronousPolicyLoader> loader =
93      new AsynchronousPolicyLoader(delegate_.release(), 10);
94  loader->Init();
95  loop_.RunAllPending();
96  loader->Stop();
97  loop_.RunAllPending();
98  loader->Reload();
99  loop_.RunAllPending();
100}
101
102// Verifies that the provider is notified upon policy reload, but only
103// if the policy changed.
104TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) {
105  InSequence s;
106  MockConfigurationPolicyObserver observer;
107  int dictionary_number_1 = 0;
108  int dictionary_number_2 = 0;
109  EXPECT_CALL(*delegate_, Load()).WillOnce(
110      CreateSequencedTestDictionary(&dictionary_number_1));
111  EXPECT_CALL(*delegate_, Load()).WillOnce(
112      CreateSequencedTestDictionary(&dictionary_number_2));
113  EXPECT_CALL(observer, OnUpdatePolicy()).Times(0);
114  EXPECT_CALL(*delegate_, Load()).WillOnce(
115      CreateSequencedTestDictionary(&dictionary_number_2));
116  EXPECT_CALL(observer, OnUpdatePolicy()).Times(1);
117  EXPECT_CALL(*delegate_, Load()).WillOnce(
118      CreateSequencedTestDictionary(&dictionary_number_1));
119  scoped_refptr<AsynchronousPolicyLoader> loader =
120      new AsynchronousPolicyLoader(delegate_.release(), 10);
121  AsynchronousPolicyProvider provider(NULL, loader);
122  // |registrar| must be declared last so that it is destroyed first.
123  ConfigurationPolicyObserverRegistrar registrar;
124  registrar.Init(&provider, &observer);
125  loop_.RunAllPending();
126  loader->Reload();
127  loop_.RunAllPending();
128  loader->Reload();
129  loop_.RunAllPending();
130  loader->Reload();
131  loop_.RunAllPending();
132}
133
134}  // namespace policy
135