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/test_extension_system.h"
12 #include "chrome/browser/ui/webui/extensions/extension_settings_handler.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "extensions/browser/management_policy.h"
17 #include "extensions/common/constants.h"
18 #include "extensions/common/extension.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 base::DictionaryValue
* DeserializeJSONTestData(
58 const base::FilePath
& path
,
62 JSONFileValueSerializer
serializer(path
);
63 value
= serializer
.Deserialize(NULL
, error
);
65 return static_cast<base::DictionaryValue
*>(value
);
68 base::DictionaryValue
* CreateExtensionDetailViewFromPath(
69 const base::FilePath
& extension_path
,
70 const std::vector
<ExtensionPage
>& pages
,
71 Manifest::Location location
) {
74 base::FilePath manifest_path
= extension_path
.Append(kManifestFilename
);
75 scoped_ptr
<base::DictionaryValue
> extension_data(DeserializeJSONTestData(
76 manifest_path
, &error
));
79 scoped_refptr
<Extension
> extension(Extension::Create(
80 extension_path
, location
, *extension_data
, Extension::REQUIRE_KEY
,
82 EXPECT_TRUE(extension
.get());
85 return handler_
->CreateExtensionDetailValue(extension
.get(), pages
, NULL
);
88 void CompareExpectedAndActualOutput(
89 const base::FilePath
& extension_path
,
90 const std::vector
<ExtensionPage
>& pages
,
91 const base::FilePath
& expected_output_path
) {
94 scoped_ptr
<base::DictionaryValue
> expected_output_data(
95 DeserializeJSONTestData(expected_output_path
, &error
));
98 // Produce test output.
99 scoped_ptr
<base::DictionaryValue
> actual_output_data(
100 CreateExtensionDetailViewFromPath(
101 extension_path
, pages
, Manifest::INVALID_LOCATION
));
103 // Compare the outputs.
104 // Ignore unknown fields in the actual output data.
105 std::string paths_details
= " - expected (" +
106 expected_output_path
.MaybeAsASCII() + ") vs. actual (" +
107 extension_path
.MaybeAsASCII() + ")";
108 for (base::DictionaryValue::Iterator
field(*expected_output_data
);
109 !field
.IsAtEnd(); field
.Advance()) {
110 const base::Value
* expected_value
= &field
.value();
111 base::Value
* actual_value
= NULL
;
112 EXPECT_TRUE(actual_output_data
->Get(field
.key(), &actual_value
)) <<
113 field
.key() + " is missing" + paths_details
;
114 EXPECT_TRUE(expected_value
->Equals(actual_value
)) << field
.key() +
119 base::MessageLoop message_loop_
;
120 content::TestBrowserThread ui_thread_
;
121 content::TestBrowserThread file_thread_
;
122 scoped_ptr
<TestingProfile
> profile_
;
123 ExtensionService
* extension_service_
;
124 ManagementPolicy
* management_policy_
;
125 scoped_ptr
<ExtensionSettingsHandler
> handler_
;
127 #if defined OS_CHROMEOS
128 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
129 chromeos::ScopedTestCrosSettings test_cros_settings_
;
130 chromeos::ScopedTestUserManager test_user_manager_
;
134 TEST_F(ExtensionUITest
, GenerateExtensionsJSONData
) {
135 base::FilePath data_test_dir_path
, extension_path
, expected_output_path
;
136 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &data_test_dir_path
));
139 extension_path
= data_test_dir_path
.AppendASCII("extensions")
141 .AppendASCII("Extensions")
142 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
143 .AppendASCII("1.0.0.0");
145 std::vector
<ExtensionPage
> pages
;
146 pages
.push_back(ExtensionPage(
147 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/bar.html"),
148 42, 88, false, false));
149 pages
.push_back(ExtensionPage(
150 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/dog.html"),
151 0, 0, false, false));
153 expected_output_path
= data_test_dir_path
.AppendASCII("extensions")
155 .AppendASCII("create_extension_detail_value_expected_output")
156 .AppendASCII("good-extension1.json");
158 CompareExpectedAndActualOutput(extension_path
, pages
, expected_output_path
);
160 #if !defined(OS_CHROMEOS)
162 extension_path
= data_test_dir_path
.AppendASCII("extensions")
164 .AppendASCII("Extensions")
165 .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk")
168 expected_output_path
= data_test_dir_path
.AppendASCII("extensions")
170 .AppendASCII("create_extension_detail_value_expected_output")
171 .AppendASCII("good-extension2.json");
173 // It's OK to have duplicate URLs, so long as the IDs are different.
174 pages
[1].url
= pages
[0].url
;
176 CompareExpectedAndActualOutput(extension_path
, pages
, expected_output_path
);
180 extension_path
= data_test_dir_path
.AppendASCII("extensions")
182 .AppendASCII("Extensions")
183 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
186 expected_output_path
= data_test_dir_path
.AppendASCII("extensions")
188 .AppendASCII("create_extension_detail_value_expected_output")
189 .AppendASCII("good-extension3.json");
193 CompareExpectedAndActualOutput(extension_path
, pages
, expected_output_path
);
196 // Test that using Manifest::UNPACKED for the extension location triggers the
197 // correct values in the details, including location, order, and allow_reload.
198 TEST_F(ExtensionUITest
, LocationLoadPropagation
) {
199 base::FilePath data_test_dir_path
, extension_path
;
200 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &data_test_dir_path
));
202 extension_path
= data_test_dir_path
.AppendASCII("extensions")
204 .AppendASCII("Extensions")
205 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
206 .AppendASCII("1.0.0.0");
208 std::vector
<ExtensionPage
> pages
;
210 scoped_ptr
<base::DictionaryValue
> extension_details(
211 CreateExtensionDetailViewFromPath(
212 extension_path
, pages
, Manifest::UNPACKED
));
214 bool ui_allow_reload
= false;
215 bool ui_is_unpacked
= false;
216 base::FilePath::StringType ui_path
;
218 EXPECT_TRUE(extension_details
->GetBoolean("allow_reload", &ui_allow_reload
));
219 EXPECT_TRUE(extension_details
->GetBoolean("isUnpacked", &ui_is_unpacked
));
220 EXPECT_TRUE(extension_details
->GetString("path", &ui_path
));
221 EXPECT_EQ(true, ui_allow_reload
);
222 EXPECT_EQ(true, ui_is_unpacked
);
223 EXPECT_EQ(extension_path
, base::FilePath(ui_path
));
226 // Test that using Manifest::EXTERNAL_PREF for the extension location triggers
227 // the correct values in the details, including location, order, and
228 // allow_reload. Contrast to Manifest::UNPACKED, which has somewhat different
230 TEST_F(ExtensionUITest
, LocationExternalPrefPropagation
) {
231 base::FilePath data_test_dir_path
, extension_path
;
232 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &data_test_dir_path
));
234 extension_path
= data_test_dir_path
.AppendASCII("extensions")
236 .AppendASCII("Extensions")
237 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
238 .AppendASCII("1.0.0.0");
240 std::vector
<ExtensionPage
> pages
;
242 scoped_ptr
<base::DictionaryValue
> extension_details(
243 CreateExtensionDetailViewFromPath(
244 extension_path
, pages
, Manifest::EXTERNAL_PREF
));
246 bool ui_allow_reload
= true;
247 bool ui_is_unpacked
= true;
248 base::FilePath::StringType ui_path
;
250 EXPECT_TRUE(extension_details
->GetBoolean("allow_reload", &ui_allow_reload
));
251 EXPECT_TRUE(extension_details
->GetBoolean("isUnpacked", &ui_is_unpacked
));
252 EXPECT_FALSE(extension_details
->GetString("path", &ui_path
));
253 EXPECT_FALSE(ui_allow_reload
);
254 EXPECT_FALSE(ui_is_unpacked
);
257 // Test that the extension path is correctly propagated into the extension
259 TEST_F(ExtensionUITest
, PathPropagation
) {
260 base::FilePath data_test_dir_path
, extension_path
;
261 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &data_test_dir_path
));
263 extension_path
= data_test_dir_path
.AppendASCII("extensions")
265 .AppendASCII("Extensions")
266 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
267 .AppendASCII("1.0.0.0");
269 std::vector
<ExtensionPage
> pages
;
271 scoped_ptr
<base::DictionaryValue
> extension_details(
272 CreateExtensionDetailViewFromPath(
273 extension_path
, pages
, Manifest::UNPACKED
));
275 base::FilePath::StringType ui_path
;
277 EXPECT_TRUE(extension_details
->GetString("path", &ui_path
));
278 EXPECT_EQ(extension_path
, base::FilePath(ui_path
));
281 } // namespace extensions