weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 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 | |
commit-queue@webkit.org | f56d8d2 | 2011-09-15 14:08:42 +0000 | [diff] [blame] | 26 | #include "config.h" |
simon.fraser@apple.com | aead0ac | 2014-12-18 19:22:48 +0000 | [diff] [blame] | 27 | |
| 28 | #if WK_HAVE_C_SPI |
| 29 | |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 30 | #include "InjectedBundleController.h" |
| 31 | |
| 32 | #include "InjectedBundleTest.h" |
| 33 | #include "PlatformUtilities.h" |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | #include <assert.h> |
| 36 | |
| 37 | namespace TestWebKitAPI { |
| 38 | |
cdumez@apple.com | 4faf1ef | 2015-01-30 20:49:33 +0000 | [diff] [blame] | 39 | InjectedBundleController& InjectedBundleController::singleton() |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 40 | { |
| 41 | static InjectedBundleController& shared = *new InjectedBundleController; |
| 42 | return shared; |
| 43 | } |
| 44 | |
| 45 | InjectedBundleController::InjectedBundleController() |
| 46 | : m_bundle(0) |
| 47 | , m_currentTest(0) |
| 48 | { |
| 49 | } |
| 50 | |
weinig@apple.com | 7763b88 | 2010-11-02 22:06:08 +0000 | [diff] [blame] | 51 | void InjectedBundleController::initialize(WKBundleRef bundle, WKTypeRef initializationUserData) |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 52 | { |
andersca@apple.com | af80593 | 2011-08-01 03:32:07 +0000 | [diff] [blame] | 53 | platformInitialize(); |
| 54 | |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 55 | m_bundle = bundle; |
| 56 | |
andersca@apple.com | af80593 | 2011-08-01 03:32:07 +0000 | [diff] [blame] | 57 | if (!initializationUserData) |
| 58 | return; |
| 59 | |
andersca@apple.com | a899664 | 2013-12-04 18:49:49 +0000 | [diff] [blame] | 60 | WKBundleClientV1 client = { |
| 61 | { 0, this }, |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 62 | didCreatePage, |
| 63 | willDestroyPage, |
weinig@apple.com | 40a04ca | 2010-12-01 22:00:01 +0000 | [diff] [blame] | 64 | didInitializePageGroup, |
ap@apple.com | 38d20fc | 2012-08-22 16:36:47 +0000 | [diff] [blame] | 65 | didReceiveMessage, |
| 66 | didReceiveMessageToPage |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 67 | }; |
andersca@apple.com | a899664 | 2013-12-04 18:49:49 +0000 | [diff] [blame] | 68 | WKBundleSetClient(m_bundle, &client.base); |
weinig@apple.com | 7763b88 | 2010-11-02 22:06:08 +0000 | [diff] [blame] | 69 | |
| 70 | // Initialize the test from the "initializationUserData". |
weinig@apple.com | 7763b88 | 2010-11-02 22:06:08 +0000 | [diff] [blame] | 71 | |
weinig@apple.com | 40a04ca | 2010-12-01 22:00:01 +0000 | [diff] [blame] | 72 | assert(WKGetTypeID(initializationUserData) == WKDictionaryGetTypeID()); |
| 73 | WKDictionaryRef initializationDictionary = static_cast<WKDictionaryRef>(initializationUserData); |
| 74 | |
commit-queue@webkit.org | cc12359 | 2012-09-21 16:14:01 +0000 | [diff] [blame] | 75 | WKRetainPtr<WKStringRef> testNameKey(AdoptWK, WKStringCreateWithUTF8CString("TestName")); |
| 76 | WKStringRef testName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(initializationDictionary, testNameKey.get())); |
andersca@apple.com | af80593 | 2011-08-01 03:32:07 +0000 | [diff] [blame] | 77 | |
commit-queue@webkit.org | cc12359 | 2012-09-21 16:14:01 +0000 | [diff] [blame] | 78 | WKRetainPtr<WKStringRef> userDataKey(AdoptWK, WKStringCreateWithUTF8CString("UserData")); |
| 79 | WKTypeRef userData = WKDictionaryGetItemForKey(initializationDictionary, userDataKey.get()); |
weinig@apple.com | 40a04ca | 2010-12-01 22:00:01 +0000 | [diff] [blame] | 80 | initializeTestNamed(bundle, Util::toSTD(testName), userData); |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void 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 | |
| 90 | void 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.com | 40a04ca | 2010-12-01 22:00:01 +0000 | [diff] [blame] | 97 | void 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.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 104 | void InjectedBundleController::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo) |
| 105 | { |
| 106 | InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo)); |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 107 | assert(self->m_currentTest); |
| 108 | self->m_currentTest->didReceiveMessage(bundle, messageName, messageBody); |
| 109 | } |
| 110 | |
ap@apple.com | 38d20fc | 2012-08-22 16:36:47 +0000 | [diff] [blame] | 111 | void 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.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 118 | void 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.com | 40a04ca | 2010-12-01 22:00:01 +0000 | [diff] [blame] | 126 | void InjectedBundleController::initializeTestNamed(WKBundleRef bundle, const std::string& identifier, WKTypeRef userData) |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 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); |
weinig@apple.com | 40a04ca | 2010-12-01 22:00:01 +0000 | [diff] [blame] | 135 | m_currentTest->initialize(bundle, userData); |
weinig@apple.com | 54e415c | 2010-10-21 00:22:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void InjectedBundleController::registerCreateInjectedBundleTestFunction(const std::string& identifier, CreateInjectedBundleTestFunction function) |
| 139 | { |
| 140 | m_createInjectedBundleTestFunctions[identifier] = function; |
| 141 | } |
| 142 | |
| 143 | } // namespace TestWebKitAPI |
simon.fraser@apple.com | aead0ac | 2014-12-18 19:22:48 +0000 | [diff] [blame] | 144 | |
| 145 | #endif |