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