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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/path_service.h"
7 #include "content/public/test/test_browser_thread.h"
8 #include "extensions/browser/info_map.h"
9 #include "extensions/common/extension.h"
10 #include "extensions/common/extension_paths.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using content::BrowserThread
;
16 namespace keys
= extensions::manifest_keys
;
18 namespace extensions
{
20 class InfoMapTest
: public testing::Test
{
23 : ui_thread_(BrowserThread::UI
, &message_loop_
),
24 io_thread_(BrowserThread::IO
, &message_loop_
) {}
27 base::MessageLoop message_loop_
;
28 content::TestBrowserThread ui_thread_
;
29 content::TestBrowserThread io_thread_
;
32 // Returns a barebones test Extension object with the given name.
33 static scoped_refptr
<Extension
> CreateExtension(const std::string
& name
) {
35 PathService::Get(DIR_TEST_DATA
, &path
);
37 base::DictionaryValue manifest
;
38 manifest
.SetString(keys::kVersion
, "1.0.0.0");
39 manifest
.SetString(keys::kName
, name
);
42 scoped_refptr
<Extension
> extension
=
43 Extension::Create(path
.AppendASCII(name
),
44 Manifest::INVALID_LOCATION
,
48 EXPECT_TRUE(extension
.get()) << error
;
53 // Test that the InfoMap handles refcounting properly.
54 TEST_F(InfoMapTest
, RefCounting
) {
55 scoped_refptr
<InfoMap
> info_map(new InfoMap());
57 // New extensions should have a single reference holding onto them.
58 scoped_refptr
<Extension
> extension1(CreateExtension("extension1"));
59 scoped_refptr
<Extension
> extension2(CreateExtension("extension2"));
60 scoped_refptr
<Extension
> extension3(CreateExtension("extension3"));
61 EXPECT_TRUE(extension1
->HasOneRef());
62 EXPECT_TRUE(extension2
->HasOneRef());
63 EXPECT_TRUE(extension3
->HasOneRef());
65 // Add a ref to each extension and give it to the info map.
66 info_map
->AddExtension(extension1
.get(), base::Time(), false, false);
67 info_map
->AddExtension(extension2
.get(), base::Time(), false, false);
68 info_map
->AddExtension(extension3
.get(), base::Time(), false, false);
70 // Release extension1, and the info map should have the only ref.
71 const Extension
* weak_extension1
= extension1
.get();
73 EXPECT_TRUE(weak_extension1
->HasOneRef());
75 // Remove extension2, and the extension2 object should have the only ref.
76 info_map
->RemoveExtension(
77 extension2
->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL
);
78 EXPECT_TRUE(extension2
->HasOneRef());
80 // Delete the info map, and the extension3 object should have the only ref.
82 EXPECT_TRUE(extension3
->HasOneRef());
85 // Tests that we can query a few extension properties from the InfoMap.
86 TEST_F(InfoMapTest
, Properties
) {
87 scoped_refptr
<InfoMap
> info_map(new InfoMap());
89 scoped_refptr
<Extension
> extension1(CreateExtension("extension1"));
90 scoped_refptr
<Extension
> extension2(CreateExtension("extension2"));
92 info_map
->AddExtension(extension1
.get(), base::Time(), false, false);
93 info_map
->AddExtension(extension2
.get(), base::Time(), false, false);
95 EXPECT_EQ(2u, info_map
->extensions().size());
96 EXPECT_EQ(extension1
.get(), info_map
->extensions().GetByID(extension1
->id()));
97 EXPECT_EQ(extension2
.get(), info_map
->extensions().GetByID(extension2
->id()));
100 // Tests that extension URLs are properly mapped to local file paths.
101 TEST_F(InfoMapTest
, MapUrlToLocalFilePath
) {
102 scoped_refptr
<InfoMap
> info_map(new InfoMap());
103 scoped_refptr
<Extension
> app(CreateExtension("platform_app"));
104 info_map
->AddExtension(app
.get(), base::Time(), false, false);
106 // Non-extension URLs don't map to anything.
107 base::FilePath non_extension_path
;
108 GURL
non_extension_url("http://not-an-extension.com/");
109 EXPECT_FALSE(info_map
->MapUrlToLocalFilePath(
110 non_extension_url
, false, &non_extension_path
));
111 EXPECT_TRUE(non_extension_path
.empty());
113 // Valid resources return a valid path.
114 base::FilePath valid_path
;
115 GURL valid_url
= app
->GetResourceURL("manifest.json");
116 EXPECT_TRUE(info_map
->MapUrlToLocalFilePath(
117 valid_url
, true /* use_blocking_api */, &valid_path
));
118 EXPECT_FALSE(valid_path
.empty());
120 // A file must exist to be mapped to a path using the blocking API.
121 base::FilePath does_not_exist_path
;
122 GURL does_not_exist_url
= app
->GetResourceURL("does-not-exist.html");
123 EXPECT_FALSE(info_map
->MapUrlToLocalFilePath(
124 does_not_exist_url
, true /* use_blocking_api */, &does_not_exist_path
));
125 EXPECT_TRUE(does_not_exist_path
.empty());
127 // A file does not need to exist to be mapped to a path with the non-blocking
128 // API. This avoids hitting the disk to see if it exists.
129 EXPECT_TRUE(info_map
->MapUrlToLocalFilePath(
130 does_not_exist_url
, false /* use_blocking_api */, &does_not_exist_path
));
131 EXPECT_FALSE(does_not_exist_path
.empty());
134 } // namespace extensions