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 "base/values.h" 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" 17 #include "chrome/test/base/testing_profile.h" 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. 41 base::RunLoop().RunUntilIdle(); 42 } 43 44 TestingProfile* TestExtensionEnvironment::profile() const { 45 return profile_.get(); 46 } 47 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( 53 CommandLine::ForCurrentProcess(), base::FilePath(), false); 54 } 55 return extension_service_; 56 } 57 58 const Extension* TestExtensionEnvironment::MakeExtension( 59 const base::Value& manifest_extra) { 60 scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder() 61 .Set("name", "Extension") 62 .Set("version", "1.0") 63 .Set("manifest_version", 2) 64 .Build(); 65 const base::DictionaryValue* manifest_extra_dict; 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(); 76 GetExtensionService()->AddExtension(result.get()); 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 89