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.
5 #include "chrome/common/extensions/permissions/chrome_permission_message_provider.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string16.h"
11 #include "base/values.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "extensions/common/permissions/permission_set.h"
14 #include "extensions/common/permissions/permissions_info.h"
15 #include "extensions/common/permissions/usb_device_permission.h"
16 #include "extensions/common/url_pattern_set.h"
17 #include "extensions/strings/grit/extensions_strings.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/base/l10n/l10n_util.h"
21 namespace extensions
{
23 // Tests that ChromePermissionMessageProvider provides correct permission
24 // messages for given permissions.
25 // NOTE: No extensions are created as part of these tests. Integration tests
26 // that test the messages are generated properly for extensions can be found in
27 // chrome/browser/extensions/permission_messages_unittest.cc.
28 class ChromePermissionMessageProviderUnittest
: public testing::Test
{
30 ChromePermissionMessageProviderUnittest()
31 : message_provider_(new ChromePermissionMessageProvider()) {}
32 ~ChromePermissionMessageProviderUnittest() override
{}
35 std::vector
<base::string16
> GetMessages(const APIPermissionSet
& permissions
,
36 Manifest::Type type
) {
37 scoped_refptr
<const PermissionSet
> permission_set
= new PermissionSet(
38 permissions
, ManifestPermissionSet(), URLPatternSet(), URLPatternSet());
39 return message_provider_
->GetLegacyWarningMessages(permission_set
.get(),
43 std::vector
<base::string16
> GetDetails(const APIPermissionSet
& permissions
,
44 Manifest::Type type
) {
45 scoped_refptr
<const PermissionSet
> permission_set
= new PermissionSet(
46 permissions
, ManifestPermissionSet(), URLPatternSet(), URLPatternSet());
47 return message_provider_
->GetLegacyWarningMessagesDetails(
48 permission_set
.get(), type
);
52 scoped_ptr
<ChromePermissionMessageProvider
> message_provider_
;
54 DISALLOW_COPY_AND_ASSIGN(ChromePermissionMessageProviderUnittest
);
57 // Checks that if an app has a superset and a subset permission, only the
58 // superset permission message is displayed if they are both present.
59 TEST_F(ChromePermissionMessageProviderUnittest
,
60 SupersetOverridesSubsetPermission
) {
61 APIPermissionSet permissions
;
62 std::vector
<base::string16
> messages
;
65 permissions
.insert(APIPermission::kTab
);
66 messages
= GetMessages(permissions
, Manifest::TYPE_PLATFORM_APP
);
67 ASSERT_EQ(1U, messages
.size());
69 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
73 permissions
.insert(APIPermission::kTopSites
);
74 messages
= GetMessages(permissions
, Manifest::TYPE_PLATFORM_APP
);
75 ASSERT_EQ(1U, messages
.size());
76 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_TOPSITES
),
80 permissions
.insert(APIPermission::kTab
);
81 permissions
.insert(APIPermission::kTopSites
);
82 messages
= GetMessages(permissions
, Manifest::TYPE_PLATFORM_APP
);
83 ASSERT_EQ(1U, messages
.size());
85 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
89 // Checks that when permissions are merged into a single message, their details
90 // are merged as well.
91 TEST_F(ChromePermissionMessageProviderUnittest
,
92 WarningsAndDetailsCoalesceTogether
) {
93 // kTab and kTopSites should be merged into a single message.
94 APIPermissionSet permissions
;
95 permissions
.insert(APIPermission::kTab
);
96 permissions
.insert(APIPermission::kTopSites
);
97 // The USB device permission message has a non-empty details string.
98 scoped_ptr
<UsbDevicePermission
> usb(new UsbDevicePermission(
99 PermissionsInfo::GetInstance()->GetByID(APIPermission::kUsbDevice
)));
100 scoped_ptr
<base::ListValue
> devices_list(new base::ListValue());
101 devices_list
->Append(
102 UsbDevicePermissionData(0x02ad, 0x138c, -1).ToValue().release());
103 devices_list
->Append(
104 UsbDevicePermissionData(0x02ad, 0x138d, -1).ToValue().release());
105 ASSERT_TRUE(usb
->FromValue(devices_list
.get(), nullptr, nullptr));
106 permissions
.insert(usb
.release());
108 std::vector
<base::string16
> messages
=
109 GetMessages(permissions
, Manifest::TYPE_EXTENSION
);
110 std::vector
<base::string16
> details
=
111 GetDetails(permissions
, Manifest::TYPE_EXTENSION
);
113 ASSERT_EQ(2U, messages
.size());
114 ASSERT_EQ(messages
.size(), details
.size());
116 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
118 EXPECT_TRUE(details
[0].empty());
120 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST
),
122 EXPECT_FALSE(details
[1].empty());
125 } // namespace extensions