Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1288)

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc

Issue 958803004: [Extensions] Make chrome://extensions use developerPrivate for packing crxs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Win fix Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_util.h" 5 #include "base/files/file_util.h"
6 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h" 6 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h"
7 #include "chrome/browser/extensions/extension_function_test_utils.h" 7 #include "chrome/browser/extensions/extension_function_test_utils.h"
8 #include "chrome/browser/extensions/extension_service.h" 8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_service_test_base.h" 9 #include "chrome/browser/extensions/extension_service_test_base.h"
10 #include "chrome/browser/extensions/extension_util.h" 10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/extensions/test_extension_dir.h" 11 #include "chrome/browser/extensions/test_extension_dir.h"
12 #include "chrome/browser/extensions/unpacked_installer.h" 12 #include "chrome/browser/extensions/unpacked_installer.h"
13 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/host_desktop.h" 14 #include "chrome/browser/ui/host_desktop.h"
15 #include "chrome/common/extensions/api/developer_private.h"
15 #include "chrome/test/base/test_browser_window.h" 16 #include "chrome/test/base/test_browser_window.h"
16 #include "extensions/browser/extension_prefs.h" 17 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_registry.h" 18 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_system.h" 19 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/test_extension_registry_observer.h" 20 #include "extensions/browser/test_extension_registry_observer.h"
20 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_set.h" 22 #include "extensions/common/extension_set.h"
22 #include "extensions/common/test_util.h" 23 #include "extensions/common/test_util.h"
23 24
24 namespace extensions { 25 namespace extensions {
(...skipping 11 matching lines...) Expand all
36 // Loads an unpacked extension that is backed by a real directory, allowing 37 // Loads an unpacked extension that is backed by a real directory, allowing
37 // it to be reloaded. 38 // it to be reloaded.
38 const Extension* LoadUnpackedExtension(); 39 const Extension* LoadUnpackedExtension();
39 40
40 // Tests a developer private function (T) that sets an extension pref, and 41 // Tests a developer private function (T) that sets an extension pref, and
41 // verifies it with |has_pref|. 42 // verifies it with |has_pref|.
42 template<typename T> 43 template<typename T>
43 void TestExtensionPrefSetting( 44 void TestExtensionPrefSetting(
44 bool (*has_pref)(const std::string&, content::BrowserContext*)); 45 bool (*has_pref)(const std::string&, content::BrowserContext*));
45 46
47 testing::AssertionResult TestPackExtensionFunction(
48 const base::ListValue& args,
49 api::developer_private::PackStatus expected_status,
50 int expected_flags);
51
46 Browser* browser() { return browser_.get(); } 52 Browser* browser() { return browser_.get(); }
47 53
48 private: 54 private:
49 // ExtensionServiceTestBase: 55 // ExtensionServiceTestBase:
50 void SetUp() override; 56 void SetUp() override;
51 void TearDown() override; 57 void TearDown() override;
52 58
53 // The browser (and accompanying window). 59 // The browser (and accompanying window).
54 scoped_ptr<TestBrowserWindow> browser_window_; 60 scoped_ptr<TestBrowserWindow> browser_window_;
55 scoped_ptr<Browser> browser_; 61 scoped_ptr<Browser> browser_;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 EXPECT_TRUE(has_pref(extension_id, profile())); 133 EXPECT_TRUE(has_pref(extension_id, profile()));
128 134
129 base::ListValue disable_args; 135 base::ListValue disable_args;
130 disable_args.AppendString(extension_id); 136 disable_args.AppendString(extension_id);
131 disable_args.AppendBoolean(false); 137 disable_args.AppendBoolean(false);
132 function = new T(); 138 function = new T();
133 EXPECT_TRUE(RunFunction(function, disable_args)); 139 EXPECT_TRUE(RunFunction(function, disable_args));
134 EXPECT_FALSE(has_pref(extension_id, profile())); 140 EXPECT_FALSE(has_pref(extension_id, profile()));
135 } 141 }
136 142
143 testing::AssertionResult DeveloperPrivateApiUnitTest::TestPackExtensionFunction(
144 const base::ListValue& args,
145 api::developer_private::PackStatus expected_status,
146 int expected_flags) {
147 scoped_refptr<UIThreadExtensionFunction> function(
148 new api::DeveloperPrivatePackDirectoryFunction());
149 if (!RunFunction(function, args))
150 return testing::AssertionFailure() << "Could not run function.";
151
152 // Extract the result. We don't have to test this here, since it's verified as
153 // part of the general extension api system.
154 const base::Value* response_value = nullptr;
155 CHECK(function->GetResultList()->Get(0u, &response_value));
156 scoped_ptr<api::developer_private::PackDirectoryResponse> response =
157 api::developer_private::PackDirectoryResponse::FromValue(*response_value);
158 CHECK(response);
159
160 if (response->status != expected_status) {
161 return testing::AssertionFailure() << "Expected status: " <<
162 expected_status << ", found status: " << response->status <<
163 ", message: " << response->message;
164 }
165
166 if (response->override_flags != expected_flags) {
167 return testing::AssertionFailure() << "Expected flags: " <<
168 expected_flags << ", found flags: " << response->override_flags;
169 }
170
171 return testing::AssertionSuccess();
172 }
173
137 void DeveloperPrivateApiUnitTest::SetUp() { 174 void DeveloperPrivateApiUnitTest::SetUp() {
138 ExtensionServiceTestBase::SetUp(); 175 ExtensionServiceTestBase::SetUp();
139 InitializeEmptyExtensionService(); 176 InitializeEmptyExtensionService();
140 177
141 browser_window_.reset(new TestBrowserWindow()); 178 browser_window_.reset(new TestBrowserWindow());
142 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); 179 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE);
143 params.type = Browser::TYPE_TABBED; 180 params.type = Browser::TYPE_TABBED;
144 params.window = browser_window_.get(); 181 params.window = browser_window_.get();
145 browser_.reset(new Browser(params)); 182 browser_.reset(new Browser(params));
146 } 183 }
(...skipping 29 matching lines...) Expand all
176 registry_observer.WaitForExtensionLoaded(); 213 registry_observer.WaitForExtensionLoaded();
177 EXPECT_EQ(extension_id, reloaded_extension->id()); 214 EXPECT_EQ(extension_id, reloaded_extension->id());
178 } 215 }
179 216
180 // Test developerPrivate.allowFileAccess. 217 // Test developerPrivate.allowFileAccess.
181 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowFileAccess) { 218 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowFileAccess) {
182 TestExtensionPrefSetting<api::DeveloperPrivateAllowFileAccessFunction>( 219 TestExtensionPrefSetting<api::DeveloperPrivateAllowFileAccessFunction>(
183 &util::AllowFileAccess); 220 &util::AllowFileAccess);
184 } 221 }
185 222
223 // Test developerPrivate.packDirectory.
224 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivatePackFunction) {
225 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT);
226
227 base::FilePath root_path = data_dir().AppendASCII("good_unpacked");
228 base::FilePath crx_path = data_dir().AppendASCII("good_unpacked.crx");
229 base::FilePath pem_path = data_dir().AppendASCII("good_unpacked.pem");
230
231 // First, test a directory that should pack properly.
232 base::ListValue pack_args;
233 pack_args.AppendString(root_path.AsUTF8Unsafe());
234 EXPECT_TRUE(TestPackExtensionFunction(
235 pack_args, api::developer_private::PACK_STATUS_SUCCESS, 0));
236
237 // Should have created crx file and pem file.
238 EXPECT_TRUE(base::PathExists(crx_path));
239 EXPECT_TRUE(base::PathExists(pem_path));
240
241 // Deliberately don't cleanup the files, and append the pem path.
242 pack_args.AppendString(pem_path.AsUTF8Unsafe());
243
244 // Try to pack again - we should get a warning abot overwriting the crx.
245 EXPECT_TRUE(TestPackExtensionFunction(
246 pack_args,
247 api::developer_private::PACK_STATUS_WARNING,
248 ExtensionCreator::kOverwriteCRX));
249
250 // Try to pack again, with the overwrite flag; this should succeed.
251 pack_args.AppendInteger(ExtensionCreator::kOverwriteCRX);
252 EXPECT_TRUE(TestPackExtensionFunction(
253 pack_args, api::developer_private::PACK_STATUS_SUCCESS, 0));
254
255 // Try to pack a final time when omitting (an existing) pem file. We should
256 // get an error.
257 base::DeleteFile(crx_path, false);
258 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the pem key argument.
259 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the flags argument.
260 EXPECT_TRUE(TestPackExtensionFunction(
261 pack_args, api::developer_private::PACK_STATUS_ERROR, 0));
262
263 base::DeleteFile(crx_path, false);
264 base::DeleteFile(pem_path, false);
265 }
266
186 } // namespace extensions 267 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698