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

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

Issue 954943007: [Extensions] Make chrome://extensions use api functions for file access, reload (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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"
(...skipping 15 matching lines...) Expand all
26 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase { 26 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase {
27 protected: 27 protected:
28 DeveloperPrivateApiUnitTest() {} 28 DeveloperPrivateApiUnitTest() {}
29 ~DeveloperPrivateApiUnitTest() override {} 29 ~DeveloperPrivateApiUnitTest() override {}
30 30
31 // A wrapper around extension_function_test_utils::RunFunction that runs with 31 // A wrapper around extension_function_test_utils::RunFunction that runs with
32 // the associated browser, no flags, and can take stack-allocated arguments. 32 // the associated browser, no flags, and can take stack-allocated arguments.
33 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, 33 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function,
34 const base::ListValue& args); 34 const base::ListValue& args);
35 35
36 // Loads an unpacked extension that is backed by a real directory, allowing
37 // it to be reloaded.
38 const Extension* LoadUnpackedExtension();
39
40 // Tests a developer private function that sets an extension pref.
41 void TestExtensionPrefSetting(
42 UIThreadExtensionFunction* (*create_function)(),
43 bool (*has_pref)(const std::string&, content::BrowserContext*));
44
36 Browser* browser() { return browser_.get(); } 45 Browser* browser() { return browser_.get(); }
37 46
38 private: 47 private:
39 // ExtensionServiceTestBase: 48 // ExtensionServiceTestBase:
40 void SetUp() override; 49 void SetUp() override;
41 void TearDown() override; 50 void TearDown() override;
42 51
43 // The browser (and accompanying window). 52 // The browser (and accompanying window).
44 scoped_ptr<TestBrowserWindow> browser_window_; 53 scoped_ptr<TestBrowserWindow> browser_window_;
45 scoped_ptr<Browser> browser_; 54 scoped_ptr<Browser> browser_;
46 55
56 ScopedVector<TestExtensionDir> test_extension_dirs_;
57
47 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest); 58 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest);
48 }; 59 };
49 60
50 bool DeveloperPrivateApiUnitTest::RunFunction( 61 bool DeveloperPrivateApiUnitTest::RunFunction(
51 const scoped_refptr<UIThreadExtensionFunction>& function, 62 const scoped_refptr<UIThreadExtensionFunction>& function,
52 const base::ListValue& args) { 63 const base::ListValue& args) {
53 return extension_function_test_utils::RunFunction( 64 return extension_function_test_utils::RunFunction(
54 function.get(), 65 function.get(),
55 make_scoped_ptr(args.DeepCopy()), 66 make_scoped_ptr(args.DeepCopy()),
56 browser(), 67 browser(),
57 extension_function_test_utils::NONE); 68 extension_function_test_utils::NONE);
58 } 69 }
59 70
60 void DeveloperPrivateApiUnitTest::SetUp() { 71 const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() {
61 ExtensionServiceTestBase::SetUp();
62 InitializeEmptyExtensionService();
63
64 browser_window_.reset(new TestBrowserWindow());
65 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE);
66 params.type = Browser::TYPE_TABBED;
67 params.window = browser_window_.get();
68 browser_.reset(new Browser(params));
69 }
70
71 void DeveloperPrivateApiUnitTest::TearDown() {
72 browser_.reset();
73 browser_window_.reset();
74 ExtensionServiceTestBase::TearDown();
75 }
76
77 // Test developerPrivate.allowIncognito.
78 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) {
79 const char kManifest[] = 72 const char kManifest[] =
80 "{" 73 "{"
81 " \"name\": \"foo\"," 74 " \"name\": \"foo\","
82 " \"version\": \"1.0\"," 75 " \"version\": \"1.0\","
83 " \"manifest_version\": 2" 76 " \"manifest_version\": 2"
84 "}"; 77 "}";
85 78
86 // Sadly, we need a "real" directory here, because toggling incognito causes 79 test_extension_dirs_.push_back(new TestExtensionDir);
87 // a reload (which needs a path). 80 TestExtensionDir* dir = test_extension_dirs_.back();
88 TestExtensionDir dir; 81 dir->WriteManifest(kManifest);
89 dir.WriteManifest(kManifest);
90 82
91 // TODO(devlin): We should extract out methods to load an unpacked extension 83 // TODO(devlin): We should extract out methods to load an unpacked extension
92 // synchronously. We do it in ExtensionBrowserTest, but that's not helpful 84 // synchronously. We do it in ExtensionBrowserTest, but that's not helpful
93 // for unittests. 85 // for unittests.
94 TestExtensionRegistryObserver registry_observer(registry()); 86 TestExtensionRegistryObserver registry_observer(registry());
95 scoped_refptr<UnpackedInstaller> installer( 87 scoped_refptr<UnpackedInstaller> installer(
96 UnpackedInstaller::Create(service())); 88 UnpackedInstaller::Create(service()));
97 installer->Load(dir.unpacked_path()); 89 installer->Load(dir->unpacked_path());
98 base::FilePath extension_path = 90 base::FilePath extension_path =
99 base::MakeAbsoluteFilePath(dir.unpacked_path()); 91 base::MakeAbsoluteFilePath(dir->unpacked_path());
100 const Extension* extension = nullptr; 92 const Extension* extension = nullptr;
101 do { 93 do {
102 extension = registry_observer.WaitForExtensionLoaded(); 94 extension = registry_observer.WaitForExtensionLoaded();
103 } while (extension->path() != extension_path); 95 } while (extension->path() != extension_path);
104 std::string extension_id = extension->id(); 96 // The fact that unpacked extensions get file access by default is an
97 // irrelevant detail to these tests. Disable it.
98 ExtensionPrefs::Get(browser_context())->SetAllowFileAccess(extension->id(),
99 false);
100 return extension;
101 }
105 102
106 scoped_refptr<api::DeveloperPrivateAllowIncognitoFunction> function( 103 void DeveloperPrivateApiUnitTest::TestExtensionPrefSetting(
107 new api::DeveloperPrivateAllowIncognitoFunction()); 104 UIThreadExtensionFunction* (*create_function)(),
105 bool (*has_pref)(const std::string&, content::BrowserContext*)) {
106 // Sadly, we need a "real" directory here, because toggling incognito causes
107 // a reload (which needs a path).
108 std::string extension_id = LoadUnpackedExtension()->id();
109
110 scoped_refptr<UIThreadExtensionFunction> function(create_function());
108 111
109 base::ListValue enable_args; 112 base::ListValue enable_args;
110 enable_args.AppendString(extension_id); 113 enable_args.AppendString(extension_id);
111 enable_args.AppendBoolean(true); 114 enable_args.AppendBoolean(true);
112 115
113 EXPECT_FALSE(util::IsIncognitoEnabled(extension_id, profile())); 116 EXPECT_FALSE(has_pref(extension_id, profile()));
117
118 // Pref-setting should require a user action.
119 EXPECT_FALSE(RunFunction(function, enable_args));
120 EXPECT_EQ(std::string("This action requires a user gesture."),
121 function->GetError());
122
123 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture;
124 function = create_function();
114 EXPECT_TRUE(RunFunction(function, enable_args)); 125 EXPECT_TRUE(RunFunction(function, enable_args));
115 EXPECT_TRUE(util::IsIncognitoEnabled(extension_id, profile())); 126 EXPECT_TRUE(has_pref(extension_id, profile()));
116 127
117 base::ListValue disable_args; 128 base::ListValue disable_args;
118 disable_args.AppendString(extension_id); 129 disable_args.AppendString(extension_id);
119 disable_args.AppendBoolean(false); 130 disable_args.AppendBoolean(false);
120 function = new api::DeveloperPrivateAllowIncognitoFunction(); 131 function = create_function();
121 EXPECT_TRUE(RunFunction(function, disable_args)); 132 EXPECT_TRUE(RunFunction(function, disable_args));
122 EXPECT_FALSE(util::IsIncognitoEnabled(extension_id, profile())); 133 EXPECT_FALSE(has_pref(extension_id, profile()));
134 }
135
136 void DeveloperPrivateApiUnitTest::SetUp() {
137 ExtensionServiceTestBase::SetUp();
138 InitializeEmptyExtensionService();
139
140 browser_window_.reset(new TestBrowserWindow());
141 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE);
142 params.type = Browser::TYPE_TABBED;
143 params.window = browser_window_.get();
144 browser_.reset(new Browser(params));
145 }
146
147 void DeveloperPrivateApiUnitTest::TearDown() {
148 test_extension_dirs_.clear();
149 browser_.reset();
150 browser_window_.reset();
151 ExtensionServiceTestBase::TearDown();
152 }
153
154 // Test developerPrivate.allowIncognito.
155 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) {
156 auto create_function = []() {
157 return static_cast<UIThreadExtensionFunction*>(
158 new api::DeveloperPrivateAllowIncognitoFunction());
159 };
160 auto has_pref = [](const std::string& extension_id,
161 content::BrowserContext* browser_context) {
162 return util::IsIncognitoEnabled(extension_id, browser_context);
163 };
164 TestExtensionPrefSetting(+create_function, +has_pref);
165 }
166
167 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateReload) {
168 const Extension* extension = LoadUnpackedExtension();
169 std::string extension_id = extension->id();
170 scoped_refptr<UIThreadExtensionFunction> function(
171 new api::DeveloperPrivateReloadFunction());
172 base::ListValue reload_args;
173 reload_args.AppendString(extension_id);
174
175 TestExtensionRegistryObserver registry_observer(registry());
176 EXPECT_TRUE(RunFunction(function, reload_args));
177 const Extension* unloaded_extension =
178 registry_observer.WaitForExtensionUnloaded();
179 EXPECT_EQ(extension, unloaded_extension);
180 const Extension* reloaded_extension =
181 registry_observer.WaitForExtensionLoaded();
182 EXPECT_EQ(extension_id, reloaded_extension->id());
183 }
184
185 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowFileAccess) {
186 auto create_function = []() {
187 return static_cast<UIThreadExtensionFunction*>(
188 new api::DeveloperPrivateAllowFileAccessFunction());
189 };
190 auto has_pref = [](const std::string& extension_id,
191 content::BrowserContext* browser_context) {
192 return util::AllowFileAccess(extension_id, browser_context);
193 };
194 TestExtensionPrefSetting(+create_function, +has_pref);
123 } 195 }
124 196
125 } // namespace extensions 197 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698