1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "config.h"
  27 
  28 #include <memory>
  29 #include "JavaEnv.h"
  30 #include "TestRunner.h"
  31 #include "GCController.h"
  32 #include "EventSender.h"
  33 #include "WorkQueue.h"
  34 #include "WebCore/testing/js/WebCoreTestSupport.h"
  35 
  36 #include <wtf/RefPtr.h>
  37 #include <JavaScriptCore/JavaScript.h>
  38 #include <JavaScriptCore/TestRunnerUtils.h>
  39 
  40 RefPtr<TestRunner> gTestRunner;
  41 std::unique_ptr<GCController> gGCController;
  42 JSGlobalContextRef gContext;
  43 
  44 #ifdef __cplusplus
  45 extern "C" {
  46 #endif
  47 
  48 JNIEXPORT void JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_init
  49     (JNIEnv* env, jclass cls, jstring testPath, jstring pixelsHash)
  50 {
  51     const char* testPathChars = env->GetStringUTFChars(testPath, NULL);
  52     const char* pixelsHashChars = env->GetStringUTFChars(pixelsHash, NULL);
  53 
  54     ASSERT(!gTestRunner);
  55     gTestRunner = TestRunner::create(testPathChars, pixelsHashChars);
  56     ASSERT(!gGCController);
  57     gGCController = std::make_unique<GCController>();
  58 
  59     WorkQueue::singleton().clear();
  60 
  61     env->ReleaseStringUTFChars(testPath, testPathChars);
  62     env->ReleaseStringUTFChars(pixelsHash, pixelsHashChars);
  63 }
  64 
  65 JNIEXPORT void JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_didClearWindowObject
  66     (JNIEnv* env, jclass cls, jlong pContext, jlong pWindowObject,
  67     jobject eventSender)
  68 {
  69     if(!gTestRunner || !gGCController)
  70         return;
  71     ASSERT(pContext);
  72     ASSERT(pWindowObject);
  73     ASSERT(eventSender);
  74 
  75     gContext = static_cast<JSGlobalContextRef>(jlong_to_ptr(pContext));
  76     JSObjectRef windowObject =
  77             static_cast<JSObjectRef>(jlong_to_ptr(pWindowObject));
  78 
  79     JSValueRef exception = 0;
  80 
  81     gTestRunner->makeWindowObject(gContext, windowObject, &exception);
  82     ASSERT(!exception);
  83 
  84     JLObject jlEventSender(eventSender, true);
  85     makeEventSender(gContext, windowObject, jlEventSender, &exception);
  86     ASSERT(!exception);
  87     WebCoreTestSupport::injectInternalsObject(gContext);
  88     gGCController->makeWindowObject(gContext, windowObject, &exception);
  89     ASSERT(!exception);
  90 }
  91 
  92 JNIEXPORT void JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_dispose
  93     (JNIEnv* env, jclass cls)
  94 {
  95     ASSERT(gTestRunner);
  96     gTestRunner->cleanup();
  97     gTestRunner = nullptr;
  98     ASSERT(gGCController);
  99     gGCController = nullptr;
 100     JSC::waitForVMDestruction();
 101 }
 102 
 103 JNIEXPORT jboolean JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_dumpAsText
 104     (JNIEnv* env, jclass cls)
 105 {
 106     ASSERT(gTestRunner);
 107     return bool_to_jbool(gTestRunner->dumpAsText());
 108 }
 109 
 110 JNIEXPORT jboolean JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_dumpChildFramesAsText
 111     (JNIEnv* env, jclass cls)
 112 {
 113     ASSERT(gTestRunner);
 114     return bool_to_jbool(gTestRunner->dumpChildFramesAsText());
 115 }
 116 
 117 JNIEXPORT jboolean JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_didFinishLoad
 118     (JNIEnv* env, jclass cls)
 119 {
 120     ASSERT(gTestRunner);
 121     return bool_to_jbool(WorkQueue::singleton().processWork());
 122 }
 123 
 124 JNIEXPORT jboolean JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_dumpBackForwardList
 125     (JNIEnv* env, jclass cls)
 126 {
 127     ASSERT(gTestRunner);
 128     return bool_to_jbool(gTestRunner->dumpBackForwardList());
 129 }
 130 
 131 JNIEXPORT jboolean JNICALL Java_com_sun_javafx_webkit_drt_DumpRenderTree_shouldStayOnPageAfterHandlingBeforeUnload
 132     (JNIEnv*, jclass)
 133 {
 134     ASSERT(gTestRunner);
 135     return bool_to_jbool(gTestRunner->shouldStayOnPageAfterHandlingBeforeUnload());
 136 }
 137 
 138 #ifdef __cplusplus
 139 }
 140 #endif