blob: 6914ab089a2f568170733642275a23b6e30c2db2 [file] [log] [blame]
[email protected]11d42c82013-02-01 00:14:321// 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]e0ec3cb12013-04-03 17:35:3010#include "base/values.h"
[email protected]11d42c82013-02-01 00:14:3211#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]e0ec3cb12013-04-03 17:35:3014#include "chrome/test/base/testing_profile.h"
[email protected]e375b8a2013-11-14 07:45:0015#include "content/public/test/test_utils.h"
[email protected]11d42c82013-02-01 00:14:3216#include "content/public/test/web_contents_tester.h"
[email protected]e4452d32013-11-15 23:07:4117#include "extensions/common/extension.h"
[email protected]22b7b2c2013-11-05 22:52:4218#include "extensions/common/extension_builder.h"
19#include "extensions/common/value_builder.h"
[email protected]11d42c82013-02-01 00:14:3220#include "testing/gtest/include/gtest/gtest.h"
21
[email protected]ecca62b2013-10-09 16:18:5322#if defined(USE_AURA)
23#include "ui/aura/env.h"
24#endif
25
[email protected]11d42c82013-02-01 00:14:3226namespace extensions {
27
28using content::BrowserThread;
29
jackhouc587f302015-04-13 08:16:3930namespace {
31
32scoped_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;
estade8d046462015-05-16 01:02:3444 base::JSONWriter::Write(manifest_extra, &manifest_json);
jackhouc587f302015-04-13 08:16:3945 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\"";
46 }
47 return manifest;
48}
49
50} // namespace
51
[email protected]11d42c82013-02-01 00:14:3252TestExtensionEnvironment::TestExtensionEnvironment()
[email protected]94cf31c2013-12-07 03:58:3253 : profile_(new TestingProfile),
54 extension_service_(NULL),
55 extension_prefs_(NULL) {
[email protected]ecca62b2013-10-09 16:18:5356#if defined(USE_AURA)
[email protected]5b883abb2014-05-05 06:44:1057 aura::Env::CreateInstance(true);
[email protected]ecca62b2013-10-09 16:18:5358#endif
[email protected]11d42c82013-02-01 00:14:3259}
60
61TestExtensionEnvironment::~TestExtensionEnvironment() {
[email protected]ecca62b2013-10-09 16:18:5362#if defined(USE_AURA)
63 aura::Env::DeleteInstance();
64#endif
[email protected]11d42c82013-02-01 00:14:3265}
66
[email protected]e0ec3cb12013-04-03 17:35:3067TestingProfile* TestExtensionEnvironment::profile() const {
68 return profile_.get();
69}
70
[email protected]94cf31c2013-12-07 03:58:3271TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() {
[email protected]5fbbea62014-02-26 20:07:3372 return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
[email protected]94cf31c2013-12-07 03:58:3273}
74
[email protected]11d42c82013-02-01 00:14:3275ExtensionService* TestExtensionEnvironment::GetExtensionService() {
76 if (extension_service_ == NULL) {
[email protected]94cf31c2013-12-07 03:58:3277 extension_service_ = GetExtensionSystem()->CreateExtensionService(
avi3ef9ec9e2014-12-22 22:50:1778 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
[email protected]11d42c82013-02-01 00:14:3279 }
80 return extension_service_;
81}
82
[email protected]94cf31c2013-12-07 03:58:3283ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() {
84 if (extension_prefs_ == NULL) {
85 extension_prefs_ = GetExtensionSystem()->CreateExtensionPrefs(
avi3ef9ec9e2014-12-22 22:50:1786 base::CommandLine::ForCurrentProcess(), base::FilePath());
[email protected]94cf31c2013-12-07 03:58:3287 }
88 return extension_prefs_;
89}
90
[email protected]11d42c82013-02-01 00:14:3291const Extension* TestExtensionEnvironment::MakeExtension(
[email protected]e0ec3cb12013-04-03 17:35:3092 const base::Value& manifest_extra) {
jackhouc587f302015-04-13 08:16:3993 scoped_ptr<base::DictionaryValue> manifest =
94 MakeExtensionManifest(manifest_extra);
[email protected]11d42c82013-02-01 00:14:3295 scoped_refptr<Extension> result =
96 ExtensionBuilder().SetManifest(manifest.Pass()).Build();
[email protected]dc24976f2013-06-02 21:15:0997 GetExtensionService()->AddExtension(result.get());
[email protected]11d42c82013-02-01 00:14:3298 return result.get();
99}
100
jackhouc587f302015-04-13 08:16:39101const 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]11d42c82013-02-01 00:14:32112scoped_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