Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Unified Diff: tools/telemetry/telemetry/page/actions/play.js

Issue 16854013: Telemetry media_measurement plus play action and tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove media files (submitted separately) Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/perf/perf_tools/media_metrics.py ('k') | tools/telemetry/telemetry/page/actions/play.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/telemetry/telemetry/page/actions/play.js
diff --git a/tools/telemetry/telemetry/page/actions/play.js b/tools/telemetry/telemetry/page/actions/play.js
new file mode 100644
index 0000000000000000000000000000000000000000..3ff8f8f6a1bf964fce6fdf1b902291fa231d7232
--- /dev/null
+++ b/tools/telemetry/telemetry/page/actions/play.js
@@ -0,0 +1,76 @@
+// Copyright (c) 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.
+
+// This file performs actions on media elements.
+(function() {
+ function findMediaElements(selector) {
+ // Returns elements matching the selector, otherwise returns the first video
+ // or audio tag element that can be found.
+ // If selector == 'all', returns all media elements.
+ if (selector == 'all') {
+ return document.querySelectorAll('video, audio');
+ } else if (selector) {
+ return document.querySelectorAll(selector);
+ } else {
+ var media = document.getElementsByTagName('video');
+ if (media.length > 0) {
+ return [media[0]];
+ } else {
+ media = document.getElementsByTagName('audio');
+ if (media.length > 0) {
+ return [media[0]];
+ }
+ }
+ }
+ console.error('Could not find any media elements matching: ' + selector);
+ return [];
+ }
+
+ function playMedia(selector) {
+ // Performs the "Play" action on media satisfying selector.
+ var mediaElements = findMediaElements(selector);
+ for (var i = 0; i < mediaElements.length; i++) {
+ console.log('Playing element: ' + mediaElements[i].src);
+ play(mediaElements[i]);
+ }
+ }
+
+ function play(element) {
+ if (element instanceof HTMLMediaElement)
+ playHTML5Element(element);
+ else
+ console.error('Can not play non HTML5 media elements.');
+ }
+
+ function playHTML5Element(element) {
+ function logEventHappened(e) {
+ element[e.type + '_completed'] = true;
+ }
+ function onError(e) {
+ console.error('Error playing media :' + e.type);
+ }
+ element.addEventListener('playing', logEventHappened);
+ element.addEventListener('ended', logEventHappened);
+ element.addEventListener('error', onError);
+ element.addEventListener('abort', onError);
+
+ var willPlayEvent = document.createEvent('Event');
+ willPlayEvent.initEvent('willPlay', false, false);
+ element.dispatchEvent(willPlayEvent);
+ element.play();
+ }
+
+ function hasEventCompleted(selector, event_name) {
+ // Return true if the event_name fired for media satisfying the selector.
+ var mediaElements = findMediaElements(selector);
+ for (var i = 0; i < mediaElements.length; i++) {
+ if (!mediaElements[i][event_name + '_completed'])
+ return false;
+ }
+ return true;
+ }
+
+ window.__playMedia = playMedia;
+ window.__hasEventCompleted = hasEventCompleted;
+})();
« no previous file with comments | « tools/perf/perf_tools/media_metrics.py ('k') | tools/telemetry/telemetry/page/actions/play.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698