1a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch/*
2a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Copyright (C) 2010 Apple Inc. All rights reserved.
3a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *
4a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Redistribution and use in source and binary forms, with or without
5a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * modification, are permitted provided that the following conditions
6a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * are met:
7a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 1. Redistributions of source code must retain the above copyright
8a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    notice, this list of conditions and the following disclaimer.
9a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 2. Redistributions in binary form must reproduce the above copyright
10a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    notice, this list of conditions and the following disclaimer in the
11a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    documentation and/or other materials provided with the distribution.
12a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *
13a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THE POSSIBILITY OF SUCH DAMAGE.
24a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch */
25a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
26a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "InjectedBundleController.h"
27a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
28a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "InjectedBundleTest.h"
29a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "PlatformUtilities.h"
30a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include <WebKit2/WebKit2.h>
31a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include <algorithm>
32a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include <assert.h>
33a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
34a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochnamespace TestWebKitAPI {
35a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
36a94275402997c11dd2e778633dacf4b7e630a35dBen MurdochInjectedBundleController& InjectedBundleController::shared()
37a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
38a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    static InjectedBundleController& shared = *new InjectedBundleController;
39a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    return shared;
40a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
41a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
42a94275402997c11dd2e778633dacf4b7e630a35dBen MurdochInjectedBundleController::InjectedBundleController()
43a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    : m_bundle(0)
44a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    , m_currentTest(0)
45a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
46a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
47a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
4828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhuvoid InjectedBundleController::initialize(WKBundleRef bundle, WKTypeRef initializationUserData)
49a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
50a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    m_bundle = bundle;
51a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
52a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    WKBundleClient client = {
53a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        0,
54a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        this,
55a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        didCreatePage,
56a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        willDestroyPage,
57f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch        didInitializePageGroup,
58a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        didReceiveMessage
59a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    };
60a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    WKBundleSetClient(m_bundle, &client);
6128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
6228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    // Initialize the test from the "initializationUserData".
6328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
64f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    assert(WKGetTypeID(initializationUserData) == WKDictionaryGetTypeID());
65f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    WKDictionaryRef initializationDictionary = static_cast<WKDictionaryRef>(initializationUserData);
66f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
67f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    WKStringRef testName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(initializationDictionary, WKStringCreateWithUTF8CString("TestName")));
68f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    WKTypeRef userData = WKDictionaryGetItemForKey(initializationDictionary, WKStringCreateWithUTF8CString("UserData"));
69f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
70f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    initializeTestNamed(bundle, Util::toSTD(testName), userData);
71a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
72a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
73a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochvoid InjectedBundleController::didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
74a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
75a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
76a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    assert(self->m_currentTest);
77a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    self->m_currentTest->didCreatePage(bundle, page);
78a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
79a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
80a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochvoid InjectedBundleController::willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
81a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
82a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
83a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    assert(self->m_currentTest);
84a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    self->m_currentTest->willDestroyPage(bundle, page);
85a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
86a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
87f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochvoid InjectedBundleController::didInitializePageGroup(WKBundleRef bundle, WKBundlePageGroupRef pageGroup, const void* clientInfo)
88f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch{
89f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
90f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    assert(self->m_currentTest);
91f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    self->m_currentTest->didInitializePageGroup(bundle, pageGroup);
92f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch}
93f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch
94a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochvoid InjectedBundleController::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
95a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
96a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    InjectedBundleController* self = static_cast<InjectedBundleController*>(const_cast<void*>(clientInfo));
97a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    assert(self->m_currentTest);
98a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    self->m_currentTest->didReceiveMessage(bundle, messageName, messageBody);
99a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
100a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
101a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochvoid InjectedBundleController::dumpTestNames()
102a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
103a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    std::map<std::string, CreateInjectedBundleTestFunction>::const_iterator it = m_createInjectedBundleTestFunctions.begin();
104a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    std::map<std::string, CreateInjectedBundleTestFunction>::const_iterator end = m_createInjectedBundleTestFunctions.end();
105a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    for (; it != end; ++it)
106a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        printf("%s\n", (*it).first.c_str());
107a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
108a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
109f05b935882198ccf7d81675736e3aeb089c5113aBen Murdochvoid InjectedBundleController::initializeTestNamed(WKBundleRef bundle, const std::string& identifier, WKTypeRef userData)
110a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
111a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    CreateInjectedBundleTestFunction createTestFunction = m_createInjectedBundleTestFunctions[identifier];
112a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    if (!createTestFunction) {
113a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        printf("ERROR: InjectedBundle test not found - %s\n", identifier.c_str());
114a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        exit(1);
115a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    }
116a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
117a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    m_currentTest = createTestFunction(identifier);
118f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch    m_currentTest->initialize(bundle, userData);
119a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
120a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
121a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochvoid InjectedBundleController::registerCreateInjectedBundleTestFunction(const std::string& identifier, CreateInjectedBundleTestFunction function)
122a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch{
123a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    m_createInjectedBundleTestFunctions[identifier] = function;
124a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch}
125a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
126a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch} // namespace TestWebKitAPI
127