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