1+ < html >
2+ < body >
3+ < button id ='runTest '> Run Test</ button >
4+ < button id ='method '> Method with param</ button >
5+ < button id ='methodAsync '> Method with param (async)</ button >
6+ < button id ='methodAsynTaskVoid '> Method with param (async) without result</ button >
7+ < button id ='methodNoParam '> Method no param</ button >
8+ < button id ='methodNoParamAsync '> Method no param (async)</ button >
9+ < button id ='methodNoParamAsyncVoid '> Method no param (async) without result</ button >
10+ < button id ='propertyGet '> Property Get</ button >
11+ < button id ='propertySet '> Property Set</ button >
12+ < button id ='indexerGet '> Get object[index]</ button >
13+ < button id ='indexerSet '> Set object[index]</ button >
14+ < button id ='indexerGetAlias '> Get Items[index]</ button >
15+ < button id ='indexerSetAlias '> Set Items[index]</ button >
16+ < div id ="div_iframe " style ="display: none; "> </ div >
17+ < script >
18+ //! Create IFrame from the parent html page and load it
19+ function createIFrame ( ) {
20+ var i = document . createElement ( "iframe" ) ;
21+ i . src = "https://appassets.example/hostObject.html" ;
22+ i . scrolling = "auto" ;
23+ i . frameborder = "0" ;
24+ i . height = "50%" ;
25+ i . width = "100%" ;
26+ i . name = "iframe_name" ;
27+ var div = document . getElementById ( "div_iframe" ) ;
28+ div . appendChild ( i ) ; div . style . display = 'block' ;
29+ } ;
30+
31+ window . onload = function ( ) {
32+ if ( window . top === window ) {
33+ createIFrame ( ) ;
34+ }
35+ } ;
36+
37+ function getStack ( ) {
38+ try {
39+ throw new Error ( '' ) ;
40+ } catch ( error ) {
41+ return error . stack ;
42+ }
43+ }
44+
45+ function valueToString ( value ) {
46+ return '(' + JSON . stringify ( value ) + ', ' + ( typeof value ) + ')' ;
47+ }
48+
49+ async function assert ( condition , text , message ) {
50+ if ( ! condition ) {
51+ alert ( 'Assertion failed, ' + text + ': ' + message ) ;
52+ } else {
53+ console . log ( 'assert passed: ' + text ) ;
54+ }
55+ }
56+
57+ function assertEqual ( actual , expected , text )
58+ {
59+ assert ( expected === actual , text , ( 'Equality assertion failed. ' +
60+ 'Expected ' + valueToString ( expected ) + ', ' +
61+ 'Actual ' + valueToString ( actual ) + ', ' + getStack ( ) ) ) ;
62+ }
63+
64+ document . getElementById ( 'runTest' ) . addEventListener ( 'click' , async ( ) => {
65+ const bridge = chrome . webview . hostObjects . bridge ;
66+ let expected_result = 'value1' ;
67+ bridge . Prop = expected_result ;
68+ let result = await bridge . Prop ;
69+ assertEqual ( result , expected_result , 'property on bridge' ) ;
70+ const value2 = 'value2' ;
71+ result = await bridge . Func ( value2 ) ;
72+ expected_result = 'BridgeAddRemoteObject.Func(' + value2 + ')' ;
73+ assertEqual ( result , expected_result , 'method with parameter' ) ;
74+ result = await bridge . Func2 ( ) ;
75+ expected_result = 'BridgeAddRemoteObject.Func2()' ;
76+ assertEqual ( result , expected_result , 'method with no parameter' ) ;
77+ result = await bridge . FuncAsync ( 500 ) ;
78+ expected_result = 'BridgeAddRemoteObject.FuncAsync(500)' ;
79+ assertEqual ( result , expected_result , 'async method with parameter' ) ;
80+ result = await bridge . FuncAsyncTaskVoid ( 500 ) ;
81+ expected_result = null ;
82+ assertEqual ( result , expected_result , 'async method with parameter without result' ) ;
83+ result = await bridge . Func2Async ( ) ;
84+ expected_result = 'BridgeAddRemoteObject.Func2Async()' ;
85+ assertEqual ( result , expected_result , 'async method with no parameter' ) ;
86+ result = await bridge . Func2AsyncTaskVoid ( ) ;
87+ expected_result = null ;
88+ assertEqual ( result , expected_result , 'async method with no parameter without result' ) ;
89+ result = await bridge . FuncAsync ( 0 ) ;
90+ expected_result = 'BridgeAddRemoteObject.FuncAsync(0)' ;
91+ assertEqual ( result , expected_result , 'async method with no delay' ) ;
92+
93+ const another_object = bridge . AnotherObject ;
94+ another_object . Prop = value2 ;
95+ result = await another_object . Prop ;
96+ expected_result = value2 ;
97+ assertEqual ( result , expected_result , 'property on another_object' ) ;
98+
99+ chrome . webview . hostObjects . options . shouldSerializeDates = true ;
100+ const date = new Date ( ) ;
101+ bridge . DateProp = date ;
102+ const date2 = await bridge . DateProp ;
103+ assertEqual ( date2 . getTime ( ) , date . getTime ( ) , 'test date object serialization' ) ;
104+
105+ let index = 123 ;
106+ expected_result = 'aa' ;
107+ bridge [ index ] = expected_result ;
108+ result = await bridge [ index ] ;
109+ assertEqual ( result , expected_result , 'bridge[index]' ) ;
110+ index = 321 ;
111+ expected_result = 'bb' ;
112+ bridge . Items [ index ] = expected_result ;
113+ result = await bridge . Items [ index ] ;
114+ assertEqual ( result , expected_result , 'bridge.Items[index]' ) ;
115+
116+ let resolved_bridge = await bridge ;
117+ result = await bridge . GetObjectType ( resolved_bridge ) ;
118+ expected_result = 'BridgeAddRemoteObject' ;
119+ assertEqual ( result , expected_result , 'type of resolved_bridge' ) ;
120+ result = await bridge . GetObjectType ( bridge ) ;
121+ expected_result = 'BridgeAddRemoteObject' ;
122+ assertEqual ( result , expected_result , 'type of bridge' ) ;
123+ result = await bridge . GetObjectType ( another_object ) ;
124+ expected_result = 'AnotherRemoteObject' ;
125+ assertEqual ( result , expected_result , 'type of another_object' ) ;
126+ result = await bridge . GetObjectTypeAsync ( another_object ) ;
127+ expected_result = 'AnotherRemoteObject' ;
128+ assertEqual ( result , expected_result , 'type of another_object asynchronously' ) ;
129+
130+ alert ( 'Test End' ) ;
131+ } ) ;
132+
133+ document . getElementById ( 'method' ) . addEventListener ( 'click' , async ( ) => {
134+ const bridge = chrome . webview . hostObjects . bridge ;
135+ const result = await bridge . Func ( prompt ( 'Method parameter text' , 'Method parameter text' ) ) ;
136+ alert ( result ) ;
137+ } ) ;
138+
139+ document . getElementById ( 'methodAsync' ) . addEventListener ( 'click' , async ( ) => {
140+ const bridge = chrome . webview . hostObjects . bridge ;
141+ const result = await bridge . FuncAsync ( prompt ( 'How many milliseconds should the async operation take? Enter zero or less to make it return immediately and synchronously.' , '2000' ) ) ;
142+ alert ( result ) ;
143+ } ) ;
144+
145+ document . getElementById ( 'methodAsynTaskVoid' ) . addEventListener ( 'click' , async ( ) => {
146+ const bridge = chrome . webview . hostObjects . bridge ;
147+ const result = await bridge . FuncAsyncTaskVoid ( prompt ( 'How many milliseconds should the async operation take? Enter zero or less to make it return immediately and synchronously.' , '2000' ) ) ;
148+ alert ( result ) ;
149+ } ) ;
150+
151+ document . getElementById ( 'methodNoParam' ) . addEventListener ( 'click' , async ( ) => {
152+ const bridge = chrome . webview . hostObjects . bridge ;
153+ const result = await bridge . Func2 ( ) ;
154+ alert ( result ) ;
155+ } ) ;
156+
157+ document . getElementById ( 'methodNoParamAsync' ) . addEventListener ( 'click' , async ( ) => {
158+ const bridge = chrome . webview . hostObjects . bridge ;
159+ const result = await bridge . Func2Async ( ) ;
160+ alert ( result ) ;
161+ } ) ;
162+
163+ document . getElementById ( 'methodNoParamAsyncVoid' ) . addEventListener ( 'click' , async ( ) => {
164+ const bridge = chrome . webview . hostObjects . bridge ;
165+ const result = await bridge . Func2AsyncTaskVoid ( ) ;
166+ alert ( result ) ;
167+ } ) ;
168+
169+ document . getElementById ( 'propertyGet' ) . addEventListener ( 'click' , async ( ) => {
170+ const bridge = chrome . webview . hostObjects . bridge ;
171+ const result = await bridge . Prop ;
172+ alert ( result ) ;
173+ } ) ;
174+
175+ document . getElementById ( 'propertySet' ) . addEventListener ( 'click' , async ( ) => {
176+ const bridge = chrome . webview . hostObjects . bridge ;
177+ bridge . Prop = prompt ( 'Property text' , 'Property text' ) ;
178+ } ) ;
179+
180+ document . getElementById ( 'indexerGet' ) . addEventListener ( 'click' , async ( ) => {
181+ const bridge = chrome . webview . hostObjects . bridge ;
182+ const result = await bridge [ 1 ] ;
183+ alert ( result ) ;
184+ } ) ;
185+
186+ document . getElementById ( 'indexerSet' ) . addEventListener ( 'click' , async ( ) => {
187+ const bridge = chrome . webview . hostObjects . bridge ;
188+ bridge [ 1 ] = prompt ( 'Property text' , 'Property text' ) ;
189+ } ) ;
190+
191+ document . getElementById ( 'indexerGetAlias' ) . addEventListener ( 'click' , async ( ) => {
192+ const bridge = chrome . webview . hostObjects . bridge ;
193+ const result = await bridge . Items [ 12 ] ;
194+ alert ( result ) ;
195+ } ) ;
196+
197+ document . getElementById ( 'indexerSetAlias' ) . addEventListener ( 'click' , async ( ) => {
198+ const bridge = chrome . webview . hostObjects . bridge ;
199+ bridge . Items [ 12 ] = prompt ( 'Property text' , 'Property text' ) ;
200+ } ) ;
201+ </ script >
202+ </ body >
203+ </ html >
0 commit comments