From dcd7d61c9aa907857fe594351d126bc0215f2928 Mon Sep 17 00:00:00 2001 From: Shivam Dave Date: Thu, 6 Aug 2015 13:43:56 -0700 Subject: [PATCH] [Mobile] Tests Completed ContextMenu and InfoButton Gesture tests where elements/body touch events occur. --- .../test/gestures/InfoButtonGestureSpec.js | 81 +++++++++++++------ .../test/gestures/ContextMenuGestureSpec.js | 54 +++++++++++-- 2 files changed, 103 insertions(+), 32 deletions(-) diff --git a/platform/commonUI/inspect/test/gestures/InfoButtonGestureSpec.js b/platform/commonUI/inspect/test/gestures/InfoButtonGestureSpec.js index a388ef3d6c..036a77685b 100644 --- a/platform/commonUI/inspect/test/gestures/InfoButtonGestureSpec.js +++ b/platform/commonUI/inspect/test/gestures/InfoButtonGestureSpec.js @@ -29,9 +29,9 @@ define( describe("The info button gesture", function () { var mockTimeout, mockDocument, + mockBody, mockAgentService, mockInfoService, - testDelay = 12321, mockElement, mockDomainObject, mockEvent, @@ -40,19 +40,15 @@ define( testMetadata, mockPromise, mockHide, - gesture; - - function fireEvent(evt, value) { - mockElement.on.calls.forEach(function (call) { - if (call.args[0] === evt) { - call.args[1](value); - } - }); - } + gesture, + fireGesture, + fireDismissGesture; beforeEach(function () { mockTimeout = jasmine.createSpy('$timeout'); mockDocument = jasmine.createSpyObj('$document', ['find']); + mockBody = jasmine.createSpyObj('body', [ 'on', 'off', 'scope', 'css', 'unbind' ]); + mockDocument.find.andReturn(mockBody); mockAgentService = jasmine.createSpyObj('agentService', ['isMobile', 'isPhone']); mockInfoService = jasmine.createSpyObj( 'infoService', @@ -66,20 +62,7 @@ define( 'domainObject', [ 'getId', 'getCapability', 'useCapability', 'getModel' ] ); - mockDocument = jasmine.createSpyObj('$document', ['find']); - mockAgentService = jasmine.createSpyObj('agentService', ['isMobile']); - mockInfoService = jasmine.createSpyObj( - 'infoService', - [ 'display' ] - ); - mockElement = jasmine.createSpyObj( - 'element', - [ 'on', 'off', 'scope', 'css', 'click' ] - ); - mockDomainObject = jasmine.createSpyObj( - 'domainObject', - [ 'getId', 'getCapability', 'useCapability', 'getModel' ] - ); + mockEvent = jasmine.createSpyObj("event", ["preventDefault", "stopPropagation"]); mockEvent.pageX = 0; mockEvent.pageY = 0; @@ -103,12 +86,58 @@ define( mockElement, mockDomainObject ); + fireGesture = mockElement.on.mostRecentCall.args[1]; }); it("expect click on the representation", function () { - expect(mockElement.on) - .toHaveBeenCalledWith('click', jasmine.any(Function)); + // Fires a click call on element and then + // expects the click to have happened + fireGesture(mockEvent); + expect(mockElement.on).toHaveBeenCalledWith( + "click", + jasmine.any(Function) + ); + }); + + it("expect click then dismiss on the representation", function () { + // Fire the click and then expect the click + fireGesture(mockEvent); + expect(mockElement.on).toHaveBeenCalledWith( + "click", + jasmine.any(Function) + ); + // Get the touch start on the body + // and fire the dismiss gesture + fireDismissGesture = mockBody.on.mostRecentCall.args[1]; + fireDismissGesture(mockEvent); + // Expect Body to have been touched, event.preventDefault() + // to be called, then the mockBody listener to be detached + // lastly unbind the touchstart used to dismiss so other + // events can be called + expect(mockBody.on).toHaveBeenCalledWith( + "touchstart", + jasmine.any(Function) + ); + expect(mockEvent.preventDefault).toHaveBeenCalled(); + expect(mockBody.off).toHaveBeenCalledWith( + "touchstart", + jasmine.any(Function) + ); + expect(mockBody.unbind).toHaveBeenCalledWith( + 'touchstart' + ); + }); + + it("detaches a callback for info bubble events when destroyed", function () { + expect(mockElement.off).not.toHaveBeenCalled(); + + gesture.destroy(); + + expect(mockElement.off).toHaveBeenCalledWith( + "click", + jasmine.any(Function) + ); }); }); diff --git a/platform/representation/test/gestures/ContextMenuGestureSpec.js b/platform/representation/test/gestures/ContextMenuGestureSpec.js index d3e5243b3b..6938dee33d 100644 --- a/platform/representation/test/gestures/ContextMenuGestureSpec.js +++ b/platform/representation/test/gestures/ContextMenuGestureSpec.js @@ -31,7 +31,7 @@ define( "use strict"; var JQLITE_FUNCTIONS = [ "on", "off", "find", "append", "remove" ], - DOMAIN_OBJECT_METHODS = [ "getId", "getModel", "getCapability", "hasCapability", "useCapability" ]; + DOMAIN_OBJECT_METHODS = [ "getId", "getModel", "getCapability", "hasCapability", "useCapability"]; describe("The 'context menu' gesture", function () { @@ -40,15 +40,35 @@ define( mockAgentService, mockDomainObject, mockEvent, + mockTouchEvent, + mockContextMenuAction, + mockActionContext, + mockTouch, gesture, - fireGesture; + fireGesture, + fireTouchStartGesture, + fireTouchEndGesture; beforeEach(function () { - mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS); mockTimeout = jasmine.createSpy("$timeout"); + mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS); mockAgentService = jasmine.createSpyObj("agentService", ["isMobile"]); mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS); mockEvent = jasmine.createSpyObj("event", ["preventDefault"]); + mockContextMenuAction = jasmine.createSpyObj( + "action", + [ "perform", "getActions" ] + ); + mockActionContext = jasmine.createSpyObj( + "actionContext", + [ "" ] + ); + + mockActionContext = {domainObject: mockDomainObject, event: mockEvent}; + mockDomainObject.getCapability.andReturn(mockContextMenuAction); + mockContextMenuAction.perform.andReturn(jasmine.any(Function)); + mockAgentService.isMobile.andReturn(false); + gesture = new ContextMenuGesture(mockTimeout, mockAgentService, mockElement, mockDomainObject); @@ -57,6 +77,8 @@ define( }); it("attaches a callback for context menu events", function () { + // Fire a click and expect it to happen + fireGesture(); expect(mockElement.on).toHaveBeenCalledWith( "contextmenu", jasmine.any(Function) @@ -75,12 +97,32 @@ define( ); }); - it("mobile", function () { + it("attaches a callback for context menu events on mobile", function () { + // Mock touch event and set to mobile device + mockTouchEvent = jasmine.createSpyObj("event", ["preventDefault", "touches"]); + mockTouch = jasmine.createSpyObj("touch", ["length"]); + mockTouch.length = 1; + mockTouchEvent.touches.andReturn(mockTouch); mockAgentService.isMobile.andReturn(true); + + // Then create new (mobile) gesture gesture = new ContextMenuGesture(mockTimeout, mockAgentService, mockElement, mockDomainObject); - // Capture the contextmenu callback - fireGesture = mockElement.on.mostRecentCall.args[1]; + // Set calls for the touchstart and touchend gestures + fireTouchStartGesture = mockElement.on.calls[1].args[1]; + fireTouchEndGesture = mockElement.on.mostRecentCall.args[1]; + + // Fire touchstart and expect touch start to begin + fireTouchStartGesture(mockTouchEvent); + expect(mockElement.on).toHaveBeenCalledWith( + "touchstart", + jasmine.any(Function) + ); + + // Expect timeout to begin and then fireTouchEnd + expect(mockTimeout).toHaveBeenCalled(); + mockTimeout.mostRecentCall.args[0](); + fireTouchEndGesture(mockTouchEvent); }); }); }