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 TestController_h
27#define TestController_h
28
29#include <WebKit2/WKRetainPtr.h>
30#include <string>
31#include <vector>
32#include <wtf/OwnPtr.h>
33
34namespace WTR {
35
36class TestInvocation;
37class PlatformWebView;
38
39// FIXME: Rename this TestRunner?
40class TestController {
41public:
42    static TestController& shared();
43
44    TestController(int argc, const char* argv[]);
45    ~TestController();
46
47    bool verbose() const { return m_verbose; }
48
49    WKStringRef injectedBundlePath() { return m_injectedBundlePath.get(); }
50    WKStringRef testPluginDirectory() { return m_testPluginDirectory.get(); }
51
52    PlatformWebView* mainWebView() { return m_mainWebView.get(); }
53    WKContextRef context() { return m_context.get(); }
54
55    // Runs the run loop until `done` is true or the timeout elapses.
56    enum TimeoutDuration { ShortTimeout, LongTimeout };
57    void runUntil(bool& done, TimeoutDuration);
58    void notifyDone();
59
60private:
61    void initialize(int argc, const char* argv[]);
62    void run();
63
64    void runTestingServerLoop();
65    bool runTest(const char* pathOrURL);
66
67    void platformInitialize();
68    void platformInitializeContext();
69    void platformRunUntil(bool& done, double timeout);
70    void initializeInjectedBundlePath();
71    void initializeTestPluginDirectory();
72
73    bool resetStateToConsistentValues();
74
75    // WKContextInjectedBundleClient
76    static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
77    static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
78    void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
79    WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
80
81    // WKPageLoaderClient
82    static void didFinishLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void*);
83    void didFinishLoadForFrame(WKPageRef page, WKFrameRef frame);
84
85    static void processDidCrash(WKPageRef, const void* clientInfo);
86    void processDidCrash();
87
88    static WKPageRef createOtherPage(WKPageRef oldPage, WKDictionaryRef, WKEventModifiers, WKEventMouseButton, const void*);
89
90    static void runModal(WKPageRef, const void* clientInfo);
91    static void runModal(PlatformWebView*);
92
93    static const char* libraryPathForTesting();
94    static const char* platformLibraryPathForTesting();
95
96    OwnPtr<TestInvocation> m_currentInvocation;
97
98    bool m_dumpPixels;
99    bool m_verbose;
100    bool m_printSeparators;
101    bool m_usingServerMode;
102    std::vector<std::string> m_paths;
103    WKRetainPtr<WKStringRef> m_injectedBundlePath;
104    WKRetainPtr<WKStringRef> m_testPluginDirectory;
105
106    OwnPtr<PlatformWebView> m_mainWebView;
107    WKRetainPtr<WKContextRef> m_context;
108    WKRetainPtr<WKPageGroupRef> m_pageGroup;
109
110    enum State {
111        Initial,
112        Resetting,
113        RunningTest
114    };
115    State m_state;
116    bool m_doneResetting;
117
118    double m_longTimeout;
119    double m_shortTimeout;
120
121    bool m_didPrintWebProcessCrashedMessage;
122    bool m_shouldExitWhenWebProcessCrashes;
123};
124
125} // namespace WTR
126
127#endif // TestController_h
128