Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / extension_cache_unittest.cc
blob0b88894b5d8d53a3ac9157a818bcffbdb7d0dd7d
1 // Copyright (c) 2015 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 <stddef.h>
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chromeos/settings/cros_settings.h"
14 #include "chrome/browser/chromeos/settings/device_settings_service.h"
15 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
16 #include "chrome/browser/extensions/updater/chromeos_extension_cache_delegate.h"
17 #include "chrome/browser/extensions/updater/extension_cache_impl.h"
18 #include "chrome/browser/extensions/updater/local_extension_cache.h"
19 #include "chromeos/settings/cros_settings_names.h"
20 #include "chromeos/settings/cros_settings_provider.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace extensions {
25 namespace {
27 const char kTestExtensionId1[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
28 const char kTestExtensionId2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
29 const char kTestExtensionId3[] = "cccccccccccccccccccccccccccccccc";
31 // 2 megabytes are a little less than 2 100 000 bytes, so the third extension
32 // will not be kept.
33 const size_t kMaxCacheSize = 2 * 1024 * 1024;
34 const size_t kExtensionSize1 = 1000 * 1000;
35 const size_t kExtensionSize2 = 1000 * 1000;
36 const size_t kExtensionSize3 = 100 * 1000;
38 static void CreateFile(const base::FilePath& file,
39 size_t size,
40 const base::Time& timestamp) {
41 const std::string data(size, 0);
42 EXPECT_EQ(base::WriteFile(file, data.data(), data.size()),
43 static_cast<int>(size));
44 EXPECT_TRUE(base::TouchFile(file, timestamp, timestamp));
47 static base::FilePath CreateExtensionFile(const base::FilePath& dir,
48 const std::string& id,
49 const std::string& version,
50 size_t size,
51 const base::Time& timestamp) {
52 const base::FilePath filename = dir.Append(
53 LocalExtensionCache::ExtensionFileName(id, version, "" /* hash */));
54 CreateFile(filename, size, timestamp);
55 return filename;
58 } // namespace
60 class ExtensionCacheTest : public testing::Test {
61 public:
62 void SetUp() override {
63 // Swap out the DeviceSettingsProvider with our stub settings provider
64 // so we can set values for maximum extension cache size.
65 chromeos::CrosSettings* const cros_settings = chromeos::CrosSettings::Get();
66 device_settings_provider_ =
67 cros_settings->GetProvider(chromeos::kExtensionCacheSize);
68 EXPECT_TRUE(device_settings_provider_);
69 EXPECT_TRUE(
70 cros_settings->RemoveSettingsProvider(device_settings_provider_));
71 cros_settings->AddSettingsProvider(&stub_settings_provider_);
74 void TearDown() override {
75 // Restore the real DeviceSettingsProvider.
76 chromeos::CrosSettings* const cros_settings = chromeos::CrosSettings::Get();
77 EXPECT_TRUE(
78 cros_settings->RemoveSettingsProvider(&stub_settings_provider_));
79 cros_settings->AddSettingsProvider(device_settings_provider_);
82 // Helpers used to mock out cros settings.
83 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
84 chromeos::ScopedTestCrosSettings test_cros_settings_;
85 chromeos::CrosSettingsProvider* device_settings_provider_ = nullptr;
86 chromeos::StubCrosSettingsProvider stub_settings_provider_;
88 content::TestBrowserThreadBundle thread_bundle_;
91 TEST_F(ExtensionCacheTest, SizePolicy) {
92 chromeos::CrosSettings::Get()->SetInteger(chromeos::kExtensionCacheSize,
93 kMaxCacheSize);
95 // Create and initialize local cache.
96 const base::Time now = base::Time::Now();
97 base::ScopedTempDir cache_dir;
98 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
99 const base::FilePath cache_path = cache_dir.path();
100 CreateFile(cache_path.Append(LocalExtensionCache::kCacheReadyFlagFileName), 0,
101 now);
103 // The extension cache only has enough space for two of the three extensions.
104 const base::FilePath file1 =
105 CreateExtensionFile(cache_path, kTestExtensionId1, "1.0", kExtensionSize1,
106 now - base::TimeDelta::FromSeconds(1));
107 const base::FilePath file2 =
108 CreateExtensionFile(cache_path, kTestExtensionId2, "2.0", kExtensionSize2,
109 now - base::TimeDelta::FromSeconds(2));
110 const base::FilePath file3 =
111 CreateExtensionFile(cache_path, kTestExtensionId3, "3.0", kExtensionSize3,
112 now - base::TimeDelta::FromSeconds(3));
114 ExtensionCacheImpl cache_impl(
115 make_scoped_ptr(new ChromeOSExtensionCacheDelegate(cache_path)));
117 scoped_ptr<base::RunLoop> run_loop(new base::RunLoop);
118 cache_impl.Start(run_loop->QuitClosure());
119 run_loop->Run();
121 run_loop.reset(new base::RunLoop);
122 cache_impl.Shutdown(run_loop->QuitClosure());
123 run_loop->Run();
125 EXPECT_TRUE(base::PathExists(file1));
126 EXPECT_TRUE(base::PathExists(file2));
127 EXPECT_FALSE(base::PathExists(file3));
130 } // namespace extensions