Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / permissions / permission_message_test_util.cc
blob2cf3a5d9b67241250f995fca5dbe3d06d8e4d5e7
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"
7 #include <algorithm>
8 #include <iterator>
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 namespace extensions {
18 namespace {
20 PermissionMessages GetMessages(const PermissionSet* permissions,
21 Manifest::Type extension_type) {
22 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
23 return provider->GetPermissionMessages(
24 provider->GetAllPermissionIDs(permissions, extension_type));
27 std::vector<base::string16> MakeVectorString16(const base::string16& str) {
28 return std::vector<base::string16>(1, str);
31 std::vector<base::string16> MakeVectorString16(const base::string16& str1,
32 const base::string16& str2) {
33 std::vector<base::string16> result;
34 result.push_back(str1);
35 result.push_back(str2);
36 return result;
39 std::vector<base::string16> MakeVectorString16(const std::string& str1,
40 const std::string& str2) {
41 return MakeVectorString16(base::UTF8ToUTF16(str1), base::UTF8ToUTF16(str2));
44 std::vector<base::string16> MakeVectorString16(
45 const std::vector<std::string>& vec) {
46 std::vector<base::string16> result;
47 for (const std::string& msg : vec)
48 result.push_back(base::UTF8ToUTF16(msg));
49 return result;
52 std::vector<std::vector<base::string16>> MakeVectorVectorString16(
53 const std::vector<base::string16>& vec) {
54 return std::vector<std::vector<base::string16>>(1, vec);
57 std::vector<std::vector<base::string16>> MakeVectorVectorString16(
58 const std::vector<std::vector<std::string>>& vecs) {
59 std::vector<std::vector<base::string16>> result;
60 for (const std::vector<std::string>& vec : vecs)
61 result.push_back(MakeVectorString16(vec));
62 return result;
65 // Returns the vector of messages concatenated into a single string, separated
66 // by newlines, e.g.: "Bar"\n"Baz"\n
67 base::string16 MessagesVectorToString(
68 const std::vector<base::string16>& messages) {
69 if (messages.empty())
70 return base::ASCIIToUTF16("\n");
71 return base::ASCIIToUTF16("\"") +
72 base::JoinString(messages, base::ASCIIToUTF16("\"\n\"")) +
73 base::ASCIIToUTF16("\"\n");
76 base::string16 MessagesToString(const PermissionMessages& messages) {
77 std::vector<base::string16> messages_vec;
78 for (const PermissionMessage& msg : messages)
79 messages_vec.push_back(msg.message());
80 return MessagesVectorToString(messages_vec);
83 bool CheckThatSubmessagesMatch(
84 const base::string16& message,
85 const std::vector<base::string16>& expected_submessages,
86 const std::vector<base::string16>& actual_submessages) {
87 bool result = true;
89 std::vector<base::string16> expected_sorted(expected_submessages);
90 std::sort(expected_sorted.begin(), expected_sorted.end());
92 std::vector<base::string16> actual_sorted(actual_submessages);
93 std::sort(actual_sorted.begin(), actual_sorted.end());
94 if (expected_sorted != actual_sorted) {
95 // This is always a failure, even within an EXPECT_FALSE.
96 // Message: Expected submessages for "Message" to be { "Foo" }, but got
97 // { "Bar", "Baz" }
98 ADD_FAILURE() << "Expected submessages for \"" << message << "\" to be:\n"
99 << MessagesVectorToString(expected_sorted) << "But got:\n"
100 << MessagesVectorToString(actual_sorted);
101 result = false;
104 return result;
107 testing::AssertionResult VerifyHasPermissionMessageImpl(
108 const base::string16& expected_message,
109 const std::vector<base::string16>& expected_submessages,
110 const PermissionMessages& actual_messages) {
111 auto message_it =
112 std::find_if(actual_messages.begin(), actual_messages.end(),
113 [&expected_message](const PermissionMessage& msg) {
114 return msg.message() == expected_message;
116 bool found = message_it != actual_messages.end();
117 if (!found) {
118 // Message: Expected messages to contain "Foo", but got { "Bar", "Baz" }
119 return testing::AssertionFailure() << "Expected messages to contain \""
120 << expected_message << "\", but got "
121 << MessagesToString(actual_messages);
124 if (!CheckThatSubmessagesMatch(expected_message, expected_submessages,
125 message_it->submessages())) {
126 return testing::AssertionFailure();
129 // Message: Expected messages NOT to contain "Foo", but got { "Bar", "Baz" }
130 return testing::AssertionSuccess() << "Expected messages NOT to contain \""
131 << expected_message << "\", but got "
132 << MessagesToString(actual_messages);
135 testing::AssertionResult VerifyPermissionMessagesWithSubmessagesImpl(
136 const std::vector<base::string16>& expected_messages,
137 const std::vector<std::vector<base::string16>>& expected_submessages,
138 const PermissionMessages& actual_messages,
139 bool check_order) {
140 CHECK_EQ(expected_messages.size(), expected_submessages.size());
141 if (expected_messages.size() != actual_messages.size()) {
142 // Message: Expected 2 messages { "Bar", "Baz" }, but got 0 {}
143 return testing::AssertionFailure()
144 << "Expected " << expected_messages.size() << " messages:\n"
145 << MessagesVectorToString(expected_messages) << "But got "
146 << actual_messages.size() << " messages:\n"
147 << MessagesToString(actual_messages);
150 if (check_order) {
151 auto it = actual_messages.begin();
152 for (size_t i = 0; i < expected_messages.size(); i++, ++it) {
153 const PermissionMessage& actual_message = *it;
154 if (expected_messages[i] != actual_message.message()) {
155 // Message: Expected messages to be { "Foo" }, but got { "Bar", "Baz" }
156 return testing::AssertionFailure()
157 << "Expected messages to be:\n"
158 << MessagesVectorToString(expected_messages) << "But got:\n"
159 << MessagesToString(actual_messages);
162 if (!CheckThatSubmessagesMatch(expected_messages[i],
163 expected_submessages[i],
164 actual_message.submessages())) {
165 return testing::AssertionFailure();
168 } else {
169 for (size_t i = 0; i < expected_messages.size(); i++) {
170 testing::AssertionResult result = VerifyHasPermissionMessageImpl(
171 expected_messages[i], expected_submessages[i], actual_messages);
172 if (!result)
173 return result;
176 return testing::AssertionSuccess();
179 } // namespace
181 testing::AssertionResult VerifyHasPermissionMessage(
182 const PermissionsData* permissions_data,
183 const std::string& expected_message) {
184 return VerifyHasPermissionMessage(permissions_data,
185 base::UTF8ToUTF16(expected_message));
188 testing::AssertionResult VerifyHasPermissionMessage(
189 const PermissionsData* permissions_data,
190 const base::string16& expected_message) {
191 return VerifyHasPermissionMessageImpl(
192 expected_message, std::vector<base::string16>(),
193 permissions_data->GetPermissionMessages());
196 testing::AssertionResult VerifyHasPermissionMessage(
197 const PermissionSet* permissions,
198 Manifest::Type extension_type,
199 const std::string& expected_message) {
200 return VerifyHasPermissionMessage(permissions, extension_type,
201 base::UTF8ToUTF16(expected_message));
204 testing::AssertionResult VerifyHasPermissionMessage(
205 const PermissionSet* permissions,
206 Manifest::Type extension_type,
207 const base::string16& expected_message) {
208 return VerifyHasPermissionMessageImpl(
209 expected_message, std::vector<base::string16>(),
210 GetMessages(permissions, extension_type));
213 testing::AssertionResult VerifyNoPermissionMessages(
214 const PermissionsData* permissions_data) {
215 return VerifyPermissionMessages(permissions_data,
216 std::vector<base::string16>(), true);
219 testing::AssertionResult VerifyOnePermissionMessage(
220 const PermissionsData* permissions_data,
221 const std::string& expected_message) {
222 return VerifyOnePermissionMessage(permissions_data,
223 base::UTF8ToUTF16(expected_message));
226 testing::AssertionResult VerifyOnePermissionMessage(
227 const PermissionsData* permissions_data,
228 const base::string16& expected_message) {
229 return VerifyPermissionMessages(permissions_data,
230 MakeVectorString16(expected_message), true);
233 testing::AssertionResult VerifyOnePermissionMessage(
234 const PermissionSet* permissions,
235 Manifest::Type extension_type,
236 const base::string16& expected_message) {
237 return VerifyPermissionMessagesWithSubmessagesImpl(
238 MakeVectorString16(expected_message),
239 std::vector<std::vector<base::string16>>(1),
240 GetMessages(permissions, extension_type), true);
243 testing::AssertionResult VerifyOnePermissionMessageWithSubmessages(
244 const PermissionsData* permissions_data,
245 const std::string& expected_message,
246 const std::vector<std::string>& expected_submessages) {
247 return VerifyOnePermissionMessageWithSubmessages(
248 permissions_data, base::UTF8ToUTF16(expected_message),
249 MakeVectorString16(expected_submessages));
252 testing::AssertionResult VerifyOnePermissionMessageWithSubmessages(
253 const PermissionsData* permissions_data,
254 const base::string16& expected_message,
255 const std::vector<base::string16>& expected_submessages) {
256 return VerifyPermissionMessagesWithSubmessages(
257 permissions_data, MakeVectorString16(expected_message),
258 MakeVectorVectorString16(expected_submessages), true);
261 testing::AssertionResult VerifyTwoPermissionMessages(
262 const PermissionsData* permissions_data,
263 const std::string& expected_message_1,
264 const std::string& expected_message_2,
265 bool check_order) {
266 return VerifyPermissionMessages(
267 permissions_data,
268 MakeVectorString16(expected_message_1, expected_message_2), check_order);
271 testing::AssertionResult VerifyTwoPermissionMessages(
272 const PermissionsData* permissions_data,
273 const base::string16& expected_message_1,
274 const base::string16& expected_message_2,
275 bool check_order) {
276 return VerifyPermissionMessages(
277 permissions_data,
278 MakeVectorString16(expected_message_1, expected_message_2), check_order);
281 testing::AssertionResult VerifyPermissionMessages(
282 const PermissionsData* permissions_data,
283 const std::vector<std::string>& expected_messages,
284 bool check_order) {
285 return VerifyPermissionMessages(
286 permissions_data, MakeVectorString16(expected_messages), check_order);
289 testing::AssertionResult VerifyPermissionMessages(
290 const PermissionsData* permissions_data,
291 const std::vector<base::string16>& expected_messages,
292 bool check_order) {
293 return VerifyPermissionMessagesWithSubmessages(
294 permissions_data, expected_messages,
295 std::vector<std::vector<base::string16>>(expected_messages.size()),
296 check_order);
299 testing::AssertionResult VerifyPermissionMessagesWithSubmessages(
300 const PermissionsData* permissions_data,
301 const std::vector<std::string>& expected_messages,
302 const std::vector<std::vector<std::string>>& expected_submessages,
303 bool check_order) {
304 return VerifyPermissionMessagesWithSubmessages(
305 permissions_data, MakeVectorString16(expected_messages),
306 MakeVectorVectorString16(expected_submessages), check_order);
309 testing::AssertionResult VerifyPermissionMessagesWithSubmessages(
310 const PermissionsData* permissions_data,
311 const std::vector<base::string16>& expected_messages,
312 const std::vector<std::vector<base::string16>>& expected_submessages,
313 bool check_order) {
314 CHECK_EQ(expected_messages.size(), expected_submessages.size());
315 return VerifyPermissionMessagesWithSubmessagesImpl(
316 expected_messages, expected_submessages,
317 permissions_data->GetPermissionMessages(), check_order);
320 } // namespace extensions