PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/ext/system/index.js

https://github.com/jcarty/BB10-WebWorks-Framework
JavaScript | 572 lines | 469 code | 86 blank | 17 comment | 1 complexity | 57a73da56975ebafc6131136f4fb41f8 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 libDir = __dirname + "./../../../../lib/",
  17. extDir = __dirname + "./../../../../ext/",
  18. ID = "system",
  19. apiDir = extDir + ID + "/",
  20. Whitelist = require(libDir + "policy/whitelist").Whitelist,
  21. events = require(libDir + "event"),
  22. eventExt = require(extDir + "event/index"),
  23. utils = require(libDir + "utils"),
  24. sysIndex,
  25. successCB,
  26. failCB;
  27. describe("system index", function () {
  28. beforeEach(function () {
  29. sysIndex = require(apiDir + "index");
  30. });
  31. afterEach(function () {
  32. delete GLOBAL.window;
  33. sysIndex = null;
  34. });
  35. it("hasPermission", function () {
  36. var success = jasmine.createSpy(),
  37. env = {
  38. "request": {
  39. "origin": "blah"
  40. },
  41. "response": {
  42. }
  43. };
  44. spyOn(Whitelist.prototype, "isFeatureAllowed").andReturn(true);
  45. sysIndex.hasPermission(success, undefined, {"module": "blackberry.system"}, env);
  46. expect(Whitelist.prototype.isFeatureAllowed).toHaveBeenCalled();
  47. expect(success).toHaveBeenCalledWith(0);
  48. });
  49. it("hasCapability", function () {
  50. var success = jasmine.createSpy();
  51. sysIndex.hasCapability(success, undefined, {"capability": "network.wlan"}, undefined);
  52. expect(success).toHaveBeenCalledWith(true);
  53. });
  54. describe("qnx.webplatform.device properties", function () {
  55. beforeEach(function () {
  56. sysIndex = require(apiDir + "index");
  57. GLOBAL.window = {
  58. qnx: {
  59. webplatform: {
  60. device: {
  61. }
  62. }
  63. }
  64. };
  65. });
  66. afterEach(function () {
  67. delete GLOBAL.window;
  68. sysIndex = null;
  69. });
  70. it("can call fail if a property isn't present", function () {
  71. ["hardwareid", "scmbundle", "devicename"].forEach(function (propertyName) {
  72. var fail = jasmine.createSpy();
  73. sysIndex.getDeviceProperties(null, fail, null, null);
  74. expect(fail).toHaveBeenCalledWith(-1, jasmine.any(String));
  75. });
  76. });
  77. it("can call success with getDeviceProperties", function () {
  78. var success = jasmine.createSpy(),
  79. fail = jasmine.createSpy(),
  80. hardwareId = "0x8500240a",
  81. softwareVersion = "10.0.6.99",
  82. name = "Device";
  83. window.qnx.webplatform.device.hardwareId = hardwareId;
  84. window.qnx.webplatform.device.scmBundle = softwareVersion;
  85. window.qnx.webplatform.device.deviceName = name;
  86. sysIndex.getDeviceProperties(success, fail, null, null);
  87. expect(success).toHaveBeenCalledWith({
  88. "hardwareId" : hardwareId,
  89. "softwareVersion" : softwareVersion,
  90. "name": name
  91. });
  92. });
  93. });
  94. describe("device region", function () {
  95. var mockApplication;
  96. beforeEach(function () {
  97. sysIndex = require(apiDir + "index");
  98. mockApplication = {};
  99. GLOBAL.window = {
  100. qnx: {
  101. webplatform: {
  102. getApplication: jasmine.createSpy().andReturn(mockApplication)
  103. }
  104. }
  105. };
  106. });
  107. afterEach(function () {
  108. delete GLOBAL.window;
  109. sysIndex = null;
  110. });
  111. it("calls success when there is no error retrieving data", function () {
  112. var success = jasmine.createSpy(),
  113. fail = jasmine.createSpy();
  114. mockApplication.systemRegion = (new Date()).getTime();
  115. sysIndex.region(success, fail);
  116. expect(success).toHaveBeenCalledWith(window.qnx.webplatform.getApplication().systemRegion);
  117. expect(fail).not.toHaveBeenCalled();
  118. });
  119. it("calls fail when there is an error", function () {
  120. var success = jasmine.createSpy(),
  121. fail = jasmine.createSpy(),
  122. errMsg = "Something bad happened";
  123. Object.defineProperty(mockApplication, "systemRegion", {
  124. get: function () {
  125. throw new Error(errMsg);
  126. }
  127. });
  128. sysIndex.region(success, fail);
  129. expect(success).not.toHaveBeenCalled();
  130. expect(fail).toHaveBeenCalledWith(-1, errMsg);
  131. });
  132. });
  133. describe("languagechanged event", function () {
  134. var appEvents;
  135. beforeEach(function () {
  136. spyOn(utils, "loadExtensionModule").andCallFake(function () {
  137. return eventExt;
  138. });
  139. appEvents = require(libDir + "events/applicationEvents");
  140. });
  141. it("responds to 'languagechanged' events", function () {
  142. var clientEventName = "languagechanged",
  143. eventName = "systemLanguageChange",
  144. args = {eventName : encodeURIComponent(clientEventName)};
  145. spyOn(events, "add");
  146. sysIndex.registerEvents(jasmine.createSpy());
  147. eventExt.add(null, null, args);
  148. expect(events.add).toHaveBeenCalledWith({
  149. context: appEvents,
  150. event: eventName,
  151. trigger: jasmine.any(Function)
  152. });
  153. });
  154. it("removes 'languagechanged' event", function () {
  155. var clientEventName = "languagechanged",
  156. eventName = "systemLanguageChange",
  157. args = {eventName : encodeURIComponent(clientEventName)};
  158. spyOn(events, "remove");
  159. eventExt.remove(null, null, args);
  160. expect(events.remove).toHaveBeenCalledWith({
  161. context: appEvents,
  162. event: eventName,
  163. trigger: jasmine.any(Function)
  164. });
  165. });
  166. });
  167. describe("regionchanged event", function () {
  168. var appEvents;
  169. beforeEach(function () {
  170. spyOn(utils, "loadExtensionModule").andCallFake(function () {
  171. return eventExt;
  172. });
  173. appEvents = require(libDir + "events/applicationEvents");
  174. });
  175. it("responds to 'regionchanged' events", function () {
  176. var clientEventName = "regionchanged",
  177. eventName = "systemRegionChange",
  178. args = {eventName : encodeURIComponent(clientEventName)};
  179. spyOn(events, "add");
  180. sysIndex.registerEvents(jasmine.createSpy());
  181. eventExt.add(null, null, args);
  182. expect(events.add).toHaveBeenCalledWith({
  183. context: appEvents,
  184. event: eventName,
  185. trigger: jasmine.any(Function)
  186. });
  187. });
  188. it("removes 'regionchanged' event", function () {
  189. var clientEventName = "regionchanged",
  190. eventName = "systemRegionChange",
  191. args = {eventName : encodeURIComponent(clientEventName)};
  192. spyOn(events, "remove");
  193. eventExt.remove(null, null, args);
  194. expect(events.remove).toHaveBeenCalledWith({
  195. context: appEvents,
  196. event: eventName,
  197. trigger: jasmine.any(Function)
  198. });
  199. });
  200. });
  201. describe("batterystatus event", function () {
  202. var deviceEvents;
  203. beforeEach(function () {
  204. spyOn(utils, "loadExtensionModule").andCallFake(function () {
  205. return eventExt;
  206. });
  207. deviceEvents = require(libDir + "events/deviceEvents");
  208. });
  209. it("responds to 'batterystatus' events", function () {
  210. var clientEventName = "batterystatus",
  211. eventName = "battery.statusChange",
  212. args = { eventName : encodeURIComponent(clientEventName) };
  213. spyOn(events, "add");
  214. sysIndex.registerEvents(jasmine.createSpy());
  215. eventExt.add(null, null, args);
  216. expect(events.add).toHaveBeenCalledWith({
  217. context: deviceEvents,
  218. event: eventName,
  219. trigger: jasmine.any(Function)
  220. });
  221. });
  222. it("removes 'batterystatus' event", function () {
  223. var clientEventName = "batterystatus",
  224. eventName = "battery.statusChange",
  225. args = {eventName : encodeURIComponent(clientEventName)};
  226. spyOn(events, "remove");
  227. eventExt.remove(null, null, args);
  228. expect(events.remove).toHaveBeenCalledWith({
  229. context: deviceEvents,
  230. event: eventName,
  231. trigger: jasmine.any(Function)
  232. });
  233. });
  234. });
  235. describe("batterylow event", function () {
  236. var deviceEvents;
  237. beforeEach(function () {
  238. spyOn(utils, "loadExtensionModule").andCallFake(function () {
  239. return eventExt;
  240. });
  241. deviceEvents = require(libDir + "events/deviceEvents");
  242. });
  243. it("responds to 'batterylow' events", function () {
  244. var clientEventName = "batterylow",
  245. eventName = "battery.chargeLow",
  246. args = { eventName : encodeURIComponent(clientEventName) };
  247. spyOn(events, "add");
  248. sysIndex.registerEvents(jasmine.createSpy());
  249. eventExt.add(null, null, args);
  250. expect(events.add).toHaveBeenCalledWith({
  251. context: deviceEvents,
  252. event: eventName,
  253. trigger: jasmine.any(Function)
  254. });
  255. });
  256. it("removes 'batterylow' event", function () {
  257. var clientEventName = "batterylow",
  258. eventName = "battery.chargeLow",
  259. args = {eventName : encodeURIComponent(clientEventName)};
  260. spyOn(events, "remove");
  261. eventExt.remove(null, null, args);
  262. expect(events.remove).toHaveBeenCalledWith({
  263. context: deviceEvents,
  264. event: eventName,
  265. trigger: jasmine.any(Function)
  266. });
  267. });
  268. });
  269. describe("batterycritical event", function () {
  270. var deviceEvents;
  271. beforeEach(function () {
  272. spyOn(utils, "loadExtensionModule").andCallFake(function () {
  273. return eventExt;
  274. });
  275. deviceEvents = require(libDir + "events/deviceEvents");
  276. });
  277. it("responds to 'batterycritical' events", function () {
  278. var clientEventName = "batterycritical",
  279. eventName = "battery.chargeCritical",
  280. args = { eventName : encodeURIComponent(clientEventName) };
  281. spyOn(events, "add");
  282. sysIndex.registerEvents(jasmine.createSpy());
  283. eventExt.add(null, null, args);
  284. expect(events.add).toHaveBeenCalledWith({
  285. context: deviceEvents,
  286. event: eventName,
  287. trigger: jasmine.any(Function)
  288. });
  289. });
  290. it("removes 'batterycritical' event", function () {
  291. var clientEventName = "batterycritical",
  292. eventName = "battery.chargeCritical",
  293. args = {eventName : encodeURIComponent(clientEventName)};
  294. spyOn(events, "remove");
  295. eventExt.remove(null, null, args);
  296. expect(events.remove).toHaveBeenCalledWith({
  297. context: deviceEvents,
  298. event: eventName,
  299. trigger: jasmine.any(Function)
  300. });
  301. });
  302. });
  303. describe("font", function () {
  304. describe("font methods", function () {
  305. var fontFamily = "courier",
  306. fontSize = 10,
  307. mockedFontFamily,
  308. mockedFontSize,
  309. ERROR_ID = -1;
  310. beforeEach(function () {
  311. successCB = jasmine.createSpy("Success Callback");
  312. failCB = jasmine.createSpy("Fail Callback");
  313. mockedFontFamily = jasmine.createSpy("getSystemFontFamily").andReturn(fontFamily);
  314. mockedFontSize = jasmine.createSpy("getSystemFontSize").andReturn(fontSize);
  315. GLOBAL.window = GLOBAL;
  316. GLOBAL.window.qnx = {
  317. webplatform: {
  318. getApplication: function () {
  319. return {
  320. getSystemFontFamily: mockedFontFamily,
  321. getSystemFontSize: mockedFontSize
  322. };
  323. }
  324. }
  325. };
  326. });
  327. afterEach(function () {
  328. delete GLOBAL.window;
  329. successCB = null;
  330. failCB = null;
  331. mockedFontFamily = null;
  332. mockedFontSize = null;
  333. });
  334. it("can call fontFamily and fontSize the qnx.weblplatform Application", function () {
  335. sysIndex.getFontInfo(successCB, null, null, null);
  336. expect(mockedFontFamily).toHaveBeenCalled();
  337. expect(mockedFontSize).toHaveBeenCalled();
  338. });
  339. it("can call success callback when getFontInfo call succeed", function () {
  340. sysIndex.getFontInfo(successCB, failCB, null, null);
  341. expect(successCB).toHaveBeenCalledWith({'fontFamily': fontFamily, 'fontSize': fontSize});
  342. expect(failCB).not.toHaveBeenCalled();
  343. });
  344. it("can call fail callback when getFontInfo call failed", function () {
  345. sysIndex.getFontInfo(null, failCB, null, null);
  346. expect(successCB).not.toHaveBeenCalledWith({'fontFamily': fontFamily, 'fontSize': fontSize});
  347. expect(failCB).toHaveBeenCalledWith(ERROR_ID, jasmine.any(Object));
  348. });
  349. });
  350. describe("fontchanged event", function () {
  351. var appEvents;
  352. beforeEach(function () {
  353. spyOn(utils, "loadExtensionModule").andCallFake(function () {
  354. return eventExt;
  355. });
  356. appEvents = require(libDir + "events/applicationEvents");
  357. });
  358. it("responds to 'fontchanged' events", function () {
  359. var eventName = "fontchanged",
  360. args = {eventName : encodeURIComponent(eventName)};
  361. spyOn(events, "add");
  362. sysIndex.registerEvents(jasmine.createSpy());
  363. eventExt.add(null, null, args);
  364. expect(events.add).toHaveBeenCalledWith({
  365. context: appEvents,
  366. event: eventName,
  367. trigger: jasmine.any(Function)
  368. });
  369. });
  370. it("removes 'fontchanged' event", function () {
  371. var eventName = "fontchanged",
  372. args = {eventName : encodeURIComponent(eventName)};
  373. spyOn(events, "remove");
  374. eventExt.remove(null, null, args);
  375. expect(events.remove).toHaveBeenCalledWith({
  376. context: appEvents,
  377. event: eventName,
  378. trigger: jasmine.any(Function)
  379. });
  380. });
  381. });
  382. });
  383. describe("getCurrentTimezone", function () {
  384. beforeEach(function () {
  385. GLOBAL.window = {
  386. qnx: {
  387. webplatform: {
  388. device: {
  389. timezone: "hello123"
  390. }
  391. }
  392. }
  393. };
  394. });
  395. it("return timezone from PPS", function () {
  396. var successCb = jasmine.createSpy();
  397. sysIndex.getCurrentTimezone(successCb);
  398. expect(successCb).toHaveBeenCalledWith("hello123");
  399. });
  400. });
  401. describe("getTimezones", function () {
  402. beforeEach(function () {
  403. GLOBAL.window = {
  404. qnx: {
  405. webplatform: {
  406. device: {
  407. getTimezones: jasmine.createSpy().andCallFake(function (callback) {
  408. callback(["America/New_York", "America/Los_Angeles"]);
  409. })
  410. }
  411. }
  412. }
  413. };
  414. });
  415. it("return timezones from native", function () {
  416. var successCb = jasmine.createSpy();
  417. sysIndex.getTimezones(successCb);
  418. expect(successCb).toHaveBeenCalledWith(["America/New_York", "America/Los_Angeles"]);
  419. });
  420. });
  421. describe("setWallpaper", function () {
  422. var mockApplication;
  423. beforeEach(function () {
  424. mockApplication = {};
  425. mockApplication.newWallpaper = jasmine.createSpy("newWallpaper method");
  426. mockApplication.getEnv = function (envName) {
  427. if (envName === "HOME") {
  428. return "/accounts/1000/appdata/data";
  429. }
  430. };
  431. GLOBAL.window = {
  432. qnx: {
  433. webplatform: {
  434. getApplication: jasmine.createSpy().andReturn(mockApplication)
  435. }
  436. }
  437. };
  438. successCB = jasmine.createSpy("Success Callback");
  439. failCB = jasmine.createSpy("Fail Callback");
  440. });
  441. afterEach(function () {
  442. mockApplication.newWallpaper = null;
  443. mockApplication = null;
  444. delete GLOBAL.window;
  445. successCB = null;
  446. failCB = null;
  447. });
  448. it("calls setWallpaper with success callback at the end for NOT local path", function () {
  449. var filePath = "/accounts/1000/shared/camera/IMG_00000001.jpg",
  450. request = {wallpaper: encodeURIComponent(JSON.stringify(filePath))};
  451. sysIndex.setWallpaper(successCB, failCB, request);
  452. expect(mockApplication.newWallpaper).toHaveBeenCalledWith(filePath);
  453. expect(successCB).toHaveBeenCalled();
  454. expect(failCB).not.toHaveBeenCalled();
  455. });
  456. it("calls setWallpaper with success callback at the end for local path", function () {
  457. var imageName = "IMG_00000001.jpg",
  458. localPath = "local:///" + imageName,
  459. request = {wallpaper: encodeURIComponent(JSON.stringify(localPath))},
  460. tranlatedPath;
  461. sysIndex.setWallpaper(successCB, failCB, request);
  462. tranlatedPath = mockApplication.newWallpaper.mostRecentCall.args[0];
  463. // Checking if the image name is at the end of translated path
  464. expect(tranlatedPath.indexOf(imageName)).toEqual(tranlatedPath.length - imageName.length);
  465. expect(successCB).toHaveBeenCalled();
  466. expect(failCB).not.toHaveBeenCalled();
  467. });
  468. it("calls setWallpaper with path no prefixed 'file://' for NOT local path", function () {
  469. var filePathPrefix = "file://",
  470. filePath = "/accounts/1000/shared/camera/IMG_00000001.jpg",
  471. request = {wallpaper: encodeURIComponent(JSON.stringify(filePathPrefix + filePath))};
  472. sysIndex.setWallpaper(successCB, failCB, request);
  473. expect(mockApplication.newWallpaper).toHaveBeenCalledWith(filePath);
  474. });
  475. it("calls setWallpaper with path no prefixed 'file://' for local path", function () {
  476. var excludedPrefix = "file://",
  477. localPath = "local:///accounts/1000/shared/camera/IMG_00000001.jpg",
  478. request = {wallpaper: encodeURIComponent(JSON.stringify(localPath))},
  479. tranlatedPath;
  480. sysIndex.setWallpaper(successCB, failCB, request);
  481. tranlatedPath = mockApplication.newWallpaper.mostRecentCall.args[0];
  482. // Checking the tranlated path not prefixed with 'file://'
  483. expect(tranlatedPath.indexOf(excludedPrefix)).toEqual(-1);
  484. });
  485. });
  486. });