1 // Copyright 2015 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 "extensions/common/permissions/permission_message_test_util.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "extensions/common/permissions/permission_message_provider.h"
14 #include "extensions/common/permissions/permissions_data.h"
16 // TODO(treib): Remove the legacy messages once we've fully switched to the new
17 // permission message system.
19 namespace extensions
{
23 class ScopedForcePermissionMessageSystem
{
25 ScopedForcePermissionMessageSystem(ForceForTesting force
) {
26 extensions::ForcePermissionMessageSystemForTesting(force
);
28 ~ScopedForcePermissionMessageSystem() {
29 extensions::ForcePermissionMessageSystemForTesting(
30 ForceForTesting::DONT_FORCE
);
34 PermissionMessageStrings
GetLegacyMessages(
35 const PermissionsData
* permissions_data
) {
36 ScopedForcePermissionMessageSystem
force(ForceForTesting::FORCE_OLD
);
37 return permissions_data
->GetPermissionMessageStrings();
40 PermissionMessageStrings
GetLegacyMessages(const PermissionSet
* permissions
,
41 Manifest::Type extension_type
) {
42 ScopedForcePermissionMessageSystem
force(ForceForTesting::FORCE_OLD
);
43 return PermissionMessageProvider::Get()->GetPermissionMessageStrings(
44 permissions
, extension_type
);
47 PermissionMessageStrings
GetNewMessages(
48 const PermissionsData
* permissions_data
) {
49 ScopedForcePermissionMessageSystem
force(ForceForTesting::FORCE_NEW
);
50 return permissions_data
->GetPermissionMessageStrings();
53 PermissionMessageStrings
GetNewMessages(const PermissionSet
* permissions
,
54 Manifest::Type extension_type
) {
55 ScopedForcePermissionMessageSystem
force(ForceForTesting::FORCE_NEW
);
56 return PermissionMessageProvider::Get()->GetPermissionMessageStrings(
57 permissions
, extension_type
);
60 std::vector
<base::string16
> MakeVectorString16(const base::string16
& str
) {
61 return std::vector
<base::string16
>(1, str
);
64 std::vector
<base::string16
> MakeVectorString16(const base::string16
& str1
,
65 const base::string16
& str2
) {
66 std::vector
<base::string16
> result
;
67 result
.push_back(str1
);
68 result
.push_back(str2
);
72 std::vector
<base::string16
> MakeVectorString16(const std::string
& str1
,
73 const std::string
& str2
) {
74 return MakeVectorString16(base::UTF8ToUTF16(str1
), base::UTF8ToUTF16(str2
));
77 std::vector
<base::string16
> MakeVectorString16(
78 const std::vector
<std::string
>& vec
) {
79 std::vector
<base::string16
> result
;
80 for (const std::string
& msg
: vec
)
81 result
.push_back(base::UTF8ToUTF16(msg
));
85 std::vector
<std::vector
<base::string16
>> MakeVectorVectorString16(
86 const std::vector
<base::string16
>& vec
) {
87 return std::vector
<std::vector
<base::string16
>>(1, vec
);
90 std::vector
<std::vector
<base::string16
>> MakeVectorVectorString16(
91 const std::vector
<std::vector
<std::string
>>& vecs
) {
92 std::vector
<std::vector
<base::string16
>> result
;
93 for (const std::vector
<std::string
>& vec
: vecs
)
94 result
.push_back(MakeVectorString16(vec
));
98 // Returns the vector of messages concatenated into a single string, separated
99 // by newlines, e.g.: "Bar"\n"Baz"\n
100 base::string16
MessagesVectorToString(
101 const std::vector
<base::string16
>& messages
) {
102 if (messages
.empty())
103 return base::ASCIIToUTF16("\n");
104 return base::ASCIIToUTF16("\"") +
105 JoinString(messages
, base::ASCIIToUTF16("\"\n\"")) +
106 base::ASCIIToUTF16("\"\n");
109 base::string16
MessagesToString(const PermissionMessageStrings
& messages
) {
110 std::vector
<base::string16
> messages_vec
;
111 for (const PermissionMessageString
& msg
: messages
)
112 messages_vec
.push_back(msg
.message
);
113 return MessagesVectorToString(messages_vec
);
116 bool CheckThatSubmessagesMatch(
117 const PermissionMessageString
& expected_message
,
118 const std::vector
<base::string16
>& actual_legacy_submessages
,
119 const std::vector
<base::string16
>& actual_submessages
) {
122 std::vector
<base::string16
> expected_sorted
;
123 for (const base::string16
& submessage
: expected_message
.submessages
)
124 expected_sorted
.push_back(submessage
);
125 std::sort(expected_sorted
.begin(), expected_sorted
.end());
127 std::vector
<base::string16
> actual_legacy_sorted(actual_legacy_submessages
);
128 std::sort(actual_legacy_sorted
.begin(), actual_legacy_sorted
.end());
129 if (expected_sorted
!= actual_legacy_sorted
) {
130 // This is always a failure, even within an EXPECT_FALSE.
131 // Message: Expected details for "Message" to be { "Foo" }, but got
132 // { "Bar", "Baz" } in the legacy system
133 ADD_FAILURE() << "Expected details for \"" << expected_message
.message
134 << "\" to be:\n" << MessagesVectorToString(expected_sorted
)
136 << MessagesVectorToString(actual_legacy_sorted
)
137 << "in the legacy system";
141 std::vector
<base::string16
> actual_sorted(actual_submessages
);
142 std::sort(actual_sorted
.begin(), actual_sorted
.end());
143 if (actual_sorted
!= actual_submessages
) {
144 // This is always a failure, even within an EXPECT_FALSE.
145 // Message: Expected submessages for "Message" to be { "Foo" }, but got
147 ADD_FAILURE() << "Expected submessages for \"" << expected_message
.message
148 << "\" to be:\n" << MessagesVectorToString(expected_sorted
)
149 << "But got:\n" << MessagesVectorToString(actual_sorted
);
156 testing::AssertionResult
VerifyHasPermissionMessageImpl(
157 const PermissionMessageString
& expected_message
,
158 const PermissionMessageStrings
& actual_legacy_messages
,
159 const PermissionMessageStrings
& actual_messages
) {
160 auto legacy_message_it
=
161 std::find_if(actual_legacy_messages
.begin(), actual_legacy_messages
.end(),
162 [&expected_message
](const PermissionMessageString
& msg
) {
163 return msg
.message
== expected_message
.message
;
165 bool legacy_found
= legacy_message_it
!= actual_legacy_messages
.end();
168 std::find_if(actual_messages
.begin(), actual_messages
.end(),
169 [&expected_message
](const PermissionMessageString
& msg
) {
170 return msg
.message
== expected_message
.message
;
172 bool found
= message_it
!= actual_messages
.end();
174 if (legacy_found
!= found
) {
175 // This is always a failure, even within an EXPECT_FALSE.
177 << "Mismatch between legacy and new system when looking for \""
178 << expected_message
.message
<< "\".\nLegacy system returned:\n"
179 << MessagesToString(actual_legacy_messages
)
180 << "New system returned:\n" << MessagesToString(actual_messages
);
181 return testing::AssertionFailure();
185 // Message: Expected messages to contain "Foo", but got { "Bar", "Baz" }
186 return testing::AssertionFailure() << "Expected messages to contain \""
187 << expected_message
.message
189 << MessagesToString(actual_messages
);
192 if (!CheckThatSubmessagesMatch(expected_message
,
193 legacy_message_it
->submessages
,
194 message_it
->submessages
)) {
195 return testing::AssertionFailure();
198 // Message: Expected messages NOT to contain "Foo", but got { "Bar", "Baz" }
199 return testing::AssertionSuccess() << "Expected messages NOT to contain \""
200 << expected_message
.message
202 << MessagesToString(actual_messages
);
205 testing::AssertionResult
VerifyPermissionMessagesWithSubmessagesImpl(
206 const PermissionMessageStrings
& expected_messages
,
207 const PermissionMessageStrings
& actual_legacy_messages
,
208 const PermissionMessageStrings
& actual_messages
,
210 if (expected_messages
.size() != actual_legacy_messages
.size()) {
211 // Message: Expected 2 messages { "Bar", "Baz" }, but got 0 {} in the
213 return testing::AssertionFailure()
214 << "Expected " << expected_messages
.size() << " messages:\n"
215 << MessagesToString(expected_messages
) << "But got "
216 << actual_legacy_messages
.size() << " messages:\n"
217 << MessagesToString(actual_legacy_messages
)
218 << "in the legacy system";
221 if (expected_messages
.size() != actual_messages
.size()) {
222 // Message: Expected 2 messages { "Bar", "Baz" }, but got 0 {}
223 return testing::AssertionFailure()
224 << "Expected " << expected_messages
.size() << " messages:\n"
225 << MessagesToString(expected_messages
) << "But got "
226 << actual_messages
.size() << " messages:\n"
227 << MessagesToString(actual_messages
);
231 for (size_t i
= 0; i
< expected_messages
.size(); i
++) {
232 if (expected_messages
[i
].message
!= actual_legacy_messages
[i
].message
) {
233 // Message: Expected messages to be { "Foo" }, but got { "Bar", "Baz" }
234 // in the legacy system
235 return testing::AssertionFailure()
236 << "Expected messages to be:\n"
237 << MessagesToString(expected_messages
) << "But got:\n"
238 << MessagesToString(actual_legacy_messages
)
239 << "in the legacy system";
242 if (expected_messages
[i
].message
!= actual_messages
[i
].message
) {
243 // Message: Expected messages to be { "Foo" }, but got { "Bar", "Baz" }
244 return testing::AssertionFailure()
245 << "Expected messages to be:\n"
246 << MessagesToString(expected_messages
) << "But got:\n"
247 << MessagesToString(actual_messages
);
250 if (!CheckThatSubmessagesMatch(expected_messages
[i
],
251 actual_legacy_messages
[i
].submessages
,
252 actual_messages
[i
].submessages
)) {
253 return testing::AssertionFailure();
257 for (size_t i
= 0; i
< expected_messages
.size(); i
++) {
258 testing::AssertionResult result
= VerifyHasPermissionMessageImpl(
259 expected_messages
[i
], actual_legacy_messages
, actual_messages
);
264 return testing::AssertionSuccess();
269 testing::AssertionResult
VerifyHasPermissionMessage(
270 const PermissionsData
* permissions_data
,
271 const std::string
& expected_message
) {
272 return VerifyHasPermissionMessage(permissions_data
,
273 base::UTF8ToUTF16(expected_message
));
276 testing::AssertionResult
VerifyHasPermissionMessage(
277 const PermissionsData
* permissions_data
,
278 const base::string16
& expected_message
) {
279 return VerifyHasPermissionMessageImpl(
280 PermissionMessageString(expected_message
),
281 GetLegacyMessages(permissions_data
), GetNewMessages(permissions_data
));
284 testing::AssertionResult
VerifyHasPermissionMessage(
285 const PermissionSet
* permissions
,
286 Manifest::Type extension_type
,
287 const std::string
& expected_message
) {
288 return VerifyHasPermissionMessage(permissions
, extension_type
,
289 base::UTF8ToUTF16(expected_message
));
292 testing::AssertionResult
VerifyHasPermissionMessage(
293 const PermissionSet
* permissions
,
294 Manifest::Type extension_type
,
295 const base::string16
& expected_message
) {
296 return VerifyHasPermissionMessageImpl(
297 PermissionMessageString(expected_message
),
298 GetLegacyMessages(permissions
, extension_type
),
299 GetNewMessages(permissions
, extension_type
));
302 testing::AssertionResult
VerifyNoPermissionMessages(
303 const PermissionsData
* permissions_data
) {
304 return VerifyPermissionMessages(permissions_data
,
305 std::vector
<base::string16
>(), true);
308 testing::AssertionResult
VerifyOnePermissionMessage(
309 const PermissionsData
* permissions_data
,
310 const std::string
& expected_message
) {
311 return VerifyOnePermissionMessage(permissions_data
,
312 base::UTF8ToUTF16(expected_message
));
315 testing::AssertionResult
VerifyOnePermissionMessage(
316 const PermissionsData
* permissions_data
,
317 const base::string16
& expected_message
) {
318 return VerifyPermissionMessages(permissions_data
,
319 MakeVectorString16(expected_message
), true);
322 testing::AssertionResult
VerifyOnePermissionMessage(
323 const PermissionSet
* permissions
,
324 Manifest::Type extension_type
,
325 const base::string16
& expected_message
) {
326 return VerifyPermissionMessagesWithSubmessagesImpl(
327 PermissionMessageStrings(1, PermissionMessageString(expected_message
)),
328 GetLegacyMessages(permissions
, extension_type
),
329 GetNewMessages(permissions
, extension_type
), true);
332 testing::AssertionResult
VerifyOnePermissionMessageWithSubmessages(
333 const PermissionsData
* permissions_data
,
334 const std::string
& expected_message
,
335 const std::vector
<std::string
>& expected_submessages
) {
336 return VerifyOnePermissionMessageWithSubmessages(
337 permissions_data
, base::UTF8ToUTF16(expected_message
),
338 MakeVectorString16(expected_submessages
));
341 testing::AssertionResult
VerifyOnePermissionMessageWithSubmessages(
342 const PermissionsData
* permissions_data
,
343 const base::string16
& expected_message
,
344 const std::vector
<base::string16
>& expected_submessages
) {
345 return VerifyPermissionMessagesWithSubmessages(
346 permissions_data
, MakeVectorString16(expected_message
),
347 MakeVectorVectorString16(expected_submessages
), true);
350 testing::AssertionResult
VerifyTwoPermissionMessages(
351 const PermissionsData
* permissions_data
,
352 const std::string
& expected_message_1
,
353 const std::string
& expected_message_2
,
355 return VerifyPermissionMessages(
357 MakeVectorString16(expected_message_1
, expected_message_2
), check_order
);
360 testing::AssertionResult
VerifyTwoPermissionMessages(
361 const PermissionsData
* permissions_data
,
362 const base::string16
& expected_message_1
,
363 const base::string16
& expected_message_2
,
365 return VerifyPermissionMessages(
367 MakeVectorString16(expected_message_1
, expected_message_2
), check_order
);
370 testing::AssertionResult
VerifyPermissionMessages(
371 const PermissionsData
* permissions_data
,
372 const std::vector
<std::string
>& expected_messages
,
374 return VerifyPermissionMessages(
375 permissions_data
, MakeVectorString16(expected_messages
), check_order
);
378 testing::AssertionResult
VerifyPermissionMessages(
379 const PermissionsData
* permissions_data
,
380 const std::vector
<base::string16
>& expected_messages
,
382 return VerifyPermissionMessagesWithSubmessages(
383 permissions_data
, expected_messages
,
384 std::vector
<std::vector
<base::string16
>>(expected_messages
.size()),
388 testing::AssertionResult
VerifyPermissionMessagesWithSubmessages(
389 const PermissionsData
* permissions_data
,
390 const std::vector
<std::string
>& expected_messages
,
391 const std::vector
<std::vector
<std::string
>>& expected_submessages
,
393 return VerifyPermissionMessagesWithSubmessages(
394 permissions_data
, MakeVectorString16(expected_messages
),
395 MakeVectorVectorString16(expected_submessages
), check_order
);
398 testing::AssertionResult
VerifyPermissionMessagesWithSubmessages(
399 const PermissionsData
* permissions_data
,
400 const std::vector
<base::string16
>& expected_messages
,
401 const std::vector
<std::vector
<base::string16
>>& expected_submessages
,
403 CHECK_EQ(expected_messages
.size(), expected_submessages
.size());
404 PermissionMessageStrings expected
;
405 for (size_t i
= 0; i
< expected_messages
.size(); i
++) {
406 expected
.push_back(PermissionMessageString(expected_messages
[i
],
407 expected_submessages
[i
]));
409 return VerifyPermissionMessagesWithSubmessagesImpl(
410 expected
, GetLegacyMessages(permissions_data
),
411 GetNewMessages(permissions_data
), check_order
);
414 } // namespace extensions