OLD | NEW |
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" | |
13 #include "chrome/browser/extensions/test_extension_system.h" | 11 #include "chrome/browser/extensions/test_extension_system.h" |
14 #include "chrome/browser/ui/webui/extensions/extension_settings_handler.h" | 12 #include "chrome/browser/ui/webui/extensions/extension_settings_handler.h" |
15 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
18 #include "extensions/browser/extension_registry.h" | |
19 #include "extensions/browser/management_policy.h" | 16 #include "extensions/browser/management_policy.h" |
20 #include "extensions/common/constants.h" | 17 #include "extensions/common/constants.h" |
21 #include "extensions/common/extension.h" | 18 #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" | |
26 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
27 | 20 |
28 #if defined(OS_CHROMEOS) | 21 #if defined(OS_CHROMEOS) |
29 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" | 22 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
30 #include "chrome/browser/chromeos/settings/cros_settings.h" | 23 #include "chrome/browser/chromeos/settings/cros_settings.h" |
31 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 24 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
32 #endif | 25 #endif |
33 | 26 |
34 namespace extensions { | 27 namespace extensions { |
35 | 28 |
36 namespace { | |
37 const char kAllHostsPermission[] = "*://*/*"; | |
38 } | |
39 | |
40 class ExtensionUITest : public testing::Test { | 29 class ExtensionUITest : public testing::Test { |
41 public: | 30 public: |
42 ExtensionUITest() | 31 ExtensionUITest() |
43 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 32 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
44 file_thread_(content::BrowserThread::FILE, &message_loop_) {} | 33 file_thread_(content::BrowserThread::FILE, &message_loop_) {} |
45 | 34 |
46 protected: | 35 protected: |
47 virtual void SetUp() OVERRIDE { | 36 virtual void SetUp() OVERRIDE { |
48 // Create an ExtensionService and ManagementPolicy to inject into the | 37 // Create an ExtensionService and ManagementPolicy to inject into the |
49 // ExtensionSettingsHandler. | 38 // ExtensionSettingsHandler. |
(...skipping 19 matching lines...) Expand all Loading... |
69 const base::FilePath& path, | 58 const base::FilePath& path, |
70 std::string *error) { | 59 std::string *error) { |
71 base::Value* value; | 60 base::Value* value; |
72 | 61 |
73 JSONFileValueSerializer serializer(path); | 62 JSONFileValueSerializer serializer(path); |
74 value = serializer.Deserialize(NULL, error); | 63 value = serializer.Deserialize(NULL, error); |
75 | 64 |
76 return static_cast<base::DictionaryValue*>(value); | 65 return static_cast<base::DictionaryValue*>(value); |
77 } | 66 } |
78 | 67 |
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 | |
99 base::DictionaryValue* CreateExtensionDetailViewFromPath( | 68 base::DictionaryValue* CreateExtensionDetailViewFromPath( |
100 const base::FilePath& extension_path, | 69 const base::FilePath& extension_path, |
101 const std::vector<ExtensionPage>& pages, | 70 const std::vector<ExtensionPage>& pages, |
102 Manifest::Location location) { | 71 Manifest::Location location) { |
103 std::string error; | 72 std::string error; |
104 | 73 |
105 base::FilePath manifest_path = extension_path.Append(kManifestFilename); | 74 base::FilePath manifest_path = extension_path.Append(kManifestFilename); |
106 scoped_ptr<base::DictionaryValue> extension_data(DeserializeJSONTestData( | 75 scoped_ptr<base::DictionaryValue> extension_data(DeserializeJSONTestData( |
107 manifest_path, &error)); | 76 manifest_path, &error)); |
108 EXPECT_EQ("", error); | 77 EXPECT_EQ("", error); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 !field.IsAtEnd(); field.Advance()) { | 109 !field.IsAtEnd(); field.Advance()) { |
141 const base::Value* expected_value = &field.value(); | 110 const base::Value* expected_value = &field.value(); |
142 base::Value* actual_value = NULL; | 111 base::Value* actual_value = NULL; |
143 EXPECT_TRUE(actual_output_data->Get(field.key(), &actual_value)) << | 112 EXPECT_TRUE(actual_output_data->Get(field.key(), &actual_value)) << |
144 field.key() + " is missing" + paths_details; | 113 field.key() + " is missing" + paths_details; |
145 EXPECT_TRUE(expected_value->Equals(actual_value)) << field.key() + | 114 EXPECT_TRUE(expected_value->Equals(actual_value)) << field.key() + |
146 paths_details; | 115 paths_details; |
147 } | 116 } |
148 } | 117 } |
149 | 118 |
150 Profile* profile() { return profile_.get(); } | |
151 ExtensionSettingsHandler* handler() { return handler_.get(); } | |
152 | |
153 base::MessageLoop message_loop_; | 119 base::MessageLoop message_loop_; |
154 content::TestBrowserThread ui_thread_; | 120 content::TestBrowserThread ui_thread_; |
155 content::TestBrowserThread file_thread_; | 121 content::TestBrowserThread file_thread_; |
156 scoped_ptr<TestingProfile> profile_; | 122 scoped_ptr<TestingProfile> profile_; |
157 ExtensionService* extension_service_; | 123 ExtensionService* extension_service_; |
158 ManagementPolicy* management_policy_; | 124 ManagementPolicy* management_policy_; |
159 scoped_ptr<ExtensionSettingsHandler> handler_; | 125 scoped_ptr<ExtensionSettingsHandler> handler_; |
160 | 126 |
161 #if defined OS_CHROMEOS | 127 #if defined OS_CHROMEOS |
162 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 128 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 scoped_ptr<base::DictionaryValue> extension_details( | 271 scoped_ptr<base::DictionaryValue> extension_details( |
306 CreateExtensionDetailViewFromPath( | 272 CreateExtensionDetailViewFromPath( |
307 extension_path, pages, Manifest::UNPACKED)); | 273 extension_path, pages, Manifest::UNPACKED)); |
308 | 274 |
309 base::FilePath::StringType ui_path; | 275 base::FilePath::StringType ui_path; |
310 | 276 |
311 EXPECT_TRUE(extension_details->GetString("path", &ui_path)); | 277 EXPECT_TRUE(extension_details->GetString("path", &ui_path)); |
312 EXPECT_EQ(extension_path, base::FilePath(ui_path)); | 278 EXPECT_EQ(extension_path, base::FilePath(ui_path)); |
313 } | 279 } |
314 | 280 |
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 | |
389 } // namespace extensions | 281 } // namespace extensions |
OLD | NEW |