1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/memory/ref_counted.h"
7 #include "base/run_loop.h"
8 #include "chrome/browser/extensions/extension_install_prompt.h"
9 #include "chrome/browser/extensions/extension_install_prompt_show_params.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/extension_builder.h"
13 #include "extensions/common/feature_switch.h"
14 #include "extensions/common/permissions/api_permission.h"
15 #include "extensions/common/permissions/api_permission_set.h"
16 #include "extensions/common/permissions/manifest_permission_set.h"
17 #include "extensions/common/permissions/permission_set.h"
18 #include "extensions/common/url_pattern_set.h"
19 #include "extensions/common/value_builder.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace extensions
{
24 void VerifyPromptPermissionsCallback(
25 const base::Closure
& quit_closure
,
26 size_t regular_permissions_count
,
27 size_t withheld_permissions_count
,
28 ExtensionInstallPromptShowParams
* params
,
29 ExtensionInstallPrompt::Delegate
* delegate
,
30 scoped_refptr
<ExtensionInstallPrompt::Prompt
> install_prompt
) {
31 ASSERT_TRUE(install_prompt
.get());
32 EXPECT_EQ(regular_permissions_count
,
33 install_prompt
->GetPermissionCount(
34 ExtensionInstallPrompt::REGULAR_PERMISSIONS
));
35 EXPECT_EQ(withheld_permissions_count
,
36 install_prompt
->GetPermissionCount(
37 ExtensionInstallPrompt::WITHHELD_PERMISSIONS
));
41 TEST(ExtensionInstallPromptUnittest
, PromptShowsPermissionWarnings
) {
42 content::TestBrowserThreadBundle thread_bundle
;
43 APIPermissionSet api_permissions
;
44 api_permissions
.insert(APIPermission::kTab
);
45 scoped_refptr
<const PermissionSet
> permission_set
=
46 new PermissionSet(api_permissions
, ManifestPermissionSet(),
47 URLPatternSet(), URLPatternSet());
48 scoped_refptr
<const Extension
> extension
=
49 ExtensionBuilder().SetManifest(
50 DictionaryBuilder().Set("name", "foo")
51 .Set("version", "1.0")
52 .Set("manifest_version", 2)
53 .Set("description", "Random Ext")).Build();
54 ExtensionInstallPrompt
prompt(nullptr /* no web contents in this test */);
55 base::RunLoop run_loop
;
56 prompt
.set_callback_for_test(
57 base::Bind(&VerifyPromptPermissionsCallback
,
58 run_loop
.QuitClosure(),
59 1u, // |regular_permissions_count|.
60 0u)); // |withheld_permissions_count|.
61 prompt
.ConfirmPermissions(nullptr, // no delegate
63 permission_set
.get());
67 TEST(ExtensionInstallPromptUnittest
, PromptShowsWithheldPermissions
) {
68 content::TestBrowserThreadBundle thread_bundle
;
70 // Enable consent flag so that <all_hosts> permissions get withheld.
71 FeatureSwitch::ScopedOverride
enable_scripts_switch(
72 FeatureSwitch::scripts_require_action(), true);
74 scoped_refptr
<const Extension
> extension
=
75 ExtensionBuilder().SetManifest(
76 DictionaryBuilder().Set("name", "foo")
77 .Set("version", "1.0")
78 .Set("manifest_version", 2)
79 .Set("description", "Random Ext")
81 ListBuilder().Append("http://*/*")
82 .Append("http://www.google.com/")
83 .Append("tabs"))).Build();
84 ExtensionInstallPrompt
prompt(nullptr /* no web contents in this test */);
85 base::RunLoop run_loop
;
87 // We expect <all_hosts> to be withheld, but http://www.google.com/ and tabs
88 // permissions should be granted as regular permissions.
89 prompt
.ConfirmInstall(
92 base::Bind(&VerifyPromptPermissionsCallback
,
93 run_loop
.QuitClosure(),
94 2u, // |regular_permissions_count|.
95 1u)); // |withheld_permissions_count|.
99 TEST(ExtensionInstallPromptUnittest
, DelegatedPromptShowsOptionalPermissions
) {
100 content::TestBrowserThreadBundle thread_bundle
;
101 scoped_refptr
<const Extension
> extension
=
102 ExtensionBuilder().SetManifest(
103 DictionaryBuilder().Set("name", "foo")
104 .Set("version", "1.0")
105 .Set("manifest_version", 2)
106 .Set("description", "Random Ext")
108 ListBuilder().Append("clipboardRead"))
109 .Set("optional_permissions",
110 ListBuilder().Append("tabs"))).Build();
111 ExtensionInstallPrompt
prompt(nullptr /* no web contents in this test */);
112 base::RunLoop run_loop
;
113 prompt
.set_callback_for_test(
114 base::Bind(&VerifyPromptPermissionsCallback
,
115 run_loop
.QuitClosure(),
116 2u, // |regular_permissions_count|.
117 0u)); // |withheld_permissions_count|.
118 prompt
.ConfirmPermissionsForDelegatedInstall(nullptr, // no delegate
125 } // namespace extensions