summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/browser_test/browser_test.js
blob: 85985aa99fb88f174a6f2d56463cb924cd679a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 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.

/**
 * @fileoverview
 * @suppress {checkTypes}  By default, JSCompile is not run on test files.
 *    However, you can modify |remoting_webapp_files.gypi| locally to include
 *    the test in the package to expedite local development.  This suppress
 *    is here so that JSCompile won't complain.
 *
 * Provides basic functionality for JavaScript based browser test.
 *
 * To define a browser test, create a class under the browserTest namespace.
 * You can pass arbitrary object literals to the browser test from the C++ test
 * harness as the test data.  Each browser test class should implement the run
 * method.
 * For example:
 *
 * browserTest.My_Test = function(myObjectLiteral) {};
 * browserTest.My_Test.prototype.run() = function() { ... };
 *
 * The browser test is async in nature.  It will keep running until
 * browserTest.fail("My error message.") or browserTest.pass() is called.
 *
 * For example:
 *
 * browserTest.My_Test.prototype.run() = function() {
 *   window.setTimeout(function() {
 *     if (doSomething()) {
 *       browserTest.pass();
 *     } else {
 *       browserTest.fail('My error message.');
 *     }
 *   }, 1000);
 * };
 *
 * You will then invoke the test in C++ by calling:
 *
 *   RunJavaScriptTest(web_content, "My_Test", "{"
 *    "pin: 123123"
 *  "}");
 */

'use strict';

var browserTest = {};

browserTest.init = function() {
  // The domAutomationController is used to communicate progress back to the
  // C++ calling code.  It will only exist if chrome is run with the flag
  // --dom-automation.  It is stubbed out here so that browser test can be run
  // under the regular app.
  browserTest.automationController_ = window.domAutomationController || {
    send: function(json) {
      var result = JSON.parse(json);
      if (result.succeeded) {
        console.log('Test Passed.');
      } else {
        console.error(result.error_message);
      }
    }
  };
};

browserTest.assert = function(expr, message) {
  if (!expr) {
    message = (message) ? '<' + message + '>' : '';
    browserTest.fail('Assertion failed.' + message);
  }
};

browserTest.fail = function(error_message, opt_stack_trace) {
  var stack_trace = opt_stack_trace || base.debug.callstack();

  // To run browserTest locally:
  // 1. Go to |remoting_webapp_files| and look for
  //    |remoting_webapp_js_browser_test_files| and uncomment it
  // 2. gclient runhooks
  // 3. rebuild the webapp
  // 4. Run it in the console browserTest.runTest(browserTest.MyTest, {});
  // 5. The line below will trap the test in the debugger in case of
  //    failure.
  debugger;

  browserTest.automationController_.send(JSON.stringify({
    succeeded: false,
    error_message: error_message,
    stack_trace: stack_trace
  }));
};

browserTest.pass = function() {
  browserTest.automationController_.send(JSON.stringify({
    succeeded: true,
    error_message: '',
    stack_trace: ''
  }));
};

browserTest.clickOnControl = function(id) {
  var element = document.getElementById(id);
  browserTest.assert(element);
  element.click();
};

/** @enum {number} */
browserTest.Timeout = {
  NONE: -1,
  DEFAULT: 5000
};

browserTest.waitForUIMode = function(expectedMode, callback, opt_timeout) {
  var uiModeChanged = remoting.testEvents.Names.uiModeChanged;
  var timerId = null;

  if (opt_timeout === undefined) {
    opt_timeout = browserTest.Timeout.DEFAULT;
  }

  function onTimeout() {
    remoting.testEvents.removeEventListener(uiModeChanged, onUIModeChanged);
    browserTest.fail('Timeout waiting for ' + expectedMode);
  }

  function onUIModeChanged (mode) {
    if (mode == expectedMode) {
      remoting.testEvents.removeEventListener(uiModeChanged, onUIModeChanged);
      window.clearTimeout(timerId);
      timerId = null;
      try {
        callback();
      } catch (e) {
        browserTest.fail(e.toString(), e.stack);
      }
    }
  }

  if (opt_timeout != browserTest.Timeout.NONE) {
    timerId = window.setTimeout(onTimeout.bind(window, timerId), opt_timeout);
  }
  remoting.testEvents.addEventListener(uiModeChanged, onUIModeChanged);
};

browserTest.runTest = function(testClass, data) {
  try {
    var test = new testClass(data);
    browserTest.assert(typeof test.run == 'function');
    test.run();
  } catch (e) {
    browserTest.fail(e.toString(), e.stack);
  }
};

browserTest.init();