1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 #include "config.h"
   5 #include "JavaEnv.h"
   6 #include "TestRunner.h"
   7 #include "WorkQueue.h"
   8 #include "WorkQueueItem.h"
   9 
  10 #include <wtf/java/JavaRef.h>
  11 #include <JavaScriptCore/JSRetainPtr.h>
  12 #include <JavaScriptCore/JSStringRef.h>
  13 
  14 extern JSGlobalContextRef gContext;
  15 
  16 jstring JSStringRef_to_jstring(JSStringRef ref, JNIEnv* env)
  17 {
  18     size_t size = JSStringGetLength(ref);
  19     const JSChar* jschars = JSStringGetCharactersPtr(ref);
  20     return env->NewString((const jchar*)jschars, (jsize)size);
  21 }
  22 
  23 JSStringRef jstring_to_JSStringRef(jstring str, JNIEnv* env)
  24 {
  25     jsize size = env->GetStringLength(str);
  26     const jchar* chars = env->GetStringCritical(str, NULL);
  27     JSStringRef ref = JSStringCreateWithCharacters((const JSChar*)chars, size);
  28     env->ReleaseStringCritical(str, chars);
  29     return ref;
  30 }
  31 
  32 bool LoadHTMLStringItem::invoke() const
  33 {
  34     return false;
  35 }
  36 
  37 
  38 TestRunner::~TestRunner()
  39 {
  40     // FIXME: implement
  41 }
  42 
  43 void TestRunner::addDisallowedURL(JSStringRef url)
  44 {
  45     // FIXME: implement
  46 }
  47 
  48 void TestRunner::clearAllDatabases()
  49 {
  50     // FIXME: implement
  51 }
  52 
  53 void TestRunner::clearBackForwardList()
  54 {
  55     JNIEnv* env = DumpRenderTree_GetJavaEnv();
  56     env->CallStaticVoidMethod(getDumpRenderTreeClass(), getClearBackForwardListMID());
  57     CheckAndClearException(env);
  58 }
  59 
  60 void TestRunner::clearPersistentUserStyleSheet()
  61 {
  62     // FIXME: implement
  63 }
  64 
  65 JSStringRef TestRunner::copyDecodedHostName(JSStringRef name)
  66 {
  67     // FIXME: implement
  68     return 0;
  69 }
  70 
  71 JSStringRef TestRunner::copyEncodedHostName(JSStringRef name)
  72 {
  73     // FIXME: implement
  74     return 0;
  75 }
  76 
  77 void TestRunner::dispatchPendingLoadRequests()
  78 {
  79     // FIXME: implement
  80 }
  81 
  82 void TestRunner::display()
  83 {
  84     // FIXME: implement
  85 }
  86 
  87 void TestRunner::displayAndTrackRepaints()
  88 {
  89     // FIXME: implement
  90 }
  91 
  92 void TestRunner::execCommand(JSStringRef name, JSStringRef value)
  93 {
  94     // FIXME: implement
  95 }
  96 
  97 bool TestRunner::isCommandEnabled(JSStringRef name)
  98 {
  99     // FIXME: implement
 100     return false;
 101 }
 102 
 103 void TestRunner::keepWebHistory()
 104 {
 105     // FIXME: implement
 106 }
 107 
 108 void TestRunner::notifyDone()
 109 {
 110     JNIEnv* env = DumpRenderTree_GetJavaEnv();
 111     env->CallStaticVoidMethod(getDumpRenderTreeClass(), getNotifyDoneMID());
 112     CheckAndClearException(env);
 113 }
 114 
 115 void TestRunner::overridePreference(JSStringRef key, JSStringRef value)
 116 {
 117     JNIEnv* env = DumpRenderTree_GetJavaEnv();
 118     JLString jRelKey(JSStringRef_to_jstring(key, env));
 119     JLString jRelValue(JSStringRef_to_jstring(value, env));
 120     env->CallStaticVoidMethod(getDumpRenderTreeClass(), getOverridePreferenceMID(), (jstring)jRelKey, (jstring)jRelValue);
 121     CheckAndClearException(env);
 122 }
 123 
 124 void TestRunner::removeAllVisitedLinks()
 125 {
 126     // FIXME: implement
 127 }
 128 
 129 JSStringRef TestRunner::pathToLocalResource(JSContextRef context, JSStringRef url)
 130 {
 131     // Function introduced in r28690. This may need special-casing on Windows.
 132     return url; // Do nothing on Unix.
 133 }
 134 
 135 size_t TestRunner::webHistoryItemCount()
 136 {
 137     JNIEnv* env = DumpRenderTree_GetJavaEnv();
 138     jint count = env->CallStaticIntMethod(getDumpRenderTreeClass(), getGetBackForwardItemCountMID());
 139     CheckAndClearException(env);
 140     return (size_t)count;
 141 }
 142 
 143 void TestRunner::queueLoad(JSStringRef url, JSStringRef target)
 144 {
 145     JNIEnv* env = DumpRenderTree_GetJavaEnv();
 146     JLString jRelUrl(JSStringRef_to_jstring(url, env));
 147     JLString jAbsUrl((jstring)env->CallStaticObjectMethod(getDumpRenderTreeClass(), getResolveURLMID(), (jstring)jRelUrl));
 148     CheckAndClearException(env);
 149     JSStringRef absUrlRef = jstring_to_JSStringRef((jstring)jAbsUrl, env);
 150     WorkQueue::singleton().queue(new LoadItem(absUrlRef, target));
 151 }
 152 
 153 void TestRunner::setAcceptsEditing(bool newAcceptsEditing)
 154 {
 155     // FIXME: implement
 156 }
 157 
 158 void TestRunner::setAppCacheMaximumSize(unsigned long long quota)
 159 {
 160     // FIXME: implement
 161 }
 162 
 163 void TestRunner::setAllowUniversalAccessFromFileURLs(bool)
 164 {
 165     // FIXME: implement
 166 }
 167 
 168 void TestRunner::setAuthorAndUserStylesEnabled(bool enabled)
 169 {
 170     // FIXME: implement
 171 }
 172 
 173 void TestRunner::setCacheModel(int model)
 174 {
 175     // FIXME: implement
 176 }
 177 
 178 void TestRunner::setCustomPolicyDelegate(bool setDelegate, bool permissive)
 179 {
 180     // FIXME: implement
 181 }
 182 
 183 void TestRunner::setDatabaseQuota(unsigned long long quota)
 184 {
 185     // FIXME: implement
 186 }
 187 
 188 void TestRunner::setIconDatabaseEnabled(bool enabled)
 189 {
 190     // FIXME: implement
 191 }
 192 
 193 void TestRunner::setMainFrameIsFirstResponder(bool enabled)
 194 {
 195     // FIXME: implement
 196 }
 197 
 198 void TestRunner::setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed, bool providesFloorLevel, double floorLevel)
 199 {
 200     // FIXME: implement
 201 }
 202 
 203 void TestRunner::setPersistentUserStyleSheetLocation(JSStringRef path)
 204 {
 205     // FIXME: implement
 206 }
 207 
 208 void TestRunner::setPopupBlockingEnabled(bool enabled)
 209 {
 210     // FIXME: implement
 211 }
 212 
 213 void TestRunner::setPrivateBrowsingEnabled(bool enabled)
 214 {
 215     // FIXME: implement
 216 }
 217 
 218 void TestRunner::setXSSAuditorEnabled(bool enabled)
 219 {
 220     // FIXME: implement
 221 }
 222 
 223 void TestRunner::setTabKeyCyclesThroughElements(bool cycles)
 224 {
 225     // FIXME: implement
 226 }
 227 
 228 void TestRunner::setUseDashboardCompatibilityMode(bool flag)
 229 {
 230     // FIXME: implement
 231 }
 232 
 233 void TestRunner::setUserStyleSheetEnabled(bool flag)
 234 {
 235     // FIXME: implement
 236 }
 237 
 238 void TestRunner::setUserStyleSheetLocation(JSStringRef path)
 239 {
 240     // FIXME: implement
 241 }
 242 
 243 void TestRunner::waitForPolicyDelegate()
 244 {
 245     // FIXME: implement
 246 }
 247 
 248 /*
 249 unsigned TestRunner::workerThreadCount() const
 250 {
 251     JNIEnv* env = DumpRenderTree_GetJavaEnv();
 252 
 253     static jmethodID workerThreadCountMID = env->GetStaticMethodID(getDRTClass(env), "getWorkerThreadCount", "()I");
 254     ASSERT(workerThreadCountMID);
 255     jint count = env->CallStaticIntMethod(getDRTClass(env), workerThreadCountMID);
 256     CheckAndClearException(env);
 257     return count;
 258 }
 259 */
 260 
 261 int TestRunner::windowCount()
 262 {
 263     // FIXME: implement
 264     return 1;
 265 }
 266 
 267 void TestRunner::setWaitToDump(bool waitUntilDone)
 268 {
 269     JNIEnv* env = DumpRenderTree_GetJavaEnv();
 270 
 271     if (!waitUntilDone) {
 272         // FIXME: implement
 273         return;
 274     }
 275 
 276     env->CallStaticVoidMethod(getDumpRenderTreeClass(), getWaitUntillDoneMethodId());
 277     CheckAndClearException(env);
 278 }
 279 
 280 void TestRunner::setWindowIsKey(bool windowIsKey)
 281 {
 282     // FIXME: implement
 283 }
 284 
 285 void TestRunner::setAlwaysAcceptCookies(bool alwaysAcceptCookies)
 286 {
 287     // FIXME: implement
 288 }
 289 
 290 
 291 void TestRunner::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
 292 {
 293     // FIXME: implement
 294 }
 295 
 296 void TestRunner::addUserStyleSheet(JSStringRef source, bool allFrames)
 297 {
 298     // FIXME: implement
 299 }
 300 
 301 /*
 302 void TestRunner::whiteListAccessFromOrigin(JSStringRef sourceOrigin,
 303                                                      JSStringRef destinationProtocol, JSStringRef destinationHost,
 304                                                      bool allowDestinationSubdomains)
 305 {
 306     // FIXME: implement
 307 }
 308 */
 309 
 310 void TestRunner::showWebInspector()
 311 {
 312     // FIXME: implement
 313 }
 314 
 315 void TestRunner::closeWebInspector()
 316 {
 317     // FIXME: implement
 318 }
 319 
 320 
 321 void TestRunner::evaluateInWebInspector(JSStringRef script)
 322 {
 323     // FIXME: implement
 324 }
 325 
 326 JSStringRef TestRunner::inspectorTestStubURL()
 327 {
 328     // FIXME: Implement this to support Web Inspector tests using `protocol-test.js`.
 329     return nullptr;
 330 }
 331 
 332 void TestRunner::evaluateScriptInIsolatedWorld(unsigned worldId, JSObjectRef globalObject, JSStringRef script)
 333 {
 334     // FIXME: implement
 335 }
 336 
 337 void TestRunner::abortModal()
 338 {
 339     //FIXME: implement
 340 }
 341 
 342 void TestRunner::addOriginAccessWhitelistEntry(
 343     JSStringRef sourceOrigin,
 344     JSStringRef destinationProtocol,
 345     JSStringRef destinationHost,
 346     bool allowDestinationSubdomains)
 347 {
 348     //FIXME: implement
 349 }
 350 
 351 void TestRunner::apiTestGoToCurrentBackForwardItem()
 352 {
 353     //FIXME: implement
 354 }
 355 
 356 void TestRunner::apiTestNewWindowDataLoadBaseURL(
 357     JSStringRef utf8Data,
 358     JSStringRef baseURL)
 359 {
 360     //FIXME: implement
 361 }
 362 
 363 // Simulate a request an embedding application could make, populating per-session credential storage.
 364 void TestRunner::authenticateSession(
 365     JSStringRef url,
 366     JSStringRef username,
 367     JSStringRef password)
 368 {
 369     //FIXME: implement
 370 }
 371 
 372 bool TestRunner::callShouldCloseOnWebView()
 373 {
 374     // FIXME: Implement for testing fix for https://bugs.webkit.org/show_bug.cgi?id=27481
 375     return false;
 376 }
 377 
 378 void TestRunner::removeOriginAccessWhitelistEntry(JSStringRef, JSStringRef, JSStringRef,bool) {
 379     //FIXME: implement
 380 }
 381 
 382 void TestRunner::setAllowFileAccessFromFileURLs(bool) {
 383     //FIXME: implement
 384 }
 385 
 386 void TestRunner::setDomainRelaxationForbiddenForURLScheme(bool,JSStringRef) {
 387     //FIXME: implement
 388 }
 389 
 390 void TestRunner::setJavaScriptCanAccessClipboard(bool enable) {
 391     JSStringRef webkitJavaScriptCanAccessClipboard = JSStringCreateWithUTF8CString("WebKitJavaScriptCanAccessClipboardPreferenceKey");
 392     JSStringRef value = JSStringCreateWithUTF8CString(enable ? "1" : "0");
 393     overridePreference(webkitJavaScriptCanAccessClipboard, value);
 394     JSStringRelease(webkitJavaScriptCanAccessClipboard);
 395     JSStringRelease(value);
 396 }
 397 
 398 void TestRunner::setPluginsEnabled(bool) {
 399     //FIXME: implement
 400 }
 401 
 402 void TestRunner::setScrollbarPolicy(JSStringRef,JSStringRef) {
 403     //FIXME: implement
 404 }
 405 
 406 void TestRunner::setSpatialNavigationEnabled(bool) {
 407     //FIXME: implement
 408 }
 409 
 410 void TestRunner::setWebViewEditable(bool) {
 411     //FIXME: implement
 412 }
 413 
 414 bool TestRunner::findString(JSContextRef context, JSStringRef target, JSObjectRef optionsArray)
 415 {
 416     //FIXME: implement
 417     return false;
 418 }
 419 
 420 void TestRunner::setSerializeHTTPLoads(bool)
 421 {
 422     // FIXME: Implement if needed for https://bugs.webkit.org/show_bug.cgi?id=50758.
 423 }
 424 
 425 void TestRunner::clearAllApplicationCaches()
 426 {
 427     // FIXME: implement to support Application Cache quotas.
 428 }
 429 
 430 void TestRunner::setGeolocationPermission(bool allow)
 431 {
 432     // FIXME: Implement for Geolocation layout tests.
 433 }
 434 
 435 void TestRunner::setMockDeviceOrientation(bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
 436 {
 437     // FIXME: Implement for DeviceOrientation layout tests.
 438     // See https://bugs.webkit.org/show_bug.cgi?id=30335.
 439 }
 440 
 441 int TestRunner::numberOfPendingGeolocationPermissionRequests()
 442 {
 443     // FIXME: Implement for Geolocation layout tests.
 444     return -1;
 445 }
 446 
 447 bool TestRunner::isGeolocationProviderActive()
 448 {
 449     // FIXME: Implement for Geolocation layout tests.
 450     return false;
 451 }
 452 
 453 
 454 JSValueRef TestRunner::originsWithApplicationCache(JSContextRef context)
 455 {
 456     // FIXME: Implement to get origins that contain application caches.
 457     return JSValueMakeUndefined(context);
 458 }
 459 
 460 void TestRunner::clearApplicationCacheForOrigin(JSStringRef origin)
 461 {
 462     // FIXME: Implement to support deleting all application cache for an origin.
 463 }
 464 
 465 void TestRunner::setValueForUser(JSContextRef context, JSValueRef element, JSStringRef value)
 466 {
 467     // FIXME: implement
 468 }
 469 
 470 void TestRunner::addChromeInputField()
 471 {
 472 }
 473 
 474 void TestRunner::focusWebView()
 475 {
 476 }
 477 
 478 void TestRunner::goBack()
 479 {
 480 }
 481 
 482 void TestRunner::removeChromeInputField()
 483 {
 484 }
 485 
 486 void TestRunner::setBackingScaleFactor(double)
 487 {
 488 }
 489 
 490 void TestRunner::setDefersLoading(bool)
 491 {
 492 }
 493 
 494 void TestRunner::setTextDirection(OpaqueJSString *)
 495 {
 496 }
 497 
 498 long long TestRunner::applicationCacheDiskUsageForOrigin(JSStringRef)
 499 {
 500     return 0;
 501 }
 502 
 503 void TestRunner::evaluateScriptInIsolatedWorldAndReturnValue(unsigned int,OpaqueJSValue *,OpaqueJSString *)
 504 {
 505 }
 506 
 507 void TestRunner::resetPageVisibility()
 508 {
 509 }
 510 
 511 void TestRunner::setAutomaticLinkDetectionEnabled(bool)
 512 {
 513 }
 514 
 515 void TestRunner::setPageVisibility(char const *)
 516 {
 517 }
 518 
 519 void TestRunner::setStorageDatabaseIdleInterval(double)
 520 {
 521 }
 522 
 523 void TestRunner::setMockGeolocationPositionUnavailableError(JSStringRef message)
 524 {
 525 }
 526 
 527 void TestRunner::simulateLegacyWebNotificationClick(JSStringRef title)
 528 {
 529 }
 530 
 531 void TestRunner::closeIdleLocalStorageDatabases()
 532 {
 533 }
 534 
 535 void TestRunner::grantWebNotificationPermission(JSStringRef origin)
 536 {
 537 }
 538 
 539 void TestRunner::denyWebNotificationPermission(JSStringRef jsOrigin)
 540 {
 541 }
 542 
 543 void TestRunner::removeAllWebNotificationPermissions()
 544 {
 545 }
 546 
 547 void TestRunner::simulateWebNotificationClick(JSValueRef jsNotification)
 548 {
 549 }
 550 
 551 JSContextRef TestRunner::mainFrameJSContext()
 552 {
 553     return gContext;
 554 }
 555 
 556 void TestRunner::setViewSize(double width, double height)
 557 {
 558     fprintf(testResult, "ERROR: TestRunner::setViewSize() not implemented\n");
 559 }
 560 
 561 void TestRunner::setSpellCheckerLoggingEnabled(bool enabled)
 562 {
 563     fprintf(testResult, "ERROR: TestRunner::setSpellCheckerLoggingEnabled() not implemented\n");
 564 }
 565 
 566 void TestRunner::setNeedsStorageAccessFromFileURLsQuirk(bool needsQuirk)
 567 {
 568     fprintf(testResult, "ERROR: TestRunner::setNeedsStorageAccessFromFileURLsQuirk() not implemented\n");
 569 }
 570 
 571 unsigned TestRunner::imageCountInGeneralPasteboard() const
 572 {
 573     fprintf(testResult, "ERROR: TestRunner::imageCountInGeneralPasteboard() not implemented\n");
 574     return 0;
 575 }
 576 
 577 void TestRunner::forceImmediateCompletion()
 578 {
 579     fprintf(testResult, "ERROR: TestRunner::forceImmediateCompletion() not implemented\n");
 580 }