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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// Copyright (c) 2009 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 "base/command_line.h"
#include "base/gfx/rect.h"
#include "base/json_reader.h"
#include "base/json_writer.h"
#include "base/values.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/automation/automation_proxy_uitest.h"
#include "chrome/test/automation/extension_automation_constants.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/ui/ui_test.h"
#include "googleurl/src/gurl.h"
namespace {
static const char kTestDirectorySimpleApiCall[] =
"extensions/uitest/simple_api_call";
static const char kTestDirectoryRoundtripApiCall[] =
"extensions/uitest/roundtrip_api_call";
// Base class to test extensions almost end-to-end by including browser
// startup, manifest parsing, and the actual process model in the
// equation. This would also let you write UITests that test individual
// Chrome Extensions as running in Chrome. Takes over implementation of
// extension API calls so that behavior can be tested deterministically
// through code, instead of having to contort the browser into a state
// suitable for testing.
template <class ParentTestType>
class ExtensionUITest : public ParentTestType {
public:
explicit ExtensionUITest(const std::string& extension_path) {
launch_arguments_.AppendSwitch(switches::kEnableExtensions);
FilePath filename(test_data_directory_);
filename = filename.AppendASCII(extension_path);
launch_arguments_.AppendSwitchWithValue(switches::kLoadExtension,
filename.value());
}
void SetUp() {
ParentTestType::SetUp();
automation()->SetEnableExtensionAutomation(true);
}
void TearDown() {
automation()->SetEnableExtensionAutomation(false);
ParentTestType::TearDown();
}
void TestWithURL(const GURL& url) {
HWND external_tab_container = NULL;
scoped_ptr<TabProxy> tab(automation()->CreateExternalTab(NULL, gfx::Rect(),
WS_POPUP, false, &external_tab_container));
ASSERT_TRUE(tab != NULL);
ASSERT_NE(FALSE, ::IsWindow(external_tab_container));
DoAdditionalPreNavigateSetup(tab.get());
// We explicitly do not make this a toolstrip in the extension manifest,
// so that the test can control when it gets loaded, and so that we test
// the intended behavior that tabs should be able to show extension pages
// (useful for development etc.)
tab->NavigateInExternalTab(url);
EXPECT_EQ(true, ExternalTabMessageLoop(external_tab_container, 5000));
// Since the tab goes away lazily, wait a bit.
PlatformThread::Sleep(1000);
EXPECT_FALSE(tab->is_valid());
}
// Override if you need additional stuff before we navigate the page.
virtual void DoAdditionalPreNavigateSetup(TabProxy* tab) {
}
private:
DISALLOW_EVIL_CONSTRUCTORS(ExtensionUITest);
};
// For tests that only need to check for a single postMessage
// being received from the tab in Chrome. These tests can send a message
// to the tab before receiving the new message, but there will not be
// a chance to respond by sending a message from the test to the tab after
// the postMessage is received.
typedef ExtensionUITest<ExternalTabTestType> SingleMessageExtensionUITest;
// A test that loads a basic extension that makes an API call that does
// not require a response.
class SimpleApiCallExtensionTest : public SingleMessageExtensionUITest {
public:
SimpleApiCallExtensionTest()
: SingleMessageExtensionUITest(kTestDirectorySimpleApiCall) {
}
private:
DISALLOW_COPY_AND_ASSIGN(SimpleApiCallExtensionTest);
};
// TODO(port) Should become portable once ExternalTabMessageLoop is ported.
#if defined(OS_WIN)
TEST_F(SimpleApiCallExtensionTest, RunTest) {
namespace keys = extension_automation_constants;
TestWithURL(GURL(
"chrome-extension://77774444789ABCDEF0123456789ABCDEF0123456/test.html"));
AutomationProxyForExternalTab* proxy =
static_cast<AutomationProxyForExternalTab*>(automation());
ASSERT_GT(proxy->messages_received(), 0);
// Using EXPECT_TRUE rather than EXPECT_EQ as the compiler (VC++) isn't
// finding the right match for EqHelper.
EXPECT_TRUE(proxy->origin() == keys::kAutomationOrigin);
EXPECT_TRUE(proxy->target() == keys::kAutomationRequestTarget);
scoped_ptr<Value> message_value(JSONReader::Read(proxy->message(), false));
ASSERT_TRUE(message_value->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* message_dict =
reinterpret_cast<DictionaryValue*>(message_value.get());
std::string result;
message_dict->GetString(keys::kAutomationNameKey, &result);
EXPECT_EQ(result, "RemoveTab");
result = "";
message_dict->GetString(keys::kAutomationArgsKey, &result);
EXPECT_NE(result, "");
int callback_id = 0xBAADF00D;
message_dict->GetInteger(keys::kAutomationRequestIdKey, &callback_id);
EXPECT_NE(callback_id, 0xBAADF00D);
bool has_callback = true;
EXPECT_TRUE(message_dict->GetBoolean(keys::kAutomationHasCallbackKey,
&has_callback));
EXPECT_FALSE(has_callback);
}
#endif // defined(OS_WIN)
// A base class for an automation proxy that checks several messages in
// a row.
class MultiMessageAutomationProxy : public AutomationProxyForExternalTab {
public:
explicit MultiMessageAutomationProxy(int execution_timeout)
: AutomationProxyForExternalTab(execution_timeout) {
}
// Call when testing with the current tab is finished.
void Quit() {
PostQuitMessage(0);
}
protected:
virtual void OnMessageReceived(const IPC::Message& msg) {
IPC_BEGIN_MESSAGE_MAP(MultiMessageAutomationProxy, msg)
IPC_MESSAGE_HANDLER(AutomationMsg_DidNavigate,
AutomationProxyForExternalTab::OnDidNavigate)
IPC_MESSAGE_HANDLER(AutomationMsg_ForwardMessageToExternalHost,
OnForwardMessageToExternalHost)
IPC_END_MESSAGE_MAP()
}
void OnForwardMessageToExternalHost(int handle,
const std::string& message,
const std::string& origin,
const std::string& target) {
messages_received_++;
message_ = message;
origin_ = origin;
target_ = target;
HandleMessageFromChrome();
}
// Override to do your custom checking and initiate any custom actions
// needed in your particular unit test.
virtual void HandleMessageFromChrome() = 0;
};
// This proxy is specific to RoundtripApiCallExtensionTest.
class RoundtripAutomationProxy : public MultiMessageAutomationProxy {
public:
explicit RoundtripAutomationProxy(int execution_timeout)
: MultiMessageAutomationProxy(execution_timeout),
tab_(NULL) {
}
// Must set before initiating test.
TabProxy* tab_;
protected:
virtual void HandleMessageFromChrome() {
namespace keys = extension_automation_constants;
ASSERT_TRUE(tab_ != NULL);
ASSERT_TRUE(messages_received_ == 1 || messages_received_ == 2);
// Using EXPECT_TRUE rather than EXPECT_EQ as the compiler (VC++) isn't
// finding the right match for EqHelper.
EXPECT_TRUE(origin_ == keys::kAutomationOrigin);
EXPECT_TRUE(target_ == keys::kAutomationRequestTarget);
scoped_ptr<Value> message_value(JSONReader::Read(message_, false));
ASSERT_TRUE(message_value->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* request_dict =
static_cast<DictionaryValue*>(message_value.get());
std::string function_name;
ASSERT_TRUE(request_dict->GetString(keys::kAutomationNameKey,
&function_name));
int request_id = -2;
EXPECT_TRUE(request_dict->GetInteger(keys::kAutomationRequestIdKey,
&request_id));
bool has_callback = false;
EXPECT_TRUE(request_dict->GetBoolean(keys::kAutomationHasCallbackKey,
&has_callback));
if (messages_received_ == 1) {
EXPECT_EQ(function_name, "GetLastFocusedWindow");
EXPECT_GE(request_id, 0);
EXPECT_TRUE(has_callback);
DictionaryValue response_dict;
EXPECT_TRUE(response_dict.SetInteger(keys::kAutomationRequestIdKey,
request_id));
EXPECT_TRUE(response_dict.SetString(keys::kAutomationResponseKey, "42"));
std::string response_json;
JSONWriter::Write(&response_dict, false, &response_json);
tab_->HandleMessageFromExternalHost(
response_json,
keys::kAutomationOrigin,
keys::kAutomationResponseTarget);
} else if (messages_received_ == 2) {
EXPECT_EQ(function_name, "RemoveTab");
EXPECT_FALSE(has_callback);
std::string args;
EXPECT_TRUE(request_dict->GetString(keys::kAutomationArgsKey, &args));
EXPECT_NE(args.find("42"), -1);
Quit();
} else {
Quit();
FAIL();
}
}
};
class RoundtripApiCallExtensionTest
: public ExtensionUITest<
CustomAutomationProxyTest<RoundtripAutomationProxy>> {
public:
RoundtripApiCallExtensionTest()
: ExtensionUITest<
CustomAutomationProxyTest<
RoundtripAutomationProxy> >(kTestDirectoryRoundtripApiCall) {
}
void DoAdditionalPreNavigateSetup(TabProxy* tab) {
RoundtripAutomationProxy* proxy =
static_cast<RoundtripAutomationProxy*>(automation());
proxy->tab_ = tab;
}
private:
DISALLOW_COPY_AND_ASSIGN(RoundtripApiCallExtensionTest);
};
// TODO(port) Should become portable once
// ExternalTabMessageLoop is ported.
#if defined(OS_WIN)
TEST_F(RoundtripApiCallExtensionTest, RunTest) {
TestWithURL(GURL(
"chrome-extension://66664444789ABCDEF0123456789ABCDEF0123456/test.html"));
RoundtripAutomationProxy* proxy =
static_cast<RoundtripAutomationProxy*>(automation());
// Validation is done in the RoundtripAutomationProxy, so we just check
// something basic here.
EXPECT_EQ(proxy->messages_received(), 2);
}
#endif // defined(OS_WIN)
} // namespace
|