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
|
// Copyright 2014 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 <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/values.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/base/web_ui_browser_test.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
using content::WebUIMessageHandler;
// According to the interface for EXPECT_FATAL_FAILURE
// (https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#catching-failures)
// the statement must be statically available. Therefore, we make a static
// global s_test_ which should point to |this| for the duration of the test run
// and be cleared afterward.
class WebUIBrowserExpectFailTest : public WebUIBrowserTest {
public:
WebUIBrowserExpectFailTest() {
EXPECT_FALSE(s_test_);
s_test_ = this;
}
// Disable new downloads UI as it is very very slow. https://crbug.com/526577
// TODO(dbeam): remove this once the downloads UI is not slow.
void SetUpCommandLine(base::CommandLine* command_line) override {
WebUIBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kDisableMaterialDesignDownloads);
}
protected:
~WebUIBrowserExpectFailTest() override {
EXPECT_TRUE(s_test_);
s_test_ = NULL;
}
static void RunJavascriptTestNoReturn(const std::string& testname) {
EXPECT_TRUE(s_test_);
s_test_->RunJavascriptTest(testname);
}
static void RunJavascriptAsyncTestNoReturn(const std::string& testname) {
EXPECT_TRUE(s_test_);
s_test_->RunJavascriptAsyncTest(testname);
}
private:
static WebUIBrowserTest* s_test_;
};
WebUIBrowserTest* WebUIBrowserExpectFailTest::s_test_ = NULL;
// Test that bogus javascript fails fast - no timeout waiting for result.
IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestFailsFast) {
AddLibrary(base::FilePath(FILE_PATH_LITERAL("sample_downloads.js")));
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL));
EXPECT_FATAL_FAILURE(RunJavascriptTestNoReturn("DISABLED_BogusFunctionName"),
"WebUITestHandler::JavaScriptComplete");
}
// Test that bogus javascript fails fast - no timeout waiting for result.
IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestRuntimeErrorFailsFast) {
AddLibrary(base::FilePath(FILE_PATH_LITERAL("runtime_error.js")));
ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL));
EXPECT_FATAL_FAILURE(RunJavascriptTestNoReturn("TestRuntimeErrorFailsFast"),
"WebUITestHandler::JavaScriptComplete");
}
// Test that bogus javascript fails async test fast as well - no timeout waiting
// for result.
IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestFailsAsyncFast) {
AddLibrary(base::FilePath(FILE_PATH_LITERAL("sample_downloads.js")));
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL));
EXPECT_FATAL_FAILURE(
RunJavascriptAsyncTestNoReturn("DISABLED_BogusFunctionName"),
"WebUITestHandler::JavaScriptComplete");
}
// Tests that the async framework works.
class WebUIBrowserAsyncTest : public WebUIBrowserTest {
public:
// Calls the testDone() function from test_api.js
void TestDone() {
RunJavascriptFunction("testDone");
}
// Starts a failing test.
void RunTestFailsAssert() {
RunJavascriptFunction("runAsync", new base::StringValue("testFailsAssert"));
}
// Starts a passing test.
void RunTestPasses() {
RunJavascriptFunction("runAsync", new base::StringValue("testPasses"));
}
protected:
WebUIBrowserAsyncTest() {}
// Class to synchronize asynchronous javascript activity with the tests.
class AsyncWebUIMessageHandler : public WebUIMessageHandler {
public:
AsyncWebUIMessageHandler() {}
MOCK_METHOD1(HandleTestContinues, void(const base::ListValue*));
MOCK_METHOD1(HandleTestFails, void(const base::ListValue*));
MOCK_METHOD1(HandleTestPasses, void(const base::ListValue*));
private:
void RegisterMessages() override {
web_ui()->RegisterMessageCallback("startAsyncTest",
base::Bind(&AsyncWebUIMessageHandler::HandleStartAsyncTest,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("testContinues",
base::Bind(&AsyncWebUIMessageHandler::HandleTestContinues,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("testFails",
base::Bind(&AsyncWebUIMessageHandler::HandleTestFails,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("testPasses",
base::Bind(&AsyncWebUIMessageHandler::HandleTestPasses,
base::Unretained(this)));
}
// Starts the test in |list_value|[0] with the runAsync wrapper.
void HandleStartAsyncTest(const base::ListValue* list_value) {
const base::Value* test_name;
ASSERT_TRUE(list_value->Get(0, &test_name));
web_ui()->CallJavascriptFunction("runAsync", *test_name);
}
DISALLOW_COPY_AND_ASSIGN(AsyncWebUIMessageHandler);
};
// Handler for this object.
::testing::StrictMock<AsyncWebUIMessageHandler> message_handler_;
private:
// Provide this object's handler.
WebUIMessageHandler* GetMockMessageHandler() override {
return &message_handler_;
}
// Set up and browse to kDummyURL for all tests.
void SetUpOnMainThread() override {
WebUIBrowserTest::SetUpOnMainThread();
AddLibrary(base::FilePath(FILE_PATH_LITERAL("async.js")));
ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL));
}
DISALLOW_COPY_AND_ASSIGN(WebUIBrowserAsyncTest);
};
// Test that assertions fail immediately after assertion fails (no testContinues
// message). (Sync version).
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncOkTestFail) {
ASSERT_FALSE(RunJavascriptTest("testFailsAssert"));
}
// Test that assertions fail immediately after assertion fails (no testContinues
// message). (Async version).
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsAssert) {
EXPECT_CALL(message_handler_, HandleTestFails(::testing::_));
ASSERT_FALSE(RunJavascriptAsyncTest(
"startAsyncTest", new base::StringValue("testFailsAssert")));
}
// Test that expectations continue the function, but fail the test.
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsExpect) {
::testing::InSequence s;
EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_));
EXPECT_CALL(message_handler_, HandleTestFails(::testing::_));
ASSERT_FALSE(RunJavascriptAsyncTest(
"startAsyncTest", new base::StringValue("testFailsExpect")));
}
// Test that test continues and passes. (Sync version).
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncPasses) {
EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_));
ASSERT_TRUE(RunJavascriptTest("testPasses"));
}
// Test that test continues and passes. (Async version).
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncPasses) {
::testing::InSequence s;
EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_));
EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_))
.WillOnce(::testing::InvokeWithoutArgs(
this, &WebUIBrowserAsyncTest::TestDone));
ASSERT_TRUE(RunJavascriptAsyncTest(
"startAsyncTest", new base::StringValue("testPasses")));
}
// Test that two tests pass.
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncPassPass) {
::testing::InSequence s;
EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_));
EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_))
.WillOnce(::testing::InvokeWithoutArgs(
this, &WebUIBrowserAsyncTest::RunTestPasses));
EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_));
EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_))
.WillOnce(::testing::InvokeWithoutArgs(
this, &WebUIBrowserAsyncTest::TestDone));
ASSERT_TRUE(RunJavascriptAsyncTest(
"startAsyncTest", new base::StringValue("testPasses")));
}
// Test that first test passes; second fails.
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncPassThenFail) {
::testing::InSequence s;
EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_));
EXPECT_CALL(message_handler_, HandleTestPasses(::testing::_))
.WillOnce(::testing::InvokeWithoutArgs(
this, &WebUIBrowserAsyncTest::RunTestFailsAssert));
EXPECT_CALL(message_handler_, HandleTestFails(::testing::_));
ASSERT_FALSE(RunJavascriptAsyncTest(
"startAsyncTest", new base::StringValue("testPasses")));
}
// Test that testDone() with failure first then sync pass still fails.
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncDoneFailFirstSyncPass) {
::testing::InSequence s;
EXPECT_CALL(message_handler_, HandleTestContinues(::testing::_));
EXPECT_CALL(message_handler_, HandleTestFails(::testing::_));
// Call runAsync directly instead of deferring through startAsyncTest. It will
// call testDone() on failure, then return.
ASSERT_FALSE(RunJavascriptAsyncTest(
"runAsync", new base::StringValue("testAsyncDoneFailFirstSyncPass")));
}
// Test that calling testDone during RunJavascriptAsyncTest still completes
// when waiting for async result. This is similar to the previous test, but call
// testDone directly and expect pass result.
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) {
ASSERT_TRUE(RunJavascriptAsyncTest("testDone"));
}
// Test that calling testDone during RunJavascriptTest still completes when
// waiting for async result.
IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) {
ASSERT_TRUE(RunJavascriptTest("testDone"));
}
|