dom/camera/test/camera_common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/camera/test/camera_common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,143 @@
     1.4 +var CameraTest = (function() {
     1.5 +  'use strict';
     1.6 +
     1.7 +  /**
     1.8 +   * 'camera.control.test.enabled' is queried in Gecko to enable different
     1.9 +   * test modes in the camera stack. The only currently-supported setting
    1.10 +   * is 'hardware', which wraps the Gonk camera abstraction class in a
    1.11 +   * shim class that supports injecting hardware camera API failures into
    1.12 +   * the execution path.
    1.13 +   *
    1.14 +   * The affected API is specified by the 'camera.control.test.hardware'
    1.15 +   * pref. Currently supported values should be determined by inspecting
    1.16 +   * TestGonkCameraHardware.cpp.
    1.17 +   *
    1.18 +   * Some API calls are simple: e.g. 'start-recording-failure' will cause
    1.19 +   * the DOM-facing startRecording() call to fail. More complex tests like
    1.20 +   * 'take-picture-failure' will cause the takePicture() API to fail, while
    1.21 +   * 'take-picture-process-failure' will simulate a failure of the
    1.22 +   * asynchronous picture-taking process, even if the initial API call
    1.23 +   * path seems to have succeeded.
    1.24 +   *
    1.25 +   * If 'camera.control.test.hardware.gonk.parameters' is set, it will cause
    1.26 +   * the contents of that string to be appended to the string of parameters
    1.27 +   * pulled from the Gonk camera library. This allows tests to inject fake
    1.28 +   * settings/capabilities for features not supported by the emulator. These
    1.29 +   * parameters are one or more semicolon-delimited key=value pairs, e.g. to
    1.30 +   * pretend the emulator supports zoom:
    1.31 +   *
    1.32 +   *   zoom-ratios=100,150,200,300,400;max-zoom=4
    1.33 +   *
    1.34 +   * This means (of course) that neither the key not the value tokens can
    1.35 +   * contain either equals signs or semicolons. The test shim doesn't enforce
    1.36 +   * this so that we can test getting junk from the camera library as well.
    1.37 +   */
    1.38 +  const PREF_TEST_ENABLED = "camera.control.test.enabled";
    1.39 +  const PREF_TEST_HARDWARE = "camera.control.test.hardware";
    1.40 +  const PREF_TEST_EXTRA_PARAMETERS = "camera.control.test.hardware.gonk.parameters";
    1.41 +  var oldTestEnabled;
    1.42 +  var oldTestHw;
    1.43 +  var testMode;
    1.44 +
    1.45 +  function testHardwareSetFakeParameters(parameters, callback) {
    1.46 +    SpecialPowers.pushPrefEnv({'set': [[PREF_TEST_EXTRA_PARAMETERS, parameters]]}, function() {
    1.47 +      var setParams = SpecialPowers.getCharPref(PREF_TEST_EXTRA_PARAMETERS);
    1.48 +      ise(setParams, parameters, "Extra test parameters '" + setParams + "'");
    1.49 +      if (callback) {
    1.50 +        callback(setParams);
    1.51 +      }
    1.52 +    });
    1.53 +  }
    1.54 +
    1.55 +  function testHardwareClearFakeParameters(callback) {
    1.56 +    SpecialPowers.pushPrefEnv({'clear': [[PREF_TEST_EXTRA_PARAMETERS]]}, callback);
    1.57 +  }
    1.58 +
    1.59 +  function testHardwareSet(test, callback) {
    1.60 +    SpecialPowers.pushPrefEnv({'set': [[PREF_TEST_HARDWARE, test]]}, function() {
    1.61 +      var setTest = SpecialPowers.getCharPref(PREF_TEST_HARDWARE);
    1.62 +      ise(setTest, test, "Test subtype set to " + setTest);
    1.63 +      if (callback) {
    1.64 +        callback(setTest);
    1.65 +      }
    1.66 +    });
    1.67 +  }
    1.68 +
    1.69 +  function testHardwareDone(callback) {
    1.70 +    testMode = null;
    1.71 +    if (oldTestHw) {
    1.72 +      SpecialPowers.pushPrefEnv({'set': [[PREF_TEST_HARDWARE, oldTestHw]]}, callback);
    1.73 +    } else {
    1.74 +      SpecialPowers.pushPrefEnv({'clear': [[PREF_TEST_HARDWARE]]}, callback);
    1.75 +    }
    1.76 +  }
    1.77 +
    1.78 +  function testBegin(mode, callback) {
    1.79 +    SimpleTest.waitForExplicitFinish();
    1.80 +    try {
    1.81 +      oldTestEnabled = SpecialPowers.getCharPref(PREF_TEST_ENABLED);
    1.82 +    } catch(e) { }
    1.83 +    SpecialPowers.pushPrefEnv({'set': [[PREF_TEST_ENABLED, mode]]}, function() {
    1.84 +      var setMode = SpecialPowers.getCharPref(PREF_TEST_ENABLED);
    1.85 +      ise(setMode, mode, "Test mode set to " + setMode);
    1.86 +      if (setMode === "hardware") {
    1.87 +        try {
    1.88 +          oldTestHw = SpecialPowers.getCharPref(PREF_TEST_HARDWARE);
    1.89 +        } catch(e) { }
    1.90 +        testMode = {
    1.91 +          set: testHardwareSet,
    1.92 +          setFakeParameters: testHardwareSetFakeParameters,
    1.93 +          clearFakeParameters: testHardwareClearFakeParameters,
    1.94 +          done: testHardwareDone
    1.95 +        };
    1.96 +        if (callback) {
    1.97 +          callback(testMode);
    1.98 +        }
    1.99 +      }
   1.100 +    });
   1.101 +  }
   1.102 +
   1.103 +  function testEnd(callback) {
   1.104 +    // A chain of clean-up functions....
   1.105 +    function allCleanedUp() {
   1.106 +      SimpleTest.finish();
   1.107 +      if (callback) {
   1.108 +        callback();
   1.109 +      }
   1.110 +    }
   1.111 +    function cleanUpTestEnabled() {
   1.112 +      var next = allCleanedUp;
   1.113 +      if (oldTestEnabled) {
   1.114 +        SpecialPowers.pushPrefEnv({'set': [[PREF_TEST_ENABLED, oldTestEnabled]]}, next);
   1.115 +      } else {
   1.116 +        SpecialPowers.pushPrefEnv({'clear': [[PREF_TEST_ENABLED]]}, next);
   1.117 +      }
   1.118 +    }
   1.119 +    function cleanUpTest() {
   1.120 +      var next = cleanUpTestEnabled;
   1.121 +      if (testMode) {
   1.122 +        testMode.done(next);
   1.123 +        testMode = null;
   1.124 +      } else {
   1.125 +        next();
   1.126 +      }
   1.127 +    }
   1.128 +    function cleanUpExtraParameters() {
   1.129 +      var next = cleanUpTest;
   1.130 +      if (testMode) {
   1.131 +        testMode.clearFakeParameters(next);
   1.132 +      } else {
   1.133 +        next();
   1.134 +      }
   1.135 +    }
   1.136 +
   1.137 +    cleanUpExtraParameters();
   1.138 +  }
   1.139 +
   1.140 +  ise(SpecialPowers.sanityCheck(), "foo", "SpecialPowers passed sanity check");
   1.141 +  return {
   1.142 +    begin: testBegin,
   1.143 +    end: testEnd
   1.144 +  };
   1.145 +
   1.146 +})();

mercurial