2
0

util_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  24. var _util = require('../../shared/util');
  25. var _primitives = require('../../core/primitives');
  26. var _test_utils = require('./test_utils');
  27. describe('util', function () {
  28. describe('bytesToString', function () {
  29. it('handles non-array arguments', function () {
  30. expect(function () {
  31. (0, _util.bytesToString)(null);
  32. }).toThrow(new Error('Invalid argument for bytesToString'));
  33. });
  34. it('handles array arguments with a length not exceeding the maximum', function () {
  35. expect((0, _util.bytesToString)(new Uint8Array([]))).toEqual('');
  36. expect((0, _util.bytesToString)(new Uint8Array([102, 111, 111]))).toEqual('foo');
  37. });
  38. it('handles array arguments with a length exceeding the maximum', function () {
  39. var length = 10000;
  40. var bytes = new Uint8Array(length);
  41. for (var i = 0; i < length; i++) {
  42. bytes[i] = 'a'.charCodeAt(0);
  43. }
  44. var string = Array(length + 1).join('a');
  45. expect((0, _util.bytesToString)(bytes)).toEqual(string);
  46. });
  47. });
  48. describe('getInheritableProperty', function () {
  49. it('handles non-dictionary arguments', function () {
  50. expect((0, _util.getInheritableProperty)({
  51. dict: null,
  52. key: 'foo'
  53. })).toEqual(undefined);
  54. expect((0, _util.getInheritableProperty)({
  55. dict: undefined,
  56. key: 'foo'
  57. })).toEqual(undefined);
  58. });
  59. it('handles dictionaries that do not contain the property', function () {
  60. var emptyDict = new _primitives.Dict();
  61. expect((0, _util.getInheritableProperty)({
  62. dict: emptyDict,
  63. key: 'foo'
  64. })).toEqual(undefined);
  65. var filledDict = new _primitives.Dict();
  66. filledDict.set('bar', 'baz');
  67. expect((0, _util.getInheritableProperty)({
  68. dict: filledDict,
  69. key: 'foo'
  70. })).toEqual(undefined);
  71. });
  72. it('fetches the property if it is not inherited', function () {
  73. var ref = new _primitives.Ref(10, 0);
  74. var xref = new _test_utils.XRefMock([{
  75. ref: ref,
  76. data: 'quux'
  77. }]);
  78. var dict = new _primitives.Dict(xref);
  79. dict.set('foo', 'bar');
  80. expect((0, _util.getInheritableProperty)({
  81. dict: dict,
  82. key: 'foo'
  83. })).toEqual('bar');
  84. dict.set('baz', ['qux', ref]);
  85. expect((0, _util.getInheritableProperty)({
  86. dict: dict,
  87. key: 'baz',
  88. getArray: true
  89. })).toEqual(['qux', 'quux']);
  90. });
  91. it('fetches the property if it is inherited and present on one level', function () {
  92. var ref = new _primitives.Ref(10, 0);
  93. var xref = new _test_utils.XRefMock([{
  94. ref: ref,
  95. data: 'quux'
  96. }]);
  97. var firstDict = new _primitives.Dict(xref);
  98. var secondDict = new _primitives.Dict(xref);
  99. firstDict.set('Parent', secondDict);
  100. secondDict.set('foo', 'bar');
  101. expect((0, _util.getInheritableProperty)({
  102. dict: firstDict,
  103. key: 'foo'
  104. })).toEqual('bar');
  105. secondDict.set('baz', ['qux', ref]);
  106. expect((0, _util.getInheritableProperty)({
  107. dict: firstDict,
  108. key: 'baz',
  109. getArray: true
  110. })).toEqual(['qux', 'quux']);
  111. });
  112. it('fetches the property if it is inherited and present on multiple levels', function () {
  113. var ref = new _primitives.Ref(10, 0);
  114. var xref = new _test_utils.XRefMock([{
  115. ref: ref,
  116. data: 'quux'
  117. }]);
  118. var firstDict = new _primitives.Dict(xref);
  119. var secondDict = new _primitives.Dict(xref);
  120. firstDict.set('Parent', secondDict);
  121. firstDict.set('foo', 'bar1');
  122. secondDict.set('foo', 'bar2');
  123. expect((0, _util.getInheritableProperty)({
  124. dict: firstDict,
  125. key: 'foo'
  126. })).toEqual('bar1');
  127. expect((0, _util.getInheritableProperty)({
  128. dict: firstDict,
  129. key: 'foo',
  130. getArray: false,
  131. stopWhenFound: false
  132. })).toEqual(['bar1', 'bar2']);
  133. firstDict.set('baz', ['qux1', ref]);
  134. secondDict.set('baz', ['qux2', ref]);
  135. expect((0, _util.getInheritableProperty)({
  136. dict: firstDict,
  137. key: 'baz',
  138. getArray: true,
  139. stopWhenFound: false
  140. })).toEqual([['qux1', 'quux'], ['qux2', 'quux']]);
  141. });
  142. it('stops searching when the loop limit is reached', function () {
  143. var dict = new _primitives.Dict();
  144. var currentDict = dict;
  145. var parentDict = null;
  146. for (var i = 0; i < 150; i++) {
  147. parentDict = new _primitives.Dict();
  148. currentDict.set('Parent', parentDict);
  149. currentDict = parentDict;
  150. }
  151. parentDict.set('foo', 'bar');
  152. expect((0, _util.getInheritableProperty)({
  153. dict: dict,
  154. key: 'foo'
  155. })).toEqual(undefined);
  156. dict.set('foo', 'baz');
  157. expect((0, _util.getInheritableProperty)({
  158. dict: dict,
  159. key: 'foo',
  160. getArray: false,
  161. stopWhenFound: false
  162. })).toEqual(['baz']);
  163. });
  164. });
  165. describe('isArrayBuffer', function () {
  166. it('handles array buffer values', function () {
  167. expect((0, _util.isArrayBuffer)(new ArrayBuffer(0))).toEqual(true);
  168. expect((0, _util.isArrayBuffer)(new Uint8Array(0))).toEqual(true);
  169. });
  170. it('handles non-array buffer values', function () {
  171. expect((0, _util.isArrayBuffer)('true')).toEqual(false);
  172. expect((0, _util.isArrayBuffer)(1)).toEqual(false);
  173. expect((0, _util.isArrayBuffer)(null)).toEqual(false);
  174. expect((0, _util.isArrayBuffer)(undefined)).toEqual(false);
  175. });
  176. });
  177. describe('isBool', function () {
  178. it('handles boolean values', function () {
  179. expect((0, _util.isBool)(true)).toEqual(true);
  180. expect((0, _util.isBool)(false)).toEqual(true);
  181. });
  182. it('handles non-boolean values', function () {
  183. expect((0, _util.isBool)('true')).toEqual(false);
  184. expect((0, _util.isBool)('false')).toEqual(false);
  185. expect((0, _util.isBool)(1)).toEqual(false);
  186. expect((0, _util.isBool)(0)).toEqual(false);
  187. expect((0, _util.isBool)(null)).toEqual(false);
  188. expect((0, _util.isBool)(undefined)).toEqual(false);
  189. });
  190. });
  191. describe('isEmptyObj', function () {
  192. it('handles empty objects', function () {
  193. expect((0, _util.isEmptyObj)({})).toEqual(true);
  194. });
  195. it('handles non-empty objects', function () {
  196. expect((0, _util.isEmptyObj)({ foo: 'bar' })).toEqual(false);
  197. });
  198. });
  199. describe('isNum', function () {
  200. it('handles numeric values', function () {
  201. expect((0, _util.isNum)(1)).toEqual(true);
  202. expect((0, _util.isNum)(0)).toEqual(true);
  203. expect((0, _util.isNum)(-1)).toEqual(true);
  204. expect((0, _util.isNum)(1000000000000000000)).toEqual(true);
  205. expect((0, _util.isNum)(12.34)).toEqual(true);
  206. });
  207. it('handles non-numeric values', function () {
  208. expect((0, _util.isNum)('true')).toEqual(false);
  209. expect((0, _util.isNum)(true)).toEqual(false);
  210. expect((0, _util.isNum)(null)).toEqual(false);
  211. expect((0, _util.isNum)(undefined)).toEqual(false);
  212. });
  213. });
  214. describe('isSpace', function () {
  215. it('handles space characters', function () {
  216. expect((0, _util.isSpace)(0x20)).toEqual(true);
  217. expect((0, _util.isSpace)(0x09)).toEqual(true);
  218. expect((0, _util.isSpace)(0x0D)).toEqual(true);
  219. expect((0, _util.isSpace)(0x0A)).toEqual(true);
  220. });
  221. it('handles non-space characters', function () {
  222. expect((0, _util.isSpace)(0x0B)).toEqual(false);
  223. expect((0, _util.isSpace)(null)).toEqual(false);
  224. expect((0, _util.isSpace)(undefined)).toEqual(false);
  225. });
  226. });
  227. describe('isString', function () {
  228. it('handles string values', function () {
  229. expect((0, _util.isString)('foo')).toEqual(true);
  230. expect((0, _util.isString)('')).toEqual(true);
  231. });
  232. it('handles non-string values', function () {
  233. expect((0, _util.isString)(true)).toEqual(false);
  234. expect((0, _util.isString)(1)).toEqual(false);
  235. expect((0, _util.isString)(null)).toEqual(false);
  236. expect((0, _util.isString)(undefined)).toEqual(false);
  237. });
  238. });
  239. describe('log2', function () {
  240. it('handles values smaller than/equal to zero', function () {
  241. expect((0, _util.log2)(0)).toEqual(0);
  242. expect((0, _util.log2)(-1)).toEqual(0);
  243. });
  244. it('handles values larger than zero', function () {
  245. expect((0, _util.log2)(1)).toEqual(0);
  246. expect((0, _util.log2)(2)).toEqual(1);
  247. expect((0, _util.log2)(3)).toEqual(2);
  248. expect((0, _util.log2)(3.14)).toEqual(2);
  249. });
  250. });
  251. describe('stringToBytes', function () {
  252. it('handles non-string arguments', function () {
  253. expect(function () {
  254. (0, _util.stringToBytes)(null);
  255. }).toThrow(new Error('Invalid argument for stringToBytes'));
  256. });
  257. it('handles string arguments', function () {
  258. expect((0, _util.stringToBytes)('')).toEqual(new Uint8Array([]));
  259. expect((0, _util.stringToBytes)('foo')).toEqual(new Uint8Array([102, 111, 111]));
  260. });
  261. });
  262. describe('stringToPDFString', function () {
  263. it('handles ISO Latin 1 strings', function () {
  264. var str = '\x8Dstring\x8E';
  265. expect((0, _util.stringToPDFString)(str)).toEqual('\u201Cstring\u201D');
  266. });
  267. it('handles UTF-16BE strings', function () {
  268. var str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
  269. expect((0, _util.stringToPDFString)(str)).toEqual('string');
  270. });
  271. it('handles empty strings', function () {
  272. var str1 = '';
  273. expect((0, _util.stringToPDFString)(str1)).toEqual('');
  274. var str2 = '\xFE\xFF';
  275. expect((0, _util.stringToPDFString)(str2)).toEqual('');
  276. });
  277. });
  278. describe('removeNullCharacters', function () {
  279. it('should not modify string without null characters', function () {
  280. var str = 'string without null chars';
  281. expect((0, _util.removeNullCharacters)(str)).toEqual('string without null chars');
  282. });
  283. it('should modify string with null characters', function () {
  284. var str = 'string\x00With\x00Null\x00Chars';
  285. expect((0, _util.removeNullCharacters)(str)).toEqual('stringWithNullChars');
  286. });
  287. });
  288. describe('ReadableStream', function () {
  289. it('should return an Object', function () {
  290. var readable = new _util.ReadableStream();
  291. expect(typeof readable === 'undefined' ? 'undefined' : _typeof(readable)).toEqual('object');
  292. });
  293. it('should have property getReader', function () {
  294. var readable = new _util.ReadableStream();
  295. expect(_typeof(readable.getReader)).toEqual('function');
  296. });
  297. });
  298. });