      1 pockets.test.ui = {};
      2
      3 pockets.test.ui.getTestWindow = function() {
      4   return pockets.test.testWindow;
      5 };
      6
      7 /**
      8  * @return {Node} DOM element representing an item of a ListSelector (aka popup menu)
      9  * @param {String} itemValue the model value of the item you want to select
     10  */
     11 pockets.test.ui.getListSelectorItemElement = function(itemValue) {
     12   return pockets.test.ui.getTestWindow().document.body.querySelector(".palm-popup-content .palm-row[x-mojo-menu-cmd='" + itemValue + "']");
     13 };
     14
     15 pockets.test.ui.chooseListSelectorItem = function(listSelectorId, selectionValue) {
     16   var listSelectorEl = pockets.test.ui.getTestWindow().document.getElementById(listSelectorId);
     17   if (!listSelectorEl) {
     18     throw 'element with id ' + listSelectorId + ' is not present in the current scene';
     19   }
     20   Mojo.Event.send(listSelectorEl, Mojo.Event.tap);
     21   pockets.test.ui.selectPopupItemFromOpenMenu(selectionValue, "list " + listSelectorId);
     22 };
     23
     24 pockets.test.ui.selectPopupItemFromOpenMenu = function(selectionValue, listDescription) {
     25   var selectionEl = pockets.test.ui.getListSelectorItemElement(selectionValue);
     26   if (!selectionEl) {
     27     throw '"' + selectionValue + '" is not a valid selection for ' + listDescription;
     28   }
     29   Mojo.Event.send(selectionEl, Mojo.Event.tap);
     30 };
     31
     32 pockets.test.ui.hasListSelectorItem = function(listSelectorId, selectionValue) {
     33   var listSelectorEl = pockets.test.ui.getTestWindow().$(listSelectorId);
     34   if (!listSelectorEl) {
     35     throw 'element with id ' + listSelectorId + ' is not present in the current scene';
     36   }
     37   Mojo.Event.send(listSelectorEl, Mojo.Event.tap);
     38   var selectionEl = pockets.test.ui.getListSelectorItemElement(selectionValue);
     39   return (!!selectionEl);
     40 };
     41
     42 pockets.test.ui.chooseIntegerPickerItem = function(integerPickerId, itemValue) {
     43   //TODO - Ideally, choose item should use DOM events, not object methods. Some behavior may not get fired
     44   var testWindow = pockets.test.ui.getTestWindow();
     45   testWindow.$(integerPickerId)._mojoController.assistant.strategy._choose(itemValue);
     46 };
     47
     48 pockets.test.ui.sendMenuCommand = function(assistant, command) {
     49   assistant.handleCommand(Mojo.Event.make(Mojo.Event.command, {
     50     command: command,
     51     originalEvent: null
     52   }));
     53 };
     54
     55 /**
     56  * @param {Object} assistant the assistant which should handle the menu click
     57  * @param {Object} matcher if a String, the contents of the 'command' property for the button.  If a function, this function is used
     58  *                 to match the button.
     59  */
     60 pockets.test.ui.clickMenuButton = function(assistant, matcher) {
     61   var matchingButtons = pockets.test.ui.getTestWindow().$$(".palm-menu-button");
     62   var matchingTexts = pockets.test.ui.getTestWindow().$$(".palm-menu-text");
     63   var matching = matchingButtons.concat(matchingTexts);
     64
     65   for (var i = 0; i < matching.length; i++) {
     66     var model = matching[i]._mojoMenuItemModel;
     67     if (typeof matcher == "string") {
     68       if (model.command == matcher) {
     69         pockets.test.ui.sendMenuCommand(assistant, model.command);
     70         return;
     71       }
     72     }
     73     if (typeof matcher == "function") {
     74       if (matcher(model)) {
     75         pockets.test.ui.sendMenuCommand(assistant, model.command);
     76         return;
     77       }
     78     }
     79   }
     80   throw new Error("Did not find a match in menu items");
     81 };
     82
     83 /**
     84  * @param {Object} assistant the assistant which should handle the menu click
     85  * @param {String} commandName the contents of the 'name' property which you have to add to the command menu item model
     86  */
     87 pockets.test.ui.clickAppMenuButton = function(assistant, commandName) {
     88   try {
     89     assistant.controller._menu.assistant.controller.stageController.sendEventToCommanders = Mojo.doNothing;
     90     assistant.controller._menu.assistant.showAppMenu();
     91   }
     92   catch (e) {
     93     throw 'A custom appMenu has not been defined';
     94   }
     95   var appModel = assistant.controller._menu.assistant.appModel;
     96
     97   for (var i = 0; i < appModel.items.length; i++) {
     98     var model = appModel.items[i];
     99     if (model.items) {
    100       for (var j = 0; j < model.items.length; j++) {
    101         if (model.items[j].command === commandName) {
    102           pockets.test.ui.sendMenuCommand(assistant, model.items[j].command);
    103           return;
    104         }
    105       }
    106     } else {
    107       if (model.command === commandName) {
    108         pockets.test.ui.sendMenuCommand(assistant, model.command);
    109         return;
    110       }
    111     }
    112   }
    113   throw new Error("Did not find a '" + commandName + "' app menu item");
    114 };
    115
    116
    117 /**
    118  * @param {Object} controller the controller which contains the button widget
    119  * @param {String} buttonId the DOM Id of the button which you want to click
    120  */
    121 pockets.test.ui.tapButton = function(controller, buttonId) {
    122   var buttonEl = controller.get(buttonId);
    123   Mojo.Event.send(buttonEl, Mojo.Event.tap);
    124 };
    125
    126 pockets.test.ui.FakeController = function() {
    127   this.elements = {};
    128 };
    129
    130 pockets.test.ui.FakeController.prototype.get = function(id) {
    131   return this.elements[id] || (this.elements[id] = pockets.dom.create('div', {
    132     id: id
    133   }));
    134 };
    135
    136 pockets.test.ui.FakeController.prototype.setupWidget = function(id, attributes, model) {
    137   var widgetEl = this.get(id);
    138   widgetEl.widgetSetup = {
    139     attributes: attributes,
    140     model: model
    141   };
    142 };
    143
    144 pockets.test.ui.FakeController.prototype.showDialog = Mojo.doNothing;
    145
    146