1// Copyright (c) 2010 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_test_base.h"
7#include "chrome/browser/policy/configuration_policy_pref_store.h"
8#include "chrome/browser/policy/configuration_policy_store_interface.h"
9#include "chrome/browser/policy/file_based_policy_provider.h"
10#include "chrome/browser/policy/mock_configuration_policy_store.h"
11#include "policy/policy_constants.h"
12#include "testing/gmock/include/gmock/gmock.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15using testing::_;
16using testing::InSequence;
17using testing::Return;
18
19namespace policy {
20
21class FileBasedPolicyProviderDelegateMock
22    : public FileBasedPolicyProvider::ProviderDelegate {
23 public:
24  FileBasedPolicyProviderDelegateMock()
25      : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {}
26  MOCK_METHOD0(Load, DictionaryValue*());
27  MOCK_METHOD0(GetLastModification, base::Time());
28};
29
30TEST_F(AsynchronousPolicyTestBase, ProviderInit) {
31  base::Time last_modified;
32  FileBasedPolicyProviderDelegateMock* provider_delegate =
33      new FileBasedPolicyProviderDelegateMock();
34  EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
35      Return(last_modified));
36  InSequence s;
37  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
38      new DictionaryValue));
39  DictionaryValue* policies = new DictionaryValue();
40  policies->SetBoolean(policy::key::kSyncDisabled, true);
41  // A second call to Load gets triggered during the provider's construction
42  // when the file watcher is initialized, since this file may have changed
43  // between the initial load and creating watcher.
44  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
45  FileBasedPolicyProvider provider(
46      ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
47      provider_delegate);
48  loop_.RunAllPending();
49  EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
50  provider.Provide(store_.get());
51}
52
53TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) {
54  base::Time last_modified;
55  FileBasedPolicyProviderDelegateMock* provider_delegate =
56      new FileBasedPolicyProviderDelegateMock();
57  EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
58      Return(last_modified));
59  InSequence s;
60  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
61      new DictionaryValue));
62  FileBasedPolicyProvider file_based_provider(
63          ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
64          provider_delegate);
65  // A second call to Load gets triggered during the provider's construction
66  // when the file watcher is initialized, since this file may have changed
67  // between the initial load and creating watcher.
68  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
69      new DictionaryValue));
70  loop_.RunAllPending();
71  // A third and final call to Load is made by the explicit Reload. This
72  // should be the one that provides the current policy.
73  DictionaryValue* policies = new DictionaryValue();
74  policies->SetBoolean(policy::key::kSyncDisabled, true);
75  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
76  file_based_provider.loader()->Reload();
77  loop_.RunAllPending();
78  EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
79  file_based_provider.Provide(store_.get());
80}
81
82}  // namespace policy
83