couple of test cases for getUserMedia
authorDominique Hazael-Massieux <dom@w3.org>
Thu, 31 May 2012 11:21:15 +0200
changeset 0 711a8a5cb607
child 1 af06970a873e
couple of test cases for getUserMedia
submitted/W3C/api-present.html
submitted/W3C/featuredetection.js
submitted/W3C/prefix.js
submitted/W3C/video.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/submitted/W3C/api-present.html	Thu May 31 11:21:15 2012 +0200
@@ -0,0 +1,24 @@
+<!doctype html>
+<html>
+<head>
+<title>getUserMedia: test that getUserMedia is present (with or without vendor prefix)</title>
+<link rel="author" title="Dominique Hazael-Massieux" href="mailto:dom@w3.org"/>
+<link rel="help" href="http://dev.w3.org/2011/webrtc/editor/getusermedia.html#navigatorusermedia">
+<meta name='assert' content='Check that the getUserMedia() method is present.'/>
+<meta name='flags' content='vendor-prefix, dom'/>
+<link rel='stylesheet' href='/resources/testharness.css' media='all'/>
+</head>
+<body>
+<h1>Description</h1>
+<p>This test checks for the presence of the <code>navigator.getUserMedia</code> method, taking vendor prefixes into account.</p>
+    <div id='log'></div>
+<script src=/resources/testharness.js></script>
+<script src=/resources/testharnessreport.js></script>
+<script src=featuredetection.js></script>
+<script>
+test(function () {
+          assert_true(undefined !== BrowserHasFeature(navigator, "getUserMedia"), "navigator.getUserMedia exists");
+      }, "getUserMedia() is present on navigator");
+</script>
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/submitted/W3C/featuredetection.js	Thu May 31 11:21:15 2012 +0200
@@ -0,0 +1,93 @@
+// 2012-01-27
+//  The code below has been stolen from Microsoft's submission tests to the Page Visibility
+//  specification. Thanks!
+
+
+// This function returns the value of an API feature if it is defined with one of
+// the known ventor prefixes. 
+//
+// Parameters:
+//  parent: the object containing the API feature
+//  feature: the name of the API feature, this should be a string value
+//  isAttribute: set this to true to indicate that this is a constant attribute
+//               as opposed to a variable
+function BrowserHasFeature(parent, feature, isAttribute)
+{
+    if (parent[feature] !== undefined)
+    {
+        //feature is defined without a vendor prefix, no further checks necessary
+        return parent[feature];
+    }
+
+    // the feature is not defined without a vendor prefix, so find the vendor prefix, if any,
+    // that it is defined with
+    var prefix = GetVendorPrefix(parent, feature, isAttribute);
+    
+    // if prefix is not undefined, then the feature has been found to exist with this prefix
+    if (prefix !== undefined)
+    {
+        var prefixedFeatureName = AppendPrefix(prefix, feature, isAttribute);
+        return parent[prefixedFeatureName];
+    }
+
+    //The feature does not exist.
+    //Callers should check for !==undefined as the feature itself could return
+    //a Bool which would fail a typical if(feature) check
+    return undefined;
+}
+
+// This function returns the vendor prefix found if a certain feature is defined with it.
+// It takes the same parameters at BrowserHasFeature().
+function GetVendorPrefix(parent, feature, isAttribute)
+{
+    //Known vendor prefixes
+    var VendorPrefixes = ["moz", "ms", "o", "webkit"];
+    for (vendor in VendorPrefixes)
+    {
+        //Build up the new feature name with the vendor prefix
+        var prefixedFeatureName = AppendPrefix(VendorPrefixes[vendor], feature, isAttribute);
+        if (parent[prefixedFeatureName] !== undefined)
+        {
+            //Vendor prefix version exists, return a pointer to the feature
+            return VendorPrefixes[vendor];
+        }
+    }
+    
+    // if no version of the feature with a vendor prefix has been found, return undefined
+    return undefined;
+}
+
+// This will properly capitalize the feature name and then return the feature name preceded
+// with the provided vendor prefix. If the prefix given is undefined, this function will
+// return the feature name given as is. The output of this function should not be used
+// as an indicator of whether or not a feature exists as it will return the same thing if
+// the inputted feature is undefined or is defined without a vendor prefix. It takes the 
+// same parameters at BrowserHasFeature().
+function AppendPrefix(prefix, feature, isAttribute)
+{
+    if (prefix !== undefined)
+    {
+        if (isAttribute)
+        {
+            // if a certain feature is an attribute, then it follows a different naming standard
+            // where it must be completely capitalized and have words split with an underscore
+            return prefix.toUpperCase() + "_" + feature.toUpperCase();
+        }
+        else
+        {
+            //Vendor prefixing should follow the standard convention: vendorprefixFeature
+            //Note that the feature is capitalized after the vendor prefix
+            //Therefore if the feature is window.feature, the vendor prefix version is:
+            //window.[vp]Feature where vp is the vendor prefix: 
+            //window.msFeature, window.webkitFeature, window.mozFeature, window.oFeature
+            var newFeature = feature[0].toUpperCase() + feature.substr(1, feature.length);
+
+            //Build up the new feature name with the vendor prefix
+            return prefix + newFeature;
+        }
+    }
+    else
+    {
+        return feature;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/submitted/W3C/prefix.js	Thu May 31 11:21:15 2012 +0200
@@ -0,0 +1,4 @@
+var api = BrowserHasFeature(navigator, "getUserMedia");
+if (!window.navigator.getUserMedia && undefined !== api) {
+   window.navigator.getUserMedia = api;
+} 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/submitted/W3C/video.html	Thu May 31 11:21:15 2012 +0200
@@ -0,0 +1,33 @@
+<!doctype html>
+<html>
+<head>
+<title></title>
+<link rel="author" title="Dominique Hazael-Massieux" href="mailto:dom@w3.org"/>
+<link rel="help" href="http://dev.w3.org/2011/webrtc/editor/getusermedia.html#navigatorusermedia">
+<link rel='stylesheet' href='/resources/testharness.css' media='all'/>
+</head>
+<body>
+<p>When prompted, accept to share your video stream.</p>
+<h1>Description</h1>
+<p>This test checks that the LocalMediaStream object returned by the success callback in getUserMedia has a video track (and only that).</p>
+
+<div id='log'></div>
+<script src=/resources/testharness.js></script>
+<script src=/resources/testharnessreport.js></script>
+<script src=featuredetection.js></script>
+<script src=prefix.js></script>
+<script>
+var t = async_test("Tests that a MediaStream with a single video track is returned");
+if (window.navigator.getUserMedia) { 
+  navigator.getUserMedia({video:true}, function (stream) {
+  t.step(function () { 
+     assert_true(stream instanceof LocalMediaStream, "getUserMedia success callback comes with a LocalMediaStream object");
+     assert_equals(stream.audioTracks.length, 0, "the media stream has zero audio track");
+     assert_equals(stream.videoTracks.length, 1, "the media stream has one video track");
+  });
+  t.done();
+});
+}
+</script>
+</body>
+</html>