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