PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/ext/io.filetransfer/index.js

https://github.com/jcarty/BB10-WebWorks-Framework
JavaScript | 185 lines | 147 code | 23 blank | 15 comment | 0 complexity | c1e6e857dcab467f7159942dc408f55c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MIT
  1. /*
  2. * Copyright 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 root = __dirname + "/../../../../",
  17. webview = require(root + "lib/webview"),
  18. index;
  19. describe("io.filetransfer index", function () {
  20. beforeEach(function () {
  21. GLOBAL.JNEXT = {
  22. require: jasmine.createSpy().andReturn(true),
  23. createObject: jasmine.createSpy().andReturn("0"),
  24. registerEvents: jasmine.createSpy(),
  25. invoke: jasmine.createSpy()
  26. };
  27. spyOn(webview, "windowGroup").andReturn(42);
  28. index = require(root + "ext/io.filetransfer/index");
  29. });
  30. afterEach(function () {
  31. GLOBAL.JNEXT = null;
  32. index = null;
  33. });
  34. it("makes sure JNEXT was not initialized on require", function () {
  35. expect(JNEXT.require).not.toHaveBeenCalledWith("libfiletransfer");
  36. expect(JNEXT.createObject).not.toHaveBeenCalledWith("libfiletransfer.FileTransfer");
  37. });
  38. describe("filetransfer upload", function () {
  39. it("should call JNEXT.invoke with custom params", function () {
  40. var mocked_args = {
  41. "_eventId": encodeURIComponent(JSON.stringify("1")),
  42. "filePath": encodeURIComponent(JSON.stringify("2")),
  43. "server": encodeURIComponent(JSON.stringify("3")),
  44. "options": encodeURIComponent(JSON.stringify({
  45. "fileKey": "test",
  46. "fileName": "test.gif",
  47. "mimeType": "image/gif",
  48. "params": { "test": "test" },
  49. "chunkedMode": false,
  50. "chunkSize": 512
  51. }))
  52. },
  53. expected_args = {
  54. "_eventId": "1",
  55. "filePath": "2",
  56. "server": "3",
  57. "options": {
  58. "fileKey": "test",
  59. "fileName": "test.gif",
  60. "mimeType": "image/gif",
  61. "params": { "test": "test" },
  62. "chunkedMode": false,
  63. "chunkSize": 512,
  64. "windowGroup" : 42
  65. }
  66. },
  67. successCB = jasmine.createSpy(),
  68. failCB = jasmine.createSpy();
  69. index.upload(successCB, failCB, mocked_args, null);
  70. expect(JNEXT.invoke).toHaveBeenCalledWith("0", "upload " + JSON.stringify(expected_args));
  71. expect(successCB).toHaveBeenCalled();
  72. expect(failCB).not.toHaveBeenCalled();
  73. });
  74. it("should call JNEXT.invoke with default params", function () {
  75. var mocked_args = {
  76. "_eventId": encodeURIComponent(JSON.stringify("1")),
  77. "filePath": encodeURIComponent(JSON.stringify("2")),
  78. "server": encodeURIComponent(JSON.stringify("3"))
  79. },
  80. expected_default_args = {
  81. "_eventId": "1",
  82. "filePath": "2",
  83. "server": "3",
  84. "options": {
  85. "fileKey": "file",
  86. "fileName": "image.jpg",
  87. "mimeType": "image/jpeg",
  88. "params": {},
  89. "chunkedMode": true,
  90. "chunkSize": 1024,
  91. "windowGroup" : 42
  92. }
  93. },
  94. successCB = jasmine.createSpy(),
  95. failCB = jasmine.createSpy();
  96. index.upload(successCB, failCB, mocked_args, null);
  97. expect(JNEXT.invoke).toHaveBeenCalledWith("0", "upload " + JSON.stringify(expected_default_args));
  98. expect(successCB).toHaveBeenCalled();
  99. expect(failCB).not.toHaveBeenCalled();
  100. });
  101. it("should call failure callback with null parameters", function () {
  102. var mocked_args = {
  103. "_eventId": encodeURIComponent(JSON.stringify("1")),
  104. "filePath": encodeURIComponent(JSON.stringify("")),
  105. "server": encodeURIComponent(JSON.stringify(""))
  106. },
  107. successCB = jasmine.createSpy(),
  108. failCB = jasmine.createSpy();
  109. index.upload(successCB, failCB, mocked_args, null);
  110. expect(successCB).not.toHaveBeenCalled();
  111. expect(failCB).toHaveBeenCalled();
  112. });
  113. it("should fail if chunkSize is not positive", function () {
  114. var mocked_args = {
  115. "_eventId": encodeURIComponent(JSON.stringify("1")),
  116. "filePath": encodeURIComponent(JSON.stringify("2")),
  117. "server": encodeURIComponent(JSON.stringify("3")),
  118. "options": encodeURIComponent(JSON.stringify({
  119. "chunkSize": -1
  120. }))
  121. },
  122. successCB = jasmine.createSpy(),
  123. failCB = jasmine.createSpy();
  124. index.upload(successCB, failCB, mocked_args, null);
  125. expect(successCB).not.toHaveBeenCalled();
  126. expect(failCB).toHaveBeenCalled();
  127. });
  128. });
  129. describe("filetransfer download", function () {
  130. it("should call JNEXT.invoke", function () {
  131. var mocked_args = {
  132. "_eventId": encodeURIComponent(JSON.stringify("1")),
  133. "source": encodeURIComponent(JSON.stringify("2")),
  134. "target": encodeURIComponent(JSON.stringify("3"))
  135. },
  136. expected_args = {
  137. "_eventId": "1",
  138. "source": "2",
  139. "target": "3",
  140. "windowGroup": 42
  141. },
  142. successCB = jasmine.createSpy(),
  143. failCB = jasmine.createSpy();
  144. index.download(successCB, failCB, mocked_args, null);
  145. expect(JNEXT.invoke).toHaveBeenCalledWith("0", "download " + JSON.stringify(expected_args));
  146. expect(successCB).toHaveBeenCalled();
  147. expect(failCB).not.toHaveBeenCalled();
  148. });
  149. it("should call failure callback with null parameters", function () {
  150. var mocked_args = {
  151. "_eventId": encodeURIComponent(JSON.stringify("1")),
  152. "filePath": encodeURIComponent(JSON.stringify("")),
  153. "server": encodeURIComponent(JSON.stringify(""))
  154. },
  155. successCB = jasmine.createSpy(),
  156. failCB = jasmine.createSpy();
  157. index.download(successCB, failCB, mocked_args, null);
  158. expect(successCB).not.toHaveBeenCalled();
  159. expect(failCB).toHaveBeenCalled();
  160. });
  161. });
  162. });