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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "TestShell.h"
#include "DRTDevToolsAgent.h"
#include "DRTDevToolsClient.h"
#include "LayoutTestController.h"
#include "MockWebPrerenderingSupport.h"
#include "platform/WebArrayBufferView.h"
#include "WebCompositor.h"
#include "WebDataSource.h"
#include "WebDocument.h"
#include "WebElement.h"
#include "WebFrame.h"
#include "WebHistoryItem.h"
#include "WebIDBFactory.h"
#include "WebTestingSupport.h"
#include "platform/WebThread.h"
#include "WebKit.h"
#include "platform/WebKitPlatformSupport.h"
#include "WebPermissions.h"
#include "platform/WebPoint.h"
#include "WebRuntimeFeatures.h"
#include "WebScriptController.h"
#include "WebSettings.h"
#include "platform/WebSize.h"
#include "platform/WebString.h"
#include "platform/WebURLRequest.h"
#include "platform/WebURLResponse.h"
#include "WebView.h"
#include "WebViewHost.h"
#include "skia/ext/platform_canvas.h"
#include "webkit/support/webkit_support.h"
#include "webkit/support/webkit_support_gfx.h"
#include <algorithm>
#include <cctype>
#include <vector>
#include <wtf/MD5.h>
#include <wtf/OwnArrayPtr.h>
using namespace WebKit;
using namespace std;
// Content area size for newly created windows.
static const int testWindowWidth = 800;
static const int testWindowHeight = 600;
// The W3C SVG layout tests use a different size than the other layout tests.
static const int SVGTestWindowWidth = 480;
static const int SVGTestWindowHeight = 360;
static const char layoutTestsPattern[] = "/LayoutTests/";
static const string::size_type layoutTestsPatternSize = sizeof(layoutTestsPattern) - 1;
static const char fileUrlPattern[] = "file:/";
static const char fileTestPrefix[] = "(file test):";
static const char dataUrlPattern[] = "data:";
static const string::size_type dataUrlPatternSize = sizeof(dataUrlPattern) - 1;
// FIXME: Move this to a common place so that it can be shared with
// WebCore::TransparencyWin::makeLayerOpaque().
static void makeCanvasOpaque(SkCanvas* canvas)
{
const SkBitmap& bitmap = canvas->getTopDevice()->accessBitmap(true);
ASSERT(bitmap.config() == SkBitmap::kARGB_8888_Config);
SkAutoLockPixels lock(bitmap);
for (int y = 0; y < bitmap.height(); y++) {
uint32_t* row = bitmap.getAddr32(0, y);
for (int x = 0; x < bitmap.width(); x++)
row[x] |= 0xFF000000; // Set alpha bits to 1.
}
}
TestShell::TestShell()
: m_testIsPending(false)
, m_testIsPreparing(false)
, m_focusedWidget(0)
, m_testShellMode(false)
, m_devTools(0)
, m_allowExternalPages(false)
, m_acceleratedCompositingForVideoEnabled(false)
, m_threadedCompositingEnabled(false)
, m_forceCompositingMode(false)
, m_accelerated2dCanvasEnabled(false)
, m_deferred2dCanvasEnabled(false)
, m_acceleratedPaintingEnabled(false)
, m_perTilePaintingEnabled(false)
, m_stressOpt(false)
, m_stressDeopt(false)
, m_dumpWhenFinished(true)
, m_isDisplayingModalDialog(false)
{
WebRuntimeFeatures::enableDataTransferItems(true);
WebRuntimeFeatures::enableGeolocation(true);
WebRuntimeFeatures::enablePointerLock(true);
WebRuntimeFeatures::enableIndexedDatabase(true);
WebRuntimeFeatures::enableFileSystem(true);
WebRuntimeFeatures::enableJavaScriptI18NAPI(true);
WebRuntimeFeatures::enableMediaSource(true);
WebRuntimeFeatures::enableEncryptedMedia(true);
WebRuntimeFeatures::enableMediaStream(true);
WebRuntimeFeatures::enablePeerConnection(true);
WebRuntimeFeatures::enableWebAudio(true);
WebRuntimeFeatures::enableVideoTrack(true);
WebRuntimeFeatures::enableGamepad(true);
WebRuntimeFeatures::enableShadowDOM(true);
WebRuntimeFeatures::enableStyleScoped(true);
WebRuntimeFeatures::enableScriptedSpeech(true);
// 30 second is the same as the value in Mac DRT.
// If we use a value smaller than the timeout value of
// (new-)run-webkit-tests, (new-)run-webkit-tests misunderstands that a
// timed-out DRT process was crashed.
m_timeout = 30 * 1000;
}
void TestShell::initialize()
{
m_webPermissions = adoptPtr(new WebPermissions(this));
m_accessibilityController = adoptPtr(new AccessibilityController(this));
m_gamepadController = adoptPtr(new GamepadController(this));
m_layoutTestController = adoptPtr(new LayoutTestController(this));
m_eventSender = adoptPtr(new EventSender(this));
m_textInputController = adoptPtr(new TextInputController(this));
#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
m_notificationPresenter = adoptPtr(new NotificationPresenter(this));
#endif
m_printer = m_testShellMode ? TestEventPrinter::createTestShellPrinter() : TestEventPrinter::createDRTPrinter();
#if ENABLE(LINK_PRERENDER)
m_prerenderingSupport = adoptPtr(new MockWebPrerenderingSupport());
#endif
WTF::initializeThreading();
if (m_threadedCompositingEnabled) {
m_webCompositorThread = adoptPtr(WebKit::webKitPlatformSupport()->createThread("Compositor"));
WebCompositor::initialize(m_webCompositorThread.get());
} else
WebCompositor::initialize(0);
createMainWindow();
}
void TestShell::createMainWindow()
{
m_drtDevToolsAgent = adoptPtr(new DRTDevToolsAgent);
m_webViewHost = adoptPtr(createNewWindow(WebURL(), m_drtDevToolsAgent.get()));
m_webView = m_webViewHost->webView();
m_drtDevToolsAgent->setWebView(m_webView);
}
TestShell::~TestShell()
{
// Note: DevTools are closed together with all the other windows in the
// windows list.
// Destroy the WebView before its WebViewHost.
m_drtDevToolsAgent->setWebView(0);
}
void TestShell::createDRTDevToolsClient(DRTDevToolsAgent* agent)
{
m_drtDevToolsClient = adoptPtr(new DRTDevToolsClient(agent, m_devTools->webView()));
}
void TestShell::showDevTools()
{
if (!m_devTools) {
WebURL url = webkit_support::GetDevToolsPathAsURL();
if (!url.isValid()) {
ASSERT(false);
return;
}
m_devTools = createNewWindow(url);
m_devTools->webView()->settings()->setMemoryInfoEnabled(true);
m_devTools->setLogConsoleOutput(false);
ASSERT(m_devTools);
createDRTDevToolsClient(m_drtDevToolsAgent.get());
}
m_devTools->show(WebKit::WebNavigationPolicyNewWindow);
}
void TestShell::closeDevTools()
{
if (m_devTools) {
m_devTools->webView()->settings()->setMemoryInfoEnabled(false);
m_drtDevToolsAgent->reset();
m_drtDevToolsClient.clear();
closeWindow(m_devTools);
m_devTools = 0;
}
}
void TestShell::resetWebSettings(WebView& webView)
{
m_prefs.reset();
m_prefs.acceleratedCompositingEnabled = true;
m_prefs.acceleratedCompositingForVideoEnabled = m_acceleratedCompositingForVideoEnabled;
m_prefs.forceCompositingMode = m_forceCompositingMode;
m_prefs.accelerated2dCanvasEnabled = m_accelerated2dCanvasEnabled;
m_prefs.deferred2dCanvasEnabled = m_deferred2dCanvasEnabled;
m_prefs.acceleratedPaintingEnabled = m_acceleratedPaintingEnabled;
m_prefs.perTilePaintingEnabled = m_perTilePaintingEnabled;
m_prefs.applyTo(&webView);
}
void TestShell::runFileTest(const TestParams& params)
{
ASSERT(params.testUrl.isValid());
m_testIsPreparing = true;
m_params = params;
string testUrl = m_params.testUrl.spec();
if (testUrl.find("loading/") != string::npos
|| testUrl.find("loading\\") != string::npos)
m_layoutTestController->setShouldDumpFrameLoadCallbacks(true);
if (testUrl.find("compositing/") != string::npos || testUrl.find("compositing\\") != string::npos) {
m_prefs.acceleratedCompositingForVideoEnabled = true;
m_prefs.accelerated2dCanvasEnabled = true;
m_prefs.deferred2dCanvasEnabled = true;
m_prefs.mockScrollbarsEnabled = true;
m_prefs.applyTo(m_webView);
}
if (testUrl.find("/dumpAsText/") != string::npos
|| testUrl.find("\\dumpAsText\\") != string::npos) {
m_layoutTestController->setShouldDumpAsText(true);
m_layoutTestController->setShouldGeneratePixelResults(false);
}
if (testUrl.find("/inspector/") != string::npos
|| testUrl.find("\\inspector\\") != string::npos)
showDevTools();
if (m_params.debugLayerTree)
m_layoutTestController->setShowDebugLayerTree(true);
if (m_dumpWhenFinished)
m_printer->handleTestHeader(testUrl.c_str());
loadURL(m_params.testUrl);
m_testIsPreparing = false;
waitTestFinished();
}
static inline bool isSVGTestURL(const WebURL& url)
{
return url.isValid() && string(url.spec()).find("W3C-SVG-1.1") != string::npos;
}
void TestShell::resizeWindowForTest(WebViewHost* window, const WebURL& url)
{
int width, height;
if (isSVGTestURL(url)) {
width = SVGTestWindowWidth;
height = SVGTestWindowHeight;
} else {
width = testWindowWidth;
height = testWindowHeight;
}
window->setWindowRect(WebRect(0, 0, width + virtualWindowBorder * 2, height + virtualWindowBorder * 2));
}
void TestShell::resetTestController()
{
resetWebSettings(*webView());
m_webPermissions->reset();
m_accessibilityController->reset();
m_gamepadController->reset();
m_layoutTestController->reset();
m_eventSender->reset();
m_webViewHost->reset();
#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
m_notificationPresenter->reset();
#endif
m_drtDevToolsAgent->reset();
if (m_drtDevToolsClient)
m_drtDevToolsClient->reset();
webView()->setPageScaleFactor(1, WebPoint(0, 0));
webView()->enableFixedLayoutMode(false);
webView()->setFixedLayoutSize(WebSize(0, 0));
webView()->mainFrame()->clearOpener();
WebTestingSupport::resetInternalsObject(webView()->mainFrame());
}
void TestShell::loadURL(const WebURL& url)
{
m_webViewHost->loadURLForFrame(url, WebString());
}
void TestShell::reload()
{
m_webViewHost->navigationController()->reload();
}
void TestShell::goToOffset(int offset)
{
m_webViewHost->navigationController()->goToOffset(offset);
}
int TestShell::navigationEntryCount() const
{
return m_webViewHost->navigationController()->entryCount();
}
void TestShell::callJSGC()
{
m_webView->mainFrame()->collectGarbage();
}
void TestShell::setFocus(WebWidget* widget, bool enable)
{
// Simulate the effects of InteractiveSetFocus(), which includes calling
// both setFocus() and setIsActive().
if (enable) {
if (m_focusedWidget != widget) {
if (m_focusedWidget)
m_focusedWidget->setFocus(false);
webView()->setIsActive(enable);
widget->setFocus(enable);
m_focusedWidget = widget;
}
} else {
if (m_focusedWidget == widget) {
widget->setFocus(enable);
webView()->setIsActive(enable);
m_focusedWidget = 0;
}
}
}
void TestShell::testFinished()
{
if (!m_testIsPending)
return;
m_testIsPending = false;
if (m_dumpWhenFinished)
dump();
webkit_support::QuitMessageLoop();
}
void TestShell::testTimedOut()
{
m_printer->handleTimedOut();
testFinished();
}
static string dumpDocumentText(WebFrame* frame)
{
// We use the document element's text instead of the body text here because
// not all documents have a body, such as XML documents.
WebElement documentElement = frame->document().documentElement();
if (documentElement.isNull())
return string();
return documentElement.innerText().utf8();
}
static string dumpFramesAsText(WebFrame* frame, bool recursive)
{
string result;
// Add header for all but the main frame. Skip empty frames.
if (frame->parent() && !frame->document().documentElement().isNull()) {
result.append("\n--------\nFrame: '");
result.append(frame->name().utf8().data());
result.append("'\n--------\n");
}
result.append(dumpDocumentText(frame));
result.append("\n");
if (recursive) {
for (WebFrame* child = frame->firstChild(); child; child = child->nextSibling())
result.append(dumpFramesAsText(child, recursive));
}
return result;
}
static string dumpFramesAsPrintedText(WebFrame* frame, bool recursive)
{
string result;
// Cannot do printed format for anything other than HTML
if (!frame->document().isHTMLDocument())
return string();
// Add header for all but the main frame. Skip empty frames.
if (frame->parent() && !frame->document().documentElement().isNull()) {
result.append("\n--------\nFrame: '");
result.append(frame->name().utf8().data());
result.append("'\n--------\n");
}
result.append(frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8());
result.append("\n");
if (recursive) {
for (WebFrame* child = frame->firstChild(); child; child = child->nextSibling())
result.append(dumpFramesAsPrintedText(child, recursive));
}
return result;
}
static void dumpFrameScrollPosition(WebFrame* frame, bool recursive)
{
WebSize offset = frame->scrollOffset();
if (offset.width > 0 || offset.height > 0) {
if (frame->parent())
printf("frame '%s' ", frame->name().utf8().data());
printf("scrolled to %d,%d\n", offset.width, offset.height);
}
if (!recursive)
return;
for (WebFrame* child = frame->firstChild(); child; child = child->nextSibling())
dumpFrameScrollPosition(child, recursive);
}
struct ToLower {
char16 operator()(char16 c) { return tolower(c); }
};
// FIXME: Eliminate std::transform(), std::vector, and std::sort().
// Returns True if item1 < item2.
static bool HistoryItemCompareLess(const WebHistoryItem& item1, const WebHistoryItem& item2)
{
string16 target1 = item1.target();
string16 target2 = item2.target();
std::transform(target1.begin(), target1.end(), target1.begin(), ToLower());
std::transform(target2.begin(), target2.end(), target2.begin(), ToLower());
return target1 < target2;
}
static string normalizeLayoutTestURLInternal(const string& url)
{
string result = url;
size_t pos;
if (!url.find(fileUrlPattern) && ((pos = url.find(layoutTestsPattern)) != string::npos)) {
// adjust file URLs to match upstream results.
result.replace(0, pos + layoutTestsPatternSize, fileTestPrefix);
} else if (!url.find(dataUrlPattern)) {
// URL-escape data URLs to match results upstream.
string path = url.substr(dataUrlPatternSize);
result.replace(dataUrlPatternSize, url.length(), path);
}
return result;
}
static string dumpHistoryItem(const WebHistoryItem& item, int indent, bool isCurrent)
{
string result;
if (isCurrent) {
result.append("curr->");
result.append(indent - 6, ' '); // 6 == "curr->".length()
} else
result.append(indent, ' ');
string url = normalizeLayoutTestURLInternal(item.urlString().utf8());
result.append(url);
if (!item.target().isEmpty()) {
result.append(" (in frame \"");
result.append(item.target().utf8());
result.append("\")");
}
if (item.isTargetItem())
result.append(" **nav target**");
result.append("\n");
const WebVector<WebHistoryItem>& children = item.children();
if (!children.isEmpty()) {
// Must sort to eliminate arbitrary result ordering which defeats
// reproducible testing.
// FIXME: WebVector should probably just be a std::vector!!
std::vector<WebHistoryItem> sortedChildren;
for (size_t i = 0; i < children.size(); ++i)
sortedChildren.push_back(children[i]);
std::sort(sortedChildren.begin(), sortedChildren.end(), HistoryItemCompareLess);
for (size_t i = 0; i < sortedChildren.size(); ++i)
result += dumpHistoryItem(sortedChildren[i], indent + 4, false);
}
return result;
}
static void dumpBackForwardList(const TestNavigationController& navigationController, string& result)
{
result.append("\n============== Back Forward List ==============\n");
for (int index = 0; index < navigationController.entryCount(); ++index) {
int currentIndex = navigationController.lastCommittedEntryIndex();
WebHistoryItem historyItem = navigationController.entryAtIndex(index)->contentState();
if (historyItem.isNull()) {
historyItem.initialize();
historyItem.setURLString(navigationController.entryAtIndex(index)->URL().spec().utf16());
}
result.append(dumpHistoryItem(historyItem, 8, index == currentIndex));
}
result.append("===============================================\n");
}
string TestShell::dumpAllBackForwardLists()
{
string result;
for (unsigned i = 0; i < m_windowList.size(); ++i)
dumpBackForwardList(*m_windowList[i]->navigationController(), result);
return result;
}
void TestShell::dump()
{
WebScriptController::flushConsoleMessages();
// Dump the requested representation.
WebFrame* frame = m_webView->mainFrame();
if (!frame)
return;
bool shouldDumpAsText = m_layoutTestController->shouldDumpAsText();
bool shouldDumpAsAudio = m_layoutTestController->shouldDumpAsAudio();
bool shouldGeneratePixelResults = m_layoutTestController->shouldGeneratePixelResults();
bool shouldDumpAsPrinted = m_layoutTestController->isPrinting();
bool dumpedAnything = false;
if (shouldDumpAsAudio) {
m_printer->handleAudioHeader();
const WebKit::WebArrayBufferView& webArrayBufferView = m_layoutTestController->audioData();
printf("Content-Length: %d\n", webArrayBufferView.byteLength());
if (fwrite(webArrayBufferView.baseAddress(), 1, webArrayBufferView.byteLength(), stdout) != webArrayBufferView.byteLength())
FATAL("Short write to stdout, disk full?\n");
m_printer->handleAudioFooter();
m_printer->handleTestFooter(true);
fflush(stdout);
fflush(stderr);
return;
}
if (m_params.dumpTree) {
dumpedAnything = true;
m_printer->handleTextHeader();
// Text output: the test page can request different types of output
// which we handle here.
if (!shouldDumpAsText) {
// Plain text pages should be dumped as text
string mimeType = frame->dataSource()->response().mimeType().utf8();
if (mimeType == "text/plain") {
shouldDumpAsText = true;
shouldGeneratePixelResults = false;
}
}
if (shouldDumpAsText) {
bool recursive = m_layoutTestController->shouldDumpChildFramesAsText();
string dataUtf8 = shouldDumpAsPrinted ? dumpFramesAsPrintedText(frame, recursive) : dumpFramesAsText(frame, recursive);
if (fwrite(dataUtf8.c_str(), 1, dataUtf8.size(), stdout) != dataUtf8.size())
FATAL("Short write to stdout, disk full?\n");
} else {
WebFrame::RenderAsTextControls renderTextBehavior = WebFrame::RenderAsTextNormal;
if (shouldDumpAsPrinted)
renderTextBehavior |= WebFrame::RenderAsTextPrinting;
if (m_params.debugRenderTree)
renderTextBehavior |= WebFrame::RenderAsTextDebug;
printf("%s", frame->renderTreeAsText(renderTextBehavior).utf8().data());
bool recursive = m_layoutTestController->shouldDumpChildFrameScrollPositions();
dumpFrameScrollPosition(frame, recursive);
}
if (m_layoutTestController->shouldDumpBackForwardList())
printf("%s", dumpAllBackForwardLists().c_str());
}
if (dumpedAnything && m_params.printSeparators)
m_printer->handleTextFooter();
if (m_params.dumpPixels && shouldGeneratePixelResults) {
// Image output: we write the image data to the file given on the
// command line (for the dump pixels argument), and the MD5 sum to
// stdout.
dumpedAnything = true;
m_webView->layout();
if (m_layoutTestController->testRepaint()) {
WebSize viewSize = m_webView->size();
int width = viewSize.width;
int height = viewSize.height;
if (m_layoutTestController->sweepHorizontally()) {
for (WebRect column(0, 0, 1, height); column.x < width; column.x++)
m_webViewHost->paintRect(column);
} else {
for (WebRect line(0, 0, width, 1); line.y < height; line.y++)
m_webViewHost->paintRect(line);
}
} else if (m_layoutTestController->isPrinting())
m_webViewHost->paintPagesWithBoundaries();
else
m_webViewHost->paintInvalidatedRegion();
// See if we need to draw the selection bounds rect. Selection bounds
// rect is the rect enclosing the (possibly transformed) selection.
// The rect should be drawn after everything is laid out and painted.
if (m_layoutTestController->shouldDumpSelectionRect()) {
// If there is a selection rect - draw a red 1px border enclosing rect
WebRect wr = frame->selectionBoundsRect();
if (!wr.isEmpty()) {
// Render a red rectangle bounding selection rect
SkPaint paint;
paint.setColor(0xFFFF0000); // Fully opaque red
paint.setStyle(SkPaint::kStroke_Style);
paint.setFlags(SkPaint::kAntiAlias_Flag);
paint.setStrokeWidth(1.0f);
SkIRect rect; // Bounding rect
rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height);
m_webViewHost->canvas()->drawIRect(rect, paint);
}
}
dumpImage(m_webViewHost->canvas());
}
m_printer->handleTestFooter(dumpedAnything);
fflush(stdout);
fflush(stderr);
}
void TestShell::dumpImage(SkCanvas* canvas) const
{
// Fix the alpha. The expected PNGs on Mac have an alpha channel, so we want
// to keep it. On Windows, the alpha channel is wrong since text/form control
// drawing may have erased it in a few places. So on Windows we force it to
// opaque and also don't write the alpha channel for the reference. Linux
// doesn't have the wrong alpha like Windows, but we match Windows.
#if OS(MAC_OS_X)
bool discardTransparency = false;
#else
bool discardTransparency = true;
makeCanvasOpaque(canvas);
#endif
const SkBitmap& sourceBitmap = canvas->getTopDevice()->accessBitmap(false);
SkAutoLockPixels sourceBitmapLock(sourceBitmap);
// Compute MD5 sum.
MD5 digester;
Vector<uint8_t, 16> digestValue;
#if OS(ANDROID)
// On Android, pixel layout is RGBA (see third_party/skia/include/core/SkColorPriv.h);
// however, other Chrome platforms use BGRA (see skia/config/SkUserConfig.h).
// To match the checksum of other Chrome platforms, we need to reorder the layout of pixels.
// NOTE: The following code assumes we use SkBitmap::kARGB_8888_Config,
// which has been checked in device.makeOpaque() (see above).
const uint8_t* rawPixels = reinterpret_cast<const uint8_t*>(sourceBitmap.getPixels());
size_t bitmapSize = sourceBitmap.getSize();
OwnArrayPtr<uint8_t> reorderedPixels = adoptArrayPtr(new uint8_t[bitmapSize]);
for (size_t i = 0; i < bitmapSize; i += 4) {
reorderedPixels[i] = rawPixels[i + 2]; // R
reorderedPixels[i + 1] = rawPixels[i + 1]; // G
reorderedPixels[i + 2] = rawPixels[i]; // B
reorderedPixels[i + 3] = rawPixels[i + 3]; // A
}
digester.addBytes(reorderedPixels.get(), bitmapSize);
reorderedPixels.clear();
#else
digester.addBytes(reinterpret_cast<const uint8_t*>(sourceBitmap.getPixels()), sourceBitmap.getSize());
#endif
digester.checksum(digestValue);
string md5hash;
md5hash.reserve(16 * 2);
for (unsigned i = 0; i < 16; ++i) {
char hex[3];
// Use "x", not "X". The string must be lowercased.
sprintf(hex, "%02x", digestValue[i]);
md5hash.append(hex);
}
// Only encode and dump the png if the hashes don't match. Encoding the
// image is really expensive.
if (md5hash.compare(m_params.pixelHash)) {
std::vector<unsigned char> png;
#if OS(ANDROID)
webkit_support::EncodeRGBAPNGWithChecksum(reinterpret_cast<const unsigned char*>(sourceBitmap.getPixels()), sourceBitmap.width(),
sourceBitmap.height(), static_cast<int>(sourceBitmap.rowBytes()), discardTransparency, md5hash, &png);
#else
webkit_support::EncodeBGRAPNGWithChecksum(reinterpret_cast<const unsigned char*>(sourceBitmap.getPixels()), sourceBitmap.width(),
sourceBitmap.height(), static_cast<int>(sourceBitmap.rowBytes()), discardTransparency, md5hash, &png);
#endif
m_printer->handleImage(md5hash.c_str(), m_params.pixelHash.c_str(), &png[0], png.size(), m_params.pixelFileName.c_str());
} else
m_printer->handleImage(md5hash.c_str(), m_params.pixelHash.c_str(), 0, 0, m_params.pixelFileName.c_str());
}
void TestShell::bindJSObjectsToWindow(WebFrame* frame)
{
WebTestingSupport::injectInternalsObject(frame);
m_accessibilityController->bindToJavascript(frame, WebString::fromUTF8("accessibilityController"));
m_gamepadController->bindToJavascript(frame, WebString::fromUTF8("gamepadController"));
m_layoutTestController->bindToJavascript(frame, WebString::fromUTF8("layoutTestController"));
m_eventSender->bindToJavascript(frame, WebString::fromUTF8("eventSender"));
m_textInputController->bindToJavascript(frame, WebString::fromUTF8("textInputController"));
}
WebViewHost* TestShell::createNewWindow(const WebKit::WebURL& url)
{
return createNewWindow(url, 0);
}
WebViewHost* TestShell::createNewWindow(const WebKit::WebURL& url, DRTDevToolsAgent* devToolsAgent)
{
WebViewHost* host = new WebViewHost(this);
WebView* view = WebView::create(host);
view->setPermissionClient(webPermissions());
view->setDevToolsAgentClient(devToolsAgent);
host->setWebWidget(view);
m_prefs.applyTo(view);
view->initializeMainFrame(host);
m_windowList.append(host);
host->loadURLForFrame(url, WebString());
return host;
}
void TestShell::closeWindow(WebViewHost* window)
{
size_t i = m_windowList.find(window);
if (i == notFound) {
ASSERT_NOT_REACHED();
return;
}
m_windowList.remove(i);
WebWidget* focusedWidget = m_focusedWidget;
if (window->webWidget() == m_focusedWidget)
focusedWidget = 0;
delete window;
// We set the focused widget after deleting the web view host because it
// can change the focus.
m_focusedWidget = focusedWidget;
if (m_focusedWidget) {
webView()->setIsActive(true);
m_focusedWidget->setFocus(true);
}
}
void TestShell::closeRemainingWindows()
{
// Just close devTools window manually because we have custom deinitialization code for it.
closeDevTools();
// Iterate through the window list and close everything except the main
// window. We don't want to delete elements as we're iterating, so we copy
// to a temp vector first.
Vector<WebViewHost*> windowsToDelete;
for (unsigned i = 0; i < m_windowList.size(); ++i) {
if (m_windowList[i] != webViewHost())
windowsToDelete.append(m_windowList[i]);
}
ASSERT(windowsToDelete.size() + 1 == m_windowList.size());
for (unsigned i = 0; i < windowsToDelete.size(); ++i)
closeWindow(windowsToDelete[i]);
ASSERT(m_windowList.size() == 1);
}
int TestShell::windowCount()
{
return m_windowList.size();
}
string TestShell::normalizeLayoutTestURL(const string& url)
{
return normalizeLayoutTestURLInternal(url);
}
|