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