Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: chrome/browser/policy/config_dir_policy_provider_unittest.cc

Issue 3124025: Support change detection and reloading in ConfigDirPolicyProvider. (Closed)
Patch Set: Address offline feedback from Danno. Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/policy/config_dir_policy_provider.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/path_service.h" 6 #include "base/path_service.h"
7 #include "base/string_number_conversions.h" 7 #include "base/string_number_conversions.h"
8 #include "chrome/browser/policy/config_dir_policy_provider.h" 8 #include "chrome/browser/policy/config_dir_policy_provider.h"
9 #include "chrome/browser/policy/mock_configuration_policy_store.h" 9 #include "chrome/browser/policy/mock_configuration_policy_store.h"
10 #include "chrome/common/json_value_serializer.h" 10 #include "chrome/common/json_value_serializer.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gmock/include/gmock/gmock.h"
12 13
13 class ConfigDirPolicyProviderTest : public testing::Test { 14 using testing::Mock;
15
16 namespace {
17
18 // Shorter reload intervals for testing PolicyDirWatcher.
19 const int kSettleIntervalSecondsForTesting = 0;
20 const int kReloadIntervalMinutesForTesting = 1;
21
22 class ConfigDirPolicyProviderTestBase : public testing::Test {
14 protected: 23 protected:
24 ConfigDirPolicyProviderTestBase()
25 : ui_thread_(ChromeThread::UI, &loop_),
26 file_thread_(ChromeThread::FILE, &loop_) {}
27
15 virtual void SetUp() { 28 virtual void SetUp() {
16 // Determine the directory to use for testing. 29 // Determine the directory to use for testing.
17 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); 30 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
18 test_dir_ = 31 test_dir_ =
19 test_dir_.Append(FILE_PATH_LITERAL("ConfigDirPolicyProviderTest")); 32 test_dir_.Append(FILE_PATH_LITERAL("ConfigDirPolicyProviderTest"));
20 33
21 // Make sure the directory is fresh. 34 // Make sure the directory is fresh.
22 file_util::Delete(test_dir_, true); 35 file_util::Delete(test_dir_, true);
23 file_util::CreateDirectory(test_dir_); 36 file_util::CreateDirectory(test_dir_);
24 ASSERT_TRUE(file_util::DirectoryExists(test_dir_)); 37 ASSERT_TRUE(file_util::DirectoryExists(test_dir_));
25
26 // Create a fresh policy store mock.
27 policy_store_.reset(new MockConfigurationPolicyStore());
28 } 38 }
29 39
30 virtual void TearDown() { 40 virtual void TearDown() {
41 loop_.RunAllPending();
31 // Clean up test directory. 42 // Clean up test directory.
32 ASSERT_TRUE(file_util::Delete(test_dir_, true)); 43 ASSERT_TRUE(file_util::Delete(test_dir_, true));
33 ASSERT_FALSE(file_util::PathExists(test_dir_)); 44 ASSERT_FALSE(file_util::PathExists(test_dir_));
34 } 45 }
35 46
36 // JSON-encode a dictionary and write it to a file. 47 // JSON-encode a dictionary and write it to a file.
37 void WriteConfigFile(const DictionaryValue& dict, 48 void WriteConfigFile(const DictionaryValue& dict,
38 const std::string& file_name) { 49 const std::string& file_name) {
39 std::string data; 50 std::string data;
40 JSONStringValueSerializer serializer(&data); 51 JSONStringValueSerializer serializer(&data);
41 serializer.Serialize(dict); 52 serializer.Serialize(dict);
42 FilePath file_path(test_dir_.AppendASCII(file_name)); 53 FilePath file_path(test_dir_.AppendASCII(file_name));
43 file_util::WriteFile(file_path, data.c_str(), data.size()); 54 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size()));
44 } 55 }
45 56
46 FilePath test_dir_; 57 FilePath test_dir_;
58 MessageLoop loop_;
59 ChromeThread ui_thread_;
60 ChromeThread file_thread_;
61 };
62
63 // A mock provider that allows us to capture to reload notifications.
64 class MockConfigDirPolicyProvider : public ConfigDirPolicyProvider {
65 public:
66 explicit MockConfigDirPolicyProvider(const FilePath& config_dir_)
67 : ConfigDirPolicyProvider(config_dir_) {}
68
69 MOCK_METHOD0(NotifyStoreOfPolicyChange, void());
70 };
71
72 class PolicyDirLoaderTest : public ConfigDirPolicyProviderTestBase {
73 protected:
74 PolicyDirLoaderTest() {}
75
76 virtual void SetUp() {
77 ConfigDirPolicyProviderTestBase::SetUp();
78 provider_.reset(new MockConfigDirPolicyProvider(test_dir_));
79 }
80
81 virtual void TearDown() {
82 provider_.reset(NULL);
83 ConfigDirPolicyProviderTestBase::TearDown();
84 }
85
86 scoped_ptr<MockConfigDirPolicyProvider> provider_;
87 };
88
89 TEST_F(PolicyDirLoaderTest, BasicLoad) {
90 DictionaryValue test_dict;
91 test_dict.SetString("HomepageLocation", "http://www.google.com");
92 WriteConfigFile(test_dict, "config_file");
93
94 scoped_refptr<PolicyDirLoader> loader_(
95 new PolicyDirLoader(provider_->AsWeakPtr(), test_dir_,
96 kSettleIntervalSecondsForTesting,
97 kReloadIntervalMinutesForTesting));
98 scoped_ptr<DictionaryValue> policy(loader_->GetPolicy());
99 EXPECT_TRUE(policy.get());
100 EXPECT_EQ(1U, policy->size());
101
102 std::string str_value;
103 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
104 EXPECT_EQ("http://www.google.com", str_value);
105
106 loader_->Stop();
107 }
108
109 TEST_F(PolicyDirLoaderTest, TestRefresh) {
110 scoped_refptr<PolicyDirLoader> loader_(
111 new PolicyDirLoader(provider_->AsWeakPtr(), test_dir_,
112 kSettleIntervalSecondsForTesting,
113 kReloadIntervalMinutesForTesting));
114 scoped_ptr<DictionaryValue> policy(loader_->GetPolicy());
115 EXPECT_TRUE(policy.get());
116 EXPECT_EQ(0U, policy->size());
117
118 DictionaryValue test_dict;
119 test_dict.SetString("HomepageLocation", "http://www.google.com");
120 WriteConfigFile(test_dict, "config_file");
121
122 EXPECT_CALL(*provider_, NotifyStoreOfPolicyChange()).Times(1);
123 loader_->OnFilePathChanged(test_dir_.AppendASCII("config_file"));
124
125 // Run the loop. The refresh should be handled immediately since the settle
126 // interval has been disabled.
127 loop_.RunAllPending();
128 Mock::VerifyAndClearExpectations(provider_.get());
129
130 policy.reset(loader_->GetPolicy());
131 EXPECT_TRUE(policy.get());
132 EXPECT_EQ(1U, policy->size());
133
134 std::string str_value;
135 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
136 EXPECT_EQ("http://www.google.com", str_value);
137
138 loader_->Stop();
139 }
140
141 class ConfigDirPolicyProviderTest : public ConfigDirPolicyProviderTestBase {
142 protected:
143 virtual void SetUp() {
144 ConfigDirPolicyProviderTestBase::SetUp();
145 // Create a fresh policy store mock.
146 policy_store_.reset(new MockConfigurationPolicyStore());
147 }
148
47 scoped_ptr<MockConfigurationPolicyStore> policy_store_; 149 scoped_ptr<MockConfigurationPolicyStore> policy_store_;
48 }; 150 };
49 151
50 // The preferences dictionary is expected to be empty when there are no files to 152 // The preferences dictionary is expected to be empty when there are no files to
51 // load. 153 // load.
52 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsEmpty) { 154 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsEmpty) {
53 ConfigDirPolicyProvider provider(test_dir_); 155 ConfigDirPolicyProvider provider(test_dir_);
54 EXPECT_TRUE(provider.Provide(policy_store_.get())); 156 EXPECT_TRUE(provider.Provide(policy_store_.get()));
55 EXPECT_TRUE(policy_store_->policy_map().empty()); 157 EXPECT_TRUE(policy_store_->policy_map().empty());
56 } 158 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 policy_store_->policy_map()); 208 policy_store_->policy_map());
107 EXPECT_EQ(1U, policy_map.size()); 209 EXPECT_EQ(1U, policy_map.size());
108 MockConfigurationPolicyStore::PolicyMap::const_iterator entry = 210 MockConfigurationPolicyStore::PolicyMap::const_iterator entry =
109 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage); 211 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage);
110 ASSERT_TRUE(entry != policy_map.end()); 212 ASSERT_TRUE(entry != policy_map.end());
111 213
112 std::string str_value; 214 std::string str_value;
113 EXPECT_TRUE(entry->second->GetAsString(&str_value)); 215 EXPECT_TRUE(entry->second->GetAsString(&str_value));
114 EXPECT_EQ("http://foo.com", str_value); 216 EXPECT_EQ("http://foo.com", str_value);
115 } 217 }
218
219 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/policy/config_dir_policy_provider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698