• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Treat two distinct objects with the same members as equal, e.g. in JS
6// ([0, 1] == [0, 1]) evaluates to false. Without this function, we can't
7// supply an expected result of object/array type for our tests.
8function kindaSortaEqual(arg1, arg2) {
9  if (arg1 == arg2) {
10    return true;
11  }
12  if (typeof arg1 != "object") {
13    return false;
14  }
15  if (typeof arg2 != "object") {
16    return false;
17  }
18  return arg1.toString() == arg2.toString();
19}
20
21function testReadProperty(that, prop, expected) {
22  try {
23    var actual = that[prop];
24    if (kindaSortaEqual(actual, expected)) {
25      app.alert('PASS: ' + prop + ' = ' + actual);
26    } else {
27      app.alert('FAIL: ' + prop + ' = ' + actual + ', expected = ' + expected);
28    }
29  } catch (e) {
30    app.alert('ERROR: ' + e.toString());
31  }
32}
33
34function testUnreadableProperty(that, prop) {
35  try {
36    var value = that[prop];
37    app.alert('FAIL: ' + prop + ', expected to throw');
38  } catch (e) {
39    app.alert('PASS: ' + prop + ' threw ' + e.toString());
40  }
41}
42
43function testWriteProperty(that, prop, newValue) {
44  try {
45    that[prop] = newValue;
46    var actual = that[prop];
47    if (kindaSortaEqual(actual, newValue)) {
48      app.alert('PASS: ' + prop + ' = ' + actual);
49    } else {
50      app.alert('FAIL: ' + prop + ' = ' + actual + ', expected = ' + newValue);
51    }
52  } catch (e) {
53    app.alert('ERROR: ' + e.toString());
54  }
55}
56
57function testWriteIgnoredProperty(that, prop, expected, newValue) {
58  try {
59    that[prop] = newValue;
60    var actual = that[prop];
61    if (kindaSortaEqual(actual, expected)) {
62      app.alert('PASS: ' + prop + ' = ' + actual);
63    } else {
64      app.alert('FAIL: ' + prop + ' = ' + actual + ', expected = ' + expected);
65    }
66  } catch (e) {
67    app.alert('ERROR: ' + e.toString());
68  }
69}
70
71function testUnwritableProperty(that, prop, newValue) {
72  try {
73    that[prop] = newValue;
74    app.alert('FAIL: ' + prop + ' = ' + newValue + ', expected to throw');
75  } catch (e) {
76    app.alert('PASS: ' + prop + ' threw ' + e.toString());
77  }
78}
79
80function testRWProperty(that, prop, expected, newValue) {
81  testReadProperty(that, prop, expected);
82  testWriteProperty(that, prop, newValue);
83}
84
85function testRIProperty(that, prop, expected, newValue) {
86  testReadProperty(that, prop, expected);
87  testWriteIgnoredProperty(that, prop, expected, newValue);
88}
89
90function testROProperty(that, prop, expected) {
91  testReadProperty(that, prop, expected);
92  testUnwritableProperty(that, prop, 42);
93}
94
95function testXXProperty(that, prop) {
96  testUnreadableProperty(that, prop);
97  testUnwritableProperty(that, prop, 42);
98}
99