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

Side by Side Diff: chrome/browser/extensions/extension_ui_unittest.cc

Issue 487913004: Only show the checkbox for all urls when the switch is on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add Test Created 6 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/ui/webui/extensions/extension_settings_handler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/command_line.h" 5 #include "base/command_line.h"
6 #include "base/json/json_file_value_serializer.h" 6 #include "base/json/json_file_value_serializer.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "chrome/browser/extensions/extension_service.h" 10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_util.h"
12 #include "chrome/browser/extensions/permissions_updater.h"
11 #include "chrome/browser/extensions/test_extension_system.h" 13 #include "chrome/browser/extensions/test_extension_system.h"
12 #include "chrome/browser/ui/webui/extensions/extension_settings_handler.h" 14 #include "chrome/browser/ui/webui/extensions/extension_settings_handler.h"
13 #include "chrome/common/chrome_paths.h" 15 #include "chrome/common/chrome_paths.h"
14 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread.h" 17 #include "content/public/test/test_browser_thread.h"
18 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/management_policy.h" 19 #include "extensions/browser/management_policy.h"
17 #include "extensions/common/constants.h" 20 #include "extensions/common/constants.h"
18 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_builder.h"
23 #include "extensions/common/feature_switch.h"
24 #include "extensions/common/id_util.h"
25 #include "extensions/common/value_builder.h"
19 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
20 27
21 #if defined(OS_CHROMEOS) 28 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" 29 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h" 30 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/settings/device_settings_service.h" 31 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 #endif 32 #endif
26 33
27 namespace extensions { 34 namespace extensions {
28 35
36 namespace {
37 const char kAllHostsPermission[] = "*://*/*";
38 }
39
29 class ExtensionUITest : public testing::Test { 40 class ExtensionUITest : public testing::Test {
30 public: 41 public:
31 ExtensionUITest() 42 ExtensionUITest()
32 : ui_thread_(content::BrowserThread::UI, &message_loop_), 43 : ui_thread_(content::BrowserThread::UI, &message_loop_),
33 file_thread_(content::BrowserThread::FILE, &message_loop_) {} 44 file_thread_(content::BrowserThread::FILE, &message_loop_) {}
34 45
35 protected: 46 protected:
36 virtual void SetUp() OVERRIDE { 47 virtual void SetUp() OVERRIDE {
37 // Create an ExtensionService and ManagementPolicy to inject into the 48 // Create an ExtensionService and ManagementPolicy to inject into the
38 // ExtensionSettingsHandler. 49 // ExtensionSettingsHandler.
(...skipping 19 matching lines...) Expand all
58 const base::FilePath& path, 69 const base::FilePath& path,
59 std::string *error) { 70 std::string *error) {
60 base::Value* value; 71 base::Value* value;
61 72
62 JSONFileValueSerializer serializer(path); 73 JSONFileValueSerializer serializer(path);
63 value = serializer.Deserialize(NULL, error); 74 value = serializer.Deserialize(NULL, error);
64 75
65 return static_cast<base::DictionaryValue*>(value); 76 return static_cast<base::DictionaryValue*>(value);
66 } 77 }
67 78
79 const Extension* CreateExtension(const std::string& name,
80 ListBuilder& permissions) {
81 const std::string kId = id_util::GenerateId(name);
82 scoped_refptr<const Extension> extension =
83 ExtensionBuilder().SetManifest(
84 DictionaryBuilder()
85 .Set("name", name)
86 .Set("description", "an extension")
87 .Set("manifest_version", 2)
88 .Set("version", "1.0.0")
89 .Set("permissions", permissions))
90 .SetLocation(Manifest::INTERNAL)
91 .SetID(kId)
92 .Build();
93
94 ExtensionRegistry::Get(profile())->AddEnabled(extension);
95 PermissionsUpdater(profile()).InitializePermissions(extension);
96 return extension;
97 }
98
68 base::DictionaryValue* CreateExtensionDetailViewFromPath( 99 base::DictionaryValue* CreateExtensionDetailViewFromPath(
69 const base::FilePath& extension_path, 100 const base::FilePath& extension_path,
70 const std::vector<ExtensionPage>& pages, 101 const std::vector<ExtensionPage>& pages,
71 Manifest::Location location) { 102 Manifest::Location location) {
72 std::string error; 103 std::string error;
73 104
74 base::FilePath manifest_path = extension_path.Append(kManifestFilename); 105 base::FilePath manifest_path = extension_path.Append(kManifestFilename);
75 scoped_ptr<base::DictionaryValue> extension_data(DeserializeJSONTestData( 106 scoped_ptr<base::DictionaryValue> extension_data(DeserializeJSONTestData(
76 manifest_path, &error)); 107 manifest_path, &error));
77 EXPECT_EQ("", error); 108 EXPECT_EQ("", error);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 !field.IsAtEnd(); field.Advance()) { 140 !field.IsAtEnd(); field.Advance()) {
110 const base::Value* expected_value = &field.value(); 141 const base::Value* expected_value = &field.value();
111 base::Value* actual_value = NULL; 142 base::Value* actual_value = NULL;
112 EXPECT_TRUE(actual_output_data->Get(field.key(), &actual_value)) << 143 EXPECT_TRUE(actual_output_data->Get(field.key(), &actual_value)) <<
113 field.key() + " is missing" + paths_details; 144 field.key() + " is missing" + paths_details;
114 EXPECT_TRUE(expected_value->Equals(actual_value)) << field.key() + 145 EXPECT_TRUE(expected_value->Equals(actual_value)) << field.key() +
115 paths_details; 146 paths_details;
116 } 147 }
117 } 148 }
118 149
150 Profile* profile() { return profile_.get(); }
151 ExtensionSettingsHandler* handler() { return handler_.get(); }
152
119 base::MessageLoop message_loop_; 153 base::MessageLoop message_loop_;
120 content::TestBrowserThread ui_thread_; 154 content::TestBrowserThread ui_thread_;
121 content::TestBrowserThread file_thread_; 155 content::TestBrowserThread file_thread_;
122 scoped_ptr<TestingProfile> profile_; 156 scoped_ptr<TestingProfile> profile_;
123 ExtensionService* extension_service_; 157 ExtensionService* extension_service_;
124 ManagementPolicy* management_policy_; 158 ManagementPolicy* management_policy_;
125 scoped_ptr<ExtensionSettingsHandler> handler_; 159 scoped_ptr<ExtensionSettingsHandler> handler_;
126 160
127 #if defined OS_CHROMEOS 161 #if defined OS_CHROMEOS
128 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 162 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 scoped_ptr<base::DictionaryValue> extension_details( 305 scoped_ptr<base::DictionaryValue> extension_details(
272 CreateExtensionDetailViewFromPath( 306 CreateExtensionDetailViewFromPath(
273 extension_path, pages, Manifest::UNPACKED)); 307 extension_path, pages, Manifest::UNPACKED));
274 308
275 base::FilePath::StringType ui_path; 309 base::FilePath::StringType ui_path;
276 310
277 EXPECT_TRUE(extension_details->GetString("path", &ui_path)); 311 EXPECT_TRUE(extension_details->GetString("path", &ui_path));
278 EXPECT_EQ(extension_path, base::FilePath(ui_path)); 312 EXPECT_EQ(extension_path, base::FilePath(ui_path));
279 } 313 }
280 314
315 // Test that the all_urls checkbox only shows up for extensions that want all
316 // urls, and only when the switch is on.
317 TEST_F(ExtensionUITest, ExtensionUIAllUrlsCheckbox) {
318 // Start with the switch enabled.
319 scoped_ptr<FeatureSwitch::ScopedOverride> enable_scripts_switch(
320 new FeatureSwitch::ScopedOverride(
321 FeatureSwitch::scripts_require_action(), true));
322 // Two extensions - one with all urls, one without.
323 const Extension* all_urls_extension =
324 CreateExtension("all_urls",
325 ListBuilder().Append(kAllHostsPermission).Pass());
326 const Extension* no_urls_extension =
327 CreateExtension("no urls", ListBuilder().Pass());
328
329 scoped_ptr<base::DictionaryValue> value(
330 handler()->CreateExtensionDetailValue(
331 all_urls_extension, std::vector<ExtensionPage>(), NULL));
332 bool result = false;
333 const std::string kWantsAllUrls = "wantsAllUrls";
334 const std::string kAllowAllUrls = "allowAllUrls";
335
336 // The extension should want all urls, but not currently have it.
337 EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
338 EXPECT_TRUE(result);
339 EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
340 EXPECT_FALSE(result);
341
342 // Give the extension all urls.
343 util::SetAllowedScriptingOnAllUrls(
344 all_urls_extension->id(), profile(), true);
345
346 // Now the extension should both want and have all urls.
347 value.reset(handler()->CreateExtensionDetailValue(
348 all_urls_extension, std::vector<ExtensionPage>(), NULL));
349 EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
350 EXPECT_TRUE(result);
351 EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
352 EXPECT_TRUE(result);
353
354 // The other extension should neither want nor have all urls.
355 value.reset(handler()->CreateExtensionDetailValue(
356 no_urls_extension, std::vector<ExtensionPage>(), NULL));
357 EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
358 EXPECT_FALSE(result);
359 EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
360 EXPECT_FALSE(result);
361
362 // Turn off the switch and load another extension (so permissions are
363 // re-initialized).
364 enable_scripts_switch.reset();
365
366 // Even though the extension has the all urls preference, the checkbox
367 // shouldn't show up with the switch off.
368 value.reset(handler()->CreateExtensionDetailValue(
369 all_urls_extension, std::vector<ExtensionPage>(), NULL));
370 EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
371 EXPECT_FALSE(result);
372 EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
373 EXPECT_TRUE(result);
374
375 // Load another extension with all urls (so permissions get re-init'd).
376 all_urls_extension = CreateExtension(
377 "all_urls_II", ListBuilder().Append(kAllHostsPermission).Pass());
378
379 // Even though the extension has all_urls permission, the checkbox shouldn't
380 // show up without the switch.
381 value.reset(handler()->CreateExtensionDetailValue(
382 all_urls_extension, std::vector<ExtensionPage>(), NULL));
383 EXPECT_TRUE(value->GetBoolean(kWantsAllUrls, &result));
384 EXPECT_FALSE(result);
385 EXPECT_TRUE(value->GetBoolean(kAllowAllUrls, &result));
386 EXPECT_FALSE(result);
387 }
388
281 } // namespace extensions 389 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/webui/extensions/extension_settings_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698