Introduce AndroidMetricsProvider class.
[chromium-blink-merge.git] / extensions / common / url_pattern_set_unittest.cc
blob267743a77435afc36e66b9978e1f371639c55cf4
1 // Copyright (c) 2012 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/url_pattern_set.h"
7 #include "base/values.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
11 namespace extensions {
13 namespace {
15 void AddPattern(URLPatternSet* set, const std::string& pattern) {
16 int schemes = URLPattern::SCHEME_ALL;
17 set->AddPattern(URLPattern(schemes, pattern));
20 URLPatternSet Patterns(const std::string& pattern) {
21 URLPatternSet set;
22 AddPattern(&set, pattern);
23 return set;
26 URLPatternSet Patterns(const std::string& pattern1,
27 const std::string& pattern2) {
28 URLPatternSet set;
29 AddPattern(&set, pattern1);
30 AddPattern(&set, pattern2);
31 return set;
34 } // namespace
36 TEST(URLPatternSetTest, Empty) {
37 URLPatternSet set;
38 EXPECT_FALSE(set.MatchesURL(GURL("http://www.foo.com/bar")));
39 EXPECT_FALSE(set.MatchesURL(GURL()));
40 EXPECT_FALSE(set.MatchesURL(GURL("invalid")));
43 TEST(URLPatternSetTest, One) {
44 URLPatternSet set;
45 AddPattern(&set, "http://www.google.com/*");
47 EXPECT_TRUE(set.MatchesURL(GURL("http://www.google.com/")));
48 EXPECT_TRUE(set.MatchesURL(GURL("http://www.google.com/monkey")));
49 EXPECT_FALSE(set.MatchesURL(GURL("https://www.google.com/")));
50 EXPECT_FALSE(set.MatchesURL(GURL("https://www.microsoft.com/")));
53 TEST(URLPatternSetTest, Two) {
54 URLPatternSet set;
55 AddPattern(&set, "http://www.google.com/*");
56 AddPattern(&set, "http://www.yahoo.com/*");
58 EXPECT_TRUE(set.MatchesURL(GURL("http://www.google.com/monkey")));
59 EXPECT_TRUE(set.MatchesURL(GURL("http://www.yahoo.com/monkey")));
60 EXPECT_FALSE(set.MatchesURL(GURL("https://www.apple.com/monkey")));
63 TEST(URLPatternSetTest, OverlapsWith) {
64 URLPatternSet set1;
65 AddPattern(&set1, "http://www.google.com/f*");
66 AddPattern(&set1, "http://www.yahoo.com/b*");
68 URLPatternSet set2;
69 AddPattern(&set2, "http://www.reddit.com/f*");
70 AddPattern(&set2, "http://www.yahoo.com/z*");
72 URLPatternSet set3;
73 AddPattern(&set3, "http://www.google.com/q/*");
74 AddPattern(&set3, "http://www.yahoo.com/b/*");
76 EXPECT_FALSE(set1.OverlapsWith(set2));
77 EXPECT_FALSE(set2.OverlapsWith(set1));
79 EXPECT_TRUE(set1.OverlapsWith(set3));
80 EXPECT_TRUE(set3.OverlapsWith(set1));
83 TEST(URLPatternSetTest, CreateDifference) {
84 URLPatternSet expected;
85 URLPatternSet set1;
86 URLPatternSet set2;
87 AddPattern(&set1, "http://www.google.com/f*");
88 AddPattern(&set1, "http://www.yahoo.com/b*");
90 // Subtract an empty set.
91 URLPatternSet result;
92 URLPatternSet::CreateDifference(set1, set2, &result);
93 EXPECT_EQ(set1, result);
95 // Subtract a real set.
96 AddPattern(&set2, "http://www.reddit.com/f*");
97 AddPattern(&set2, "http://www.yahoo.com/z*");
98 AddPattern(&set2, "http://www.google.com/f*");
100 AddPattern(&expected, "http://www.yahoo.com/b*");
102 result.ClearPatterns();
103 URLPatternSet::CreateDifference(set1, set2, &result);
104 EXPECT_EQ(expected, result);
105 EXPECT_FALSE(result.is_empty());
106 EXPECT_TRUE(set1.Contains(result));
107 EXPECT_FALSE(result.Contains(set2));
108 EXPECT_FALSE(set2.Contains(result));
110 URLPatternSet intersection;
111 URLPatternSet::CreateIntersection(result, set2, &intersection);
112 EXPECT_TRUE(intersection.is_empty());
115 TEST(URLPatternSetTest, CreateIntersection) {
116 URLPatternSet empty_set;
117 URLPatternSet expected;
118 URLPatternSet set1;
119 AddPattern(&set1, "http://www.google.com/f*");
120 AddPattern(&set1, "http://www.yahoo.com/b*");
122 // Intersection with an empty set.
123 URLPatternSet result;
124 URLPatternSet::CreateIntersection(set1, empty_set, &result);
125 EXPECT_EQ(expected, result);
126 EXPECT_TRUE(result.is_empty());
127 EXPECT_TRUE(empty_set.Contains(result));
128 EXPECT_TRUE(result.Contains(empty_set));
129 EXPECT_TRUE(set1.Contains(result));
131 // Intersection with a real set.
132 URLPatternSet set2;
133 AddPattern(&set2, "http://www.reddit.com/f*");
134 AddPattern(&set2, "http://www.yahoo.com/z*");
135 AddPattern(&set2, "http://www.google.com/f*");
137 AddPattern(&expected, "http://www.google.com/f*");
139 result.ClearPatterns();
140 URLPatternSet::CreateIntersection(set1, set2, &result);
141 EXPECT_EQ(expected, result);
142 EXPECT_FALSE(result.is_empty());
143 EXPECT_TRUE(set1.Contains(result));
144 EXPECT_TRUE(set2.Contains(result));
147 TEST(URLPatternSetTest, CreateUnion) {
148 URLPatternSet empty_set;
150 URLPatternSet set1;
151 AddPattern(&set1, "http://www.google.com/f*");
152 AddPattern(&set1, "http://www.yahoo.com/b*");
154 URLPatternSet expected;
155 AddPattern(&expected, "http://www.google.com/f*");
156 AddPattern(&expected, "http://www.yahoo.com/b*");
158 // Union with an empty set.
159 URLPatternSet result;
160 URLPatternSet::CreateUnion(set1, empty_set, &result);
161 EXPECT_EQ(expected, result);
163 // Union with a real set.
164 URLPatternSet set2;
165 AddPattern(&set2, "http://www.reddit.com/f*");
166 AddPattern(&set2, "http://www.yahoo.com/z*");
167 AddPattern(&set2, "http://www.google.com/f*");
169 AddPattern(&expected, "http://www.reddit.com/f*");
170 AddPattern(&expected, "http://www.yahoo.com/z*");
172 result.ClearPatterns();
173 URLPatternSet::CreateUnion(set1, set2, &result);
174 EXPECT_EQ(expected, result);
177 TEST(URLPatternSetTest, Contains) {
178 URLPatternSet set1;
179 URLPatternSet set2;
180 URLPatternSet empty_set;
182 AddPattern(&set1, "http://www.google.com/*");
183 AddPattern(&set1, "http://www.yahoo.com/*");
185 AddPattern(&set2, "http://www.reddit.com/*");
187 EXPECT_FALSE(set1.Contains(set2));
188 EXPECT_TRUE(set1.Contains(empty_set));
189 EXPECT_FALSE(empty_set.Contains(set1));
191 AddPattern(&set2, "http://www.yahoo.com/*");
193 EXPECT_FALSE(set1.Contains(set2));
194 EXPECT_FALSE(set2.Contains(set1));
196 AddPattern(&set2, "http://www.google.com/*");
198 EXPECT_FALSE(set1.Contains(set2));
199 EXPECT_TRUE(set2.Contains(set1));
201 // Note that this checks if individual patterns contain other patterns, not
202 // just equality. For example:
203 AddPattern(&set1, "http://*.reddit.com/*");
204 EXPECT_TRUE(set1.Contains(set2));
205 EXPECT_FALSE(set2.Contains(set1));
208 TEST(URLPatternSetTest, Duplicates) {
209 URLPatternSet set1;
210 URLPatternSet set2;
212 AddPattern(&set1, "http://www.google.com/*");
213 AddPattern(&set2, "http://www.google.com/*");
215 AddPattern(&set1, "http://www.google.com/*");
217 // The sets should still be equal after adding a duplicate.
218 EXPECT_EQ(set2, set1);
221 TEST(URLPatternSetTest, ToValueAndPopulate) {
222 URLPatternSet set1;
223 URLPatternSet set2;
225 std::vector<std::string> patterns;
226 patterns.push_back("http://www.google.com/*");
227 patterns.push_back("http://www.yahoo.com/*");
229 for (size_t i = 0; i < patterns.size(); ++i)
230 AddPattern(&set1, patterns[i]);
232 std::string error;
233 bool allow_file_access = false;
234 scoped_ptr<base::ListValue> value(set1.ToValue());
235 set2.Populate(*value, URLPattern::SCHEME_ALL, allow_file_access, &error);
236 EXPECT_EQ(set1, set2);
238 set2.ClearPatterns();
239 set2.Populate(patterns, URLPattern::SCHEME_ALL, allow_file_access, &error);
240 EXPECT_EQ(set1, set2);
243 TEST(URLPatternSetTest, NwayUnion) {
244 std::string google_a = "http://www.google.com/a*";
245 std::string google_b = "http://www.google.com/b*";
246 std::string google_c = "http://www.google.com/c*";
247 std::string yahoo_a = "http://www.yahoo.com/a*";
248 std::string yahoo_b = "http://www.yahoo.com/b*";
249 std::string yahoo_c = "http://www.yahoo.com/c*";
250 std::string reddit_a = "http://www.reddit.com/a*";
251 std::string reddit_b = "http://www.reddit.com/b*";
252 std::string reddit_c = "http://www.reddit.com/c*";
254 // Empty list.
256 std::vector<URLPatternSet> empty;
258 URLPatternSet result;
259 URLPatternSet::CreateUnion(empty, &result);
261 URLPatternSet expected;
262 EXPECT_EQ(expected, result);
265 // Singleton list.
267 std::vector<URLPatternSet> test;
268 test.push_back(Patterns(google_a));
270 URLPatternSet result;
271 URLPatternSet::CreateUnion(test, &result);
273 URLPatternSet expected = Patterns(google_a);
274 EXPECT_EQ(expected, result);
277 // List with 2 elements.
279 std::vector<URLPatternSet> test;
280 test.push_back(Patterns(google_a, google_b));
281 test.push_back(Patterns(google_b, google_c));
283 URLPatternSet result;
284 URLPatternSet::CreateUnion(test, &result);
286 URLPatternSet expected;
287 AddPattern(&expected, google_a);
288 AddPattern(&expected, google_b);
289 AddPattern(&expected, google_c);
290 EXPECT_EQ(expected, result);
293 // List with 3 elements.
295 std::vector<URLPatternSet> test;
296 test.push_back(Patterns(google_a, google_b));
297 test.push_back(Patterns(google_b, google_c));
298 test.push_back(Patterns(yahoo_a, yahoo_b));
300 URLPatternSet result;
301 URLPatternSet::CreateUnion(test, &result);
303 URLPatternSet expected;
304 AddPattern(&expected, google_a);
305 AddPattern(&expected, google_b);
306 AddPattern(&expected, google_c);
307 AddPattern(&expected, yahoo_a);
308 AddPattern(&expected, yahoo_b);
309 EXPECT_EQ(expected, result);
312 // List with 7 elements.
314 std::vector<URLPatternSet> test;
315 test.push_back(Patterns(google_a));
316 test.push_back(Patterns(google_b));
317 test.push_back(Patterns(google_c));
318 test.push_back(Patterns(yahoo_a));
319 test.push_back(Patterns(yahoo_b));
320 test.push_back(Patterns(yahoo_c));
321 test.push_back(Patterns(reddit_a));
323 URLPatternSet result;
324 URLPatternSet::CreateUnion(test, &result);
326 URLPatternSet expected;
327 AddPattern(&expected, google_a);
328 AddPattern(&expected, google_b);
329 AddPattern(&expected, google_c);
330 AddPattern(&expected, yahoo_a);
331 AddPattern(&expected, yahoo_b);
332 AddPattern(&expected, yahoo_c);
333 AddPattern(&expected, reddit_a);
334 EXPECT_EQ(expected, result);
337 // List with 8 elements.
339 std::vector<URLPatternSet> test;
340 test.push_back(Patterns(google_a));
341 test.push_back(Patterns(google_b));
342 test.push_back(Patterns(google_c));
343 test.push_back(Patterns(yahoo_a));
344 test.push_back(Patterns(yahoo_b));
345 test.push_back(Patterns(yahoo_c));
346 test.push_back(Patterns(reddit_a));
347 test.push_back(Patterns(reddit_b));
349 URLPatternSet result;
350 URLPatternSet::CreateUnion(test, &result);
352 URLPatternSet expected;
353 AddPattern(&expected, google_a);
354 AddPattern(&expected, google_b);
355 AddPattern(&expected, google_c);
356 AddPattern(&expected, yahoo_a);
357 AddPattern(&expected, yahoo_b);
358 AddPattern(&expected, yahoo_c);
359 AddPattern(&expected, reddit_a);
360 AddPattern(&expected, reddit_b);
361 EXPECT_EQ(expected, result);
364 // List with 9 elements.
366 std::vector<URLPatternSet> test;
367 test.push_back(Patterns(google_a));
368 test.push_back(Patterns(google_b));
369 test.push_back(Patterns(google_c));
370 test.push_back(Patterns(yahoo_a));
371 test.push_back(Patterns(yahoo_b));
372 test.push_back(Patterns(yahoo_c));
373 test.push_back(Patterns(reddit_a));
374 test.push_back(Patterns(reddit_b));
375 test.push_back(Patterns(reddit_c));
377 URLPatternSet result;
378 URLPatternSet::CreateUnion(test, &result);
380 URLPatternSet expected;
381 AddPattern(&expected, google_a);
382 AddPattern(&expected, google_b);
383 AddPattern(&expected, google_c);
384 AddPattern(&expected, yahoo_a);
385 AddPattern(&expected, yahoo_b);
386 AddPattern(&expected, yahoo_c);
387 AddPattern(&expected, reddit_a);
388 AddPattern(&expected, reddit_b);
389 AddPattern(&expected, reddit_c);
390 EXPECT_EQ(expected, result);
394 } // namespace extensions