[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 1 | // Copyright (c) 2012 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. |
| 4 | |
| 5 | #include "chrome/browser/extensions/test_extension_environment.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/json/json_writer.h" |
| 9 | #include "base/run_loop.h" |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_service.h" |
| 12 | #include "chrome/browser/extensions/test_extension_system.h" |
| 13 | #include "chrome/browser/sessions/session_tab_helper.h" |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 14 | #include "chrome/test/base/testing_profile.h" |
[email protected] | e375b8a | 2013-11-14 07:45:00 | [diff] [blame] | 15 | #include "content/public/test/test_utils.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 16 | #include "content/public/test/web_contents_tester.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 17 | #include "extensions/common/extension.h" |
[email protected] | 22b7b2c | 2013-11-05 22:52:42 | [diff] [blame] | 18 | #include "extensions/common/extension_builder.h" |
| 19 | #include "extensions/common/value_builder.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
[email protected] | ecca62b | 2013-10-09 16:18:53 | [diff] [blame] | 22 | #if defined(USE_AURA) |
| 23 | #include "ui/aura/env.h" |
| 24 | #endif |
| 25 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 26 | namespace extensions { |
| 27 | |
| 28 | using content::BrowserThread; |
| 29 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | scoped_ptr<base::DictionaryValue> MakeExtensionManifest( |
| 33 | const base::Value& manifest_extra) { |
| 34 | scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder() |
| 35 | .Set("name", "Extension") |
| 36 | .Set("version", "1.0") |
| 37 | .Set("manifest_version", 2) |
| 38 | .Build(); |
| 39 | const base::DictionaryValue* manifest_extra_dict; |
| 40 | if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) { |
| 41 | manifest->MergeDictionary(manifest_extra_dict); |
| 42 | } else { |
| 43 | std::string manifest_json; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame^] | 44 | base::JSONWriter::Write(manifest_extra, &manifest_json); |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 45 | ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\""; |
| 46 | } |
| 47 | return manifest; |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 52 | TestExtensionEnvironment::TestExtensionEnvironment() |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 53 | : profile_(new TestingProfile), |
| 54 | extension_service_(NULL), |
| 55 | extension_prefs_(NULL) { |
[email protected] | ecca62b | 2013-10-09 16:18:53 | [diff] [blame] | 56 | #if defined(USE_AURA) |
[email protected] | 5b883abb | 2014-05-05 06:44:10 | [diff] [blame] | 57 | aura::Env::CreateInstance(true); |
[email protected] | ecca62b | 2013-10-09 16:18:53 | [diff] [blame] | 58 | #endif |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TestExtensionEnvironment::~TestExtensionEnvironment() { |
[email protected] | ecca62b | 2013-10-09 16:18:53 | [diff] [blame] | 62 | #if defined(USE_AURA) |
| 63 | aura::Env::DeleteInstance(); |
| 64 | #endif |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 67 | TestingProfile* TestExtensionEnvironment::profile() const { |
| 68 | return profile_.get(); |
| 69 | } |
| 70 | |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 71 | TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() { |
[email protected] | 5fbbea6 | 2014-02-26 20:07:33 | [diff] [blame] | 72 | return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile())); |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 75 | ExtensionService* TestExtensionEnvironment::GetExtensionService() { |
| 76 | if (extension_service_ == NULL) { |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 77 | extension_service_ = GetExtensionSystem()->CreateExtensionService( |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 78 | base::CommandLine::ForCurrentProcess(), base::FilePath(), false); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 79 | } |
| 80 | return extension_service_; |
| 81 | } |
| 82 | |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 83 | ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() { |
| 84 | if (extension_prefs_ == NULL) { |
| 85 | extension_prefs_ = GetExtensionSystem()->CreateExtensionPrefs( |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 86 | base::CommandLine::ForCurrentProcess(), base::FilePath()); |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 87 | } |
| 88 | return extension_prefs_; |
| 89 | } |
| 90 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 91 | const Extension* TestExtensionEnvironment::MakeExtension( |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 92 | const base::Value& manifest_extra) { |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 93 | scoped_ptr<base::DictionaryValue> manifest = |
| 94 | MakeExtensionManifest(manifest_extra); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 95 | scoped_refptr<Extension> result = |
| 96 | ExtensionBuilder().SetManifest(manifest.Pass()).Build(); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 97 | GetExtensionService()->AddExtension(result.get()); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 98 | return result.get(); |
| 99 | } |
| 100 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 101 | const Extension* TestExtensionEnvironment::MakeExtension( |
| 102 | const base::Value& manifest_extra, |
| 103 | const std::string& id) { |
| 104 | scoped_ptr<base::DictionaryValue> manifest = |
| 105 | MakeExtensionManifest(manifest_extra); |
| 106 | scoped_refptr<Extension> result = |
| 107 | ExtensionBuilder().SetManifest(manifest.Pass()).SetID(id).Build(); |
| 108 | GetExtensionService()->AddExtension(result.get()); |
| 109 | return result.get(); |
| 110 | } |
| 111 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 112 | scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const { |
| 113 | scoped_ptr<content::WebContents> contents( |
| 114 | content::WebContentsTester::CreateTestWebContents(profile(), NULL)); |
| 115 | // Create a tab id. |
| 116 | SessionTabHelper::CreateForWebContents(contents.get()); |
| 117 | return contents.Pass(); |
| 118 | } |
| 119 | |
| 120 | } // namespace extensions |