Bug 1337133 - Replace some checks in actions with calls to assert.js; r?ato draft
authorMaja Frydrychowicz <mjzffr@gmail.com>
Tue, 07 Feb 2017 18:27:38 -0500
changeset 487124 5737a57570ef90d83b35a550e8dd27568a7a5ddb
parent 487123 fe00bf42fc0a810f5b41955cf9657e88127748d4
child 487125 957dcfbdb55069c08d0fbd586025339e5cb23cc2
push id46140
push userbmo:mjzffr@gmail.com
push dateMon, 20 Feb 2017 22:22:08 +0000
reviewersato
bugs1337133
milestone54.0a1
Bug 1337133 - Replace some checks in actions with calls to assert.js; r?ato MozReview-Commit-ID: Isiesr65yx3
testing/marionette/action.js
--- a/testing/marionette/action.js
+++ b/testing/marionette/action.js
@@ -347,19 +347,17 @@ action.PointerOrigin = {
  *     If |obj| is not a valid origin.
  */
 action.PointerOrigin.get = function(obj) {
   let origin = obj;
   if (typeof obj == "undefined") {
     origin = this.Viewport;
   } else if (typeof obj == "string") {
     let name = capitalize(obj);
-    if (!(name in this)) {
-      throw new InvalidArgumentError(`Unknown pointer-move origin: ${obj}`);
-    }
+    assert.in(name, this, error.pprint`Unknown pointer-move origin: ${obj}`);
     origin = this[name];
   } else if (!element.isWebElementReference(obj)) {
     throw new InvalidArgumentError("Expected 'origin' to be a string or a " +
       `web element reference, got: ${obj}`);
   }
   return origin;
 };
 
@@ -379,19 +377,17 @@ action.PointerType = {
  * @return {string}
  *     A pointer type for processing pointer parameters.
  *
  * @throws {InvalidArgumentError}
  *     If |str| is not a valid pointer type.
  */
 action.PointerType.get = function (str) {
   let name = capitalize(str);
-  if (!(name in this)) {
-    throw new InvalidArgumentError(`Unknown pointerType: ${str}`);
-  }
+  assert.in(name, this, error.pprint`Unknown pointerType: ${str}`);
   return this[name];
 };
 
 /**
  * Input state associated with current session. This is a map between input ID and
  * the device state for that input source, with one entry for each active input source.
  *
  * Initialized in listener.js
@@ -442,19 +438,17 @@ class InputState {
    * @return {action.InputState}
    *     An |action.InputState| object for the type of the |actionSequence|.
    *
    * @throws {InvalidArgumentError}
    *     If |actionSequence.type| is not valid.
    */
   static fromJson(obj) {
     let type = obj.type;
-    if (!(type in ACTIONS)) {
-      throw new InvalidArgumentError(`Unknown action type: ${type}`);
-    }
+    assert.in(type, ACTIONS, error.pprint`Unknown action type: ${type}`);
     let name = type == "none" ? "Null" : capitalize(type);
     return new action.InputState[name]();
   }
 }
 
 /** Possible kinds of |InputState| for supported input sources. */
 action.InputState = {};
 
@@ -571,19 +565,17 @@ action.InputState.Pointer = class Pointe
  *      If any parameters are undefined.
  */
 action.Action = class {
   constructor(id, type, subtype) {
     if ([id, type, subtype].includes(undefined)) {
       throw new InvalidArgumentError("Missing id, type or subtype");
     }
     for (let attr of [id, type, subtype]) {
-      if (typeof attr != "string") {
-        throw new InvalidArgumentError(`Expected string, got: ${attr}`);
-      }
+      assert.string(attr, error.pprint`Expected string, got: ${attr}`);
     }
     this.id = id;
     this.type = type;
     this.subtype = subtype;
   };
 
   toString() {
     return `[action ${this.type}]`;
@@ -623,21 +615,19 @@ action.Action = class {
 
     switch (item.subtype) {
       case action.KeyUp:
       case action.KeyDown:
         let key = actionItem.value;
         // TODO countGraphemes
         // TODO key.value could be a single code point like "\uE012" (see rawKey)
         // or "grapheme cluster"
-        if (typeof key != "string") {
-          throw new InvalidArgumentError(
-              "Expected 'value' to be a string that represents single code point " +
-              "or grapheme cluster, got: " + key);
-        }
+        assert.string(key,
+            error.pprint("Expected 'value' to be a string that represents single code point " +
+                `or grapheme cluster, got: ${key}`));
         item.value = key;
         break;
 
       case action.PointerDown:
       case action.PointerUp:
         assert.positiveInteger(actionItem.button,
             error.pprint`Expected 'button' (${actionItem.button}) to be >= 0`);
         item.button = actionItem.button;
@@ -736,24 +726,23 @@ action.Sequence = class extends Array {
    *     If |actionSequence.actions| is not an Array.
    */
   static fromJson(actionSequence) {
     // used here only to validate 'type' and InputState type
     let inputSourceState = InputState.fromJson(actionSequence);
     let id = actionSequence.id;
     if (typeof id == "undefined") {
       actionSequence.id = id = element.generateUUID();
-    } else if (typeof id != "string") {
-      throw new InvalidArgumentError(`Expected 'id' to be a string, got: ${id}`);
+    } else {
+      assert.string(id, error.pprint`Expected 'id' to be a string, got: ${id}`);
     }
     let actionItems = actionSequence.actions;
-    if (!Array.isArray(actionItems)) {
-      throw new InvalidArgumentError(
-          `Expected 'actionSequence.actions' to be an Array, got: ${actionSequence.actions}`);
-    }
+    assert.array(actionItems,
+        error.pprint("Expected 'actionSequence.actions' to be an Array, " +
+            `got: ${actionSequence.actions}`));
 
     if (action.inputStateMap.has(id) && !action.inputStateMap.get(id).is(inputSourceState)) {
       throw new InvalidArgumentError(
           `Expected ${id} to be mapped to ${inputSourceState}, ` +
           `got: ${action.inputStateMap.get(id)}`);
     }
     let actions = new action.Sequence();
     for (let actionItem of actionItems) {
@@ -1049,13 +1038,11 @@ function dispatchPause(a, tickDuration) 
  * @return {Promise}
  *     Promise to flush DOM events.
  */
 function flushEvents(container) {
   return new Promise(resolve => container.frame.requestAnimationFrame(resolve));
 }
 
 function capitalize(str) {
-  if (typeof str != "string") {
-    throw new InvalidArgumentError(`Expected string, got: ${str}`);
-  }
+  assert.string(str);
   return str.charAt(0).toUpperCase() + str.slice(1);
 }