1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#import <TestViewController.h>
#import <TestUtil.h>
#import <AppDelegate.h>
NSArray* noSSL = @[ @"Ice/metrics" ];
NSArray* noWS = @[ @"Ice/metrics" ];
NSArray* noCpp11 = @[ @"Ice/custom" ];
static NSArray* protocols = @[ @"tcp", @"ssl", @"ws", @"wss" ];
//
// Avoid warning for undocumented method.
//
@interface UIApplication(UndocumentedAPI)
-(void)launchApplicationWithIdentifier:(NSString*)id suspended:(BOOL)flag;
@end
// TODO: Would be nice to have a red font for fatal, and error messages.
@interface MessageCell : UITableViewCell
{
@private
UILabel* body;
}
@property (nonatomic, retain) UILabel* body;
+(CGFloat)heightForMessage:(NSString*)messsage;
@end
@implementation MessageCell
@synthesize body;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
body = [[UILabel alloc] initWithFrame:CGRectZero];
body.textColor = [UIColor blackColor];
body.font = [UIFont boldSystemFontOfSize:14];
body.numberOfLines = 0;
[self.contentView addSubview:self.body];
}
return self;
}
+(CGFloat)heightForMessage:(NSString*)text
{
// The header is always one line, the body is multiple lines.
// The width of the table is 320 - 20px of left & right padding. We don't want to let the body
// text go past 200px.
CGRect body = [text boundingRectWithSize:CGSizeMake(300.f, 200.0f) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14] } context:nil];
return body.size.height + 20.f;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGRect bodyFrame = CGRectMake(10.f, 0.f, CGRectGetWidth(contentRect)-20.f, CGRectGetHeight(contentRect));
self.body.frame = bodyFrame;
}
- (void)dealloc
{
[body release];
[super dealloc];
}
-(void)setMessage:(NSString*)m
{
self.body.text = m;
}
@end
@interface TestViewController()
@property (nonatomic, retain) UITableView* output;
@property (nonatomic, retain) UIActivityIndicatorView* activity;
@property (nonatomic, retain) UIButton* nextButton;
@property (nonatomic, retain) NSMutableString* currentMessage;
@property (nonatomic, retain) NSMutableArray* messages;
@property (nonatomic, retain) NSOperationQueue* queue;
@property (retain) TestSuite* testSuite;
-(void)add:(NSString*)d;
-(void)startTest;
@end
@interface TestRun : NSObject
{
NSString* testSuiteId;
TestCase* testCase;
NSString* protocol;
int completed;
int running;
int error;
MainHelperI* clientHelper;
MainHelperI* serverHelper;
TestViewController* viewController;
}
+(id) testRun:(NSString*)testSuiteId testCase:(TestCase*)testCase protocol:(NSString*)protocol;
-(NSInvocationOperation*) runInvocation:(TestViewController*)callback;
@end
@implementation TestViewController
@synthesize output;
@synthesize activity;
@synthesize nextButton;
@synthesize currentMessage;
@synthesize messages;
@synthesize queue;
@synthesize testSuite;
- (void)viewDidLoad
{
self.currentMessage = [NSMutableString string];
self.messages = [NSMutableArray array];
queue = [[NSOperationQueue alloc] init];
self.queue.maxConcurrentOperationCount = 2; // We need at least 2 concurrent operations.
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.testSuite = (TestSuite*)[appDelegate.testSuites objectAtIndex:appDelegate.currentTestSuite];
[self startTest];
}
-(void)viewWillDisappear:(BOOL)animated
{
// TODO: Waiting isn't possible until the tests periodically find out whether
// they should terminate.
// Wait until the tests are complete.
//[queue waitUntilAllOperationsAreFinished];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc
{
[output release];
[activity release];
[nextButton release];
[currentMessage release];
[messages release];
[queue release];
[testSuite release];
[super dealloc];
}
#pragma mark -
-(void)startTest
{
self.title = testSuite.testSuiteId;
[self.navigationItem setHidesBackButton:YES animated:YES];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDelegate.loop)
{
[nextButton setTitle:@"Stop running" forState:UIControlStateNormal];
}
else
{
nextButton.enabled = NO;
[nextButton setAlpha:0.5];
[nextButton setTitle:@"Test is running" forState:UIControlStateDisabled];
}
[currentMessage deleteCharactersInRange:NSMakeRange(0, currentMessage.length)];
[messages removeAllObjects];
[output reloadData];
[activity startAnimating];
#ifdef ICE_CPP11_MAPPING
if([testSuite isIn:noCpp11])
{
[self add:@"C++11 not supported by this test\n"];
[self testComplete:YES];
return;
}
#endif
if(([appDelegate.protocol isEqualToString:@"ws"] || [appDelegate.protocol isEqualToString:@"wss"]) &&
[testSuite isIn:noWS])
{
[self add:@"WS not supported by this test\n"];
[self testComplete:YES];
return;
}
if([appDelegate.protocol isEqualToString:@"ssl"] &&
[testSuite isIn:noSSL])
{
[self add:@"SSL not supported by this test\n"];
[self testComplete:YES];
return;
}
NSMutableArray* testRuns = [NSMutableArray array];
for(id testCase in testSuite.testCases)
{
[testRuns addObject:[TestRun testRun:testSuite.testSuiteId testCase:testCase protocol:appDelegate.protocol]];
}
testRunEnumator = [[testRuns objectEnumerator] retain];
id testRun = [testRunEnumator nextObject];
[queue addOperation:[testRun runInvocation:self]];
}
-(void)testRunComplete:(BOOL)success
{
if(!success)
{
[self testComplete:NO];
return;
}
id testRun = [testRunEnumator nextObject];
if(testRun == nil)
{
[self testComplete:YES];
}
else
{
[queue addOperation:[testRun runInvocation:self]];
}
}
-(void)testComplete:(BOOL)success
{
[activity stopAnimating];
nextButton.enabled = YES;
[nextButton setAlpha:1.0];
[self.navigationItem setHidesBackButton:NO animated:YES];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSInteger next = (appDelegate.currentTestSuite + 1) % (appDelegate.testSuites.count);
TestSuite* nextTestSuite = (TestSuite*)[appDelegate.testSuites objectAtIndex:next];
NSString* buttonTitle = [NSString stringWithFormat:@"Run %@", nextTestSuite.testSuiteId];
[nextButton setTitle:buttonTitle forState:UIControlStateNormal];
self.testSuite = nil;
[testRunEnumator release];
testRunEnumator = nil;
if([appDelegate testCompleted:success])
{
NSAssert(self.testSuite == nil, @"testSuite == nil");
self.testSuite = (TestSuite*)[appDelegate.testSuites objectAtIndex:appDelegate.currentTestSuite];
[self startTest];
}
}
-(IBAction)next:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDelegate.loop)
{
appDelegate.loop = NO;
nextButton.enabled = NO;
[nextButton setAlpha:0.5];
[nextButton setTitle:@"Waiting..." forState:UIControlStateDisabled];
}
else
{
NSAssert(self.testSuite == nil, @"testSuite == nil");
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.testSuite = (TestSuite*)[appDelegate.testSuites objectAtIndex:appDelegate.currentTestSuite];
[self startTest];
}
}
-(NSOperationQueue*) queue
{
return queue;
}
-(void)add:(NSString*)s
{
[currentMessage appendString:s];
NSRange range = [currentMessage rangeOfString:@"\n" options:NSBackwardsSearch];
if(range.location != NSNotFound)
{
[messages addObject:[[currentMessage copy] autorelease]];
[currentMessage deleteCharactersInRange:NSMakeRange(0, currentMessage.length)];
// reloadData hangs if called too rapidly... we call at most every 100ms.
if(!reloadScheduled)
{
reloadScheduled = TRUE;
[self performSelector:@selector(reloadOutput) withObject:nil afterDelay:0.1];
}
}
}
-(void) reloadOutput
{
reloadScheduled = FALSE;
[output reloadData];
if(messages.count == 0)
{
return;
}
NSUInteger path[] = {0, messages.count -1};
[output scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndexes:path length:2]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
}
#pragma mark <UITableViewDelegate, UITableViewDataSource> Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tv
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
return messages.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([messages count] <= indexPath.row)
{
return [MessageCell heightForMessage:@""];
}
return [MessageCell heightForMessage:[messages objectAtIndex:indexPath.row]];
}
-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MessageCell *cell = (MessageCell*)[output dequeueReusableCellWithIdentifier:@"MessageCell"];
if(cell == nil)
{
cell = [[[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageCell"] autorelease];
}
[cell setMessage:[messages objectAtIndex:indexPath.row]];
return cell;
}
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}
@end
@implementation TestRun
-(id) init:(NSString*)ts testCase:(TestCase*)tc protocol:(NSString*)p
{
self = [super init];
if(!self)
{
return nil;
}
self->testSuiteId = ts;
self->testCase = tc;
self->protocol = p;
self->completed = 0;
self->error = 0;
self->running = 0;
return self;
}
+(id) testRun:(NSString*)ts testCase:(TestCase*)tc protocol:(NSString*)p
{
return [[[TestRun alloc] init:ts testCase:tc protocol:p] autorelease];
}
-(NSInvocationOperation*) runInvocation:(TestViewController*)ctl
{
viewController = ctl;
return [[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
}
-(void)run
{
// [self add:[NSString stringWithFormat:@"[ running %@ ]\n", testCase.name]];
if(testCase.server)
{
[self runServer];
}
else
{
[self runClient];
}
}
-(void)add:(NSString*)s
{
[viewController add:s];
}
-(void)clientComplete:(NSNumber*)rc
{
if(clientHelper)
{
delete clientHelper;
clientHelper = 0;
}
if([rc intValue] != 0)
{
[viewController add:[NSString stringWithFormat:@"client error: %@!\n", rc]];
if(serverHelper)
{
serverHelper->shutdown();
}
++error;
}
completed++;
if(!testCase.server || completed == 2)
{
[viewController testRunComplete:error == 0];
}
}
// Run in a separate thread.
-(void)runClient
{
running++;
std::string name = [testCase.name UTF8String];
std::string prefix = std::string("test/") + [testSuiteId UTF8String];
replace(prefix.begin(), prefix.end(), '/', '_');
std::string clt = [testCase.client UTF8String];
TestConfig config;
config.protocol = [protocol UTF8String];
for(id arg in testCase.args)
{
config.args.push_back([arg UTF8String]);
}
clientHelper = new MainHelperI(name, prefix + "/" + clt, TestTypeClient, config,
self, @selector(add:), @selector(serverReady));
clientHelper->run();
int rc = clientHelper->status();
[self performSelectorOnMainThread:@selector(clientComplete:) withObject:[NSNumber numberWithInt:rc] waitUntilDone:NO];
}
-(void)serverComplete:(NSNumber*)rc
{
if(serverHelper)
{
delete serverHelper;
serverHelper = 0;
}
if([rc intValue] != 0)
{
[viewController add:[NSString stringWithFormat:@"server error: %@!\n", rc]];
++error;
}
completed++;
if(completed == 2)
{
[viewController testRunComplete:error == 0];
}
}
// Run in a separate thread.
-(void)runServer
{
running++;
std::string name = [testCase.name UTF8String];
std::string prefix = std::string("test/") + [testSuiteId UTF8String];
replace(prefix.begin(), prefix.end(), '/', '_');
std::string srv = [testCase.server UTF8String];
TestConfig config;
config.protocol = [protocol UTF8String];
for(id arg in testCase.args)
{
config.args.push_back([arg UTF8String]);
}
serverHelper = new MainHelperI(name, prefix + "/" + srv, TestTypeServer, config,
self, @selector(add:), @selector(serverReady));
serverHelper->run();
int rc = serverHelper->status();
[self performSelectorOnMainThread:@selector(serverComplete:) withObject:[NSNumber numberWithInt:rc] waitUntilDone:NO];
}
// Kick off the client.
-(void)serverReady
{
[[viewController queue] addOperation:[[[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(runClient)
object:nil] autorelease]];
}
@end
|