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
<PermissionSet
> permission_set
=
46 new PermissionSet(api_permissions
,
47 ManifestPermissionSet(),
50 scoped_refptr
<const Extension
> extension
=
51 ExtensionBuilder().SetManifest(
52 DictionaryBuilder().Set("name", "foo")
53 .Set("version", "1.0")
54 .Set("manifest_version", 2)
55 .Set("description", "Random Ext")).Build();
56 ExtensionInstallPrompt
prompt(nullptr /* no web contents in this test */);
57 base::RunLoop run_loop
;
58 prompt
.set_callback_for_test(
59 base::Bind(&VerifyPromptPermissionsCallback
,
60 run_loop
.QuitClosure(),
61 1u, // |regular_permissions_count|.
62 0u)); // |withheld_permissions_count|.
63 prompt
.ConfirmPermissions(nullptr, // no delegate
65 permission_set
.get());
69 TEST(ExtensionInstallPromptUnittest
, PromptShowsWithheldPermissions
) {
70 content::TestBrowserThreadBundle thread_bundle
;
72 // Enable consent flag so that <all_hosts> permissions get withheld.
73 FeatureSwitch::ScopedOverride
enable_scripts_switch(
74 FeatureSwitch::scripts_require_action(), true);
76 scoped_refptr
<const Extension
> extension
=
77 ExtensionBuilder().SetManifest(
78 DictionaryBuilder().Set("name", "foo")
79 .Set("version", "1.0")
80 .Set("manifest_version", 2)
81 .Set("description", "Random Ext")
83 ListBuilder().Append("http://*/*")
84 .Append("http://www.google.com/")
85 .Append("tabs"))).Build();
86 ExtensionInstallPrompt
prompt(nullptr /* no web contents in this test */);
87 base::RunLoop run_loop
;
89 // We expect <all_hosts> to be withheld, but http://www.google.com/ and tabs
90 // permissions should be granted as regular permissions.
91 prompt
.ConfirmInstall(
94 base::Bind(&VerifyPromptPermissionsCallback
,
95 run_loop
.QuitClosure(),
96 2u, // |regular_permissions_count|.
97 1u)); // |withheld_permissions_count|.
101 TEST(ExtensionInstallPromptUnittest
, DelegatedPromptShowsOptionalPermissions
) {
102 content::TestBrowserThreadBundle thread_bundle
;
103 scoped_refptr
<const Extension
> extension
=
104 ExtensionBuilder().SetManifest(
105 DictionaryBuilder().Set("name", "foo")
106 .Set("version", "1.0")
107 .Set("manifest_version", 2)
108 .Set("description", "Random Ext")
110 ListBuilder().Append("clipboardRead"))
111 .Set("optional_permissions",
112 ListBuilder().Append("tabs"))).Build();
113 ExtensionInstallPrompt
prompt(nullptr /* no web contents in this test */);
114 base::RunLoop run_loop
;
115 prompt
.set_callback_for_test(
116 base::Bind(&VerifyPromptPermissionsCallback
,
117 run_loop
.QuitClosure(),
118 2u, // |regular_permissions_count|.
119 0u)); // |withheld_permissions_count|.
120 prompt
.ConfirmPermissionsForDelegatedInstall(nullptr, // no delegate
127 } // namespace extensions