2015-06-06 20:37:36 +00:00
|
|
|
/**
|
2015-07-13 15:42:32 +00:00
|
|
|
* The examples provided by Facebook are for non-commercial testing and
|
|
|
|
* evaluation purposes only.
|
|
|
|
*
|
|
|
|
* Facebook reserves all rights not expressly granted.
|
2015-06-06 20:37:36 +00:00
|
|
|
*
|
2015-07-13 15:42:32 +00:00
|
|
|
* 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.
|
2015-06-06 20:37:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
|
|
|
#import <OCMock/OCMock.h>
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
|
2016-02-03 13:22:11 +00:00
|
|
|
@interface RCTTestEvent : NSObject <RCTEvent>
|
|
|
|
@property (atomic, assign, readwrite) BOOL canCoalesce;
|
2015-06-06 20:37:36 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTTestEvent
|
2016-02-03 13:22:11 +00:00
|
|
|
{
|
|
|
|
NSDictionary<NSString *, id> *_body;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize viewTag = _viewTag;
|
|
|
|
@synthesize eventName = _eventName;
|
|
|
|
@synthesize body = _body;
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (instancetype)initWithViewTag:(NSNumber *)viewTag eventName:(NSString *)eventName body:(NSDictionary<NSString *, id> *)body
|
2015-06-06 20:37:36 +00:00
|
|
|
{
|
2016-02-03 13:22:11 +00:00
|
|
|
if (self = [super init]) {
|
|
|
|
_viewTag = viewTag;
|
|
|
|
_eventName = eventName;
|
|
|
|
_body = body;
|
|
|
|
_canCoalesce = YES;
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:22:11 +00:00
|
|
|
- (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent
|
|
|
|
{
|
|
|
|
return newEvent;
|
|
|
|
}
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
+ (NSString *)moduleDotMethod
|
|
|
|
{
|
|
|
|
return @"RCTDeviceEventEmitter.emit";
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RCTEventDispatcherTests : XCTestCase
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTEventDispatcherTests
|
|
|
|
{
|
|
|
|
id _bridge;
|
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
|
|
|
|
|
|
|
NSString *_eventName;
|
2015-11-14 18:25:00 +00:00
|
|
|
NSDictionary<NSString *, id> *_body;
|
2015-06-06 20:37:36 +00:00
|
|
|
RCTTestEvent *_testEvent;
|
|
|
|
NSString *_JSMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
[super setUp];
|
|
|
|
|
|
|
|
_bridge = [OCMockObject mockForClass:[RCTBridge class]];
|
2015-08-17 14:35:34 +00:00
|
|
|
_eventDispatcher = [RCTEventDispatcher new];
|
2015-11-25 11:09:00 +00:00
|
|
|
[_eventDispatcher setValue:_bridge forKey:@"bridge"];
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2015-08-11 13:37:12 +00:00
|
|
|
_eventName = RCTNormalizeInputEventName(@"sampleEvent");
|
2015-06-06 20:37:36 +00:00
|
|
|
_body = @{ @"foo": @"bar" };
|
|
|
|
_testEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
|
|
eventName:_eventName
|
|
|
|
body:_body];
|
|
|
|
|
|
|
|
_JSMethod = [[_testEvent class] moduleDotMethod];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testLegacyEventsAreImmediatelyDispatched
|
|
|
|
{
|
|
|
|
[[_bridge expect] enqueueJSCall:_JSMethod
|
|
|
|
args:@[_eventName, _body]];
|
|
|
|
|
|
|
|
[_eventDispatcher sendDeviceEventWithName:_eventName body:_body];
|
|
|
|
|
|
|
|
[_bridge verify];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testNonCoalescingEventsAreImmediatelyDispatched
|
|
|
|
{
|
|
|
|
_testEvent.canCoalesce = NO;
|
|
|
|
[[_bridge expect] enqueueJSCall:_JSMethod
|
|
|
|
args:@[_eventName, _body]];
|
|
|
|
|
|
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
|
|
|
|
|
|
[_bridge verify];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testCoalescedEventShouldBeDispatchedOnFrameUpdate
|
|
|
|
{
|
|
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
|
|
|
|
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
|
|
args:@[_eventName, _body]];
|
|
|
|
|
|
|
|
[(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil];
|
|
|
|
|
|
|
|
[_bridge verify];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testBasicCoalescingReturnsLastEvent
|
|
|
|
{
|
|
|
|
RCTTestEvent *ignoredEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
|
|
eventName:_eventName
|
|
|
|
body:@{ @"other": @"body" }];
|
|
|
|
|
|
|
|
[_eventDispatcher sendEvent:ignoredEvent];
|
|
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
|
|
|
|
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
|
|
args:@[_eventName, _body]];
|
|
|
|
|
|
|
|
[(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil];
|
|
|
|
|
|
|
|
[_bridge verify];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testDifferentEventTypesDontCoalesce
|
|
|
|
{
|
2015-08-11 13:37:12 +00:00
|
|
|
NSString *firstEventName = RCTNormalizeInputEventName(@"firstEvent");
|
2015-06-06 20:37:36 +00:00
|
|
|
RCTTestEvent *firstEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
|
|
eventName:firstEventName
|
|
|
|
body:_body];
|
|
|
|
|
|
|
|
[_eventDispatcher sendEvent:firstEvent];
|
|
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
|
|
|
|
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
|
|
args:@[firstEventName, _body]];
|
|
|
|
|
|
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
|
|
args:@[_eventName, _body]];
|
|
|
|
|
|
|
|
[(id<RCTFrameUpdateObserver>)_eventDispatcher didUpdateFrame:nil];
|
|
|
|
|
|
|
|
[_bridge verify];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|