blob: cffd96c0fc7d8cf8a6d8d9e138be2bb1c5e2b4f0 [file] [log] [blame]
[email protected]50de9aa22013-11-14 06:30:341// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]6f371442011-11-09 06:45:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]50de9aa22013-11-14 06:30:345#include "extensions/browser/process_map.h"
[email protected]6f371442011-11-09 06:45:466
tbarzic8e89b0b12017-06-10 03:25:517#include "base/memory/ref_counted.h"
8#include "base/values.h"
9#include "extensions/common/extension.h"
10#include "extensions/common/extension_builder.h"
11#include "extensions/common/features/feature.h"
12#include "extensions/common/value_builder.h"
[email protected]6f371442011-11-09 06:45:4613#include "testing/gtest/include/gtest/gtest.h"
14
tbarzic8e89b0b12017-06-10 03:25:5115namespace extensions {
16namespace {
17
18enum class TypeToCreate { kExtension, kHostedApp, kPlatformApp };
19
20scoped_refptr<const Extension> CreateExtensionWithFlags(TypeToCreate type,
21 const std::string& id) {
22 DictionaryBuilder manifest_builder;
23 manifest_builder.Set("name", "Test extension")
24 .Set("version", "1.0")
25 .Set("manifest_version", 2);
26
27 switch (type) {
28 case TypeToCreate::kExtension:
29 manifest_builder.Set(
30 "background",
31 DictionaryBuilder()
32 .Set("scripts", ListBuilder().Append("background.js").Build())
33 .Build());
34 break;
35 case TypeToCreate::kHostedApp:
36 manifest_builder.Set(
37 "app", DictionaryBuilder()
38 .Set("launch", DictionaryBuilder()
39 .Set("web_url", "https://www.foo.bar")
40 .Build())
41 .Build());
42 break;
43 case TypeToCreate::kPlatformApp:
44 manifest_builder.Set(
45 "app",
46 DictionaryBuilder()
47 .Set("background",
48 DictionaryBuilder()
49 .Set("scripts",
50 ListBuilder().Append("background.js").Build())
51 .Build())
52 .Build());
53 break;
54 }
55
56 return ExtensionBuilder()
57 .SetID(id)
58 .SetManifest(manifest_builder.Build())
59 .Build();
60}
61
62} // namespace
63} // namespace extensions
64
[email protected]6f371442011-11-09 06:45:4665using extensions::ProcessMap;
66
67TEST(ExtensionProcessMapTest, Test) {
68 ProcessMap map;
69
70 // Test behavior when empty.
71 EXPECT_FALSE(map.Contains("a", 1));
[email protected]6bc04fd82011-12-04 02:29:3572 EXPECT_FALSE(map.Remove("a", 1, 1));
[email protected]6f371442011-11-09 06:45:4673 EXPECT_EQ(0u, map.size());
74
75 // Test insertion and behavior with one item.
[email protected]6bc04fd82011-12-04 02:29:3576 EXPECT_TRUE(map.Insert("a", 1, 1));
[email protected]6f371442011-11-09 06:45:4677 EXPECT_TRUE(map.Contains("a", 1));
78 EXPECT_FALSE(map.Contains("a", 2));
79 EXPECT_FALSE(map.Contains("b", 1));
80 EXPECT_EQ(1u, map.size());
81
82 // Test inserting a duplicate item.
[email protected]6bc04fd82011-12-04 02:29:3583 EXPECT_FALSE(map.Insert("a", 1, 1));
[email protected]6f371442011-11-09 06:45:4684 EXPECT_TRUE(map.Contains("a", 1));
85 EXPECT_EQ(1u, map.size());
86
87 // Insert some more items.
[email protected]6bc04fd82011-12-04 02:29:3588 EXPECT_TRUE(map.Insert("a", 2, 2));
89 EXPECT_TRUE(map.Insert("b", 1, 3));
90 EXPECT_TRUE(map.Insert("b", 2, 4));
[email protected]6f371442011-11-09 06:45:4691 EXPECT_EQ(4u, map.size());
92
93 EXPECT_TRUE(map.Contains("a", 1));
94 EXPECT_TRUE(map.Contains("a", 2));
95 EXPECT_TRUE(map.Contains("b", 1));
96 EXPECT_TRUE(map.Contains("b", 2));
97 EXPECT_FALSE(map.Contains("a", 3));
98
[email protected]6bc04fd82011-12-04 02:29:3599 // Note that this only differs from an existing item because of the site
100 // instance id.
101 EXPECT_TRUE(map.Insert("a", 1, 5));
102 EXPECT_TRUE(map.Contains("a", 1));
[email protected]6f371442011-11-09 06:45:46103
[email protected]6bc04fd82011-12-04 02:29:35104 // Test removal.
105 EXPECT_TRUE(map.Remove("a", 1, 1));
106 EXPECT_FALSE(map.Remove("a", 1, 1));
107 EXPECT_EQ(4u, map.size());
108
109 // Should still return true because there were two site instances for this
110 // extension/process pair.
111 EXPECT_TRUE(map.Contains("a", 1));
112
113 EXPECT_TRUE(map.Remove("a", 1, 5));
114 EXPECT_EQ(3u, map.size());
115 EXPECT_FALSE(map.Contains("a", 1));
116
117 EXPECT_EQ(2, map.RemoveAllFromProcess(2));
[email protected]6f371442011-11-09 06:45:46118 EXPECT_EQ(1u, map.size());
[email protected]6bc04fd82011-12-04 02:29:35119 EXPECT_EQ(0, map.RemoveAllFromProcess(2));
[email protected]6f371442011-11-09 06:45:46120 EXPECT_EQ(1u, map.size());
121}
tbarzic8e89b0b12017-06-10 03:25:51122
123TEST(ExtensionProcessMapTest, GetMostLikelyContextType) {
124 ProcessMap map;
125
126 EXPECT_EQ(extensions::Feature::WEB_PAGE_CONTEXT,
127 map.GetMostLikelyContextType(nullptr, 1));
128
129 scoped_refptr<const extensions::Extension> extension =
130 CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "a");
131
132 EXPECT_EQ(extensions::Feature::CONTENT_SCRIPT_CONTEXT,
133 map.GetMostLikelyContextType(extension.get(), 2));
134
135 map.Insert("b", 3, 1);
136 extension =
137 CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "b");
138 EXPECT_EQ(extensions::Feature::BLESSED_EXTENSION_CONTEXT,
139 map.GetMostLikelyContextType(extension.get(), 3));
140
141 map.Insert("c", 4, 2);
142 extension =
143 CreateExtensionWithFlags(extensions::TypeToCreate::kPlatformApp, "c");
144 EXPECT_EQ(extensions::Feature::BLESSED_EXTENSION_CONTEXT,
145 map.GetMostLikelyContextType(extension.get(), 4));
146
147 map.set_is_lock_screen_context(true);
148
149 map.Insert("d", 5, 3);
150 extension =
151 CreateExtensionWithFlags(extensions::TypeToCreate::kPlatformApp, "d");
152 EXPECT_EQ(extensions::Feature::LOCK_SCREEN_EXTENSION_CONTEXT,
153 map.GetMostLikelyContextType(extension.get(), 5));
154
155 map.Insert("e", 6, 4);
156 extension =
157 CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "e");
158 EXPECT_EQ(extensions::Feature::LOCK_SCREEN_EXTENSION_CONTEXT,
159 map.GetMostLikelyContextType(extension.get(), 6));
160
161 map.Insert("f", 7, 5);
162 extension =
163 CreateExtensionWithFlags(extensions::TypeToCreate::kHostedApp, "f");
164 EXPECT_EQ(extensions::Feature::BLESSED_WEB_PAGE_CONTEXT,
165 map.GetMostLikelyContextType(extension.get(), 7));
166}