summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/robocop/testVideoControls.js
blob: e0a41b5b69bbc7821008fa523db9bca59201ebd2 (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
156
157
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/SimpleServiceDiscovery.jsm");

// The chrome window
var chromeWin;

// Track the <browser> where the tests are happening
var browser;

// The document of the video_controls web content
var contentDocument;

// The <video> we will be testing
var video;

add_test(function setup_browser() {
  chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
  let BrowserApp = chromeWin.BrowserApp;

  do_register_cleanup(function cleanup() {
    BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
  });

  // Load our test web page with <video> elements
  let url = "http://mochi.test:8888/tests/robocop/video_controls.html";
  browser = BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }).browser;
  browser.addEventListener("load", function startTests(event) {
    browser.removeEventListener("load", startTests, true);
    contentDocument = browser.contentDocument;

    video = contentDocument.getElementById("video");
    ok(video, "Found the video element");

    Services.tm.mainThread.dispatch(run_next_test, Ci.nsIThread.DISPATCH_NORMAL);
  }, true);
});

add_test(function test_webm() {
  // Load the test video
  video.src = "http://mochi.test:8888/tests/robocop/video-pattern.webm";

  Services.tm.mainThread.dispatch(testLoad, Ci.nsIThread.DISPATCH_NORMAL);
});

add_test(function test_ogg() {
  // Load the test video
  video.src = "http://mochi.test:8888/tests/robocop/video-pattern.ogg";

  Services.tm.mainThread.dispatch(testLoad, Ci.nsIThread.DISPATCH_NORMAL);
});

function getButtonByAttribute(aName, aValue) {
  let domUtil = Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
  let kids = domUtil.getChildrenForNode(video, true);
  let videocontrols = kids[1];
  return contentDocument.getAnonymousElementByAttribute(videocontrols, aName, aValue);
}

function getPixelColor(aCanvas, aX, aY) {
  let cx = aCanvas.getContext("2d");
  let pixel = cx.getImageData(aX, aY, 1, 1);
  return {
    r: pixel.data[0],
    g: pixel.data[1],
    b: pixel.data[2],
    a: pixel.data[3]
  };
}

function testLoad() {
  // The video is not auto-play, so it starts paused
  let playButton = getButtonByAttribute("class", "playButton");
  ok(playButton.getAttribute("paused") == "true", "Play button is paused");

  // Let's start playing it
  video.play();
  video.addEventListener("play", testPlay, false);
}

function testPlay(aEvent) {
  video.removeEventListener("play", testPlay, false);
  let playButton = getButtonByAttribute("class", "playButton");
  ok(playButton.hasAttribute("paused") == false, "Play button is not paused");

  // Let the video play for 2 seconds, then pause it
  chromeWin.setTimeout(function() {
    video.pause();
    video.addEventListener("pause", testPause, false);
  }, 2000);
}

function testPause(aEvent) {
  video.removeEventListener("pause", testPause, false);

  // If we got here, the play button should be paused
  let playButton = getButtonByAttribute("class", "playButton");
  ok(playButton.getAttribute("paused") == "true", "Play button is paused again");

  // Let's grab an image of the frame and test it
  let width = 640;
  let height = 480;
  let canvas = contentDocument.getElementById("canvas");
  canvas.width = width;
  canvas.height = height;
  canvas.getContext("2d").drawImage(video, 0, 0, width, height);

  // Let's grab some pixel colors to verify we actually displayed a video.
  // For some reason the canvas copy of the frame does not recreate the colors
  // exactly for some devices. To keep things passing on automation and local
  // runs, we fudge it.

  // The purpose of this code is not to test drawImage, but whether a video
  // frame was displayed.
  const MAX_COLOR = 235; // ideally, 255
  const MIN_COLOR = 20; // ideally, 0

  let bar1 = getPixelColor(canvas, 45, 10);
  do_print("Color at (45, 10): " + JSON.stringify(bar1));
  ok(bar1.r >= MAX_COLOR && bar1.g >= MAX_COLOR && bar1.b >= MAX_COLOR, "Bar 1 is white");

  let bar2 = getPixelColor(canvas, 135, 10);
  do_print("Color at (135, 10): " + JSON.stringify(bar2));
  ok(bar2.r >= MAX_COLOR && bar2.g >= MAX_COLOR && bar2.b <= MIN_COLOR, "Bar 2 is yellow");

  let bar3 = getPixelColor(canvas, 225, 10);
  do_print("Color at (225, 10): " + JSON.stringify(bar3));
  ok(bar3.r <= MIN_COLOR && bar3.g >= MAX_COLOR && bar3.b >= MAX_COLOR, "Bar 3 is Cyan");

  let bar4 = getPixelColor(canvas, 315, 10);
  do_print("Color at (315, 10): " + JSON.stringify(bar4));
  ok(bar4.r <= MIN_COLOR && bar4.g >= MAX_COLOR && bar4.b <= MIN_COLOR, "Bar 4 is Green");

  let bar5 = getPixelColor(canvas, 405, 10);
  do_print("Color at (405, 10): " + JSON.stringify(bar5));
  ok(bar5.r >= MAX_COLOR && bar5.g <= MIN_COLOR && bar5.b >= MAX_COLOR, "Bar 5 is Purple");

  let bar6 = getPixelColor(canvas, 495, 10);
  do_print("Color at (495, 10): " + JSON.stringify(bar6));
  ok(bar6.r >= MAX_COLOR && bar6.g <= MIN_COLOR && bar6.b <= MIN_COLOR, "Bar 6 is Red");

  let bar7 = getPixelColor(canvas, 585, 10);
  do_print("Color at (585, 10): " + JSON.stringify(bar7));
  ok(bar7.r <= MIN_COLOR && bar7.g <= MIN_COLOR && bar7.b >= MAX_COLOR, "Bar 7 is Blue");

  run_next_test();
}

run_next_test();