dom/camera/test/test_camera_3.html
author Jim Mathies <jmathies@mozilla.com>
Wed, 12 Nov 2014 14:59:19 -0600
changeset 239674 dcc233b91a99e7d8cfaa39b508d2eb2c2496fab6
parent 231884 05df8323b82079743e8af61b83625739bbadf905
child 246063 ce9707a48002c8fd80f1c921b13e8f6e40d54f43
permissions -rw-r--r--
Bug 669200 - Various widget changes to support two new types of plugin widget. r=roc

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for multiple calls to mozCameras.getCamera()</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<video id="viewfinder" width="200" height="200" autoplay></video>
<img src="#" alt="This image is going to load" id="testimage"/>
<script class="testbody" type="text/javascript;version=1.7">

var whichCamera = navigator.mozCameras.getListOfCameras()[0];
var options = {
  mode: 'picture',
  recorderProfile: 'cif',
  previewSize: {
    width: 352,
    height: 288
  }
};

function onError(e) {
  ok(false, "Error " + e);
}

var Camera = {
  cameraObj: null,
  get viewfinder() {
    return document.getElementById('viewfinder');
  },
  onReady: function take_two() {
    function onSuccess(d) {
      ok(false, "Unexpectedly got second camera instance: " + d.config.toSource);
    }
    function onFailure(error) {
      ok(true, "Correctly failed to get camera again");
      SimpleTest.finish();
    }
    navigator.mozCameras.getCamera(whichCamera, options).then(onSuccess, onFailure);
  },
  onPreviewStateChange: function onPreviewStateChange(e) {
    if (e.newState === 'started') {
      ok(true, "viewfinder is ready and playing");
      Camera.cameraObj.removeEventListener('previewstatechange', Camera.onPreviewStateChange);
      Camera.onReady();
    }
  },
  release: function release() {
    cameraObj = null;
  },
  start: function run_test() {
    function onSuccess(d) {
      Camera.cameraObj = d.camera;
      Camera.cameraObj.addEventListener('previewstatechange', Camera.onPreviewStateChange);
      Camera.viewfinder.mozSrcObject = d.camera;
      Camera.viewfinder.play();
    };
    navigator.mozCameras.getCamera(whichCamera, options).then(onSuccess, onError);
  }
}

SimpleTest.waitForExplicitFinish();

window.addEventListener('beforeunload', function() {
  Camera.viewfinder.mozSrcObject = null;
  Camera.cameraObj.release();
  Camera.cameraObj = null;
});

Camera.start();

</script>
</body>

</html>