[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" |
| 14 | #include "chrome/common/extensions/extension.h" |
| 15 | #include "chrome/common/extensions/extension_builder.h" |
| 16 | #include "chrome/common/extensions/value_builder.h" |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 17 | #include "chrome/test/base/testing_profile.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 18 | #include "content/public/test/web_contents_tester.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
| 21 | namespace extensions { |
| 22 | |
| 23 | using content::BrowserThread; |
| 24 | |
| 25 | TestExtensionEnvironment::TestExtensionEnvironment() |
| 26 | : ui_thread_(BrowserThread::UI, &loop_), |
| 27 | file_thread_(BrowserThread::FILE), |
| 28 | file_blocking_thread_(BrowserThread::FILE_USER_BLOCKING), |
| 29 | io_thread_(BrowserThread::IO), |
| 30 | profile_(new TestingProfile), |
| 31 | extension_service_(NULL) { |
| 32 | file_thread_.Start(); |
| 33 | file_blocking_thread_.Start(); |
| 34 | io_thread_.StartIOThread(); |
| 35 | } |
| 36 | |
| 37 | TestExtensionEnvironment::~TestExtensionEnvironment() { |
| 38 | profile_.reset(); |
| 39 | // Delete the profile, and then cycle the message loop to clear |
| 40 | // out delayed deletions. |
[email protected] | b8afde4 | 2013-05-29 03:55:40 | [diff] [blame] | 41 | base::RunLoop().RunUntilIdle(); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 42 | } |
| 43 | |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 44 | TestingProfile* TestExtensionEnvironment::profile() const { |
| 45 | return profile_.get(); |
| 46 | } |
| 47 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 48 | ExtensionService* TestExtensionEnvironment::GetExtensionService() { |
| 49 | if (extension_service_ == NULL) { |
| 50 | TestExtensionSystem* extension_system = |
| 51 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile())); |
| 52 | extension_service_ = extension_system->CreateExtensionService( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 53 | CommandLine::ForCurrentProcess(), base::FilePath(), false); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 54 | } |
| 55 | return extension_service_; |
| 56 | } |
| 57 | |
| 58 | const Extension* TestExtensionEnvironment::MakeExtension( |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 59 | const base::Value& manifest_extra) { |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 60 | scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder() |
| 61 | .Set("name", "Extension") |
| 62 | .Set("version", "1.0") |
| 63 | .Set("manifest_version", 2) |
| 64 | .Build(); |
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 65 | const base::DictionaryValue* manifest_extra_dict; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 66 | if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) { |
| 67 | manifest->MergeDictionary(manifest_extra_dict); |
| 68 | } else { |
| 69 | std::string manifest_json; |
| 70 | base::JSONWriter::Write(&manifest_extra, &manifest_json); |
| 71 | ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\""; |
| 72 | } |
| 73 | |
| 74 | scoped_refptr<Extension> result = |
| 75 | ExtensionBuilder().SetManifest(manifest.Pass()).Build(); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame^] | 76 | GetExtensionService()->AddExtension(result.get()); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 77 | return result.get(); |
| 78 | } |
| 79 | |
| 80 | scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const { |
| 81 | scoped_ptr<content::WebContents> contents( |
| 82 | content::WebContentsTester::CreateTestWebContents(profile(), NULL)); |
| 83 | // Create a tab id. |
| 84 | SessionTabHelper::CreateForWebContents(contents.get()); |
| 85 | return contents.Pass(); |
| 86 | } |
| 87 | |
| 88 | } // namespace extensions |