blob: 53f39a8743c343588fdae11f09e450c3df1c2891 [file] [log] [blame]
weinig@apple.com54e415c2010-10-21 00:22:13 +00001/*
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
commit-queue@webkit.orgf56d8d22011-09-15 14:08:42 +000026#include "config.h"
simon.fraser@apple.comaead0ac2014-12-18 19:22:48 +000027
28#if WK_HAVE_C_SPI
29
weinig@apple.com54e415c2010-10-21 00:22:13 +000030#include "InjectedBundleController.h"
31
32#include "InjectedBundleTest.h"
33#include "PlatformUtilities.h"
weinig@apple.com54e415c2010-10-21 00:22:13 +000034#include <algorithm>
35#include <assert.h>
36
37namespace TestWebKitAPI {
38
cdumez@apple.com4faf1ef2015-01-30 20:49:33 +000039InjectedBundleController& InjectedBundleController::singleton()
weinig@apple.com54e415c2010-10-21 00:22:13 +000040{
41 static InjectedBundleController& shared = *new InjectedBundleController;
42 return shared;
43}
44
45InjectedBundleController::InjectedBundleController()
46 : m_bundle(0)
47 , m_currentTest(0)
48{
49}
50
weinig@apple.com7763b882010-11-02 22:06:08 +000051void InjectedBundleController::initialize(WKBundleRef bundle, WKTypeRef initializationUserData)
weinig@apple.com54e415c2010-10-21 00:22:13 +000052{
andersca@apple.comaf805932011-08-01 03:32:07 +000053 platformInitialize();
54
weinig@apple.com54e415c2010-10-21 00:22:13 +000055 m_bundle = bundle;
56
andersca@apple.comaf805932011-08-01 03:32:07 +000057 if (!initializationUserData)
58 return;
59
andersca@apple.coma8996642013-12-04 18:49:49 +000060 WKBundleClientV1 client = {
61 { 0, this },
weinig@apple.com54e415c2010-10-21 00:22:13 +000062 didCreatePage,
63 willDestroyPage,
weinig@apple.com40a04ca2010-12-01 22:00:01 +000064 didInitializePageGroup,
ap@apple.com38d20fc2012-08-22 16:36:47 +000065 didReceiveMessage,
66 didReceiveMessageToPage
weinig@apple.com54e415c2010-10-21 00:22:13 +000067 };
andersca@apple.coma8996642013-12-04 18:49:49 +000068 WKBundleSetClient(m_bundle, &client.base);
weinig@apple.com7763b882010-11-02 22:06:08 +000069
70 // Initialize the test from the "initializationUserData".
weinig@apple.com7763b882010-11-02 22:06:08 +000071
weinig@apple.com40a04ca2010-12-01 22:00:01 +000072 assert(WKGetTypeID(initializationUserData) == WKDictionaryGetTypeID());
73 WKDictionaryRef initializationDictionary = static_cast<WKDictionaryRef>(initializationUserData);
74
commit-queue@webkit.orgcc123592012-09-21 16:14:01 +000075 WKRetainPtr<WKStringRef> testNameKey(AdoptWK, WKStringCreateWithUTF8CString("TestName"));
76 WKStringRef testName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(initializationDictionary, testNameKey.get()));
andersca@apple.comaf805932011-08-01 03:32:07 +000077
commit-queue@webkit.orgcc123592012-09-21 16:14:01 +000078 WKRetainPtr<WKStringRef> userDataKey(AdoptWK, WKStringCreateWithUTF8CString("UserData"));
79 WKTypeRef userData = WKDictionaryGetItemForKey(initializationDictionary, userDataKey.get());
weinig@apple.com40a04ca2010-12-01 22:00:01 +000080 initializeTestNamed(bundle, Util::toSTD(testName), userData);
weinig@apple.com54e415c2010-10-21 00:22:13 +000081}
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
weinig@apple.com40a04ca2010-12-01 22:00:01 +000097void 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
weinig@apple.com54e415c2010-10-21 00:22:13 +0000104void InjectedBundleController::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
105{
106 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
weinig@apple.com54e415c2010-10-21 00:22:13 +0000107 assert(self->m_currentTest);
108 self->m_currentTest->didReceiveMessage(bundle, messageName, messageBody);
109}
110
ap@apple.com38d20fc2012-08-22 16:36:47 +0000111void 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
weinig@apple.com54e415c2010-10-21 00:22:13 +0000118void 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
weinig@apple.com40a04ca2010-12-01 22:00:01 +0000126void InjectedBundleController::initializeTestNamed(WKBundleRef bundle, const std::string& identifier, WKTypeRef userData)
weinig@apple.com54e415c2010-10-21 00:22:13 +0000127{
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);
weinig@apple.com40a04ca2010-12-01 22:00:01 +0000135 m_currentTest->initialize(bundle, userData);
weinig@apple.com54e415c2010-10-21 00:22:13 +0000136}
137
138void InjectedBundleController::registerCreateInjectedBundleTestFunction(const std::string& identifier, CreateInjectedBundleTestFunction function)
139{
140 m_createInjectedBundleTestFunctions[identifier] = function;
141}
142
143} // namespace TestWebKitAPI
simon.fraser@apple.comaead0ac2014-12-18 19:22:48 +0000144
145#endif