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#include "config.h"
27
28#if WK_HAVE_C_SPI
29
30#include "InjectedBundleController.h"
31
32#include "InjectedBundleTest.h"
33#include "PlatformUtilities.h"
34#include <algorithm>
35#include <assert.h>
36
37namespace TestWebKitAPI {
38
39InjectedBundleController& InjectedBundleController::singleton()
40{
41 static InjectedBundleController& shared = *new InjectedBundleController;
42 return shared;
43}
44
45InjectedBundleController::InjectedBundleController()
46 : m_bundle(0)
47 , m_currentTest(0)
48{
49}
50
51void InjectedBundleController::initialize(WKBundleRef bundle, WKTypeRef initializationUserData)
52{
53 platformInitialize();
54
55 m_bundle = bundle;
56
57 if (!initializationUserData)
58 return;
59
60 WKBundleClientV1 client = {
61 { 0, this },
62 didCreatePage,
63 willDestroyPage,
64 didInitializePageGroup,
65 didReceiveMessage,
66 didReceiveMessageToPage
67 };
68 WKBundleSetClient(m_bundle, &client.base);
69
70 // Initialize the test from the "initializationUserData".
71
72 assert(WKGetTypeID(initializationUserData) == WKDictionaryGetTypeID());
73 WKDictionaryRef initializationDictionary = static_cast<WKDictionaryRef>(initializationUserData);
74
75 WKRetainPtr<WKStringRef> testNameKey = adoptWK(WKStringCreateWithUTF8CString("TestName"));
76 WKStringRef testName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(initializationDictionary, testNameKey.get()));
77
78 WKRetainPtr<WKStringRef> userDataKey = adoptWK(WKStringCreateWithUTF8CString("UserData"));
79 WKTypeRef userData = WKDictionaryGetItemForKey(initializationDictionary, userDataKey.get());
80 initializeTestNamed(bundle, Util::toSTD(testName), userData);
81}
82
83void InjectedBundleController::didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
84{
85 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
86 assert(self->m_currentTest);
87 self->m_currentTest->didCreatePage(bundle, page);
88}
89
90void InjectedBundleController::willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
91{
92 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
93 assert(self->m_currentTest);
94 self->m_currentTest->willDestroyPage(bundle, page);
95}
96
97void InjectedBundleController::didInitializePageGroup(WKBundleRef bundle, WKBundlePageGroupRef pageGroup, const void* clientInfo)
98{
99 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
100 assert(self->m_currentTest);
101 self->m_currentTest->didInitializePageGroup(bundle, pageGroup);
102}
103
104void InjectedBundleController::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
105{
106 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
107 assert(self->m_currentTest);
108 self->m_currentTest->didReceiveMessage(bundle, messageName, messageBody);
109}
110
111void InjectedBundleController::didReceiveMessageToPage(WKBundleRef bundle, WKBundlePageRef page, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
112{
113 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
114 assert(self->m_currentTest);
115 self->m_currentTest->didReceiveMessageToPage(bundle, page, messageName, messageBody);
116}
117
118void InjectedBundleController::dumpTestNames()
119{
120 std::map<std::string, CreateInjectedBundleTestFunction>::const_iterator it = m_createInjectedBundleTestFunctions.begin();
121 std::map<std::string, CreateInjectedBundleTestFunction>::const_iterator end = m_createInjectedBundleTestFunctions.end();
122 for (; it != end; ++it)
123 printf("%s\n", (*it).first.c_str());
124}
125
126void InjectedBundleController::initializeTestNamed(WKBundleRef bundle, const std::string& identifier, WKTypeRef userData)
127{
128 CreateInjectedBundleTestFunction createTestFunction = m_createInjectedBundleTestFunctions[identifier];
129 if (!createTestFunction) {
130 printf("ERROR: InjectedBundle test not found - %s\n", identifier.c_str());
131 exit(1);
132 }
133
134 m_currentTest = createTestFunction(identifier);
135 m_currentTest->initialize(bundle, userData);
136}
137
138void InjectedBundleController::registerCreateInjectedBundleTestFunction(const std::string& identifier, CreateInjectedBundleTestFunction function)
139{
140 m_createInjectedBundleTestFunctions[identifier] = function;
141}
142
143} // namespace TestWebKitAPI
144
145#endif
146