1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_apitest.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/test/ui_test_utils.h"
ExtensionApiTest::ResultCatcher::ResultCatcher()
: profile_restriction_(NULL),
waiting_(false) {
registrar_.Add(this, NotificationType::EXTENSION_TEST_PASSED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSION_TEST_FAILED,
NotificationService::AllSources());
}
ExtensionApiTest::ResultCatcher::~ResultCatcher() {
}
bool ExtensionApiTest::ResultCatcher::GetNextResult() {
// Depending on the tests, multiple results can come in from a single call
// to RunMessageLoop(), so we maintain a queue of results and just pull them
// off as the test calls this, going to the run loop only when the queue is
// empty.
if (results_.empty()) {
waiting_ = true;
ui_test_utils::RunMessageLoop();
waiting_ = false;
}
if (!results_.empty()) {
bool ret = results_.front();
results_.pop_front();
message_ = messages_.front();
messages_.pop_front();
return ret;
}
NOTREACHED();
return false;
}
void ExtensionApiTest::ResultCatcher::Observe(
NotificationType type, const NotificationSource& source,
const NotificationDetails& details) {
if (profile_restriction_ &&
Source<Profile>(source).ptr() != profile_restriction_) {
return;
}
switch (type.value) {
case NotificationType::EXTENSION_TEST_PASSED:
std::cout << "Got EXTENSION_TEST_PASSED notification.\n";
results_.push_back(true);
messages_.push_back("");
if (waiting_)
MessageLoopForUI::current()->Quit();
break;
case NotificationType::EXTENSION_TEST_FAILED:
std::cout << "Got EXTENSION_TEST_FAILED notification.\n";
results_.push_back(false);
messages_.push_back(*(Details<std::string>(details).ptr()));
if (waiting_)
MessageLoopForUI::current()->Quit();
break;
default:
NOTREACHED();
}
}
bool ExtensionApiTest::RunExtensionTest(const char* extension_name) {
return RunExtensionTestImpl(extension_name, "");
}
bool ExtensionApiTest::RunExtensionSubtest(const char* extension_name,
const std::string& page_url) {
DCHECK(!page_url.empty()) << "Argument page_url is required.";
return RunExtensionTestImpl(extension_name, page_url);
}
bool ExtensionApiTest::RunPageTest(const std::string& page_url) {
return RunExtensionSubtest("", page_url);
}
// Load |extension_name| extension and/or |page_url| and wait for
// PASSED or FAILED notification.
bool ExtensionApiTest::RunExtensionTestImpl(const char* extension_name,
const std::string& page_url) {
ResultCatcher catcher;
DCHECK(!std::string(extension_name).empty() || !page_url.empty()) <<
"extension_name and page_url cannot both be empty";
LOG(INFO) << "Running ExtensionApiTest";
if (!std::string(extension_name).empty()) {
LOG(INFO) << "Loading Extension: " << extension_name;
if (!LoadExtension(test_data_dir_.AppendASCII(extension_name))) {
message_ = "Failed to load extension.";
return false;
}
}
// If there is a page_url to load, navigate it.
if (!page_url.empty()) {
GURL url = GURL(page_url);
// Note: We use is_valid() here in the expectation that the provided url
// may lack a scheme & host and thus be a relative url within the loaded
// extension.
if (!url.is_valid()) {
DCHECK(!std::string(extension_name).empty()) <<
"Relative page_url given with no extension_name";
ExtensionsService* service = browser()->profile()->GetExtensionsService();
Extension* extension =
service->GetExtensionById(last_loaded_extension_id_, false);
if (!extension)
return false;
url = extension->GetResourceURL(page_url);
}
LOG(ERROR) << "Loading page url: " << url.spec();
ui_test_utils::NavigateToURL(browser(), url);
}
if (!catcher.GetNextResult()) {
message_ = catcher.message();
return false;
} else {
return true;
}
}
// Test that exactly one extension loaded.
Extension* ExtensionApiTest::GetSingleLoadedExtension() {
ExtensionsService* service = browser()->profile()->GetExtensionsService();
int found_extension_index = -1;
for (size_t i = 0; i < service->extensions()->size(); ++i) {
// Ignore any component extensions. They are automatically loaded into all
// profiles and aren't the extension we're looking for here.
if (service->extensions()->at(i)->location() == Extension::COMPONENT)
continue;
if (found_extension_index != -1) {
message_ = base::StringPrintf(
"Expected only one extension to be present. Found %u.",
static_cast<unsigned>(service->extensions()->size()));
return NULL;
}
found_extension_index = static_cast<int>(i);
}
Extension* extension = service->extensions()->at(found_extension_index);
if (!extension) {
message_ = "extension pointer is NULL.";
return NULL;
}
return extension;
}
void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) {
ExtensionBrowserTest::SetUpCommandLine(command_line);
test_data_dir_ = test_data_dir_.AppendASCII("api_test");
}
|