@@ -28,6 +28,7 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2828
2929#import " TScriptObject.h"
3030#import " GAScriptObject.h"
31+ #import " NSObject+GAJavaScript.h"
3132#import " UIWebView+GAJavaScript.h"
3233
3334@implementation TScriptObject
@@ -42,20 +43,8 @@ - (void)setUp
4243{
4344 UIApplication* app = [UIApplication sharedApplication ];
4445 UIWindow* mainWindow = app.keyWindow ;
45- CGRect webFrame = [[UIScreen mainScreen ] applicationFrame ];
4646
47- m_webView = (UIWebView *) [mainWindow viewWithTag: 9999 ];
48-
49- if (m_webView == nil )
50- {
51- m_webView = [[UIWebView alloc ] initWithFrame: webFrame];
52- m_webView.tag = 9999 ;
53- m_webView.delegate = self;
54- m_webView.hidden = YES ;
55- [mainWindow addSubview: m_webView];
56- }
57-
58- [m_webView loadHTMLString: @" <html><body><p>Hello World</p></body></html>" baseURL: nil ];
47+ m_webView = (UIWebView *) [mainWindow viewWithTag: 9999 ];
5948}
6049
6150- (BOOL )compareValues : (id )gotValue testValue : (id )testValue
@@ -84,23 +73,7 @@ - (BOOL)compareValues:(id)gotValue testValue:(id)testValue
8473 return YES ;
8574}
8675
87- - (void )webViewDidFinishLoad : (UIWebView *)webView
88- {
89- // Load the GAJavaScript runtime here
90- [webView loadScriptRuntime ];
91-
92- [self performSelector: m_curTest];
93- }
94-
9576- (void )testKeyValueCoding
96- {
97- [self prepare ];
98- m_curTest = @selector (finishKeyValueCoding );
99-
100- [self waitForStatus: kGHUnitWaitStatusSuccess timeout: 3.0 ];
101- }
102-
103- - (void )finishKeyValueCoding
10477{
10578 NSArray * kTestValues = [NSArray arrayWithObjects:
10679 @" abcd" , // String
@@ -112,16 +85,14 @@ - (void)finishKeyValueCoding
11285 [NSDate date ], // Date
11386 nil ];
11487
115- NSInteger status = kGHUnitWaitStatusSuccess ;
116- GAScriptObject* jsObject = [[GAScriptObject alloc ] initForReference: @" location" view: m_webView];
88+ GAScriptObject* jsObject = [m_webView newScriptObject ];
11789
11890 for (id testValue in kTestValues )
11991 {
12092 [jsObject setValue: testValue forKey: @" js_test" ];
12193 id gotValue = [jsObject valueForKey: @" js_test" ];
12294
123- if (![self compareValues: gotValue testValue: testValue])
124- status = kGHUnitWaitStatusFailure ;
95+ GHAssertTrue ([self compareValues: gotValue testValue: testValue], nil );
12596 }
12697
12798 // Test with a character that cannot be in an identifier
@@ -130,22 +101,12 @@ - (void)finishKeyValueCoding
130101 [jsObject setValue: testValue forKey: @" background-color" ];
131102 id gotValue = [jsObject valueForKey: @" background-color" ];
132103
133- if (![self compareValues: gotValue testValue: testValue])
134- status = kGHUnitWaitStatusFailure ;
104+ GHAssertTrue ([self compareValues: gotValue testValue: testValue], nil );
135105
136- [self notify: status forSelector: @selector (testKeyValueCoding )];
137106 [jsObject release ];
138107}
139108
140109- (void )testKeyValueCodingWithArrays
141- {
142- [self prepare ];
143- m_curTest = @selector (finishKeyValueCodingWithArrays );
144-
145- [self waitForStatus: kGHUnitWaitStatusSuccess timeout: 3.0 ];
146- }
147-
148- - (void )finishKeyValueCodingWithArrays
149110{
150111 NSArray * kTestValues = [NSArray arrayWithObjects:
151112 @" abcd" , // String
@@ -157,34 +118,22 @@ - (void)finishKeyValueCodingWithArrays
157118 [NSDate date ], // Date
158119 nil ];
159120
160- NSInteger status = kGHUnitWaitStatusSuccess ;
161- GAScriptObject* jsObject = [[GAScriptObject alloc ] initForReference: @" location" view: m_webView];
121+ GAScriptObject* jsObject = [m_webView newScriptObject ];
162122
163123 [jsObject setValue: kTestValues forKey: @" js_test" ];
164124 NSArray * gotValue = [jsObject valueForKey: @" js_test" ];
165125
166- if (![gotValue isKindOfClass: [NSArray class ]])
167- status = kGHUnitWaitStatusFailure ;
126+ GHAssertTrue ([gotValue isKindOfClass: [NSArray class ]], nil );
168127
169128 for (NSInteger i = 0 ; i < [gotValue count ]; ++i)
170129 {
171- if (![self compareValues: [gotValue objectAtIndex: i] testValue: [kTestValues objectAtIndex: i]])
172- status = kGHUnitWaitStatusFailure ;
130+ GHAssertTrue ([self compareValues: [gotValue objectAtIndex: i] testValue: [kTestValues objectAtIndex: i]], nil );
173131 }
174132
175- [self notify: status forSelector: @selector (testKeyValueCodingWithArrays )];
176133 [jsObject release ];
177134}
178135
179136- (void )testKeyValueCodingWithDictionary
180- {
181- [self prepare ];
182- m_curTest = @selector (finishKeyValueCodingWithDictionary );
183-
184- [self waitForStatus: kGHUnitWaitStatusSuccess timeout: 3.0 ];
185- }
186-
187- - (void )finishKeyValueCodingWithDictionary
188137{
189138 NSDictionary * kTestDict = [NSDictionary dictionaryWithObjectsAndKeys:
190139 @" abcd" , @" string" ,
@@ -196,94 +145,72 @@ - (void)finishKeyValueCodingWithDictionary
196145 [NSDate date ], @" dateprop" ,
197146 nil ];
198147
199- NSInteger status = kGHUnitWaitStatusSuccess ;
200148 GAScriptObject* jsObject = [m_webView newScriptObject ];
201149
202150 [jsObject setValue: kTestDict forKey: @" js_test" ];
203151 GAScriptObject* gotValue = [jsObject valueForKey: @" js_test" ];
204152
205- if (![gotValue isKindOfClass: [GAScriptObject class ]])
206- status = kGHUnitWaitStatusFailure ;
153+ GHAssertTrue ([gotValue isKindOfClass: [GAScriptObject class ]], nil );
207154
208155 for (NSString * key in kTestDict )
209156 {
210- if (![self compareValues: [gotValue valueForKey: key] testValue: [kTestDict objectForKey: key]])
211- status = kGHUnitWaitStatusFailure ;
157+ GHAssertTrue ([self compareValues: [gotValue valueForKey: key] testValue: [kTestDict objectForKey: key]], nil );
212158 }
213159
214- [self notify: status forSelector: @selector (testKeyValueCodingWithDictionary )];
215160 [jsObject release ];
216161}
217162
218163- (void )testAllKeys
219164{
220- [self prepare ];
221- m_curTest = @selector (finishAllKeys );
222-
223- [self waitForStatus: kGHUnitWaitStatusSuccess timeout: 3.0 ];
224- }
225-
226- - (void )finishAllKeys
227- {
228- NSInteger status = kGHUnitWaitStatusSuccess ;
229-
230165 GAScriptObject* jsObject = [[GAScriptObject alloc ] initForReference: @" location" view: m_webView];
231166 NSArray * allKeys = [jsObject allKeys ];
232167
233- if (allKeys == nil )
234- status = kGHUnitWaitStatusFailure ;
235- if ([allKeys count ] == 0 )
236- status = kGHUnitWaitStatusFailure ;
237- if ([allKeys containsObject: @" hostname" ] == NO )
238- status = kGHUnitWaitStatusFailure ;
168+ GHAssertNotNil (allKeys, nil );
169+ GHAssertTrue ([allKeys count ] != 0 , nil );
170+ GHAssertTrue ([allKeys containsObject: @" hostname" ], nil );
239171
240- [self notify: status forSelector: @selector (testAllKeys )];
241172 [jsObject release ];
242173}
243174
244175- (void )testFastEnumeration
245176{
246- [self prepare ];
247- m_curTest = @selector (finishFastEnumeration );
248-
249- [self waitForStatus: kGHUnitWaitStatusSuccess timeout: 3.0 ];
250- }
251-
252- - (void )finishFastEnumeration
253- {
254- NSInteger status = kGHUnitWaitStatusFailure ;
255-
256177 GAScriptObject* jsObject = [[GAScriptObject alloc ] initForReference: @" location" view: m_webView];
257-
178+ BOOL foundHostName = NO ;
179+
258180 for (id key in jsObject)
259181 {
260182 if ([key isEqual: @" hostname" ])
261- status = kGHUnitWaitStatusSuccess ;
183+ foundHostName = YES ;
262184 }
263185
264- [ self notify: status forSelector: @selector ( testFastEnumeration )];
186+ GHAssertTrue (foundHostName, nil );
265187 [jsObject release ];
266188}
267189
268190- (void )testCallFunction
269191{
270- [self prepare ];
271- m_curTest = @selector (finishCallFunction );
192+ GAScriptObject* jsObject = [[GAScriptObject alloc ] initForReference: @" document" view: m_webView];
193+ id retVal = [jsObject callFunction: @" createElement" withObject: @" strong" ];
194+
195+ GHAssertTrue ([retVal isKindOfClass: [GAScriptObject class ]], nil );
272196
273- [self waitForStatus: kGHUnitWaitStatusSuccess timeout: 3.0 ];
197+ [jsObject release ];
274198}
275199
276- - (void )finishCallFunction
200+ - (void )testJavaScriptTrue
277201{
278- NSInteger status = kGHUnitWaitStatusSuccess ;
279-
280- GAScriptObject* jsObject = [[GAScriptObject alloc ] initForReference: @" document" view: m_webView];
281- id retVal = [jsObject callFunction: @" createElement" withObject: @" strong" ];
282-
283- if (![retVal isKindOfClass: [GAScriptObject class ]])
284- status = kGHUnitWaitStatusFailure ;
202+ GAScriptObject* jsObject = [m_webView newScriptObject ];
203+
204+ [jsObject setValue: [NSNull null ] forKey: @" prop-null" ];
205+ [jsObject setValue: [NSNumber numberWithFloat: 0.0 ] forKey: @" prop-num" ];
206+ [jsObject setValue: [NSNumber numberWithBool: YES ] forKey: @" prop-bool" ];
207+ [jsObject setValue: @" " forKey: @" prop-str" ];
208+
209+ GHAssertFalse ([[jsObject valueForKey: @" prop-null" ] isJavaScriptTrue ], @" NSNull failed" );
210+ GHAssertFalse ([[jsObject valueForKey: @" prop-num" ] isJavaScriptTrue ], @" NSNumber failed" );
211+ GHAssertTrue ([[jsObject valueForKey: @" prop-bool" ] isJavaScriptTrue ], @" BOOL failed" );
212+ GHAssertFalse ([[jsObject valueForKey: @" prop-str" ] isJavaScriptTrue ], @" NSString failed" );
285213
286- [self notify: status forSelector: @selector (testCallFunction )];
287214 [jsObject release ];
288215}
289216
0 commit comments