2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "InjectedBundleController.h"
29 #include "InjectedBundleTest.h"
30 #include "PlatformUtilities.h"
34 namespace TestWebKitAPI {
36 InjectedBundleController& InjectedBundleController::shared()
38 static InjectedBundleController& shared = *new InjectedBundleController;
42 InjectedBundleController::InjectedBundleController()
48 void InjectedBundleController::initialize(WKBundleRef bundle, WKTypeRef initializationUserData)
54 if (!initializationUserData)
57 WKBundleClient client = {
62 didInitializePageGroup,
65 WKBundleSetClient(m_bundle, &client);
67 // Initialize the test from the "initializationUserData".
69 assert(WKGetTypeID(initializationUserData) == WKDictionaryGetTypeID());
70 WKDictionaryRef initializationDictionary = static_cast<WKDictionaryRef>(initializationUserData);
72 WKStringRef testName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(initializationDictionary, WKStringCreateWithUTF8CString("TestName")));
74 WKTypeRef userData = WKDictionaryGetItemForKey(initializationDictionary, WKStringCreateWithUTF8CString("UserData"));
75 initializeTestNamed(bundle, Util::toSTD(testName), userData);
78 void InjectedBundleController::didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
80 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
81 assert(self->m_currentTest);
82 self->m_currentTest->didCreatePage(bundle, page);
85 void InjectedBundleController::willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
87 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
88 assert(self->m_currentTest);
89 self->m_currentTest->willDestroyPage(bundle, page);
92 void InjectedBundleController::didInitializePageGroup(WKBundleRef bundle, WKBundlePageGroupRef pageGroup, const void* clientInfo)
94 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
95 assert(self->m_currentTest);
96 self->m_currentTest->didInitializePageGroup(bundle, pageGroup);
99 void InjectedBundleController::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
101 InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
102 assert(self->m_currentTest);
103 self->m_currentTest->didReceiveMessage(bundle, messageName, messageBody);
106 void InjectedBundleController::dumpTestNames()
108 std::map<std::string, CreateInjectedBundleTestFunction>::const_iterator it = m_createInjectedBundleTestFunctions.begin();
109 std::map<std::string, CreateInjectedBundleTestFunction>::const_iterator end = m_createInjectedBundleTestFunctions.end();
110 for (; it != end; ++it)
111 printf("%s\n", (*it).first.c_str());
114 void InjectedBundleController::initializeTestNamed(WKBundleRef bundle, const std::string& identifier, WKTypeRef userData)
116 CreateInjectedBundleTestFunction createTestFunction = m_createInjectedBundleTestFunctions[identifier];
117 if (!createTestFunction) {
118 printf("ERROR: InjectedBundle test not found - %s\n", identifier.c_str());
122 m_currentTest = createTestFunction(identifier);
123 m_currentTest->initialize(bundle, userData);
126 void InjectedBundleController::registerCreateInjectedBundleTestFunction(const std::string& identifier, CreateInjectedBundleTestFunction function)
128 m_createInjectedBundleTestFunctions[identifier] = function;
131 } // namespace TestWebKitAPI