summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/extension_set_unittest.cc
blob: e51d5718c97af769eea232078bc02f816857d7ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_path.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_set.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

scoped_refptr<Extension> CreateTestExtension(const std::string& name,
                                             const std::string& launch_url,
                                             const std::string& extent) {
#if defined(OS_WIN)
  FilePath path(FILE_PATH_LITERAL("c:\\"));
#else
  FilePath path(FILE_PATH_LITERAL("/"));
#endif
  path = path.AppendASCII(name);

  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()) {
    ListValue* urls = new ListValue();
    manifest.Set("app.urls", urls);
    urls->Append(Value::CreateStringValue(extent));
  }

  std::string error;
  scoped_refptr<Extension> extension(
      Extension::Create(path, Extension::INTERNAL, manifest,
                        Extension::STRICT_ERROR_CHECKS, &error));
  EXPECT_TRUE(extension.get()) << error;
  return extension;
}

} // namespace

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", "", ""));

  ASSERT_TRUE(ext1 && ext2 && ext3 && ext4);

  ExtensionSet extensions;

  // Add an extension.
  extensions.Insert(ext1);
  EXPECT_EQ(1u, extensions.size());
  EXPECT_EQ(ext1, extensions.GetByID(ext1->id()));

  // Since extension2 has same ID, it should overwrite extension1.
  extensions.Insert(ext2);
  EXPECT_EQ(1u, extensions.size());
  EXPECT_EQ(ext2, extensions.GetByID(ext1->id()));

  // Add the other extensions.
  extensions.Insert(ext3);
  extensions.Insert(ext4);
  EXPECT_EQ(3u, extensions.size());

  // Get extension by its chrome-extension:// URL
  EXPECT_EQ(ext2, extensions.GetByURL(
      ext2->GetResourceURL("test.html")));
  EXPECT_EQ(ext3, extensions.GetByURL(
      ext3->GetResourceURL("test.html")));
  EXPECT_EQ(ext4, extensions.GetByURL(
      ext4->GetResourceURL("test.html")));

  // Get extension by web extent.
  EXPECT_EQ(ext2, extensions.GetByURL(
      GURL("http://code.google.com/p/chromium/monkey")));
  EXPECT_EQ(ext3, extensions.GetByURL(
    GURL("http://dev.chromium.org/design-docs/")));
  EXPECT_FALSE(extensions.GetByURL(
      GURL("http://blog.chromium.org/")));

  // Test InSameExtent().
  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/")));

  // Both of these should be NULL, which mean true for InSameExtent.
  EXPECT_TRUE(extensions.InSameExtent(
      GURL("http://www.google.com/"),
      GURL("http://blog.chromium.org/")));

  // Remove one of the extensions.
  extensions.Remove(ext2->id());
  EXPECT_EQ(2u, extensions.size());
  EXPECT_FALSE(extensions.GetByID(ext2->id()));
}