Bug 990729 - Tests for writeToGlobalPrototype (r=bholley)
authorBill McCloskey <wmccloskey@mozilla.com>
Sat, 21 Jun 2014 11:54:36 -0700
changeset 189944 3289f6b88862ac4752bb200937d1dce3b67df052
parent 189943 7b44740b12b18eeb5bb521a147d345cc1c42c31c
child 189945 1b5f05eeebd558b593eb042cb05cc0edb8ef15bf
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbholley
bugs990729
milestone33.0a1
Bug 990729 - Tests for writeToGlobalPrototype (r=bholley)
js/xpconnect/tests/unit/test_writeToGlobalPrototype.js
js/xpconnect/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/unit/test_writeToGlobalPrototype.js
@@ -0,0 +1,72 @@
+const A = 3;
+
+const Cu = Components.utils;
+
+function run_test()
+{
+  let s = Cu.Sandbox(this, {sandboxPrototype: this, writeToGlobalPrototype: true});
+
+  Cu.evalInSandbox("a = 3", s);
+  Cu.evalInSandbox("var b = 3", s);
+  Cu.evalInSandbox("const c = 3", s);
+  Cu.evalInSandbox("this.d = 3", s);
+  Cu.evalInSandbox("function e() { return 3; }", s);
+
+  do_check_eq(Cu.evalInSandbox("a", s), 3);
+  do_check_eq(Cu.evalInSandbox("b", s), 3);
+  do_check_eq(Cu.evalInSandbox("c", s), 3);
+  do_check_eq(Cu.evalInSandbox("d", s), 3);
+  do_check_eq(Cu.evalInSandbox("e()", s), 3);
+
+  do_check_eq(a, 3);
+  do_check_eq(b, 3);
+  do_check_eq(c, 3);
+  do_check_eq(d, 3);
+  do_check_eq(e(), 3);
+
+  a = 12;
+  do_check_eq(Cu.evalInSandbox("a", s), 12);
+  b = 12;
+  do_check_eq(Cu.evalInSandbox("b", s), 12);
+  d = 12;
+  do_check_eq(Cu.evalInSandbox("d", s), 12);
+
+  this.q = 3;
+  do_check_eq(Cu.evalInSandbox("q", s), 3);
+  Cu.evalInSandbox("q = 12", s);
+  do_check_eq(q, 12);
+
+  Cu.evalInSandbox("Object.defineProperty(this, 'x', {enumerable: false, value: 3})", s);
+  do_check_eq(Cu.evalInSandbox("x", s), 3);
+  do_check_eq(x, 3);
+  for (var p in this) {
+    do_check_neq(p, "x");
+  }
+
+  Cu.evalInSandbox("Object.defineProperty(this, 'y', {get: function() { this.gotten = true; return 3; }})", s);
+  do_check_eq(y, 3);
+  do_check_eq(Cu.evalInSandbox("gotten", s), true);
+  do_check_eq(gotten, true);
+
+  Cu.evalInSandbox("this.gotten = false", s);
+  do_check_eq(Cu.evalInSandbox("y", s), 3);
+  do_check_eq(Cu.evalInSandbox("gotten", s), true);
+  do_check_eq(gotten, true);
+
+  Cu.evalInSandbox("Object.defineProperty(this, 'z', {get: function() { this.gotten = true; return 3; }, set: function(v) { this.setTo = v; }})", s);
+  z = 12;
+  do_check_eq(setTo, 12);
+  do_check_eq(z, 3);
+  do_check_eq(gotten, true);
+  do_check_eq(Cu.evalInSandbox("gotten", s), true);
+  gotten = false;
+  do_check_eq(Cu.evalInSandbox("gotten", s), false);
+
+  Cu.evalInSandbox("z = 20", s);
+  do_check_eq(setTo, 20);
+  do_check_eq(Cu.evalInSandbox("z", s), 3);
+  do_check_eq(gotten, true);
+  do_check_eq(Cu.evalInSandbox("gotten", s), true);
+  gotten = false;
+  do_check_eq(Cu.evalInSandbox("gotten", s), false);
+}
--- a/js/xpconnect/tests/unit/xpcshell.ini
+++ b/js/xpconnect/tests/unit/xpcshell.ini
@@ -85,9 +85,9 @@ head = head_watchdog.js
 [test_watchdog_disable.js]
 head = head_watchdog.js
 [test_watchdog_toggle.js]
 head = head_watchdog.js
 [test_watchdog_default.js]
 head = head_watchdog.js
 [test_watchdog_hibernate.js]
 head = head_watchdog.js
-
+[test_writeToGlobalPrototype.js]