1 // extcomm.as - Host container communication (ExternalInterface) tests
3 // Copyright (C) 2015, 2016 Free Software Foundation, Inc.
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 // Original author: Nutchanon Wetchasit <Nutchanon.Wetchasit@gmail.com>
23 import flash
.external
.*;
27 class ExternalCommTest
29 var testVariable
= "Variable in this";
32 public static function main
(mc
:MovieClip):Void
34 var app
:ExternalCommTest
;
36 app
= new ExternalCommTest
(mc
);
39 public function ExternalCommTest
(mc
:MovieClip)
44 obj
.testVariable
= "Variable in obj";
45 _root
.testVariable
= "Variable in _root";
47 // ExternalInterface should be available
48 check
(ExternalInterface.available
);
50 mc
.script_call
= function(arg1
, arg2
):String
52 // This function should NOT be called
54 check_equals
(arg1
, "Hello");
55 check_equals
(arg2
, "World");
59 // Registering callback shouldn't fail
61 ExternalInterface.addCallback
("script_call", obj
,
62 function(arg1
, arg2
):String
64 // This function should be called
66 check_equals
(arg1
, "Hello");
67 check_equals
(arg2
, "World");
69 // `this` should point to user-specified object
70 check_equals
(this.testVariable
, "Variable in obj");
77 // Invalid callback registrations should fail
78 check
(!ExternalInterface.addCallback
("invalid_reg1", mc
, null));
79 check
(!ExternalInterface.addCallback
("invalid_reg2", mc
, undefined));
80 check
(!ExternalInterface.addCallback
("invalid_reg3", null, null));
81 check
(!ExternalInterface.addCallback
("invalid_reg4", null, undefined));
82 check
(!ExternalInterface.addCallback
("invalid_reg5", undefined, null));
83 check
(!ExternalInterface.addCallback
("invalid_reg6", undefined, undefined));
85 // Registering callbacks with no `this` instance shouldn't fail
87 ExternalInterface.addCallback
("script_nothis1", null,
90 // `this` should be an "undefined" object like one in
91 // a function called via `function.call(null)`
92 check_equals
(typeof(this), "object");
93 check
(this == undefined);
95 check
(this !== undefined);
97 check_equals
("" + this, "undefined");
98 check
(this === _global
);
99 this.nothis1_value
= "script_nothis1";
104 ExternalInterface.addCallback
("script_nothis2", undefined,
107 // `this` should be an "undefined" object like one in
108 // a function called via `function.call(undefined)`
109 check_equals
(typeof(this), "object");
110 check
(this == undefined);
112 check
(this !== undefined);
113 check
(this !== null);
114 check_equals
("" + this, "undefined");
115 check
(this === _global
);
116 this.nothis2_value
= "script_nothis2";
121 // Registering callback for checking _global properties shouldn't fail
123 ExternalInterface.addCallback
("script_globalcheck", mc
,
126 check_equals
(_global
.nothis1_value
, "script_nothis1");
127 check_equals
(_global
.nothis2_value
, "script_nothis2");
132 // Registering another ordinary callback shouldn't fail
134 ExternalInterface.addCallback
("script_longarglist", mc
,
135 function(arg1
:String, arg2
:String, arg3
:String,
136 arg4
:String, arg5
:String, arg6
:String,
137 arg7
:String, arg8
:String, arg9
:String):String
139 // Long argument list should be passed correctly
140 check_equals
(arg1
, "The");
141 check_equals
(arg2
, "quick");
142 check_equals
(arg3
, "brown");
143 check_equals
(arg4
, "fox");
144 check_equals
(arg5
, "jumps");
145 check_equals
(arg6
, "over");
146 check_equals
(arg7
, "the");
147 check_equals
(arg8
, "lazy");
148 check_equals
(arg9
, "dog");
155 // Calling JavaScript function without any argument should give
156 // a correct return value
157 check_equals
(ExternalInterface.call
("js_simple"), "Correct");
159 // Calling JavaScript function 20 times in row should give all
160 // correct return value
161 var numreadings
:Array = new Array(
162 "one", "two", "three", "four", "five", "six", "seven", "eight",
163 "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
164 "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"
167 for(i
= 0; i
< numreadings
.length
; i
++) {
168 check_equals
(ExternalInterface.call
("js_readnumber", i
+ 1),