Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: chrome/browser/extensions/test_extension_environment.cc

Issue 1175783003: Split out Ash dependency in app info dialog unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split out ScopedViewsTestHelper Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_utils.h" 16 #include "content/public/test/test_utils.h"
16 #include "content/public/test/web_contents_tester.h" 17 #include "content/public/test/web_contents_tester.h"
17 #include "extensions/browser/extension_prefs.h" 18 #include "extensions/browser/extension_prefs.h"
18 #include "extensions/common/extension.h" 19 #include "extensions/common/extension.h"
19 #include "extensions/common/extension_builder.h" 20 #include "extensions/common/extension_builder.h"
20 #include "extensions/common/value_builder.h" 21 #include "extensions/common/value_builder.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 #if defined(USE_AURA) 24 #if defined(USE_AURA)
24 #include "ui/aura/env.h" 25 #include "ui/aura/env.h"
(...skipping 16 matching lines...) Expand all
41 if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) { 42 if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) {
42 manifest->MergeDictionary(manifest_extra_dict); 43 manifest->MergeDictionary(manifest_extra_dict);
43 } else { 44 } else {
44 std::string manifest_json; 45 std::string manifest_json;
45 base::JSONWriter::Write(manifest_extra, &manifest_json); 46 base::JSONWriter::Write(manifest_extra, &manifest_json);
46 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\""; 47 ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\"";
47 } 48 }
48 return manifest; 49 return manifest;
49 } 50 }
50 51
52 scoped_ptr<base::DictionaryValue> MakePackagedAppManifest() {
53 return extensions::DictionaryBuilder()
54 .Set("name", "Test App Name")
55 .Set("version", "2.0")
56 .Set("manifest_version", 2)
57 .Set("app", extensions::DictionaryBuilder().Set(
58 "background",
59 extensions::DictionaryBuilder().Set(
60 "scripts",
61 extensions::ListBuilder().Append("background.js"))))
62 .Build();
63 }
64
51 } // namespace 65 } // namespace
52 66
67 // static
68 ExtensionService* TestExtensionEnvironment::CreateExtensionServiceForProfile(
69 TestingProfile* profile) {
70 TestExtensionSystem* extension_system =
71 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile));
72 return extension_system->CreateExtensionService(
73 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
74 }
75
53 TestExtensionEnvironment::TestExtensionEnvironment() 76 TestExtensionEnvironment::TestExtensionEnvironment()
54 : profile_(new TestingProfile), 77 : thread_bundle_(new content::TestBrowserThreadBundle),
55 extension_service_(NULL), 78 profile_(new TestingProfile),
56 extension_prefs_(NULL) { 79 extension_service_(nullptr) {
57 #if defined(USE_AURA) 80 #if defined(USE_AURA)
58 aura::Env::CreateInstance(true); 81 aura::Env::CreateInstance(true);
59 #endif 82 #endif
83 }
84
85 TestExtensionEnvironment::TestExtensionEnvironment(
86 base::MessageLoopForUI* message_loop)
87 : profile_(new TestingProfile), extension_service_(nullptr) {
88 #if defined(USE_AURA)
89 aura::Env::CreateInstance(true);
90 #endif
60 } 91 }
61 92
62 TestExtensionEnvironment::~TestExtensionEnvironment() { 93 TestExtensionEnvironment::~TestExtensionEnvironment() {
63 #if defined(USE_AURA) 94 #if defined(USE_AURA)
64 aura::Env::DeleteInstance(); 95 aura::Env::DeleteInstance();
65 #endif 96 #endif
66 } 97 }
67 98
68 TestingProfile* TestExtensionEnvironment::profile() const { 99 TestingProfile* TestExtensionEnvironment::profile() const {
69 return profile_.get(); 100 return profile_.get();
70 } 101 }
71 102
72 TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() { 103 TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() {
73 return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile())); 104 return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
74 } 105 }
75 106
76 ExtensionService* TestExtensionEnvironment::GetExtensionService() { 107 ExtensionService* TestExtensionEnvironment::GetExtensionService() {
77 if (extension_service_ == NULL) { 108 if (!extension_service_)
78 extension_service_ = GetExtensionSystem()->CreateExtensionService( 109 extension_service_ = CreateExtensionServiceForProfile(profile());
79 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
80 }
81 return extension_service_; 110 return extension_service_;
82 } 111 }
83 112
84 ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() { 113 ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() {
85 if (extension_prefs_ == NULL) 114 return ExtensionPrefs::Get(profile_.get());
86 extension_prefs_ = ExtensionPrefs::Get(profile_.get());
87 return extension_prefs_;
88 } 115 }
89 116
90 const Extension* TestExtensionEnvironment::MakeExtension( 117 const Extension* TestExtensionEnvironment::MakeExtension(
91 const base::Value& manifest_extra) { 118 const base::Value& manifest_extra) {
92 scoped_ptr<base::DictionaryValue> manifest = 119 scoped_ptr<base::DictionaryValue> manifest =
93 MakeExtensionManifest(manifest_extra); 120 MakeExtensionManifest(manifest_extra);
94 scoped_refptr<Extension> result = 121 scoped_refptr<Extension> result =
95 ExtensionBuilder().SetManifest(manifest.Pass()).Build(); 122 ExtensionBuilder().SetManifest(manifest.Pass()).Build();
96 GetExtensionService()->AddExtension(result.get()); 123 GetExtensionService()->AddExtension(result.get());
97 return result.get(); 124 return result.get();
98 } 125 }
99 126
100 const Extension* TestExtensionEnvironment::MakeExtension( 127 const Extension* TestExtensionEnvironment::MakeExtension(
101 const base::Value& manifest_extra, 128 const base::Value& manifest_extra,
102 const std::string& id) { 129 const std::string& id) {
103 scoped_ptr<base::DictionaryValue> manifest = 130 scoped_ptr<base::DictionaryValue> manifest =
104 MakeExtensionManifest(manifest_extra); 131 MakeExtensionManifest(manifest_extra);
105 scoped_refptr<Extension> result = 132 scoped_refptr<Extension> result =
106 ExtensionBuilder().SetManifest(manifest.Pass()).SetID(id).Build(); 133 ExtensionBuilder().SetManifest(manifest.Pass()).SetID(id).Build();
107 GetExtensionService()->AddExtension(result.get()); 134 GetExtensionService()->AddExtension(result.get());
108 return result.get(); 135 return result.get();
109 } 136 }
110 137
138 scoped_refptr<Extension> TestExtensionEnvironment::MakePackagedApp(
139 const std::string& id,
140 bool install) {
141 scoped_refptr<Extension> result = ExtensionBuilder()
142 .SetManifest(MakePackagedAppManifest())
143 .SetID(id)
144 .Build();
145 if (install)
146 GetExtensionService()->AddExtension(result.get());
147 return result;
148 }
149
111 scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const { 150 scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const {
112 scoped_ptr<content::WebContents> contents( 151 scoped_ptr<content::WebContents> contents(
113 content::WebContentsTester::CreateTestWebContents(profile(), NULL)); 152 content::WebContentsTester::CreateTestWebContents(profile(), NULL));
114 // Create a tab id. 153 // Create a tab id.
115 SessionTabHelper::CreateForWebContents(contents.get()); 154 SessionTabHelper::CreateForWebContents(contents.get());
116 return contents.Pass(); 155 return contents.Pass();
117 } 156 }
118 157
158 void TestExtensionEnvironment::DeleteProfile() {
159 profile_.reset();
160 extension_service_ = nullptr;
161 }
162
119 } // namespace extensions 163 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698