blob: 5dfe7ded96c5c710c9186f4b47bf74b1699f20ac [file] [log] [blame]
[email protected]38427a12013-11-09 17:34:201// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]4361c7c2010-09-30 21:57:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b19fe572013-07-18 04:54:265#include "base/message_loop/message_loop.h"
[email protected]4361c7c2010-09-30 21:57:536#include "base/path_service.h"
[email protected]e97882f2012-06-04 02:23:177#include "content/public/test/test_browser_thread.h"
[email protected]38427a12013-11-09 17:34:208#include "extensions/browser/info_map.h"
[email protected]e4452d32013-11-15 23:07:419#include "extensions/common/extension.h"
[email protected]76e9211f2014-08-22 16:34:1010#include "extensions/common/extension_paths.h"
[email protected]6bf90612013-08-15 00:36:2711#include "extensions/common/manifest_constants.h"
[email protected]4361c7c2010-09-30 21:57:5312#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]631bb742011-11-02 11:29:3914using content::BrowserThread;
15
[email protected]6bf90612013-08-15 00:36:2716namespace keys = extensions::manifest_keys;
[email protected]4361c7c2010-09-30 21:57:5317
[email protected]38427a12013-11-09 17:34:2018namespace extensions {
[email protected]4361c7c2010-09-30 21:57:5319
[email protected]38427a12013-11-09 17:34:2020class InfoMapTest : public testing::Test {
[email protected]4361c7c2010-09-30 21:57:5321 public:
[email protected]38427a12013-11-09 17:34:2022 InfoMapTest()
[email protected]ca4b5fa32010-10-09 12:42:1823 : ui_thread_(BrowserThread::UI, &message_loop_),
[email protected]38427a12013-11-09 17:34:2024 io_thread_(BrowserThread::IO, &message_loop_) {}
[email protected]4361c7c2010-09-30 21:57:5325
26 private:
[email protected]b3a25092013-05-28 22:08:1627 base::MessageLoop message_loop_;
[email protected]c38831a12011-10-28 12:44:4928 content::TestBrowserThread ui_thread_;
29 content::TestBrowserThread io_thread_;
[email protected]4361c7c2010-09-30 21:57:5330};
31
32// Returns a barebones test Extension object with the given name.
[email protected]66e4eb32010-10-27 20:37:4133static scoped_refptr<Extension> CreateExtension(const std::string& name) {
[email protected]f30ee8b2014-08-15 15:20:2234 base::FilePath path;
[email protected]76e9211f2014-08-22 16:34:1035 PathService::Get(DIR_TEST_DATA, &path);
[email protected]4361c7c2010-09-30 21:57:5336
[email protected]4b3006f2013-12-23 22:23:0837 base::DictionaryValue manifest;
[email protected]4361c7c2010-09-30 21:57:5338 manifest.SetString(keys::kVersion, "1.0.0.0");
39 manifest.SetString(keys::kName, name);
40
41 std::string error;
[email protected]38427a12013-11-09 17:34:2042 scoped_refptr<Extension> extension =
43 Extension::Create(path.AppendASCII(name),
44 Manifest::INVALID_LOCATION,
45 manifest,
46 Extension::NO_FLAGS,
47 &error);
[email protected]dc24976f2013-06-02 21:15:0948 EXPECT_TRUE(extension.get()) << error;
[email protected]4361c7c2010-09-30 21:57:5349
[email protected]66e4eb32010-10-27 20:37:4150 return extension;
[email protected]4361c7c2010-09-30 21:57:5351}
52
[email protected]38427a12013-11-09 17:34:2053// Test that the InfoMap handles refcounting properly.
54TEST_F(InfoMapTest, RefCounting) {
55 scoped_refptr<InfoMap> info_map(new InfoMap());
[email protected]4361c7c2010-09-30 21:57:5356
[email protected]6f229e82010-11-02 17:47:2657 // New extensions should have a single reference holding onto them.
[email protected]66e4eb32010-10-27 20:37:4158 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
59 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
60 scoped_refptr<Extension> extension3(CreateExtension("extension3"));
[email protected]6f229e82010-11-02 17:47:2661 EXPECT_TRUE(extension1->HasOneRef());
62 EXPECT_TRUE(extension2->HasOneRef());
63 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5364
[email protected]05cc4e72011-03-08 21:29:4865 // Add a ref to each extension and give it to the info map.
[email protected]9afacd22013-11-13 20:23:3166 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);
[email protected]4361c7c2010-09-30 21:57:5369
[email protected]6f229e82010-11-02 17:47:2670 // Release extension1, and the info map should have the only ref.
[email protected]dc24976f2013-06-02 21:15:0971 const Extension* weak_extension1 = extension1.get();
[email protected]66e4eb32010-10-27 20:37:4172 extension1 = NULL;
[email protected]6f229e82010-11-02 17:47:2673 EXPECT_TRUE(weak_extension1->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5374
75 // Remove extension2, and the extension2 object should have the only ref.
[email protected]814a7bf0f2011-08-13 05:30:5976 info_map->RemoveExtension(
[email protected]b0af4792013-10-23 09:12:1377 extension2->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]6f229e82010-11-02 17:47:2678 EXPECT_TRUE(extension2->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5379
80 // Delete the info map, and the extension3 object should have the only ref.
81 info_map = NULL;
[email protected]6f229e82010-11-02 17:47:2682 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5383}
84
[email protected]38427a12013-11-09 17:34:2085// Tests that we can query a few extension properties from the InfoMap.
86TEST_F(InfoMapTest, Properties) {
87 scoped_refptr<InfoMap> info_map(new InfoMap());
[email protected]4361c7c2010-09-30 21:57:5388
[email protected]66e4eb32010-10-27 20:37:4189 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
90 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
[email protected]4361c7c2010-09-30 21:57:5391
[email protected]9afacd22013-11-13 20:23:3192 info_map->AddExtension(extension1.get(), base::Time(), false, false);
93 info_map->AddExtension(extension2.get(), base::Time(), false, false);
[email protected]4361c7c2010-09-30 21:57:5394
[email protected]be0a2cfd2011-06-02 21:36:4295 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()));
[email protected]4361c7c2010-09-30 21:57:5398}
99
[email protected]f30ee8b2014-08-15 15:20:22100// Tests that extension URLs are properly mapped to local file paths.
101TEST_F(InfoMapTest, MapUrlToLocalFilePath) {
102 scoped_refptr<InfoMap> info_map(new InfoMap());
[email protected]76e9211f2014-08-22 16:34:10103 scoped_refptr<Extension> app(CreateExtension("platform_app"));
[email protected]f30ee8b2014-08-15 15:20:22104 info_map->AddExtension(app.get(), base::Time(), false, false);
105
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());
112
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());
119
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());
126
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());
132}
133
[email protected]38427a12013-11-09 17:34:20134} // namespace extensions