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

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

Powered by Google App Engine
This is Rietveld 408576698