File: test_attributes.js

package info (click to toggle)
thunderbird 1%3A115.16.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,476,252 kB
  • sloc: cpp: 6,972,150; javascript: 5,209,211; ansic: 3,507,222; python: 1,137,609; asm: 432,531; xml: 205,149; java: 175,761; sh: 116,485; makefile: 22,152; perl: 13,971; objc: 12,561; yacc: 4,583; pascal: 2,840; lex: 1,720; ruby: 1,075; exp: 762; sql: 666; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (103 lines) | stat: -rw-r--r-- 3,480 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var ObjectReadWrite = {
  QueryInterface: ChromeUtils.generateQI(["nsIXPCTestObjectReadWrite"]),

  /* nsIXPCTestObjectReadWrite */
  stringProperty: "XPConnect Read-Writable String",
  booleanProperty: true,
  shortProperty: 32767,
  longProperty: 2147483647,
  floatProperty: 5.5,
  charProperty: "X",
  // timeProperty is PRTime and signed type.
  // So it has to allow negative value.
  timeProperty: -1,
};

var ObjectReadOnly = {
  QueryInterface: ChromeUtils.generateQI(["nsIXPCTestObjectReadOnly"]),

  /* nsIXPCTestObjectReadOnly */
  strReadOnly: "XPConnect Read-Only String",
  boolReadOnly: true,
  shortReadOnly: 32767,
  longReadOnly: 2147483647,
  floatReadOnly: 5.5,
  charReadOnly: "X",
  // timeProperty is PRTime and signed type.
  // So it has to allow negative value.
  timeReadOnly: -1,
};

function run_test() {
  // Load the component manifests.
  registerXPCTestComponents();

  // Test for each component.
  test_component_readwrite(Cc["@mozilla.org/js/xpc/test/native/ObjectReadWrite;1"].createInstance());
  test_component_readwrite(xpcWrap(ObjectReadWrite));
  test_component_readonly(Cc["@mozilla.org/js/xpc/test/native/ObjectReadOnly;1"].createInstance());
  test_component_readonly(xpcWrap(ObjectReadOnly));
}

function test_component_readwrite(obj) {
  // Instantiate the object.
  var o = obj.QueryInterface(Ci.nsIXPCTestObjectReadWrite);

  // Test the initial values.
  Assert.equal("XPConnect Read-Writable String", o.stringProperty);
  Assert.equal(true, o.booleanProperty);
  Assert.equal(32767, o.shortProperty);
  Assert.equal(2147483647, o.longProperty);
  Assert.ok(5.25 < o.floatProperty && 5.75 > o.floatProperty);
  Assert.equal("X", o.charProperty);
  Assert.equal(-1, o.timeProperty);

  // Write new values.
  o.stringProperty = "another string";
  o.booleanProperty = false;
  o.shortProperty = -12345;
  o.longProperty = 1234567890;
  o.floatProperty = 10.2;
  o.charProperty = "Z";
  o.timeProperty = 1;

  // Test the new values.
  Assert.equal("another string", o.stringProperty);
  Assert.equal(false, o.booleanProperty);
  Assert.equal(-12345, o.shortProperty);
  Assert.equal(1234567890, o.longProperty);
  Assert.ok(10.15 < o.floatProperty && 10.25 > o.floatProperty);
  Assert.equal("Z", o.charProperty);
  Assert.equal(1, o.timeProperty);

  // Assign values that differ from the expected type to verify conversion.

  function SetAndTestBooleanProperty(newValue, expectedValue) {
    o.booleanProperty = newValue;
    Assert.equal(expectedValue, o.booleanProperty);
  };
  SetAndTestBooleanProperty(false, false);
  SetAndTestBooleanProperty(1, true);
  SetAndTestBooleanProperty(null, false);
  SetAndTestBooleanProperty("A", true);
  SetAndTestBooleanProperty(undefined, false);
  SetAndTestBooleanProperty([], true);
  SetAndTestBooleanProperty({}, true);
}

function test_component_readonly(obj) {
  var o = obj.QueryInterface(Ci.nsIXPCTestObjectReadOnly);

  // Test the initial values.
  Assert.equal("XPConnect Read-Only String", o.strReadOnly);
  Assert.equal(true, o.boolReadOnly);
  Assert.equal(32767, o.shortReadOnly);
  Assert.equal(2147483647, o.longReadOnly);
  Assert.ok(5.25 < o.floatReadOnly && 5.75 > o.floatReadOnly);
  Assert.equal("X", o.charReadOnly);
  Assert.equal(-1, o.timeReadOnly);
}