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"
27 namespace extensions
{
29 class ExtensionUITest
: public testing::Test
{
32 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
33 file_thread_(content::BrowserThread::FILE, &message_loop_
) {}
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_
,
50 virtual void TearDown() OVERRIDE
{
53 // Execute any pending deletion tasks.
54 message_loop_
.RunUntilIdle();
57 static DictionaryValue
* DeserializeJSONTestData(const base::FilePath
& path
,
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
) {
73 base::FilePath manifest_path
= extension_path
.Append(kManifestFilename
);
74 scoped_ptr
<DictionaryValue
> extension_data(DeserializeJSONTestData(
75 manifest_path
, &error
));
78 scoped_refptr
<Extension
> extension(Extension::Create(
79 extension_path
, location
, *extension_data
, Extension::REQUIRE_KEY
,
81 EXPECT_TRUE(extension
.get());
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
) {
93 scoped_ptr
<DictionaryValue
> expected_output_data(DeserializeJSONTestData(
94 expected_output_path
, &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() +
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_
;
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
));
138 extension_path
= data_test_dir_path
.AppendASCII("extensions")
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")
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)
161 extension_path
= data_test_dir_path
.AppendASCII("extensions")
163 .AppendASCII("Extensions")
164 .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk")
167 expected_output_path
= data_test_dir_path
.AppendASCII("extensions")
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
);
179 extension_path
= data_test_dir_path
.AppendASCII("extensions")
181 .AppendASCII("Extensions")
182 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
185 expected_output_path
= data_test_dir_path
.AppendASCII("extensions")
187 .AppendASCII("create_extension_detail_value_expected_output")
188 .AppendASCII("good-extension3.json");
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")
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
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")
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
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")
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