1// Copyright (c) 2011 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/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 "chrome/common/extensions/extension.h"
11#include "chrome/common/extensions/extension_set.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace {
15
16scoped_refptr<Extension> CreateTestExtension(const std::string& name,
17                                             const std::string& launch_url,
18                                             const std::string& extent) {
19#if defined(OS_WIN)
20  FilePath path(FILE_PATH_LITERAL("c:\\"));
21#else
22  FilePath path(FILE_PATH_LITERAL("/"));
23#endif
24  path = path.AppendASCII(name);
25
26  DictionaryValue manifest;
27  manifest.SetString("name", name);
28  manifest.SetString("version", "1");
29
30  if (!launch_url.empty())
31    manifest.SetString("app.launch.web_url", launch_url);
32
33  if (!extent.empty()) {
34    ListValue* urls = new ListValue();
35    manifest.Set("app.urls", urls);
36    urls->Append(Value::CreateStringValue(extent));
37  }
38
39  std::string error;
40  scoped_refptr<Extension> extension(
41      Extension::Create(path, Extension::INTERNAL, manifest,
42                        Extension::STRICT_ERROR_CHECKS, &error));
43  EXPECT_TRUE(extension.get()) << error;
44  return extension;
45}
46
47} // namespace
48
49TEST(ExtensionSetTest, ExtensionSet) {
50  scoped_refptr<Extension> ext1(CreateTestExtension(
51      "a", "https://chrome.google.com/launch", "https://chrome.google.com/"));
52
53  scoped_refptr<Extension> ext2(CreateTestExtension(
54      "a", "http://code.google.com/p/chromium",
55      "http://code.google.com/p/chromium/"));
56
57  scoped_refptr<Extension> ext3(CreateTestExtension(
58      "b", "http://dev.chromium.org/", "http://dev.chromium.org/"));
59
60  scoped_refptr<Extension> ext4(CreateTestExtension("c", "", ""));
61
62  ASSERT_TRUE(ext1 && ext2 && ext3 && ext4);
63
64  ExtensionSet extensions;
65
66  // Add an extension.
67  extensions.Insert(ext1);
68  EXPECT_EQ(1u, extensions.size());
69  EXPECT_EQ(ext1, extensions.GetByID(ext1->id()));
70
71  // Since extension2 has same ID, it should overwrite extension1.
72  extensions.Insert(ext2);
73  EXPECT_EQ(1u, extensions.size());
74  EXPECT_EQ(ext2, extensions.GetByID(ext1->id()));
75
76  // Add the other extensions.
77  extensions.Insert(ext3);
78  extensions.Insert(ext4);
79  EXPECT_EQ(3u, extensions.size());
80
81  // Get extension by its chrome-extension:// URL
82  EXPECT_EQ(ext2, extensions.GetByURL(
83      ext2->GetResourceURL("test.html")));
84  EXPECT_EQ(ext3, extensions.GetByURL(
85      ext3->GetResourceURL("test.html")));
86  EXPECT_EQ(ext4, extensions.GetByURL(
87      ext4->GetResourceURL("test.html")));
88
89  // Get extension by web extent.
90  EXPECT_EQ(ext2, extensions.GetByURL(
91      GURL("http://code.google.com/p/chromium/monkey")));
92  EXPECT_EQ(ext3, extensions.GetByURL(
93    GURL("http://dev.chromium.org/design-docs/")));
94  EXPECT_FALSE(extensions.GetByURL(
95      GURL("http://blog.chromium.org/")));
96
97  // Test InSameExtent().
98  EXPECT_TRUE(extensions.InSameExtent(
99      GURL("http://code.google.com/p/chromium/monkey/"),
100      GURL("http://code.google.com/p/chromium/")));
101  EXPECT_FALSE(extensions.InSameExtent(
102      GURL("http://code.google.com/p/chromium/"),
103      GURL("https://code.google.com/p/chromium/")));
104  EXPECT_FALSE(extensions.InSameExtent(
105      GURL("http://code.google.com/p/chromium/"),
106      GURL("http://dev.chromium.org/design-docs/")));
107
108  // Both of these should be NULL, which mean true for InSameExtent.
109  EXPECT_TRUE(extensions.InSameExtent(
110      GURL("http://www.google.com/"),
111      GURL("http://blog.chromium.org/")));
112
113  // Remove one of the extensions.
114  extensions.Remove(ext2->id());
115  EXPECT_EQ(2u, extensions.size());
116  EXPECT_FALSE(extensions.GetByID(ext2->id()));
117}
118