You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by dd...@apache.org on 2012/08/15 14:41:36 UTC

svn commit: r1373372 - in /shindig/trunk/features/src: main/javascript/features/actions/actions_container.js test/javascript/features/actions/actions_test.js

Author: ddumont
Date: Wed Aug 15 12:41:36 2012
New Revision: 1373372

URL: http://svn.apache.org/viewvc?rev=1373372&view=rev
Log:
SHINDIG-1854 - If a gadget has an action contribution that is invalid (no path or data type) the container still get notified about the actions in its show action handler

Modified:
    shindig/trunk/features/src/main/javascript/features/actions/actions_container.js
    shindig/trunk/features/src/test/javascript/features/actions/actions_test.js

Modified: shindig/trunk/features/src/main/javascript/features/actions/actions_container.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/actions/actions_container.js?rev=1373372&r1=1373371&r2=1373372&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/actions/actions_container.js (original)
+++ shindig/trunk/features/src/main/javascript/features/actions/actions_container.js Wed Aug 15 12:41:36 2012
@@ -21,6 +21,27 @@
  *               of the UI. Available to the common container.
  */
 (function() {
+  /**
+   * Determines if the passed param is valid action
+   *
+   * @param {Object} actionObj
+   * @return {boolean} If the passed actionObj is valid
+   */
+  function isValidActionObject(actionObj) {
+    var isValid = true;
+    if (!actionObj) {
+      isValid = false;
+    } else {
+      var id = actionObj.id,
+          path = actionObj.path,
+          dataType = actionObj.dataType;
+
+      if (!id || !(path || dataType)) {
+        isValid = false;
+      }
+    }
+    return isValid;
+  }
 
   /**
    * @constructor Object that tracks the actions currently registered with the
@@ -56,12 +77,13 @@
      *          originated.
      */
     this.addAction = function(actionObj, url) {
-      var id = actionObj.id;
-      if (!id) { /* invalid object */
-        return;
+      if (!isValidActionObject(actionObj)) {
+        return; // invalid object
       }
+      var id = actionObj.id,
+          path = actionObj.path,
+          dataType = actionObj.dataType;
 
-      var path = actionObj.path;
       if (path) {
         /**
          * We maintain a tree of arrays for actions that are contributed
@@ -84,19 +106,15 @@
         } else {
           parent['@actions'] = actionsAtPath.concat(actionObj);
         }
-      } else if (actionObj.dataType) {
+      } else if (dataType) {
         /**
          * We maintain a simple map for actions that are bound to an
          * OpenSocial data object type such as the person object.
          */
-        var dataType = actionObj.dataType;
         this.registryByDataType[dataType] =
           this.registryByDataType[dataType] ?
               this.registryByDataType[dataType].concat(actionObj) :
                 [actionObj];
-      } else {
-        // invalid object, no valid path or dataType to bind action
-        return;
       }
 
       // add action to id registry
@@ -104,7 +122,7 @@
 
       // map actions to url, used by runAction to render gadget
       if (url) {
-        this.actionToUrl[actionObj.id] = url;
+        this.actionToUrl[id] = url;
         this.registryByUrl[url] =
           this.registryByUrl[url] ?
               this.registryByUrl[url].concat(actionObj) :
@@ -356,6 +374,9 @@
    *
    */
   function addAction(actionObj, url) {
+    if (!isValidActionObject(actionObj)) {
+      return; // invalid action
+    }
     registry.addAction(actionObj, url);
 
     // Comply with spec by passing an array of the object
@@ -713,12 +734,6 @@
         }
       },
 
-      /*
-       * Uncomment the below two functions to run full jsunit tests.
-       */
-      // addAction : function(actionObj) { addAction(actionObj); },
-      // removeAction : function(actionId) { removeAction(actionId); },
-
       /**
        * Executes the action associated with the action id.
        *

Modified: shindig/trunk/features/src/test/javascript/features/actions/actions_test.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/actions/actions_test.js?rev=1373372&r1=1373371&r2=1373372&view=diff
==============================================================================
--- shindig/trunk/features/src/test/javascript/features/actions/actions_test.js (original)
+++ shindig/trunk/features/src/test/javascript/features/actions/actions_test.js Wed Aug 15 12:41:36 2012
@@ -29,205 +29,220 @@ DeclarativeActionsTest.inherits(TestCase
 
 (function() {
 
-DeclarativeActionsTest.prototype.setUp = function() {
+  DeclarativeActionsTest.prototype.setUp = function() {
     this.apiUri = window.__API_URI;
     window.__API_URI = shindig.uri('http://shindig.com');
     this.containerUri = window.__CONTAINER_URI;
     window.__CONTAINER_URI = shindig.uri('http://container.com');
 
     this.gadgetsRpc = gadgets.rpc;
-    var that = this;
+    var self = this;
     gadgets.rpc = {};
-    gadgets.rpc.register = function() {};
+    gadgets.rpc.register = function(service, callback) {
+      if (self.captures && self.captures.hasOwnProperty(service)) {
+        self.captures[service] = callback;
+      }
+    };
     gadgets.rpc.call = function() {
-      that.rpcArguments = Array.prototype.slice.call(arguments);
+      self.rpcArguments = Array.prototype.slice.call(arguments);
     };
-};
+  };
 
-DeclarativeActionsTest.prototype.tearDown = function() {
+  DeclarativeActionsTest.prototype.tearDown = function() {
     window.__API_URI = this.apiUri;
     window.__CONTAINER_URI = this.containerUri;
 
     gadgets.rpc = this.gadgetsRpc;
-    this.rpcArguments = undefined;
-};
+    delete this.rpcArguments;
+    delete this.captures;
+  };
+
+  DeclarativeActionsTest.prototype.testGadgetsAddAction = function() {
+    var actionId = "testAction";
+    var callbackFn = function(){};
+    var _actionObj = {
+        id: actionId,
+        label:"Test Action",
+        path:"container/navigationLinks",
+        callback: callbackFn
+      };
+    gadgets.actions.addAction(_actionObj);
+    this.assertRpcCalled('..', 'actions.bindAction', null, _actionObj);
+  };
+
+  DeclarativeActionsTest.prototype.testGadgetsRemoveAction = function() {
+    var actionId = "testAction";
+    gadgets.actions.removeAction(actionId);
+    this.assertRpcCalled('..', 'actions.removeAction', null, actionId);
+  };
 
-DeclarativeActionsTest.prototype.testGadgetsAddAction = function() {
-  var actionId = "testAction";
-  var callbackFn = function(){};
-  var _actionObj = {
-      id: actionId,
-      label:"Test Action",
-      path:"container/navigationLinks",
-      callback: callbackFn
-    };
-  gadgets.actions.addAction(_actionObj);
-  this.assertRpcCalled('..', 'actions.bindAction', null, _actionObj);
-};
-
-DeclarativeActionsTest.prototype.testGadgetsRemoveAction = function() {
-  var actionId = "testAction";
-  gadgets.actions.removeAction(actionId);
-  this.assertRpcCalled('..', 'actions.removeAction', null, actionId);
-};
-
-DeclarativeActionsTest.prototype.testGadgetsRunAction = function() {
-  var actionId = "testAction";
-  var opt_selection = "testSelection";
-  gadgets.actions.runAction(actionId, opt_selection);
-  this.assertRpcCalled('..', 'actions.runAction', null,
-    actionId, opt_selection
-  );
-};
-
-
-DeclarativeActionsTest.prototype.testContainerGetAction = function() {
-  var container = new osapi.container.Container({});
-  var actionId = "testAction";
-  var actionObj = container.actions.getAction(actionId);
-  // registry is empty
-  this.assertUndefined(actionObj);
-};
-
-
-DeclarativeActionsTest.prototype.testContainerGetActionsByPath = function() {
-  var container = new osapi.container.Container();
-  var actionId = "testAction";
-  var actionsArray = container.actions
-    .getActionsByPath("container/navigationLinks");
-  //registry is empty
-  this.assertEquals(actionsArray, []);
-};
+  DeclarativeActionsTest.prototype.testGadgetsRunAction = function() {
+    var actionId = "testAction";
+    var opt_selection = "testSelection";
+    gadgets.actions.runAction(actionId, opt_selection);
+    this.assertRpcCalled('..', 'actions.runAction', null,
+      actionId, opt_selection
+    );
+  };
 
-DeclarativeActionsTest.prototype.testContainerGetActionsByDataType =
-  function(){
+
+  DeclarativeActionsTest.prototype.testContainerGetAction = function() {
+    var container = new osapi.container.Container({});
+    var actionId = "testAction";
+    var actionObj = container.actions.getAction(actionId);
+    // registry is empty
+    this.assertUndefined(actionObj);
+  };
+
+
+  DeclarativeActionsTest.prototype.testContainerGetActionsByPath = function() {
     var container = new osapi.container.Container();
     var actionId = "testAction";
     var actionsArray = container.actions
-      .getActionsByDataType("opensocial.Person");
+      .getActionsByPath("container/navigationLinks");
     //registry is empty
     this.assertEquals(actionsArray, []);
   };
 
-/**
- * Uncomment following _Full tests once addAction() and removeAction()
- * functions in actions_container.js are uncommented
- */
-/* FULL TESTS
-DeclarativeActionsTest.prototype.testContainerGetAction_Full = function() {
-  var container = new osapi.container.Container({});
-  var actionId = "testAction";
-  var actionObj_ = {
+  DeclarativeActionsTest.prototype.testContainerGetActionsByDataType = function() {
+    var container = new osapi.container.Container();
+    var actionId = "testAction";
+    var actionsArray = container.actions.getActionsByDataType("opensocial.Person");
+    // registry is empty
+    this.assertEquals(actionsArray, []);
+  };
+
+  DeclarativeActionsTest.prototype.testBindInvalidAction = function() {
+    var undef, captures = this.captures = {
+      'actions.bindAction': undef
+    };
+
+    var container = new osapi.container.Container(),
+        showActionCalled = false,
+        actionId = 'testAction',
+        actionObj = {
+          id: actionId
+        };
+
+    this.assertNotUndefined('RPC endpoint "actions.bindAction" was not registered.', captures['actions.bindAction']);
+    container.actions.registerShowActionsHandler(function() {
+      showActionCalled = true;
+    });
+    captures['actions.bindAction'](actionObj);
+
+    this.assertUndefined(container.actions.getAction(actionId));
+    this.assertFalse(showActionCalled);
+  };
+
+  DeclarativeActionsTest.prototype.testContainerGetAction_Full = function() {
+    var undef, captures = this.captures = {
+      'actions.bindAction': undef,
+      'actions.removeAction': undef
+    };
+
+    var container = new osapi.container.Container({}),
+        actionId = "testAction",
+        actionObj = {
           id: actionId,
           label: "Test Action",
           path: "container/navigationLinks"
-  }
-  container.actions.addAction(actionObj_);
-  var actionObj = container.actions.getAction(actionId);
-  this.assertEquals(actionObj_, actionObj);
-
-  container.actions.removeAction(actionId);
-  actionObj = container.actions.getAction(actionId);
-  this.assertUndefined(actionObj);
-
-};
-
-DeclarativeActionsTest.prototype.testContainerGetActions_Full = function() {
-  var container = new osapi.container.Container({});
-  var actionId = "testAction",
-      actions = [
-        {
-          id: "test1",
-          label: "Test Action1",
-          path: "container/navigationLinks"
-        },
-        {
-          id: "test2",
-          label: "Test Action2",
-          path: "container/navigationLinks"
-        },
-        {
-          id: "test3",
-          label: "Test Action3",
-          dataType: "opensocial.Person"
-        },
-        {
-          id: "test4",
-          label: "Test Action4",
-          dataType: "opensocial.Person"
-        }
-      ];
-  for (actionIndex in actions) {
-    container.actions.addAction(actions[actionIndex]);
-  }
-
-  var allActions = container.actions.getAllActions();
-  this.assertEquals(actions, allActions);
-
-  for (actionIndex in actions) {
-    container.actions.removeAction(actions[actionIndex].id);
-  }
-
-  allActions = container.actions.getAllActions();
-  this.assertEquals([], allActions);
-
-};
-DeclarativeActionsTest.prototype.testContainerGetActionsByPath_Full =
-  function(){
-    var container = new osapi.container.Container();
-    var actionId = "testAction";
-    var actionObj_ = {
-            id: actionId,
-            label: "Test Action",
-            path: "container/navigationLinks"
+        };
+
+    this.assertNotUndefined('RPC endpoint "actions.bindAction" was not registered.', captures['actions.bindAction']);
+    this.assertNotUndefined('RPC endpoint "actions.removeAction" was not registered.', captures['actions.removeAction']);
+    captures['actions.bindAction'](actionObj);
+
+    this.assertEquals(actionObj, container.actions.getAction(actionId));
+
+    captures['actions.removeAction'](actionId);
+    this.assertUndefined(container.actions.getAction(actionId));
+  };
+
+
+  DeclarativeActionsTest.prototype.testContainerGetActions_Full = function() {
+    var undef, captures = this.captures = {
+      'actions.bindAction': undef,
+      'actions.removeAction': undef
+    };
+
+    var container = new osapi.container.Container({}),
+        actions = [
+          { id: "test1", label: "Test Action1", path: "container/navigationLinks" },
+          { id: "test2", label: "Test Action2", path: "container/navigationLinks" },
+          { id: "test3", label: "Test Action3", dataType: "opensocial.Person" },
+          { id: "test4", label: "Test Action4", dataType: "opensocial.Person" }
+        ];
+    this.assertNotUndefined('RPC endpoint "actions.bindAction" was not registered.', captures['actions.bindAction']);
+    this.assertNotUndefined('RPC endpoint "actions.removeAction" was not registered.', captures['actions.removeAction']);
+
+    for (var i = 0; i < actions.length; i++) {
+      captures['actions.bindAction'](actions[i]);
     }
-    container.actions.addAction(actionObj_);
-    var actionsArray = container.actions
-      .getActionsByPath("container/navigationLinks");
-    this.assertEquals(actionsArray, [ actionObj_ ]);
+    this.assertEquals(actions, container.actions.getAllActions());
+
+    for (var i = 0; i < actions.length; i++) {
+      captures['actions.removeAction'](actions[i].id);
+    }
+    this.assertEquals([], container.actions.getAllActions());
 
-    container.actions.removeAction(actionId);
-    actionsArray = container.actions
-      .getActionsByPath("container/navigationLinks");
-    this.assertEquals(actionsArray, []);
   };
 
-DeclarativeActionsTest.prototype.testContainerGetActionsByDataType_Full =
-  function() {
-    var container = new osapi.container.Container();
-    var actionId = "testAction";
-    var actionObj_ = {
-            id: actionId,
-            label: "Test Action",
-            dataType: "opensocial.Person"
+
+  DeclarativeActionsTest.prototype.testContainerGetActionsByPath_Full = function() {
+    var undef, captures = this.captures = {
+      'actions.bindAction': undef,
+      'actions.removeAction': undef
     };
 
-    container.actions.addAction(actionObj_);
-    var actionsArray = container.actions
-      .getActionsByDataType("opensocial.Person");
-    this.assertEquals([ actionObj_ ], actionsArray);
+    var container = new osapi.container.Container(),
+        actionObj = {
+          id: "testAction",
+          label: "Test Action",
+          path: "container/navigationLinks"
+        };
+    this.assertNotUndefined('RPC endpoint "actions.bindAction" was not registered.', captures['actions.bindAction']);
+    this.assertNotUndefined('RPC endpoint "actions.removeAction" was not registered.', captures['actions.removeAction']);
 
-    container.actions.removeAction(actionId);
-    actionsArray = container.actions
-      .getActionsByDataType("opensocial.Person");
-    this.assertEquals([], actionsArray);
+    captures['actions.bindAction'](actionObj);
+    this.assertEquals([actionObj], container.actions.getActionsByPath("container/navigationLinks"));
 
+    captures['actions.removeAction'](actionObj.id);
+    this.assertEquals([], container.actions.getActionsByPath("container/navigationLinks"));
   };
 
- FULL TESTS */
-
-/**
- * Asserts gadgets.rpc.call() is called with the expected arguments given.
- */
-DeclarativeActionsTest.prototype.assertRpcCalled = function() {
-  this.assertNotUndefined("RPC was not called.", this.rpcArguments);
-  this.assertEquals("RPC argument list not valid length.", arguments.length,
-      this.rpcArguments.length);
-
-  for ( var i = 0; i < arguments.length; i++) {
-    this.assertEquals(arguments[i], this.rpcArguments[i]);
-  }
-  this.rpcArguments = undefined;
-};
+  DeclarativeActionsTest.prototype.testContainerGetActionsByDataType_Full = function() {
+    var undef, captures = this.captures = {
+      'actions.bindAction': undef,
+      'actions.removeAction': undef
+    };
 
+    var container = new osapi.container.Container();
+        actionObj = {
+          id: "testAction",
+          label: "Test Action",
+          dataType: "opensocial.Person"
+        };
+    this.assertNotUndefined('RPC endpoint "actions.bindAction" was not registered.', captures['actions.bindAction']);
+    this.assertNotUndefined('RPC endpoint "actions.removeAction" was not registered.', captures['actions.removeAction']);
+
+    captures['actions.bindAction'](actionObj);
+    this.assertEquals([actionObj], container.actions.getActionsByDataType("opensocial.Person"));
+
+    captures['actions.removeAction'](actionObj.id);
+    this.assertEquals([], container.actions.getActionsByDataType("opensocial.Person"));
+  };
+
+  /**
+   * Asserts gadgets.rpc.call() is called with the expected arguments given.
+   */
+  DeclarativeActionsTest.prototype.assertRpcCalled = function() {
+    this.assertNotUndefined("RPC was not called.", this.rpcArguments);
+    this.assertEquals("RPC argument list not valid length.", arguments.length,
+        this.rpcArguments.length);
+
+    for ( var i = 0; i < arguments.length; i++) {
+      this.assertEquals(arguments[i], this.rpcArguments[i]);
+    }
+    this.rpcArguments = undefined;
+  };
 })();
\ No newline at end of file