forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCTModuleInitNotificationRaceTests.m
More file actions
129 lines (99 loc) · 3.08 KB
/
RCTModuleInitNotificationRaceTests.m
File metadata and controls
129 lines (99 loc) · 3.08 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
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
/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
#import "RCTBridge.h"
#import "RCTBridge+Private.h"
#import "RCTBridgeModule.h"
#import "RCTUtils.h"
#import "RCTUIManager.h"
#import "RCTViewManager.h"
#define RUN_RUNLOOP_WHILE(CONDITION) \
{ \
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:5]; \
while ((CONDITION)) { \
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; \
if ([timeout timeIntervalSinceNow] <= 0) { \
XCTFail(@"Runloop timed out before condition was met"); \
break; \
} \
} \
}
@interface RCTTestViewManager : RCTViewManager
@end
@implementation RCTTestViewManager
RCT_EXPORT_MODULE()
- (NSArray<NSString *> *)customDirectEventTypes
{
return @[@"foo"];
}
@end
@interface RCTNotificationObserverModule : NSObject <RCTBridgeModule>
@property (nonatomic, assign) BOOL didDetectViewManagerInit;
@end
@implementation RCTNotificationObserverModule
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didInitViewManager:) name:RCTDidInitializeModuleNotification object:nil];
}
- (void)didInitViewManager:(NSNotification *)note
{
id<RCTBridgeModule> module = note.userInfo[@"module"];
if ([module isKindOfClass:[RCTTestViewManager class]]) {
_didDetectViewManagerInit = YES;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
@interface RCTModuleInitNotificationRaceTests : XCTestCase <RCTBridgeDelegate>
{
RCTBridge *_bridge;
RCTNotificationObserverModule *_notificationObserver;
}
@end
@implementation RCTModuleInitNotificationRaceTests
- (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge
{
return nil;
}
- (NSArray *)extraModulesForBridge:(__unused RCTBridge *)bridge
{
return @[[RCTTestViewManager new], _notificationObserver];
}
- (void)setUp
{
[super setUp];
_notificationObserver = [RCTNotificationObserverModule new];
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
}
- (void)tearDown
{
[super tearDown];
_notificationObserver = nil;
id<RCTJavaScriptExecutor> jsExecutor = _bridge.batchedBridge.javaScriptExecutor;
[_bridge invalidate];
RUN_RUNLOOP_WHILE(jsExecutor.isValid);
_bridge = nil;
}
- (void)testViewManagerNotInitializedBeforeSetBridgeModule
{
RUN_RUNLOOP_WHILE(!_notificationObserver.didDetectViewManagerInit);
}
@end