This source file includes following definitions.
- CreateTestExtension
- TEST
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace {
scoped_refptr<Extension> CreateTestExtension(const std::string& name,
const std::string& launch_url,
const std::string& extent) {
#if defined(OS_WIN)
base::FilePath path(FILE_PATH_LITERAL("c:\\"));
#else
base::FilePath path(FILE_PATH_LITERAL("/"));
#endif
path = path.AppendASCII(name);
base::DictionaryValue manifest;
manifest.SetString("name", name);
manifest.SetString("version", "1");
if (!launch_url.empty())
manifest.SetString("app.launch.web_url", launch_url);
if (!extent.empty()) {
base::ListValue* urls = new base::ListValue();
manifest.Set("app.urls", urls);
urls->Append(new base::StringValue(extent));
}
std::string error;
scoped_refptr<Extension> extension(
Extension::Create(path, Manifest::INTERNAL, manifest,
Extension::NO_FLAGS, &error));
EXPECT_TRUE(extension.get()) << error;
return extension;
}
}
TEST(ExtensionSetTest, ExtensionSet) {
scoped_refptr<Extension> ext1(CreateTestExtension(
"a", "https://chrome.google.com/launch", "https://chrome.google.com/"));
scoped_refptr<Extension> ext2(CreateTestExtension(
"a", "http://code.google.com/p/chromium",
"http://code.google.com/p/chromium/"));
scoped_refptr<Extension> ext3(CreateTestExtension(
"b", "http://dev.chromium.org/", "http://dev.chromium.org/"));
scoped_refptr<Extension> ext4(
CreateTestExtension("c", std::string(), std::string()));
ASSERT_TRUE(ext1.get() && ext2.get() && ext3.get() && ext4.get());
ExtensionSet extensions;
EXPECT_TRUE(extensions.Insert(ext1));
EXPECT_EQ(1u, extensions.size());
EXPECT_EQ(ext1, extensions.GetByID(ext1->id()));
EXPECT_FALSE(extensions.Insert(ext2));
EXPECT_EQ(1u, extensions.size());
EXPECT_EQ(ext2, extensions.GetByID(ext1->id()));
EXPECT_TRUE(extensions.Insert(ext3));
EXPECT_TRUE(extensions.Insert(ext4));
EXPECT_EQ(3u, extensions.size());
EXPECT_EQ(ext2, extensions.GetExtensionOrAppByURL(
ext2->GetResourceURL("test.html")));
EXPECT_EQ(ext3, extensions.GetExtensionOrAppByURL(
ext3->GetResourceURL("test.html")));
EXPECT_EQ(ext4, extensions.GetExtensionOrAppByURL(
ext4->GetResourceURL("test.html")));
EXPECT_EQ(ext2, extensions.GetExtensionOrAppByURL(
GURL("http://code.google.com/p/chromium/monkey")));
EXPECT_EQ(ext3, extensions.GetExtensionOrAppByURL(
GURL("http://dev.chromium.org/design-docs/")));
EXPECT_FALSE(extensions.GetExtensionOrAppByURL(
GURL("http://blog.chromium.org/")));
EXPECT_TRUE(extensions.InSameExtent(
GURL("http://code.google.com/p/chromium/monkey/"),
GURL("http://code.google.com/p/chromium/")));
EXPECT_FALSE(extensions.InSameExtent(
GURL("http://code.google.com/p/chromium/"),
GURL("https://code.google.com/p/chromium/")));
EXPECT_FALSE(extensions.InSameExtent(
GURL("http://code.google.com/p/chromium/"),
GURL("http://dev.chromium.org/design-docs/")));
EXPECT_TRUE(extensions.InSameExtent(
GURL("http://www.google.com/"),
GURL("http://blog.chromium.org/")));
EXPECT_TRUE(extensions.Remove(ext2->id()));
EXPECT_EQ(2u, extensions.size());
EXPECT_FALSE(extensions.GetByID(ext2->id()));
scoped_refptr<Extension> ext5(
CreateTestExtension("d", std::string(), std::string()));
scoped_refptr<Extension> ext6(
CreateTestExtension("e", std::string(), std::string()));
ASSERT_TRUE(ext5.get() && ext6.get());
scoped_ptr<ExtensionSet> to_add(new ExtensionSet());
EXPECT_TRUE(to_add->Insert(ext3));
EXPECT_TRUE(to_add->Insert(ext5));
EXPECT_TRUE(to_add->Insert(ext6));
ASSERT_TRUE(extensions.Contains(ext3->id()));
ASSERT_TRUE(extensions.InsertAll(*to_add));
EXPECT_EQ(4u, extensions.size());
ASSERT_FALSE(extensions.InsertAll(*to_add));
EXPECT_EQ(4u, extensions.size());
}
}