1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * NASHORN-478 : Provide mozilla compatibility script for nashorn
  26  *
  27  * @test
  28  * @run
  29  */
  30 
  31 // compatibility script is loaded using "nashorn:" pseudo URL scheme
  32 try {
  33     load('nashorn:mozilla_compat.js');
  34 } catch(e) {
  35 }
  36 
  37 var obj = {};
  38 if (obj.__proto__ !== Object.prototype) {
  39     fail("#1 obj.__proto__ read not supported");
  40 }
  41 
  42 function fooGetter() { return 3.14; }
  43 function fooSetter(x) {}
  44 
  45 Object.defineProperty(obj, "foo", { set: fooSetter, get: fooGetter });
  46 if (!obj.__lookupGetter__ || obj.__lookupGetter__('foo') !== fooGetter) {
  47     fail("#2 Object.prototype.__lookupGetter__ not supported");
  48 }
  49 
  50 if (!obj.__lookupSetter__ || obj.__lookupSetter__('foo') !== fooSetter) {
  51     fail("#3 Object.prototype.__lookupSetter__ not supported");
  52 }
  53 
  54 function barGetter() { return 42; }
  55 
  56 if (obj.__defineGetter__) {
  57     obj.__defineGetter__("bar", barGetter);
  58 }
  59 
  60 if (obj.bar !== 42) {
  61     fail("#4 Object.prototype.__defineGetter__ not supported");
  62 }
  63 
  64 var barSetterCalled = false;
  65 function barSetter(x) {
  66     barSetterCalled = true;
  67 }
  68 
  69 if (obj.__defineSetter__) {
  70     obj.__defineSetter__("bar", barSetter);
  71 }
  72 
  73 obj.bar = 'hello';
  74 if (! barSetterCalled) {
  75     fail("#5 Object.prototype.__defineSetter__ not supported");
  76 }
  77 
  78 var obj = { bar: 343, foo : new Boolean(true) };
  79 obj.self = obj;
  80 if (!obj.toSource ||
  81     obj.toSource() !== '({bar:343, foo:(new Boolean(true)), self:{}})') {
  82     fail("#6 Object.prototype.toSource method failed");
  83 }
  84 
  85 // check String html generation methods
  86 if (!'sss'.anchor || "sss".anchor("foo") !== '<a name="foo">sss</a>') {
  87     fail("#7 String.prototype.anchor method failed");
  88 }
  89 
  90 if (!'hello'.blink || "hello".blink() !== '<blink>hello</blink>') {
  91     fail("#8 String.prototype.blink method failed");
  92 }
  93 
  94 if (!'hello'.fixed || "hello".fixed() !== '<tt>hello</tt>') {
  95     fail("#9 String.prototype.fixed method failed");
  96 }
  97 
  98 if (!'ss'.link || "ss".link('foo') !== '<a href="foo">ss</a>') {
  99     fail("#10 String.prototype.link method failed");
 100 }
 101 
 102 if (typeof importClass != 'function') {
 103     fail("#11 importClass function not defined");
 104 }
 105 
 106 importClass(java.util.HashMap);
 107 if (typeof HashMap != 'function') {
 108     fail("#12 global.importClass method failed");
 109 }
 110 var m = new HashMap();
 111 m.put('foo', 'bar');
 112 if (m.toString() != '{foo=bar}') {
 113     fail("#13 global.importClass failed to work");
 114 }
 115