# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from telemetry import test
from telemetry.page import page_set
from webgl_conformance import WebglConformanceValidator
from webgl_conformance import conformance_harness_script
from webgl_conformance import conformance_path


robustness_harness_script = conformance_harness_script + r"""
  var robustnessTestHarness = {};
  robustnessTestHarness._contextLost = false;
  robustnessTestHarness.initialize = function() {
    var canvas = document.getElementById('example');
    canvas.addEventListener('webglcontextlost', function() {
      robustnessTestHarness._contextLost = true;
    });
  }
  robustnessTestHarness.runTestLoop = function() {
    // Run the test in a loop until the context is lost.
    main();
    if (!robustnessTestHarness._contextLost)
      window.requestAnimationFrame(robustnessTestHarness.runTestLoop);
    else
      robustnessTestHarness.notifyFinished();
  }
  robustnessTestHarness.notifyFinished = function() {
    // The test may fail in unpredictable ways depending on when the context is
    // lost. We ignore such errors and only require that the browser doesn't
    // crash.
    webglTestHarness._allTestSucceeded = true;
    // Notify test completion after a delay to make sure the browser is able to
    // recover from the lost context.
    setTimeout(webglTestHarness.notifyFinished, 3000);
  }

  window.confirm = function() {
    robustnessTestHarness.initialize();
    robustnessTestHarness.runTestLoop();
    return false;
  }
  window.webglRobustnessTestHarness = robustnessTestHarness;
"""


class WebglRobustness(test.Test):
  enabled = False
  test = WebglConformanceValidator

  def CreatePageSet(self, options):
    page_set_dict = {
      'description': 'Test cases for WebGL robustness',
      'user_agent_type': 'desktop',
      'serving_dirs': [''],
      'pages': [
        {
          'url': 'file:///extra/lots-of-polys-example.html',
          'script_to_evaluate_on_commit': robustness_harness_script,
          'wait_for_javascript_expression': 'webglTestHarness._finished'
        }
      ]
    }
    return page_set.PageSet.FromDict(page_set_dict, conformance_path)

Add WebGL robustness telemetry test
gpu: Add WebGL robustness telemetry test

This patch adds a telemetry-based WebGL robustness test. It executes an
optional test from the WebGL conformance test suite to trigger a GPU
context loss and verifies this doesn't crash the browser.

To run the test for example on Android, execute:

$ content/test/gpu/run_gpu_test --browser=android-chrome webgl_robustness

BUG=232449

Review URL: https://chromiumcodereview.appspot.com/21150015

git-svn-id: de016e52bd170d2d4f2344f9bf92d50478b649e0@215929 0039d316-1c4b-4281-b951-d872f2087c98
