Make default apps cache multiprofile friendly
[chromium-blink-merge.git] / chrome / browser / extensions / extension_ui_unittest.cc
blobd856cf19838db2f747013d32758322d049aecd0f
1 // Copyright (c) 2012 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 "base/command_line.h"
6 #include "base/json/json_file_value_serializer.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/management_policy.h"
12 #include "chrome/browser/extensions/test_extension_system.h"
13 #include "chrome/browser/ui/webui/extensions/extension_settings_handler.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/extensions/extension.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "extensions/common/constants.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/user_manager.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 #endif
27 namespace extensions {
29 class ExtensionUITest : public testing::Test {
30 public:
31 ExtensionUITest()
32 : ui_thread_(content::BrowserThread::UI, &message_loop_),
33 file_thread_(content::BrowserThread::FILE, &message_loop_) {}
35 protected:
36 virtual void SetUp() OVERRIDE {
37 // Create an ExtensionService and ManagementPolicy to inject into the
38 // ExtensionSettingsHandler.
39 profile_.reset(new TestingProfile());
40 TestExtensionSystem* system =
41 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get()));
42 extension_service_ = system->CreateExtensionService(
43 CommandLine::ForCurrentProcess(), base::FilePath(), false);
44 management_policy_ = system->management_policy();
46 handler_.reset(new ExtensionSettingsHandler(extension_service_,
47 management_policy_));
50 virtual void TearDown() OVERRIDE {
51 handler_.reset();
52 profile_.reset();
53 // Execute any pending deletion tasks.
54 message_loop_.RunUntilIdle();
57 static DictionaryValue* DeserializeJSONTestData(const base::FilePath& path,
58 std::string *error) {
59 Value* value;
61 JSONFileValueSerializer serializer(path);
62 value = serializer.Deserialize(NULL, error);
64 return static_cast<DictionaryValue*>(value);
67 DictionaryValue* CreateExtensionDetailViewFromPath(
68 const base::FilePath& extension_path,
69 const std::vector<ExtensionPage>& pages,
70 Manifest::Location location) {
71 std::string error;
73 base::FilePath manifest_path = extension_path.Append(kManifestFilename);
74 scoped_ptr<DictionaryValue> extension_data(DeserializeJSONTestData(
75 manifest_path, &error));
76 EXPECT_EQ("", error);
78 scoped_refptr<Extension> extension(Extension::Create(
79 extension_path, location, *extension_data, Extension::REQUIRE_KEY,
80 &error));
81 EXPECT_TRUE(extension.get());
82 EXPECT_EQ("", error);
84 return handler_->CreateExtensionDetailValue(extension.get(), pages, NULL);
87 void CompareExpectedAndActualOutput(
88 const base::FilePath& extension_path,
89 const std::vector<ExtensionPage>& pages,
90 const base::FilePath& expected_output_path) {
91 std::string error;
93 scoped_ptr<DictionaryValue> expected_output_data(DeserializeJSONTestData(
94 expected_output_path, &error));
95 EXPECT_EQ("", error);
97 // Produce test output.
98 scoped_ptr<DictionaryValue> actual_output_data(
99 CreateExtensionDetailViewFromPath(
100 extension_path, pages, Manifest::INVALID_LOCATION));
102 // Compare the outputs.
103 // Ignore unknown fields in the actual output data.
104 std::string paths_details = " - expected (" +
105 expected_output_path.MaybeAsASCII() + ") vs. actual (" +
106 extension_path.MaybeAsASCII() + ")";
107 for (DictionaryValue::Iterator field(*expected_output_data);
108 !field.IsAtEnd(); field.Advance()) {
109 const Value* expected_value = &field.value();
110 Value* actual_value = NULL;
111 EXPECT_TRUE(actual_output_data->Get(field.key(), &actual_value)) <<
112 field.key() + " is missing" + paths_details;
113 EXPECT_TRUE(expected_value->Equals(actual_value)) << field.key() +
114 paths_details;
118 base::MessageLoop message_loop_;
119 content::TestBrowserThread ui_thread_;
120 content::TestBrowserThread file_thread_;
121 scoped_ptr<TestingProfile> profile_;
122 ExtensionService* extension_service_;
123 ManagementPolicy* management_policy_;
124 scoped_ptr<ExtensionSettingsHandler> handler_;
126 #if defined OS_CHROMEOS
127 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
128 chromeos::ScopedTestCrosSettings test_cros_settings_;
129 chromeos::ScopedTestUserManager test_user_manager_;
130 #endif
133 TEST_F(ExtensionUITest, GenerateExtensionsJSONData) {
134 base::FilePath data_test_dir_path, extension_path, expected_output_path;
135 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
137 // Test Extension1
138 extension_path = data_test_dir_path.AppendASCII("extensions")
139 .AppendASCII("good")
140 .AppendASCII("Extensions")
141 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
142 .AppendASCII("1.0.0.0");
144 std::vector<ExtensionPage> pages;
145 pages.push_back(ExtensionPage(
146 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/bar.html"),
147 42, 88, false, false));
148 pages.push_back(ExtensionPage(
149 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/dog.html"),
150 0, 0, false, false));
152 expected_output_path = data_test_dir_path.AppendASCII("extensions")
153 .AppendASCII("ui")
154 .AppendASCII("create_extension_detail_value_expected_output")
155 .AppendASCII("good-extension1.json");
157 CompareExpectedAndActualOutput(extension_path, pages, expected_output_path);
159 #if !defined(OS_CHROMEOS)
160 // Test Extension2
161 extension_path = data_test_dir_path.AppendASCII("extensions")
162 .AppendASCII("good")
163 .AppendASCII("Extensions")
164 .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk")
165 .AppendASCII("2");
167 expected_output_path = data_test_dir_path.AppendASCII("extensions")
168 .AppendASCII("ui")
169 .AppendASCII("create_extension_detail_value_expected_output")
170 .AppendASCII("good-extension2.json");
172 // It's OK to have duplicate URLs, so long as the IDs are different.
173 pages[1].url = pages[0].url;
175 CompareExpectedAndActualOutput(extension_path, pages, expected_output_path);
176 #endif
178 // Test Extension3
179 extension_path = data_test_dir_path.AppendASCII("extensions")
180 .AppendASCII("good")
181 .AppendASCII("Extensions")
182 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
183 .AppendASCII("1.0");
185 expected_output_path = data_test_dir_path.AppendASCII("extensions")
186 .AppendASCII("ui")
187 .AppendASCII("create_extension_detail_value_expected_output")
188 .AppendASCII("good-extension3.json");
190 pages.clear();
192 CompareExpectedAndActualOutput(extension_path, pages, expected_output_path);
195 // Test that using Manifest::UNPACKED for the extension location triggers the
196 // correct values in the details, including location, order, and allow_reload.
197 TEST_F(ExtensionUITest, LocationLoadPropagation) {
198 base::FilePath data_test_dir_path, extension_path;
199 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
201 extension_path = data_test_dir_path.AppendASCII("extensions")
202 .AppendASCII("good")
203 .AppendASCII("Extensions")
204 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
205 .AppendASCII("1.0.0.0");
207 std::vector<ExtensionPage> pages;
209 scoped_ptr<DictionaryValue> extension_details(
210 CreateExtensionDetailViewFromPath(
211 extension_path, pages, Manifest::UNPACKED));
213 bool ui_allow_reload = false;
214 bool ui_is_unpacked = false;
215 base::FilePath::StringType ui_path;
217 EXPECT_TRUE(extension_details->GetBoolean("allow_reload", &ui_allow_reload));
218 EXPECT_TRUE(extension_details->GetBoolean("isUnpacked", &ui_is_unpacked));
219 EXPECT_TRUE(extension_details->GetString("path", &ui_path));
220 EXPECT_EQ(true, ui_allow_reload);
221 EXPECT_EQ(true, ui_is_unpacked);
222 EXPECT_EQ(extension_path, base::FilePath(ui_path));
225 // Test that using Manifest::EXTERNAL_PREF for the extension location triggers
226 // the correct values in the details, including location, order, and
227 // allow_reload. Contrast to Manifest::UNPACKED, which has somewhat different
228 // values.
229 TEST_F(ExtensionUITest, LocationExternalPrefPropagation) {
230 base::FilePath data_test_dir_path, extension_path;
231 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
233 extension_path = data_test_dir_path.AppendASCII("extensions")
234 .AppendASCII("good")
235 .AppendASCII("Extensions")
236 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
237 .AppendASCII("1.0.0.0");
239 std::vector<ExtensionPage> pages;
241 scoped_ptr<DictionaryValue> extension_details(
242 CreateExtensionDetailViewFromPath(
243 extension_path, pages, Manifest::EXTERNAL_PREF));
245 bool ui_allow_reload = true;
246 bool ui_is_unpacked = true;
247 base::FilePath::StringType ui_path;
249 EXPECT_TRUE(extension_details->GetBoolean("allow_reload", &ui_allow_reload));
250 EXPECT_TRUE(extension_details->GetBoolean("isUnpacked", &ui_is_unpacked));
251 EXPECT_FALSE(extension_details->GetString("path", &ui_path));
252 EXPECT_FALSE(ui_allow_reload);
253 EXPECT_FALSE(ui_is_unpacked);
256 // Test that the extension path is correctly propagated into the extension
257 // details.
258 TEST_F(ExtensionUITest, PathPropagation) {
259 base::FilePath data_test_dir_path, extension_path;
260 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
262 extension_path = data_test_dir_path.AppendASCII("extensions")
263 .AppendASCII("good")
264 .AppendASCII("Extensions")
265 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
266 .AppendASCII("1.0.0.0");
268 std::vector<ExtensionPage> pages;
270 scoped_ptr<DictionaryValue> extension_details(
271 CreateExtensionDetailViewFromPath(
272 extension_path, pages, Manifest::UNPACKED));
274 base::FilePath::StringType ui_path;
276 EXPECT_TRUE(extension_details->GetString("path", &ui_path));
277 EXPECT_EQ(extension_path, base::FilePath(ui_path));
280 } // namespace extensions