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 PermissionMessages
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_
->GetPermissionMessages(
40 message_provider_
->GetAllPermissionIDs(permission_set
.get(), type
));
44 scoped_ptr
<ChromePermissionMessageProvider
> message_provider_
;
46 DISALLOW_COPY_AND_ASSIGN(ChromePermissionMessageProviderUnittest
);
49 // Checks that if an app has a superset and a subset permission, only the
50 // superset permission message is displayed if they are both present.
51 TEST_F(ChromePermissionMessageProviderUnittest
,
52 SupersetOverridesSubsetPermission
) {
54 APIPermissionSet permissions
;
55 permissions
.insert(APIPermission::kTab
);
56 PermissionMessages messages
=
57 GetMessages(permissions
, Manifest::TYPE_PLATFORM_APP
);
58 ASSERT_EQ(1U, messages
.size());
60 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
61 messages
.front().message());
64 APIPermissionSet permissions
;
65 permissions
.insert(APIPermission::kTopSites
);
66 PermissionMessages messages
=
67 GetMessages(permissions
, Manifest::TYPE_PLATFORM_APP
);
68 ASSERT_EQ(1U, messages
.size());
69 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_TOPSITES
),
70 messages
.front().message());
73 APIPermissionSet permissions
;
74 permissions
.insert(APIPermission::kTab
);
75 permissions
.insert(APIPermission::kTopSites
);
76 PermissionMessages messages
=
77 GetMessages(permissions
, Manifest::TYPE_PLATFORM_APP
);
78 ASSERT_EQ(1U, messages
.size());
80 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
81 messages
.front().message());
85 // Checks that when permissions are merged into a single message, their details
86 // are merged as well.
87 TEST_F(ChromePermissionMessageProviderUnittest
,
88 WarningsAndDetailsCoalesceTogether
) {
89 // kTab and kTopSites should be merged into a single message.
90 APIPermissionSet permissions
;
91 permissions
.insert(APIPermission::kTab
);
92 permissions
.insert(APIPermission::kTopSites
);
93 // The USB device permission message has a non-empty details string.
94 scoped_ptr
<UsbDevicePermission
> usb(new UsbDevicePermission(
95 PermissionsInfo::GetInstance()->GetByID(APIPermission::kUsbDevice
)));
96 scoped_ptr
<base::ListValue
> devices_list(new base::ListValue());
98 UsbDevicePermissionData(0x02ad, 0x138c, -1).ToValue().release());
100 UsbDevicePermissionData(0x02ad, 0x138d, -1).ToValue().release());
101 ASSERT_TRUE(usb
->FromValue(devices_list
.get(), nullptr, nullptr));
102 permissions
.insert(usb
.release());
104 PermissionMessages messages
=
105 GetMessages(permissions
, Manifest::TYPE_EXTENSION
);
107 ASSERT_EQ(2U, messages
.size());
108 auto it
= messages
.begin();
109 const PermissionMessage
& message0
= *it
++;
111 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
113 EXPECT_TRUE(message0
.submessages().empty());
114 const PermissionMessage
& message1
= *it
++;
116 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST
),
118 EXPECT_FALSE(message1
.submessages().empty());
121 } // namespace extensions