BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / device_local_account_external_policy_loader_unittest.cc
blobac94f312267af2ab6f34c8a109fa2b9596074b10
1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
7 #include <string>
9 #include "base/callback.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/values.h"
18 #include "base/version.h"
19 #include "chrome/browser/extensions/external_provider_impl.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
23 #include "components/policy/core/common/policy_map.h"
24 #include "components/policy/core/common/policy_types.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "content/public/test/test_utils.h"
29 #include "extensions/browser/external_provider_interface.h"
30 #include "extensions/browser/notification_types.h"
31 #include "extensions/browser/updater/extension_downloader.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/extension_urls.h"
34 #include "extensions/common/manifest.h"
35 #include "net/url_request/test_url_fetcher_factory.h"
36 #include "net/url_request/url_fetcher_delegate.h"
37 #include "net/url_request/url_request_context_getter.h"
38 #include "net/url_request/url_request_test_util.h"
39 #include "policy/policy_constants.h"
40 #include "testing/gmock/include/gmock/gmock.h"
41 #include "testing/gtest/include/gtest/gtest.h"
42 #include "url/gurl.h"
44 #if defined(OS_CHROMEOS)
45 #include "chrome/browser/chromeos/settings/cros_settings.h"
46 #include "chrome/browser/chromeos/settings/device_settings_service.h"
47 #endif // defined(OS_CHROMEOS)
49 using ::testing::InvokeWithoutArgs;
50 using ::testing::Mock;
51 using ::testing::_;
53 namespace chromeos {
55 namespace {
57 const char kCacheDir[] = "cache";
58 const char kExtensionId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
59 const char kExtensionUpdateManifest[] =
60 "extensions/good_v1_update_manifest.xml";
61 const char kExtensionCRXSourceDir[] = "extensions";
62 const char kExtensionCRXFile[] = "good.crx";
63 const char kExtensionCRXVersion[] = "1.0.0.0";
65 class MockExternalPolicyProviderVisitor
66 : public extensions::ExternalProviderInterface::VisitorInterface {
67 public:
68 MockExternalPolicyProviderVisitor();
69 virtual ~MockExternalPolicyProviderVisitor();
71 MOCK_METHOD7(OnExternalExtensionFileFound,
72 bool(const std::string&,
73 const base::Version*,
74 const base::FilePath&,
75 extensions::Manifest::Location,
76 int,
77 bool,
78 bool));
79 MOCK_METHOD6(OnExternalExtensionUpdateUrlFound,
80 bool(const std::string&,
81 const std::string&,
82 const GURL&,
83 extensions::Manifest::Location,
84 int,
85 bool));
86 MOCK_METHOD1(OnExternalProviderReady,
87 void(const extensions::ExternalProviderInterface* provider));
89 private:
90 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyProviderVisitor);
93 MockExternalPolicyProviderVisitor::MockExternalPolicyProviderVisitor() {
96 MockExternalPolicyProviderVisitor::~MockExternalPolicyProviderVisitor() {
99 } // namespace
101 class DeviceLocalAccountExternalPolicyLoaderTest : public testing::Test {
102 protected:
103 DeviceLocalAccountExternalPolicyLoaderTest();
104 ~DeviceLocalAccountExternalPolicyLoaderTest() override;
106 void SetUp() override;
107 void TearDown() override;
109 void VerifyAndResetVisitorCallExpectations();
110 void SetForceInstallListPolicy();
112 content::TestBrowserThreadBundle thread_bundle_;
113 base::ScopedTempDir temp_dir_;
114 base::FilePath cache_dir_;
115 policy::MockCloudPolicyStore store_;
116 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
117 base::FilePath test_dir_;
119 scoped_refptr<DeviceLocalAccountExternalPolicyLoader> loader_;
120 MockExternalPolicyProviderVisitor visitor_;
121 scoped_ptr<extensions::ExternalProviderImpl> provider_;
123 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
125 #if defined(OS_CHROMEOS)
126 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
127 chromeos::ScopedTestCrosSettings test_cros_settings_;
128 #endif // defined(OS_CHROMEOS)
131 DeviceLocalAccountExternalPolicyLoaderTest::
132 DeviceLocalAccountExternalPolicyLoaderTest()
133 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
136 DeviceLocalAccountExternalPolicyLoaderTest::
137 ~DeviceLocalAccountExternalPolicyLoaderTest() {
140 void DeviceLocalAccountExternalPolicyLoaderTest::SetUp() {
141 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
142 cache_dir_ = temp_dir_.path().Append(kCacheDir);
143 ASSERT_TRUE(base::CreateDirectoryAndGetError(cache_dir_, NULL));
144 request_context_getter_ =
145 new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get());
146 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(
147 request_context_getter_.get());
148 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
150 loader_ = new DeviceLocalAccountExternalPolicyLoader(&store_, cache_dir_);
151 provider_.reset(new extensions::ExternalProviderImpl(
152 &visitor_,
153 loader_,
154 NULL,
155 extensions::Manifest::EXTERNAL_POLICY,
156 extensions::Manifest::EXTERNAL_POLICY_DOWNLOAD,
157 extensions::Extension::NO_FLAGS));
159 VerifyAndResetVisitorCallExpectations();
162 void DeviceLocalAccountExternalPolicyLoaderTest::TearDown() {
163 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(NULL);
166 void DeviceLocalAccountExternalPolicyLoaderTest::
167 VerifyAndResetVisitorCallExpectations() {
168 Mock::VerifyAndClearExpectations(&visitor_);
169 EXPECT_CALL(visitor_, OnExternalExtensionFileFound(_, _, _, _, _, _, _))
170 .Times(0);
171 EXPECT_CALL(visitor_, OnExternalExtensionUpdateUrlFound(_, _, _, _, _, _))
172 .Times(0);
173 EXPECT_CALL(visitor_, OnExternalProviderReady(_))
174 .Times(0);
177 void DeviceLocalAccountExternalPolicyLoaderTest::SetForceInstallListPolicy() {
178 scoped_ptr<base::ListValue> forcelist(new base::ListValue);
179 forcelist->AppendString("invalid");
180 forcelist->AppendString(base::StringPrintf(
181 "%s;%s",
182 kExtensionId,
183 extension_urls::GetWebstoreUpdateUrl().spec().c_str()));
184 store_.policy_map_.Set(policy::key::kExtensionInstallForcelist,
185 policy::POLICY_LEVEL_MANDATORY,
186 policy::POLICY_SCOPE_USER,
187 policy::POLICY_SOURCE_CLOUD,
188 forcelist.release(),
189 NULL);
190 store_.NotifyStoreLoaded();
193 // Verifies that when the cache is not explicitly started, the loader does not
194 // serve any extensions, even if the force-install list policy is set or a load
195 // is manually requested.
196 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest, CacheNotStarted) {
197 // Set the force-install list policy.
198 SetForceInstallListPolicy();
200 // Manually request a load.
201 loader_->StartLoading();
203 EXPECT_FALSE(loader_->IsCacheRunning());
204 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
207 // Verifies that the cache can be started and stopped correctly.
208 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest, ForceInstallListEmpty) {
209 // Set an empty force-install list policy.
210 store_.NotifyStoreLoaded();
212 // Start the cache. Verify that the loader announces an empty extension list.
213 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
214 .Times(1);
215 loader_->StartCache(base::ThreadTaskRunnerHandle::Get());
216 base::RunLoop().RunUntilIdle();
217 VerifyAndResetVisitorCallExpectations();
219 // Stop the cache. Verify that the loader announces an empty extension list.
220 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
221 .Times(1);
222 base::RunLoop run_loop;
223 loader_->StopCache(run_loop.QuitClosure());
224 VerifyAndResetVisitorCallExpectations();
226 // Spin the loop until the cache shutdown callback is invoked. Verify that at
227 // that point, no further file I/O tasks are pending.
228 run_loop.Run();
229 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
232 // Verifies that when a force-install list policy referencing an extension is
233 // set and the cache is started, the loader downloads, caches and serves the
234 // extension.
235 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest, ForceInstallListSet) {
236 // Set a force-install list policy that contains an invalid entry (which
237 // should be ignored) and a valid reference to an extension.
238 SetForceInstallListPolicy();
240 // Start the cache.
241 loader_->StartCache(base::ThreadTaskRunnerHandle::Get());
243 // Spin the loop, allowing the loader to process the force-install list.
244 // Verify that the loader announces an empty extension list.
245 net::TestURLFetcherFactory factory;
246 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
247 .Times(1);
248 base::MessageLoop::current()->RunUntilIdle();
250 // Verify that a downloader has started and is attempting to download an
251 // update manifest.
252 net::TestURLFetcher* fetcher = factory.GetFetcherByID(
253 extensions::ExtensionDownloader::kManifestFetcherId);
254 ASSERT_TRUE(fetcher);
255 ASSERT_TRUE(fetcher->delegate());
257 // Return a manifest to the downloader.
258 std::string manifest;
259 EXPECT_TRUE(base::ReadFileToString(test_dir_.Append(kExtensionUpdateManifest),
260 &manifest));
261 fetcher->set_response_code(200);
262 fetcher->SetResponseString(manifest);
263 fetcher->delegate()->OnURLFetchComplete(fetcher);
265 // Wait for the manifest to be parsed.
266 content::WindowedNotificationObserver(
267 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND,
268 content::NotificationService::AllSources()).Wait();
270 // Verify that the downloader is attempting to download a CRX file.
271 fetcher = factory.GetFetcherByID(
272 extensions::ExtensionDownloader::kExtensionFetcherId);
273 ASSERT_TRUE(fetcher);
274 ASSERT_TRUE(fetcher->delegate());
276 // Create a temporary CRX file and return its path to the downloader.
277 EXPECT_TRUE(base::CopyFile(
278 test_dir_.Append(kExtensionCRXSourceDir).Append(kExtensionCRXFile),
279 temp_dir_.path().Append(kExtensionCRXFile)));
280 fetcher->set_response_code(200);
281 fetcher->SetResponseFilePath(temp_dir_.path().Append(kExtensionCRXFile));
282 fetcher->delegate()->OnURLFetchComplete(fetcher);
284 // Spin the loop. Verify that the loader announces the presence of a new CRX
285 // file, served from the cache directory.
286 const base::FilePath cached_crx_path = cache_dir_.Append(base::StringPrintf(
287 "%s-%s.crx", kExtensionId, kExtensionCRXVersion));
288 base::RunLoop cache_run_loop;
289 EXPECT_CALL(visitor_, OnExternalExtensionFileFound(
290 kExtensionId,
292 cached_crx_path,
293 extensions::Manifest::EXTERNAL_POLICY,
296 _));
297 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
298 .Times(1)
299 .WillOnce(InvokeWithoutArgs(&cache_run_loop, &base::RunLoop::Quit));
300 cache_run_loop.Run();
301 VerifyAndResetVisitorCallExpectations();
303 // Verify that the CRX file actually exists in the cache directory and its
304 // contents matches the file returned to the downloader.
305 EXPECT_TRUE(base::ContentsEqual(
306 test_dir_.Append(kExtensionCRXSourceDir).Append(kExtensionCRXFile),
307 cached_crx_path));
309 // Stop the cache. Verify that the loader announces an empty extension list.
310 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
311 .Times(1);
312 base::RunLoop shutdown_run_loop;
313 loader_->StopCache(shutdown_run_loop.QuitClosure());
314 VerifyAndResetVisitorCallExpectations();
316 // Spin the loop until the cache shutdown callback is invoked. Verify that at
317 // that point, no further file I/O tasks are pending.
318 shutdown_run_loop.Run();
319 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
322 } // namespace chromeos