summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/file_based_policy_provider_unittest.cc
blob: c78c5869675229064759713b11efaff799860227 (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
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
// 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/configuration_policy_pref_store.h"
#include "chrome/browser/policy/file_based_policy_provider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::Mock;

namespace policy {

// Shorter reload intervals for testing FileBasedPolicyLoader.
const int kSettleIntervalSecondsForTesting = 0;
const int kReloadIntervalMinutesForTesting = 1;

// A delegate for testing that can feed arbitrary information to the loader.
class TestDelegate : public FileBasedPolicyProvider::Delegate {
 public:
  TestDelegate()
      : FileBasedPolicyProvider::Delegate(FilePath(FILE_PATH_LITERAL("fake"))) {
  }

  // FileBasedPolicyProvider::Delegate implementation:
  virtual DictionaryValue* Load() {
    return static_cast<DictionaryValue*>(dict_.DeepCopy());
  }

  virtual base::Time GetLastModification() {
    return last_modification_;
  }

  DictionaryValue* dict() { return &dict_; }
  void set_last_modification(const base::Time& last_modification) {
    last_modification_ = last_modification;
  }

 private:
  DictionaryValue dict_;
  base::Time last_modification_;
};

// A mock provider that allows us to capture reload notifications.
class MockPolicyProvider : public ConfigurationPolicyProvider,
                           public base::SupportsWeakPtr<MockPolicyProvider> {
 public:
  explicit MockPolicyProvider()
      : ConfigurationPolicyProvider(
          ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()) {
  }

  virtual bool Provide(ConfigurationPolicyStoreInterface* store) {
    return true;
  }

  MOCK_METHOD0(NotifyStoreOfPolicyChange, void());
};

class FileBasedPolicyLoaderTest : public testing::Test {
 protected:
  FileBasedPolicyLoaderTest()
      : ui_thread_(BrowserThread::UI, &loop_),
        file_thread_(BrowserThread::FILE, &loop_) {}

  virtual void TearDown() {
    loop_.RunAllPending();
  }

  MessageLoop loop_;

 private:
  BrowserThread ui_thread_;
  BrowserThread file_thread_;
};

TEST_F(FileBasedPolicyLoaderTest, BasicLoad) {
  TestDelegate* test_delegate = new TestDelegate;
  test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com");

  scoped_refptr<FileBasedPolicyLoader> loader(
      new FileBasedPolicyLoader(base::WeakPtr<FileBasedPolicyProvider>(),
                                test_delegate,
                                kSettleIntervalSecondsForTesting,
                                kReloadIntervalMinutesForTesting));
  scoped_ptr<DictionaryValue> policy(loader->GetPolicy());
  EXPECT_TRUE(policy.get());
  EXPECT_EQ(1U, policy->size());

  std::string str_value;
  EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
  EXPECT_EQ("http://www.google.com", str_value);

  loader->Stop();
}

TEST_F(FileBasedPolicyLoaderTest, TestRefresh) {
  MockPolicyProvider provider;
  TestDelegate* test_delegate = new TestDelegate;

  scoped_refptr<FileBasedPolicyLoader> loader(
      new FileBasedPolicyLoader(provider.AsWeakPtr(),
                                test_delegate,
                                kSettleIntervalSecondsForTesting,
                                kReloadIntervalMinutesForTesting));
  scoped_ptr<DictionaryValue> policy(loader->GetPolicy());
  EXPECT_TRUE(policy.get());
  EXPECT_EQ(0U, policy->size());

  test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com");

  EXPECT_CALL(provider, NotifyStoreOfPolicyChange()).Times(1);
  loader->OnFilePathChanged(FilePath(FILE_PATH_LITERAL("fake")));

  // Run the loop. The refresh should be handled immediately since the settle
  // interval has been disabled.
  loop_.RunAllPending();
  Mock::VerifyAndClearExpectations(&provider);

  policy.reset(loader->GetPolicy());
  EXPECT_TRUE(policy.get());
  EXPECT_EQ(1U, policy->size());

  std::string str_value;
  EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
  EXPECT_EQ("http://www.google.com", str_value);

  loader->Stop();
}

}  // namespace policy