| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/test_extension_environment.h" | 5 #include "chrome/browser/extensions/test_extension_environment.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/test_extension_system.h" | 12 #include "chrome/browser/extensions/test_extension_system.h" |
| 13 #include "chrome/browser/sessions/session_tab_helper.h" | 13 #include "chrome/browser/sessions/session_tab_helper.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 17 #include "content/public/test/web_contents_tester.h" | 17 #include "content/public/test/web_contents_tester.h" |
| 18 #include "extensions/browser/extension_prefs.h" | 18 #include "extensions/browser/extension_prefs.h" |
| 19 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/extension_builder.h" | 20 #include "extensions/common/extension_builder.h" |
| 21 #include "extensions/common/value_builder.h" | 21 #include "extensions/common/value_builder.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 #if defined(OS_CHROMEOS) |
| 25 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
| 26 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 27 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 28 #endif |
| 29 |
| 24 #if defined(USE_AURA) | 30 #if defined(USE_AURA) |
| 25 #include "ui/aura/env.h" | 31 #include "ui/aura/env.h" |
| 26 #endif | 32 #endif |
| 27 | 33 |
| 28 namespace extensions { | 34 namespace extensions { |
| 29 | 35 |
| 30 using content::BrowserThread; | 36 using content::BrowserThread; |
| 31 | 37 |
| 32 namespace { | 38 namespace { |
| 33 | 39 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 57 .Set("app", extensions::DictionaryBuilder().Set( | 63 .Set("app", extensions::DictionaryBuilder().Set( |
| 58 "background", | 64 "background", |
| 59 extensions::DictionaryBuilder().Set( | 65 extensions::DictionaryBuilder().Set( |
| 60 "scripts", | 66 "scripts", |
| 61 extensions::ListBuilder().Append("background.js")))) | 67 extensions::ListBuilder().Append("background.js")))) |
| 62 .Build(); | 68 .Build(); |
| 63 } | 69 } |
| 64 | 70 |
| 65 } // namespace | 71 } // namespace |
| 66 | 72 |
| 73 // Extra environment state required for ChromeOS. |
| 74 class TestExtensionEnvironment::ChromeOSEnv { |
| 75 public: |
| 76 ChromeOSEnv() {} |
| 77 |
| 78 private: |
| 79 #if defined(OS_CHROMEOS) |
| 80 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 81 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 82 chromeos::ScopedTestUserManager test_user_manager_; |
| 83 #endif |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(ChromeOSEnv); |
| 86 }; |
| 87 |
| 67 // static | 88 // static |
| 68 ExtensionService* TestExtensionEnvironment::CreateExtensionServiceForProfile( | 89 ExtensionService* TestExtensionEnvironment::CreateExtensionServiceForProfile( |
| 69 TestingProfile* profile) { | 90 TestingProfile* profile) { |
| 70 TestExtensionSystem* extension_system = | 91 TestExtensionSystem* extension_system = |
| 71 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile)); | 92 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile)); |
| 72 return extension_system->CreateExtensionService( | 93 return extension_system->CreateExtensionService( |
| 73 base::CommandLine::ForCurrentProcess(), base::FilePath(), false); | 94 base::CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 74 } | 95 } |
| 75 | 96 |
| 76 TestExtensionEnvironment::TestExtensionEnvironment() | 97 TestExtensionEnvironment::TestExtensionEnvironment() |
| 77 : thread_bundle_(new content::TestBrowserThreadBundle), | 98 : thread_bundle_(new content::TestBrowserThreadBundle), |
| 78 profile_(new TestingProfile), | |
| 79 extension_service_(nullptr) { | 99 extension_service_(nullptr) { |
| 80 #if defined(USE_AURA) | 100 Init(); |
| 81 aura::Env::CreateInstance(true); | |
| 82 #endif | |
| 83 } | 101 } |
| 84 | 102 |
| 85 TestExtensionEnvironment::TestExtensionEnvironment( | 103 TestExtensionEnvironment::TestExtensionEnvironment( |
| 86 base::MessageLoopForUI* message_loop) | 104 base::MessageLoopForUI* message_loop) |
| 87 : profile_(new TestingProfile), extension_service_(nullptr) { | 105 : extension_service_(nullptr) { |
| 106 Init(); |
| 107 } |
| 108 |
| 109 void TestExtensionEnvironment::Init() { |
| 110 profile_.reset(new TestingProfile); |
| 111 #if defined(OS_CHROMEOS) |
| 112 if (!chromeos::DeviceSettingsService::IsInitialized()) |
| 113 chromeos_env_.reset(new ChromeOSEnv); |
| 114 #endif |
| 88 #if defined(USE_AURA) | 115 #if defined(USE_AURA) |
| 89 aura::Env::CreateInstance(true); | 116 aura::Env::CreateInstance(true); |
| 90 #endif | 117 #endif |
| 91 } | 118 } |
| 92 | 119 |
| 93 TestExtensionEnvironment::~TestExtensionEnvironment() { | 120 TestExtensionEnvironment::~TestExtensionEnvironment() { |
| 94 #if defined(USE_AURA) | 121 #if defined(USE_AURA) |
| 95 aura::Env::DeleteInstance(); | 122 aura::Env::DeleteInstance(); |
| 96 #endif | 123 #endif |
| 97 } | 124 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 ExtensionBuilder().SetManifest(manifest.Pass()).SetID(id).Build(); | 160 ExtensionBuilder().SetManifest(manifest.Pass()).SetID(id).Build(); |
| 134 GetExtensionService()->AddExtension(result.get()); | 161 GetExtensionService()->AddExtension(result.get()); |
| 135 return result.get(); | 162 return result.get(); |
| 136 } | 163 } |
| 137 | 164 |
| 138 scoped_refptr<Extension> TestExtensionEnvironment::MakePackagedApp( | 165 scoped_refptr<Extension> TestExtensionEnvironment::MakePackagedApp( |
| 139 const std::string& id, | 166 const std::string& id, |
| 140 bool install) { | 167 bool install) { |
| 141 scoped_refptr<Extension> result = ExtensionBuilder() | 168 scoped_refptr<Extension> result = ExtensionBuilder() |
| 142 .SetManifest(MakePackagedAppManifest()) | 169 .SetManifest(MakePackagedAppManifest()) |
| 170 .AddFlags(Extension::FROM_WEBSTORE) |
| 143 .SetID(id) | 171 .SetID(id) |
| 144 .Build(); | 172 .Build(); |
| 145 if (install) | 173 if (install) |
| 146 GetExtensionService()->AddExtension(result.get()); | 174 GetExtensionService()->AddExtension(result.get()); |
| 147 return result; | 175 return result; |
| 148 } | 176 } |
| 149 | 177 |
| 150 scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const { | 178 scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const { |
| 151 scoped_ptr<content::WebContents> contents( | 179 scoped_ptr<content::WebContents> contents( |
| 152 content::WebContentsTester::CreateTestWebContents(profile(), NULL)); | 180 content::WebContentsTester::CreateTestWebContents(profile(), NULL)); |
| 153 // Create a tab id. | 181 // Create a tab id. |
| 154 SessionTabHelper::CreateForWebContents(contents.get()); | 182 SessionTabHelper::CreateForWebContents(contents.get()); |
| 155 return contents.Pass(); | 183 return contents.Pass(); |
| 156 } | 184 } |
| 157 | 185 |
| 158 void TestExtensionEnvironment::DeleteProfile() { | 186 void TestExtensionEnvironment::DeleteProfile() { |
| 159 profile_.reset(); | 187 profile_.reset(); |
| 160 extension_service_ = nullptr; | 188 extension_service_ = nullptr; |
| 161 } | 189 } |
| 162 | 190 |
| 163 } // namespace extensions | 191 } // namespace extensions |
| OLD | NEW |