-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSenAsyncTestCase.m
More file actions
executable file
·84 lines (67 loc) · 2.52 KB
/
Copy pathSenAsyncTestCase.m
File metadata and controls
executable file
·84 lines (67 loc) · 2.52 KB
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
//
// SenAsyncTestCase.m
// AsyncSenTestingKit
//
// Created by 小野 将司 on 12/03/17.
// Copyright (c) 2012年 AppBankGames Inc. All rights reserved.
//
#import "SenAsyncTestCase.h"
@interface SenAsyncTestCase ()
@property (nonatomic, retain) NSDate *loopUntil;
@property (nonatomic, assign) BOOL notified;
@property (nonatomic, assign) SenAsyncTestCaseStatus notifiedStatus;
@property (nonatomic, assign) SenAsyncTestCaseStatus expectedStatus;
@end
@implementation SenAsyncTestCase
@synthesize loopUntil = _loopUntil;
@synthesize notified = _notified;
@synthesize notifiedStatus = _notifiedStatus;
@synthesize expectedStatus = _expectedStatus;
#pragma mark - Public
- (void)waitForStatus:(SenAsyncTestCaseStatus)status timeout:(NSTimeInterval)timeout
{
self.notified = NO;
self.expectedStatus = status;
self.loopUntil = [NSDate dateWithTimeIntervalSinceNow:timeout];
NSDate *dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
while (!self.notified && [self.loopUntil timeIntervalSinceNow] > 0)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:dt];
dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
}
if (self.notified) {
STAssertEquals(self.notifiedStatus, self.expectedStatus, @"Notified status does not match the expected status.");
} else {
STFail(@"Async test timed out.");
}
}
- (void)waitForTimeout:(NSTimeInterval)timeout
{
self.notified = NO;
self.expectedStatus = SenAsyncTestCaseStatusUnknown;
self.loopUntil = [NSDate dateWithTimeIntervalSinceNow:timeout];
NSDate *dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
while (!self.notified && [self.loopUntil timeIntervalSinceNow] > 0)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:dt];
dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
}
}
- (void)waitForTimeoutInt:(NSInteger) timeout
{
NSDate *now = [NSDate date];
[self waitForTimeout:[[self nextDate:now plusMinutes:timeout] timeIntervalSinceNow]];
}
- (void)notify:(SenAsyncTestCaseStatus)status
{
self.notifiedStatus = status;
self.notified = YES;
}
- (NSDate*) nextDate:(NSDate*)inDate plusMinutes:(NSInteger) minutes
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components: NSEraCalendarUnit|NSYearCalendarUnit| NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate: inDate];
[comps setMinute:[comps minute]+minutes];
return [calendar dateFromComponents:comps];
}
@end