Search code examples
javascriptgoogle-chromev8

Google unit tests seems like custom syntax


Looking at googles unit tests for blink. I noticed in most of them there is the following bit of code

accessibilityController.accessibleElementById('some-id').decrement();

Now in trying to figure out what this is. It appears to me that this is a separate thing that looks like it is added to the compiler(v8) in order to make these unit tests go smothly...

The file seam to be $HOME/chromium/src/content/shell/test_runner/accessibility_controller.cc

So if this is the case my question is as follows: since I am tring to bring these tests into my aplication and I most certinely do not want to use some comile add on to v8. How do I write the line

accessibilityController.accessibleElementById('some-id').decrement();

in pure v8 compilable javascript. And also what exactly is this "accessibilityController" doing because my c++ not being par I could not discover it.


Solution

  • Well, it's not exactly custom syntax, it's just an embedder-provided object that JavaScript code can interact with.

    Every embedder of V8 provides their own objects and/or functions. For example, Node.js provides process, browsers/Chromium provide document, the "d8" shell has a quit() function. What you have found is that the "content_shell", which is a simple Blink+V8 embedder for the purpose of running tests, provides a few things of its own.

    In your own embedder, you can either implement that too, or you have to edit scripts you want to execute so that they don't use the functionality that's not available in your embedder. A simpler workaround can be to load a simple polyfill to mock out such calls, in your case e.g.:

    var accessibilityController = {
      accessibleElementById: function() { 
        return {
          decrement: function(){}
        }
      }
    }
    

    before executing the imported tests.

    There is no way to write the line in question in "pure JavaScript", just like there is no way to express document.createElement(...) in pure JavaScript -- you always need a browser to provide a document object. (You can create an entire fake DOM implementation, of course, but that still won't draw to the screen, so it's not the same thing.) On the bright side, if you don't know what an "accessibilityController" might do, then you probably don't need it.

    (Existing example: d8 used to have a custom print() function instead of console.log, but we found it useful to be able to run scripts that use console.log without having to manually modify them, so we eventually added console.log to d8.)