InjectedBundleController.cpp 5.8 KB

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