summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/file_based_policy_provider_unittest.cc
blob: 1bb2185f397fb823ceafecec8180b6d4b7cac914 (plain)
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
// Copyright (c) 2010 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 "chrome/browser/policy/asynchronous_policy_loader.h"
#include "chrome/browser/policy/asynchronous_policy_test_base.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/configuration_policy_store_interface.h"
#include "chrome/browser/policy/file_based_policy_provider.h"
#include "chrome/common/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::InSequence;
using testing::Return;

namespace policy {

class FileBasedPolicyProviderDelegateMock
    : public FileBasedPolicyProvider::ProviderDelegate {
 public:
  FileBasedPolicyProviderDelegateMock()
      : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {}
  MOCK_METHOD0(Load, DictionaryValue*());
  MOCK_METHOD0(GetLastModification, base::Time());
};

TEST_F(AsynchronousPolicyTestBase, ProviderInit) {
  base::Time last_modified;
  FileBasedPolicyProviderDelegateMock* provider_delegate =
      new FileBasedPolicyProviderDelegateMock();
  EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
      Return(last_modified));
  InSequence s;
  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
      new DictionaryValue));
  DictionaryValue* policies = new DictionaryValue();
  policies->SetBoolean(policy::key::kSyncDisabled, true);
  // A second call to Load gets triggered during the provider's construction
  // when the file watcher is initialized, since this file may have changed
  // between the initial load and creating watcher.
  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
  FileBasedPolicyProvider provider(
      ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
      provider_delegate);
  loop_.RunAllPending();
  EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
  provider.Provide(store_.get());
}

TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) {
  base::Time last_modified;
  FileBasedPolicyProviderDelegateMock* provider_delegate =
      new FileBasedPolicyProviderDelegateMock();
  EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
      Return(last_modified));
  InSequence s;
  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
      new DictionaryValue));
  FileBasedPolicyProvider file_based_provider(
          ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
          provider_delegate);
  // A second call to Load gets triggered during the provider's construction
  // when the file watcher is initialized, since this file may have changed
  // between the initial load and creating watcher.
  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
      new DictionaryValue));
  loop_.RunAllPending();
  // A third and final call to Load is made by the explicit Reload. This
  // should be the one that provides the current policy.
  DictionaryValue* policies = new DictionaryValue();
  policies->SetBoolean(policy::key::kSyncDisabled, true);
  EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
  file_based_provider.loader()->Reload();
  loop_.RunAllPending();
  EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
  file_based_provider.Provide(store_.get());
}

}  // namespace policy