= Summary = It's now possible to write layout tests that exercise code in the UI process in iOS WebKit2. This allows testing of things like tapping, zooming, keyboard interaction, selection, text input etc. This is done by running JavaScript in an isolated JS context in the UI process form a test. That JavaScript has access to a UIScriptController, which has properties that expose information about the state of the UI process,. and functions to make things happen, including event synthesis. The UI script itself runs asynchronously, and many of the UIScriptController functions are asynchronous. Callbacks are used to tell the test when something completes. = A simple example = {{{ }}} = Some more detail = Let's say we want to write a test that reads back the zoom scale of the WKWebView's UIScrollView (e.g. to test viewport scale). First, since we're testing viewport zooming, we need to have the test opt into iOS-style viewport behavior with an HTML comment after the doctype: {{{ }}} then let's put in the viewport settings to test: {{{ ... }}} Now we want to write some script to read the zoom scale in the UI process. [https://trac.webkit.org/browser/trunk/Tools/WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl TestRunner] now has a runUIScript() function, which takes a string and a callback: {{{ interface TestRunner { ... // UI Process Testing void runUIScript(DOMString script, object callback); }}} so we can use this in our test in some script that would run from the onload handler: {{{ }}} The script passed in to runUIScript() is just a string. You can pass a string literal here, or, for longer scripts make script tag and get its content as text: {{{ }}} So, what script can you run in the UI process? This UI-side script runs in a separate JS context that has no access to the DOM, no timers, no requestAnimationFrame, no XHR etc. What it can do is run vanilla JavaScript, and access properties and functions on [https://trac.webkit.org/browser/trunk/Tools/WebKitTestRunner/UIScriptContext/Bindings/UIScriptController.idl UIScriptController], which is exposed as uiController. Here's a simple example, using function-like syntax for the inline script, that returns zoom scale: {{{ }}} Because testRunner.runUIScript() is asynchronous, the caller has to supply a callback function. The string result returned by the UI process script is passed to this callback function as an argument. You can use this to return simple bits of data as strings, or use JSON.stringify() in the UI script, and JSON.parse() in the test script to get more complex objects across the process boundary. = UIScriptController functions = UIScriptController is the mechanism for driving behavior in the UI process, and to accessing UI state. Many of the functions will trigger UI behaviors that take time (e.g. a zoom animation, or bringing up the keyboard). Rather than blocking, these functions take a callback that is triggered in the UI script context when that operation is complete: {{{ (function() { uiController.doubleTapAtPoint(50, 50, function() { console.log("Taps were dispatched"); }); })(); }}} Note that this console.log() won't show in layout test output, since it's being generated in the UI process (but it will be logged if you run webkittestrunner directly, which is useful for debugging). However, the web process needs to be told that the UI script is complete, so when your test has finished doing work in the UI process, it should call uiScriptComplete(), passing the result: {{{ (function() { uiController.doubleTapAtPoint(50, 50, function() { uiController.uiScriptComplete("Taps were dispatched"); }); })(); }}}