1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef TestInvocation_h
27#define TestInvocation_h
28
29#include "JSWrappable.h"
30#include "TestOptions.h"
31#include "UIScriptContext.h"
32#include "WhatToDump.h"
33#include <WebKit/WKRetainPtr.h>
34#include <string>
35#include <wtf/Noncopyable.h>
36#include <wtf/RunLoop.h>
37#include <wtf/Seconds.h>
38#include <wtf/text/StringBuilder.h>
39
40namespace WTR {
41
42class TestInvocation final : public UIScriptContextDelegate {
43 WTF_MAKE_NONCOPYABLE(TestInvocation);
44public:
45 explicit TestInvocation(WKURLRef, const TestOptions&);
46 ~TestInvocation();
47
48 WKURLRef url() const;
49 bool urlContains(const char*) const;
50
51 const TestOptions& options() const { return m_options; }
52
53 void setIsPixelTest(const std::string& expectedPixelHash);
54
55 void setCustomTimeout(WTF::Seconds duration) { m_timeout = duration; }
56 void setDumpJSConsoleLogInStdErr(bool value) { m_dumpJSConsoleLogInStdErr = value; }
57
58 WTF::Seconds shortTimeout() const;
59
60 void invoke();
61 void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
62 WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
63
64 static void dumpWebProcessUnresponsiveness(const char* errorMessage);
65 void outputText(const WTF::String&);
66
67 void didBeginSwipe();
68 void willEndSwipe();
69 void didEndSwipe();
70 void didRemoveSwipeSnapshot();
71
72 void notifyDownloadDone();
73
74 void didClearStatisticsThroughWebsiteDataRemoval();
75 void didResetStatisticsToConsistentState();
76 void didSetBlockCookiesForHost();
77 void didSetStatisticsDebugMode();
78 void didSetPrevalentResourceForDebugMode();
79 void didSetLastSeen();
80 void didSetPrevalentResource();
81 void didSetVeryPrevalentResource();
82 void didSetHasHadUserInteraction();
83 void didReceiveAllStorageAccessEntries(Vector<String>& domains);
84
85 void didRemoveAllSessionCredentials();
86
87 void dumpResourceLoadStatistics();
88
89 bool canOpenWindows() const { return m_canOpenWindows; }
90
91 void dumpAdClickAttribution();
92 void performCustomMenuAction();
93
94private:
95 WKRetainPtr<WKMutableDictionaryRef> createTestSettingsDictionary();
96
97 void waitToDumpWatchdogTimerFired();
98 void initializeWaitToDumpWatchdogTimerIfNeeded();
99 void invalidateWaitToDumpWatchdogTimer();
100
101 void done();
102 void setWaitUntilDone(bool);
103
104 void dumpResults();
105 static void dump(const char* textToStdout, const char* textToStderr = 0, bool seenError = false);
106 enum class SnapshotResultType { WebView, WebContents };
107 void dumpPixelsAndCompareWithExpected(SnapshotResultType, WKArrayRef repaintRects, WKImageRef = nullptr);
108 void dumpAudio(WKDataRef);
109 bool compareActualHashToExpectedAndDumpResults(const char[33]);
110
111 static void forceRepaintDoneCallback(WKErrorRef, void* context);
112
113 struct UIScriptInvocationData {
114 unsigned callbackID;
115 WebKit::WKRetainPtr<WKStringRef> scriptString;
116 TestInvocation* testInvocation;
117 };
118 static void runUISideScriptAfterUpdateCallback(WKErrorRef, void* context);
119
120 bool shouldLogHistoryClientCallbacks() const;
121
122 void runUISideScript(WKStringRef, unsigned callbackID);
123 // UIScriptContextDelegate
124 void uiScriptDidComplete(const String& result, unsigned callbackID) override;
125
126 const TestOptions m_options;
127
128 WKRetainPtr<WKURLRef> m_url;
129 WTF::String m_urlString;
130 RunLoop::Timer<TestInvocation> m_waitToDumpWatchdogTimer;
131
132 std::string m_expectedPixelHash;
133
134 WTF::Seconds m_timeout;
135 bool m_dumpJSConsoleLogInStdErr { false };
136
137 // Invocation state
138 bool m_startedTesting { false };
139 bool m_gotInitialResponse { false };
140 bool m_gotFinalMessage { false };
141 bool m_gotRepaint { false };
142 bool m_error { false };
143
144 bool m_waitUntilDone { false };
145 bool m_dumpFrameLoadCallbacks { false };
146 bool m_dumpPixels { false };
147 bool m_pixelResultIsPending { false };
148 bool m_shouldDumpResourceLoadStatistics { false };
149 bool m_canOpenWindows { false };
150 bool m_shouldDumpAdClickAttribution { false };
151 WhatToDump m_whatToDump { WhatToDump::RenderTree };
152
153 StringBuilder m_textOutput;
154 String m_savedResourceLoadStatistics;
155 WKRetainPtr<WKDataRef> m_audioResult;
156 WKRetainPtr<WKImageRef> m_pixelResult;
157 WKRetainPtr<WKArrayRef> m_repaintRects;
158
159 std::unique_ptr<UIScriptContext> m_UIScriptContext;
160 UIScriptInvocationData* m_pendingUIScriptInvocationData { nullptr };
161};
162
163} // namespace WTR
164
165#endif // TestInvocation_h
166