6a3c87d10ca7b5c0c72935edd613c9256cd0d98d
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / media / media-controls.js
1 var captionsButtonElement;
2 var captionsButtonCoordinates;
3
4 // These reflect the values used to fade in/out the media controls. Should
5 // mirror the values 'fadeInDuration'/'fadeOutDuration' in MediaControlElements.cpp.
6 const controlsFadeInDurationMs = 100;
7 const controlsFadeOutDurationMs = 300;
8
9 // The timeout for the hide-after-no-mouse-movement behavior. Defined (and
10 // should mirror) the value 'timeWithoutMouseMovementBeforeHidingMediaControls'
11 // in MediaControls.cpp.
12 const controlsMouseMovementTimeoutMs = 3000;
13
14 function mediaControlsElement(first, id)
15 {
16     for (var element = first; element; element = element.nextSibling) {
17         // Not every element in the media controls has a shadow pseudo ID, eg. the
18         // text nodes for the time values, so guard against exceptions.
19         try {
20             if (internals.shadowPseudoId(element) == id)
21                 return element;
22         } catch (exception) { }
23
24         if (element.firstChild) {
25             var childElement = mediaControlsElement(element.firstChild, id);
26             if (childElement)
27                 return childElement;
28         }
29     }
30
31     return null;
32 }
33
34 function mediaControlsButton(element, id)
35 {
36     var controlID = "-webkit-media-controls-" + id;
37     var button = mediaControlsElement(internals.shadowRoot(element).firstChild, controlID);
38     if (!button)
39         throw "Failed to find media control element ID '" + id + "'";
40     return button;
41 }
42
43 function mediaControlsButtonCoordinates(element, id)
44 {
45     var button = mediaControlsButton(element, id);
46     var buttonBoundingRect = button.getBoundingClientRect();
47     var x = buttonBoundingRect.left + buttonBoundingRect.width / 2;
48     var y = buttonBoundingRect.top + buttonBoundingRect.height / 2;
49     return new Array(x, y);
50 }
51
52 function mediaControlsButtonDimensions(element, id)
53 {
54     var button = mediaControlsButton(element, id);
55     var buttonBoundingRect = button.getBoundingClientRect();
56     return new Array(buttonBoundingRect.width, buttonBoundingRect.height);
57 }
58
59 function textTrackDisplayElement(parentElement, id, cueNumber)
60 {
61     var textTrackContainerID = "-webkit-media-text-track-container";
62     var containerElement = mediaControlsElement(internals.shadowRoot(parentElement).firstChild, textTrackContainerID);
63
64     if (!containerElement)
65         throw "Failed to find text track container element";
66
67     if (!id)
68         return containerElement;
69
70     if (arguments[1] != 'cue')
71         var controlID = "-webkit-media-text-track-" + arguments[1];
72     else
73         var controlID = arguments[1];
74
75     var displayElement = mediaControlsElement(containerElement.firstChild, controlID);
76     if (!displayElement)
77         throw "No text track cue with display id '" + controlID + "' is currently visible";
78
79     if (cueNumber) {
80         for (i = 0; i < cueNumber; i++)
81             displayElement = displayElement.nextSibling;
82
83         if (!displayElement)
84             throw "There are not " + cueNumber + " text track cues visible";
85     }
86
87     return displayElement;
88 }
89
90 function testClosedCaptionsButtonVisibility(expected)
91 {
92     try {
93         captionsButtonElement = mediaControlsElement(internals.shadowRoot(mediaElement).firstChild, "-webkit-media-controls-toggle-closed-captions-button");
94         captionsButtonCoordinates = mediaControlsButtonCoordinates(mediaElement, "toggle-closed-captions-button");
95     } catch (exception) {
96         consoleWrite("Failed to find a closed captions button or its coordinates: " + exception);
97         if (expected)
98             failTest();
99         return;
100     }
101
102     consoleWrite("");
103     if (expected == true) {
104         consoleWrite("** Caption button should be visible and enabled.");
105         testExpected("captionsButtonCoordinates[0]", 0, ">");
106         testExpected("captionsButtonCoordinates[1]", 0, ">");
107         testExpected("captionsButtonElement.disabled", false);
108     } else {
109         consoleWrite("** Caption button should not be visible.");
110         testExpected("captionsButtonCoordinates[0]", 0, "<=");
111         testExpected("captionsButtonCoordinates[1]", 0, "<=");
112     }
113 }
114
115 function clickCCButton()
116 {
117     consoleWrite("*** Click the CC button.");
118     eventSender.mouseMoveTo(captionsButtonCoordinates[0], captionsButtonCoordinates[1]);
119     eventSender.mouseDown();
120     eventSender.mouseUp();
121 }
122
123 function runAfterControlsHidden(func, mediaElement)
124 {
125     if (mediaElement.paused)
126         throw "The media element is not playing";
127
128     // Compute the time it'll take until the controls will be invisible -
129     // assuming playback has been started prior to invoking this
130     // function. Allow 500ms slack.
131     var hideTimeoutMs = controlsMouseMovementTimeoutMs + controlsFadeOutDurationMs + 500;
132
133     if (!mediaElement.loop && hideTimeoutMs >= 1000 * (mediaElement.duration - mediaElement.currentTime))
134         throw "The media will end before the controls have been hidden";
135
136     setTimeout(func, hideTimeoutMs);
137 }