[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b19fe57 | 2013-07-18 04:54:26 | [diff] [blame] | 5 | #include "base/message_loop/message_loop.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 6 | #include "base/path_service.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame] | 7 | #include "content/public/test/test_browser_thread.h" |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 8 | #include "extensions/browser/info_map.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 9 | #include "extensions/common/extension.h" |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 10 | #include "extensions/common/extension_paths.h" |
[email protected] | 6bf9061 | 2013-08-15 00:36:27 | [diff] [blame] | 11 | #include "extensions/common/manifest_constants.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 14 | using content::BrowserThread; |
| 15 | |
[email protected] | 6bf9061 | 2013-08-15 00:36:27 | [diff] [blame] | 16 | namespace keys = extensions::manifest_keys; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 17 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 18 | namespace extensions { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 19 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 20 | class InfoMapTest : public testing::Test { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 21 | public: |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 22 | InfoMapTest() |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 23 | : ui_thread_(BrowserThread::UI, &message_loop_), |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 24 | io_thread_(BrowserThread::IO, &message_loop_) {} |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 25 | |
| 26 | private: |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 27 | base::MessageLoop message_loop_; |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 28 | content::TestBrowserThread ui_thread_; |
| 29 | content::TestBrowserThread io_thread_; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | // Returns a barebones test Extension object with the given name. |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 33 | static scoped_refptr<Extension> CreateExtension(const std::string& name) { |
[email protected] | f30ee8b | 2014-08-15 15:20:22 | [diff] [blame] | 34 | base::FilePath path; |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 35 | PathService::Get(DIR_TEST_DATA, &path); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 36 | |
[email protected] | 4b3006f | 2013-12-23 22:23:08 | [diff] [blame] | 37 | base::DictionaryValue manifest; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 38 | manifest.SetString(keys::kVersion, "1.0.0.0"); |
| 39 | manifest.SetString(keys::kName, name); |
| 40 | |
| 41 | std::string error; |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 42 | scoped_refptr<Extension> extension = |
| 43 | Extension::Create(path.AppendASCII(name), |
| 44 | Manifest::INVALID_LOCATION, |
| 45 | manifest, |
| 46 | Extension::NO_FLAGS, |
| 47 | &error); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 48 | EXPECT_TRUE(extension.get()) << error; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 49 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 50 | return extension; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 53 | // Test that the InfoMap handles refcounting properly. |
| 54 | TEST_F(InfoMapTest, RefCounting) { |
| 55 | scoped_refptr<InfoMap> info_map(new InfoMap()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 56 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 57 | // New extensions should have a single reference holding onto them. |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 58 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 59 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
| 60 | scoped_refptr<Extension> extension3(CreateExtension("extension3")); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 61 | EXPECT_TRUE(extension1->HasOneRef()); |
| 62 | EXPECT_TRUE(extension2->HasOneRef()); |
| 63 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 64 | |
[email protected] | 05cc4e7 | 2011-03-08 21:29:48 | [diff] [blame] | 65 | // Add a ref to each extension and give it to the info map. |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 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); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 69 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 70 | // Release extension1, and the info map should have the only ref. |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 71 | const Extension* weak_extension1 = extension1.get(); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 72 | extension1 = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 73 | EXPECT_TRUE(weak_extension1->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 74 | |
| 75 | // Remove extension2, and the extension2 object should have the only ref. |
[email protected] | 814a7bf0f | 2011-08-13 05:30:59 | [diff] [blame] | 76 | info_map->RemoveExtension( |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 77 | extension2->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 78 | EXPECT_TRUE(extension2->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 79 | |
| 80 | // Delete the info map, and the extension3 object should have the only ref. |
| 81 | info_map = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 82 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 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()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 88 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 89 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 90 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 91 | |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 92 | info_map->AddExtension(extension1.get(), base::Time(), false, false); |
| 93 | info_map->AddExtension(extension2.get(), base::Time(), false, false); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 94 | |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 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())); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | f30ee8b | 2014-08-15 15:20:22 | [diff] [blame] | 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()); |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 103 | scoped_refptr<Extension> app(CreateExtension("platform_app")); |
[email protected] | f30ee8b | 2014-08-15 15:20:22 | [diff] [blame] | 104 | 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] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 134 | } // namespace extensions |