Home | History | Annotate | Download | only in common
      1 // Copyright 2013 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 "base/files/file_path.h"
      6 #include "base/logging.h"
      7 #include "base/memory/ref_counted.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/values.h"
     10 #include "extensions/common/extension.h"
     11 #include "extensions/common/extension_set.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 namespace extensions {
     15 
     16 namespace {
     17 
     18 scoped_refptr<Extension> CreateTestExtension(const std::string& name,
     19                                              const std::string& launch_url,
     20                                              const std::string& extent) {
     21 #if defined(OS_WIN)
     22   base::FilePath path(FILE_PATH_LITERAL("c:\\"));
     23 #else
     24   base::FilePath path(FILE_PATH_LITERAL("/"));
     25 #endif
     26   path = path.AppendASCII(name);
     27 
     28   base::DictionaryValue manifest;
     29   manifest.SetString("name", name);
     30   manifest.SetString("version", "1");
     31 
     32   if (!launch_url.empty())
     33     manifest.SetString("app.launch.web_url", launch_url);
     34 
     35   if (!extent.empty()) {
     36     base::ListValue* urls = new base::ListValue();
     37     manifest.Set("app.urls", urls);
     38     urls->Append(new base::StringValue(extent));
     39   }
     40 
     41   std::string error;
     42   scoped_refptr<Extension> extension(
     43       Extension::Create(path, Manifest::INTERNAL, manifest,
     44                         Extension::NO_FLAGS, &error));
     45   EXPECT_TRUE(extension.get()) << error;
     46   return extension;
     47 }
     48 
     49 }  // namespace
     50 
     51 TEST(ExtensionSetTest, ExtensionSet) {
     52   scoped_refptr<Extension> ext1(CreateTestExtension(
     53       "a", "https://chrome.google.com/launch", "https://chrome.google.com/"));
     54 
     55   scoped_refptr<Extension> ext2(CreateTestExtension(
     56       "a", "http://code.google.com/p/chromium",
     57       "http://code.google.com/p/chromium/"));
     58 
     59   scoped_refptr<Extension> ext3(CreateTestExtension(
     60       "b", "http://dev.chromium.org/", "http://dev.chromium.org/"));
     61 
     62   scoped_refptr<Extension> ext4(
     63       CreateTestExtension("c", std::string(), std::string()));
     64 
     65   ASSERT_TRUE(ext1.get() && ext2.get() && ext3.get() && ext4.get());
     66 
     67   ExtensionSet extensions;
     68 
     69   // Add an extension.
     70   EXPECT_TRUE(extensions.Insert(ext1));
     71   EXPECT_EQ(1u, extensions.size());
     72   EXPECT_EQ(ext1, extensions.GetByID(ext1->id()));
     73 
     74   // Since extension2 has same ID, it should overwrite extension1.
     75   EXPECT_FALSE(extensions.Insert(ext2));
     76   EXPECT_EQ(1u, extensions.size());
     77   EXPECT_EQ(ext2, extensions.GetByID(ext1->id()));
     78 
     79   // Add the other extensions.
     80   EXPECT_TRUE(extensions.Insert(ext3));
     81   EXPECT_TRUE(extensions.Insert(ext4));
     82   EXPECT_EQ(3u, extensions.size());
     83 
     84   // Get extension by its chrome-extension:// URL
     85   EXPECT_EQ(ext2, extensions.GetExtensionOrAppByURL(
     86       ext2->GetResourceURL("test.html")));
     87   EXPECT_EQ(ext3, extensions.GetExtensionOrAppByURL(
     88       ext3->GetResourceURL("test.html")));
     89   EXPECT_EQ(ext4, extensions.GetExtensionOrAppByURL(
     90       ext4->GetResourceURL("test.html")));
     91 
     92   // Get extension by web extent.
     93   EXPECT_EQ(ext2, extensions.GetExtensionOrAppByURL(
     94       GURL("http://code.google.com/p/chromium/monkey")));
     95   EXPECT_EQ(ext3, extensions.GetExtensionOrAppByURL(
     96       GURL("http://dev.chromium.org/design-docs/")));
     97   EXPECT_FALSE(extensions.GetExtensionOrAppByURL(
     98       GURL("http://blog.chromium.org/")));
     99 
    100   // Test InSameExtent().
    101   EXPECT_TRUE(extensions.InSameExtent(
    102       GURL("http://code.google.com/p/chromium/monkey/"),
    103       GURL("http://code.google.com/p/chromium/")));
    104   EXPECT_FALSE(extensions.InSameExtent(
    105       GURL("http://code.google.com/p/chromium/"),
    106       GURL("https://code.google.com/p/chromium/")));
    107   EXPECT_FALSE(extensions.InSameExtent(
    108       GURL("http://code.google.com/p/chromium/"),
    109       GURL("http://dev.chromium.org/design-docs/")));
    110 
    111   // Both of these should be NULL, which mean true for InSameExtent.
    112   EXPECT_TRUE(extensions.InSameExtent(
    113       GURL("http://www.google.com/"),
    114       GURL("http://blog.chromium.org/")));
    115 
    116   // Remove one of the extensions.
    117   EXPECT_TRUE(extensions.Remove(ext2->id()));
    118   EXPECT_EQ(2u, extensions.size());
    119   EXPECT_FALSE(extensions.GetByID(ext2->id()));
    120 
    121   // Make a union of a set with 3 more extensions (only 2 are new).
    122   scoped_refptr<Extension> ext5(
    123       CreateTestExtension("d", std::string(), std::string()));
    124   scoped_refptr<Extension> ext6(
    125       CreateTestExtension("e", std::string(), std::string()));
    126   ASSERT_TRUE(ext5.get() && ext6.get());
    127 
    128   scoped_ptr<ExtensionSet> to_add(new ExtensionSet());
    129   // |ext3| is already in |extensions|, should not affect size.
    130   EXPECT_TRUE(to_add->Insert(ext3));
    131   EXPECT_TRUE(to_add->Insert(ext5));
    132   EXPECT_TRUE(to_add->Insert(ext6));
    133 
    134   ASSERT_TRUE(extensions.Contains(ext3->id()));
    135   ASSERT_TRUE(extensions.InsertAll(*to_add));
    136   EXPECT_EQ(4u, extensions.size());
    137 
    138   ASSERT_FALSE(extensions.InsertAll(*to_add));  // Re-adding same set no-ops.
    139   EXPECT_EQ(4u, extensions.size());
    140 }
    141 
    142 }  // namespace extensions
    143