Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2010 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.
      4 
      5 #include "chrome/common/extensions/extension.h"
      6 
      7 #include "googleurl/src/gurl.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 static const int kAllSchemes =
     11     URLPattern::SCHEME_HTTP |
     12     URLPattern::SCHEME_HTTPS |
     13     URLPattern::SCHEME_FILE |
     14     URLPattern::SCHEME_FTP |
     15     URLPattern::SCHEME_CHROMEUI;
     16 
     17 TEST(ExtensionExtentTest, Empty) {
     18   ExtensionExtent extent;
     19   EXPECT_FALSE(extent.ContainsURL(GURL("http://www.foo.com/bar")));
     20   EXPECT_FALSE(extent.ContainsURL(GURL()));
     21   EXPECT_FALSE(extent.ContainsURL(GURL("invalid")));
     22 }
     23 
     24 TEST(ExtensionExtentTest, One) {
     25   ExtensionExtent extent;
     26   extent.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/*"));
     27 
     28   EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/")));
     29   EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/monkey")));
     30   EXPECT_FALSE(extent.ContainsURL(GURL("https://www.google.com/")));
     31   EXPECT_FALSE(extent.ContainsURL(GURL("https://www.microsoft.com/")));
     32 }
     33 
     34 TEST(ExtensionExtentTest, Two) {
     35   ExtensionExtent extent;
     36   extent.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/*"));
     37   extent.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/*"));
     38 
     39   EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/monkey")));
     40   EXPECT_TRUE(extent.ContainsURL(GURL("http://www.yahoo.com/monkey")));
     41   EXPECT_FALSE(extent.ContainsURL(GURL("https://www.apple.com/monkey")));
     42 }
     43 
     44 TEST(ExtensionExtentTest, OverlapsWith) {
     45   ExtensionExtent extent1;
     46   extent1.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/f*"));
     47   extent1.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/b*"));
     48 
     49   ExtensionExtent extent2;
     50   extent2.AddPattern(URLPattern(kAllSchemes, "http://www.reddit.com/f*"));
     51   extent2.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/z*"));
     52 
     53   ExtensionExtent extent3;
     54   extent3.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/q/*"));
     55   extent3.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/b/*"));
     56 
     57   EXPECT_FALSE(extent1.OverlapsWith(extent2));
     58   EXPECT_FALSE(extent2.OverlapsWith(extent1));
     59 
     60   EXPECT_TRUE(extent1.OverlapsWith(extent3));
     61   EXPECT_TRUE(extent3.OverlapsWith(extent1));
     62 }
     63