PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/ext/invoke/index.js

https://github.com/jcarty/BB10-WebWorks-Framework
JavaScript | 450 lines | 370 code | 65 blank | 15 comment | 0 complexity | 37c673b8f8d968ab680a8e334d3aab1c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MIT
  1. /*
  2. * Copyright 2011-2012 Research In Motion Limited.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var _apiDir = __dirname + "./../../../../ext/invoke/",
  17. _libDir = __dirname + "./../../../../lib/",
  18. _extDir = __dirname + "./../../../../ext/",
  19. mockedInvocation,
  20. mockedApplication,
  21. mockedController,
  22. _event,
  23. index,
  24. successCB,
  25. failCB,
  26. errorCode = -1;
  27. describe("invoke index", function () {
  28. beforeEach(function () {
  29. mockedInvocation = {
  30. invoke: jasmine.createSpy("invocation.invoke"),
  31. queryTargets: jasmine.createSpy("invocation.queryTargets"),
  32. TARGET_TYPE_MASK_APPLICATION: 1,
  33. TARGET_TYPE_MASK_CARD: 2,
  34. TARGET_TYPE_MASK_VIEWER: 4
  35. };
  36. mockedController = {
  37. dispatchEvent : jasmine.createSpy(),
  38. addEventListener : jasmine.createSpy(),
  39. removeEventListener : jasmine.createSpy()
  40. };
  41. mockedApplication = {
  42. interrupter : false,
  43. invocation: mockedInvocation,
  44. addEventListener : jasmine.createSpy().andCallFake(function (eventName, callback) {
  45. callback();
  46. }),
  47. removeEventListener : jasmine.createSpy()
  48. };
  49. GLOBAL.window = {};
  50. GLOBAL.window.qnx = {
  51. callExtensionMethod : function () {},
  52. webplatform: {
  53. getApplication: function () {
  54. return mockedApplication;
  55. },
  56. getController : function () {
  57. return mockedController;
  58. },
  59. }
  60. };
  61. GLOBAL.qnx = GLOBAL.window.qnx;
  62. _event = require(_libDir + "./event");
  63. index = require(_apiDir + "index");
  64. successCB = jasmine.createSpy("success callback");
  65. failCB = jasmine.createSpy("fail callback");
  66. });
  67. afterEach(function () {
  68. mockedInvocation = null;
  69. GLOBAL.window.qnx = null;
  70. index = null;
  71. successCB = null;
  72. failCB = null;
  73. });
  74. describe("invoke", function () {
  75. it("can invoke with target", function () {
  76. var successCB = jasmine.createSpy(),
  77. mockedArgs = {
  78. "request": encodeURIComponent(JSON.stringify({target: "abc.xyz"}))
  79. };
  80. index.invoke(successCB, null, mockedArgs);
  81. expect(mockedInvocation.invoke).toHaveBeenCalledWith({
  82. target: "abc.xyz"
  83. }, jasmine.any(Function));
  84. expect(successCB).toHaveBeenCalled();
  85. });
  86. it("can invoke with uri", function () {
  87. var successCB = jasmine.createSpy(),
  88. mockedArgs = {
  89. "request": encodeURIComponent(JSON.stringify({uri: "http://www.rim.com"}))
  90. };
  91. index.invoke(successCB, null, mockedArgs);
  92. expect(mockedInvocation.invoke).toHaveBeenCalledWith({
  93. uri: "http://www.rim.com"
  94. }, jasmine.any(Function));
  95. expect(successCB).toHaveBeenCalled();
  96. });
  97. it("can invoke with uri using the file_transfer_mode property", function () {
  98. var successCB = jasmine.createSpy(),
  99. mockedArgs = {
  100. "request": encodeURIComponent(JSON.stringify({uri: "http://www.rim.com", file_transfer_mode: "PRESERVE"})),
  101. };
  102. index.invoke(successCB, null, mockedArgs);
  103. expect(mockedInvocation.invoke).toHaveBeenCalledWith({
  104. uri: "http://www.rim.com",
  105. file_transfer_mode : "PRESERVE"
  106. }, jasmine.any(Function));
  107. expect(successCB).toHaveBeenCalled();
  108. });
  109. });
  110. describe("query", function () {
  111. var APPLICATION = 1,
  112. CARD = 2,
  113. VIEWER = 4;
  114. it("can query the invocation framework", function () {
  115. var success = jasmine.createSpy(),
  116. fail = jasmine.createSpy(),
  117. request = {
  118. "action": "bb.action.OPEN",
  119. "type": "image/*",
  120. "target_type": ["APPLICATION", "VIEWER", "CARD"]
  121. },
  122. args = {
  123. "request": encodeURIComponent(JSON.stringify(request))
  124. };
  125. index.query(success, fail, args);
  126. delete request["target_type"];
  127. request["target_type_mask"] = APPLICATION | VIEWER | CARD;
  128. expect(mockedInvocation.queryTargets).toHaveBeenCalledWith(request, jasmine.any(Function));
  129. expect(success).toHaveBeenCalled();
  130. expect(fail).not.toHaveBeenCalled();
  131. });
  132. it("can perform a query for application targets", function () {
  133. var success = jasmine.createSpy(),
  134. fail = jasmine.createSpy(),
  135. request = {
  136. "action": "bb.action.OPEN",
  137. "type": "image/*",
  138. "target_type": ["APPLICATION"]
  139. },
  140. args = {
  141. "request": encodeURIComponent(JSON.stringify(request))
  142. };
  143. index.query(success, fail, args);
  144. delete request["target_type"];
  145. request["target_type_mask"] = APPLICATION;
  146. expect(mockedInvocation.queryTargets).toHaveBeenCalledWith(request, jasmine.any(Function));
  147. expect(success).toHaveBeenCalled();
  148. expect(fail).not.toHaveBeenCalled();
  149. });
  150. it("can perform a query for viewer targets", function () {
  151. var success = jasmine.createSpy(),
  152. fail = jasmine.createSpy(),
  153. request = {
  154. "action": "bb.action.OPEN",
  155. "type": "image/*",
  156. "target_type": ["VIEWER"]
  157. },
  158. args = {
  159. "request": encodeURIComponent(JSON.stringify(request))
  160. };
  161. index.query(success, fail, args);
  162. delete request["target_type"];
  163. request["target_type_mask"] = VIEWER;
  164. expect(mockedInvocation.queryTargets).toHaveBeenCalledWith(request, jasmine.any(Function));
  165. expect(success).toHaveBeenCalled();
  166. expect(fail).not.toHaveBeenCalled();
  167. });
  168. it("can perform a query for card targets", function () {
  169. var success = jasmine.createSpy(),
  170. fail = jasmine.createSpy(),
  171. request = {
  172. "action": "bb.action.OPEN",
  173. "type": "image/*",
  174. "target_type": ["CARD"]
  175. },
  176. args = {
  177. "request": encodeURIComponent(JSON.stringify(request))
  178. };
  179. index.query(success, fail, args);
  180. delete request["target_type"];
  181. request["target_type_mask"] = CARD;
  182. expect(mockedInvocation.queryTargets).toHaveBeenCalledWith(request, jasmine.any(Function));
  183. expect(success).toHaveBeenCalled();
  184. expect(fail).not.toHaveBeenCalled();
  185. });
  186. it("can perform a query for all targets", function () {
  187. var success = jasmine.createSpy(),
  188. fail = jasmine.createSpy(),
  189. request = {
  190. "action": "bb.action.OPEN",
  191. "type": "image/*",
  192. "target_type": ["APPLICATION", "VIEWER", "CARD"]
  193. },
  194. args = {
  195. "request": encodeURIComponent(JSON.stringify(request))
  196. };
  197. index.query(success, fail, args);
  198. delete request["target_type"];
  199. request["target_type_mask"] = APPLICATION | VIEWER | CARD;
  200. expect(mockedInvocation.queryTargets).toHaveBeenCalledWith(request, jasmine.any(Function));
  201. expect(success).toHaveBeenCalled();
  202. expect(fail).not.toHaveBeenCalled();
  203. });
  204. it("will not generate a target_type property in the request if it is not an array", function () {
  205. var success = jasmine.createSpy(),
  206. fail = jasmine.createSpy(),
  207. request = {
  208. "action": "bb.action.OPEN",
  209. "type": "image/*",
  210. "target_type": "APPLICATION"
  211. },
  212. args = {
  213. "request": encodeURIComponent(JSON.stringify(request))
  214. };
  215. index.query(success, fail, args);
  216. expect(mockedInvocation.queryTargets).toHaveBeenCalledWith(request, jasmine.any(Function));
  217. expect(success).toHaveBeenCalled();
  218. expect(fail).not.toHaveBeenCalled();
  219. });
  220. it("will only use valid entries in the target type array to generate the target type mask", function () {
  221. var success = jasmine.createSpy(),
  222. fail = jasmine.createSpy(),
  223. request = {
  224. "action": "bb.action.OPEN",
  225. "type": "image/*",
  226. "target_type": ["APPLICATION", "VIEWER", "CARD", "INVALID_ENTRY"]
  227. },
  228. args = {
  229. "request": encodeURIComponent(JSON.stringify(request))
  230. };
  231. index.query(success, fail, args);
  232. request["target_type"] = ["INVALID_ENTRY"];
  233. request["target_type_mask"] = APPLICATION | VIEWER | CARD;
  234. expect(mockedInvocation.queryTargets).toHaveBeenCalledWith(request, jasmine.any(Function));
  235. expect(success).toHaveBeenCalled();
  236. expect(fail).not.toHaveBeenCalled();
  237. });
  238. });
  239. describe("card", function () {
  240. beforeEach(function () {
  241. mockedInvocation.closeChildCard = jasmine.createSpy("invocation.closeChildCard");
  242. });
  243. afterEach(function () {
  244. delete mockedInvocation.closeChildCard;
  245. });
  246. describe("methods", function () {
  247. it("can call closeChildCard with success callback at the end", function () {
  248. index.closeChildCard(successCB, failCB);
  249. expect(mockedInvocation.closeChildCard).toHaveBeenCalled();
  250. expect(successCB).toHaveBeenCalled();
  251. expect(failCB).not.toHaveBeenCalled();
  252. });
  253. it("can call closeChildCard with fail callback on wrong call", function () {
  254. index.closeChildCard(null, failCB);
  255. expect(mockedInvocation.closeChildCard).toHaveBeenCalled();
  256. expect(successCB).not.toHaveBeenCalled();
  257. expect(failCB).toHaveBeenCalledWith(errorCode, jasmine.any(Object));
  258. });
  259. });
  260. describe("events", function () {
  261. var utils,
  262. events,
  263. eventExt;
  264. beforeEach(function () {
  265. utils = require(_libDir + "utils");
  266. events = require(_libDir + "event");
  267. eventExt = require(__dirname + "./../../../../ext/event/index");
  268. spyOn(utils, "loadExtensionModule").andCallFake(function () {
  269. return eventExt;
  270. });
  271. });
  272. afterEach(function () {
  273. utils = null;
  274. events = null;
  275. eventExt = null;
  276. });
  277. it("can register for events", function () {
  278. var evts = ["onChildCardStartPeek", "onChildCardEndPeek", "onChildCardClosed"],
  279. args;
  280. spyOn(events, "add");
  281. evts.forEach(function (e) {
  282. args = {eventName : encodeURIComponent(e)};
  283. index.registerEvents(successCB);
  284. eventExt.add(null, null, args);
  285. expect(successCB).toHaveBeenCalled();
  286. expect(events.add).toHaveBeenCalled();
  287. expect(events.add.mostRecentCall.args[0].event).toEqual(e);
  288. expect(events.add.mostRecentCall.args[0].trigger).toEqual(jasmine.any(Function));
  289. });
  290. });
  291. it("call successCB when all went well", function () {
  292. var eventName = "onChildCardClosed",
  293. args = {eventName: encodeURIComponent(eventName)};
  294. spyOn(events, "add");
  295. index.registerEvents(jasmine.createSpy(), jasmine.createSpy());
  296. eventExt.add(successCB, failCB, args);
  297. expect(events.add).toHaveBeenCalled();
  298. expect(successCB).toHaveBeenCalled();
  299. expect(failCB).not.toHaveBeenCalled();
  300. });
  301. it("call errorCB when there was an error", function () {
  302. var eventName = "onChildCardClosed",
  303. args = {eventName: encodeURIComponent(eventName)};
  304. spyOn(events, "add").andCallFake(function () {
  305. throw "";
  306. });
  307. index.registerEvents(jasmine.createSpy(), jasmine.createSpy());
  308. eventExt.add(successCB, failCB, args);
  309. expect(events.add).toHaveBeenCalled();
  310. expect(successCB).not.toHaveBeenCalled();
  311. expect(failCB).toHaveBeenCalledWith(-1, jasmine.any(String));
  312. });
  313. it("can un-register from events", function () {
  314. var evts = ["onChildCardStartPeek", "onChildCardEndPeek", "onChildCardClosed"],
  315. args;
  316. spyOn(events, "remove");
  317. evts.forEach(function (e) {
  318. args = {eventName : encodeURIComponent(e)};
  319. eventExt.remove(null, null, args);
  320. expect(events.remove).toHaveBeenCalled();
  321. expect(events.remove.mostRecentCall.args[0].event).toEqual(e);
  322. expect(events.remove.mostRecentCall.args[0].trigger).toEqual(jasmine.any(Function));
  323. });
  324. });
  325. it("call successCB when all went well even when event is not defined", function () {
  326. var eventName = "eventnotdefined",
  327. args = {eventName: encodeURIComponent(eventName)};
  328. spyOn(events, "remove");
  329. eventExt.remove(successCB, failCB, args);
  330. expect(events.remove).toHaveBeenCalled();
  331. expect(successCB).toHaveBeenCalled();
  332. expect(failCB).not.toHaveBeenCalled();
  333. });
  334. it("call errorCB when there was exception occured", function () {
  335. var eventName = "onChildCardClosed",
  336. args = {eventName: encodeURIComponent(eventName)};
  337. spyOn(events, "remove").andCallFake(function () {
  338. throw "";
  339. });
  340. eventExt.remove(successCB, failCB, args);
  341. expect(events.remove).toHaveBeenCalled();
  342. expect(successCB).not.toHaveBeenCalled();
  343. expect(failCB).toHaveBeenCalledWith(-1, jasmine.any(String));
  344. });
  345. });
  346. describe("interrupt invocation", function () {
  347. it("expect returnInterruption to be defined", function () {
  348. expect(index.returnInterruption).toBeDefined();
  349. });
  350. it("can properly register for invocation interruption", function () {
  351. var args = {someTestStuff : 'Test arges'};
  352. index.registerInterrupter(successCB, failCB, args);
  353. expect(mockedApplication.invocation.interrupter).toEqual(jasmine.any(Function));
  354. expect(successCB).toHaveBeenCalled();
  355. });
  356. it("can properly trigger an event to the client after registration", function () {
  357. var args = {someTestStuff : 'Test arges'},
  358. mockedRequest = {'mocked' : 'request'},
  359. returnCallback = jasmine.createSpy();
  360. index.registerInterrupter(successCB, failCB, args);
  361. expect(mockedApplication.invocation.interrupter).toEqual(jasmine.any(Function));
  362. spyOn(_event, 'trigger');
  363. mockedApplication.invocation.interrupter(mockedRequest, returnCallback);
  364. expect(_event.trigger).toHaveBeenCalledWith('invocation.interrupted', mockedRequest);
  365. expect(successCB).toHaveBeenCalled();
  366. });
  367. it("can properly return from the client to continue invocation", function () {
  368. var mockedRequest = { request : encodeURIComponent(JSON.stringify({something : 'some other data'})) },
  369. returnCallback = jasmine.createSpy();
  370. index.registerInterrupter(successCB, failCB, mockedRequest);
  371. expect(mockedApplication.invocation.interrupter).toEqual(jasmine.any(Function));
  372. spyOn(_event, 'trigger');
  373. mockedApplication.invocation.interrupter(mockedRequest, returnCallback);
  374. expect(_event.trigger).toHaveBeenCalledWith('invocation.interrupted', mockedRequest);
  375. index.returnInterruption(successCB, failCB, mockedRequest);
  376. expect(returnCallback).toHaveBeenCalledWith(JSON.parse(decodeURIComponent(mockedRequest.request)));
  377. expect(successCB).toHaveBeenCalled();
  378. });
  379. it("can properly clear invocation interruption", function () {
  380. index.clearInterrupter(successCB, failCB);
  381. expect(mockedApplication.invocation.interrupter).toEqual(undefined);
  382. });
  383. });
  384. });
  385. });