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'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "TestsController.h"
26
27#include "Test.h"
28#include <wtf/PassOwnPtr.h>
29
30using namespace std;
31
32namespace WebKitAPITest {
33
34static const LPCWSTR testsControllerWindowClassName = L"TestsControllerWindowClass";
35
36enum { runNextTestTimerID = 1 };
37
38inline TestsController::TestsController()
39    : m_testFailed(false)
40    , m_anyTestFailed(false)
41{
42    registerWindowClass();
43    m_window = CreateWindowExW(0, testsControllerWindowClassName, 0, WS_CHILD, 0, 0, 0, 0, HWND_MESSAGE, 0, GetModuleHandle(0), 0);
44}
45
46TestsController& TestsController::shared()
47{
48    static TestsController& shared = *new TestsController;
49    return shared;
50}
51
52bool TestsController::runAllTests()
53{
54    if (m_tests.isEmpty())
55        return true;
56
57    MSG msg;
58    BOOL result;
59    while ((result = GetMessage(&msg, 0, 0, 0))) {
60        if (result == -1)
61            break;
62        TranslateMessage(&msg);
63        DispatchMessage(&msg);
64    }
65
66    if (msg.message != WM_QUIT)
67        return false;
68
69    return !m_anyTestFailed;
70}
71
72void TestsController::addTest(PassOwnPtr<Test> test)
73{
74    m_tests.append(test.release());
75    runNextTestSoon();
76}
77
78void TestsController::testFailed(const char* file, int line, const char* message)
79{
80    ASSERT(!m_tests.isEmpty());
81
82    m_testFailed = true;
83    m_anyTestFailed = true;
84
85    printf("FAIL: %s\n\t%s (%s:%d)\n", m_tests.first()->name(), message, file, line);
86    fflush(stdout);
87}
88
89void TestsController::runNextTest()
90{
91    if (m_tests.isEmpty()) {
92        PostQuitMessage(0);
93        return;
94    }
95
96    Test* test = m_tests.first();
97
98    m_testFailed = false;
99    printf("RUN: %s\n", test->name());
100    fflush(stdout);
101    test->run();
102
103    if (!m_testFailed) {
104        printf("PASS: %s\n", test->name());
105        fflush(stdout);
106    }
107
108    m_tests.removeFirst();
109    delete test;
110
111    runNextTestSoon();
112}
113
114void TestsController::runNextTestSoon()
115{
116    SetTimer(m_window, runNextTestTimerID, 0, 0);
117}
118
119void TestsController::registerWindowClass()
120{
121    static bool initialized;
122    if (initialized)
123        return;
124    initialized = true;
125
126    WNDCLASSEXW wndClass = {0};
127    wndClass.cbSize = sizeof(wndClass);
128    wndClass.lpfnWndProc = wndProc;
129    wndClass.hCursor = LoadCursor(0, IDC_ARROW);
130    wndClass.hInstance = GetModuleHandle(0);
131    wndClass.lpszClassName = testsControllerWindowClassName;
132
133    RegisterClassExW(&wndClass);
134}
135
136LRESULT TestsController::wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
137{
138    if (uMsg == WM_TIMER && wParam == runNextTestTimerID) {
139        KillTimer(hWnd, runNextTestTimerID);
140        TestsController::shared().runNextTest();
141        return 0;
142    }
143
144    return DefWindowProcW(hWnd, uMsg, wParam, lParam);
145}
146
147} // namespace WebKitAPITest
148