Created
March 29, 2018 04:15
-
-
Save levantAJ/fbc462891a4a9eddf85a995e2b319688 to your computer and use it in GitHub Desktop.
Combinations test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ColdStartEventManagerTest.m | |
// InteractorTest | |
// | |
// Created by BBM on 28/3/18. | |
// | |
#import "ColdStartEventManager.h" | |
#import "AhoyReporterProtocol.h" | |
@interface ColdStartEventManager() | |
@property (nonatomic, strong) id<AhoyReporterProtocol> ahoyReporter; | |
@property (nonatomic, assign) CFTimeInterval coldStartTime; | |
@property (nonatomic, assign) CFTimeInterval setupStateSuccessTime; | |
@property (nonatomic, assign) CFTimeInterval didBecomeActiveTime; | |
@property (nonatomic, assign) CFTimeInterval didFinishLaunchingTime; | |
@property (nonatomic, assign) BOOL eventSent; | |
@end | |
@implementation ColdStartEventManager | |
- (instancetype)initWithAhoryReporter:(id<AhoyReporterProtocol>)ahoyReporter { | |
self = [super init]; | |
if (self) { | |
_ahoyReporter = ahoyReporter; | |
} | |
return self; | |
} | |
- (void)setTime:(CFTimeInterval)time forPhase:(ColdStartEventPhase)phase { | |
if (time <= 0) { | |
return; | |
} | |
switch (phase) { | |
case ColdStartEventPhaseDidFinishLaunching: | |
self.didFinishLaunchingTime = time; | |
break; | |
case ColdStartEventPhaseDidBecomeActive: | |
self.didBecomeActiveTime = time; | |
break; | |
case ColdStartEventPhaseSetupStateSuccess: | |
self.setupStateSuccessTime = time; | |
break; | |
case ColdStartEventPhaseTotal: | |
self.coldStartTime = time; | |
break; | |
} | |
if (self.didFinishLaunchingTime > 0 | |
&& self.didBecomeActiveTime > 0 | |
&& self.setupStateSuccessTime > 0 | |
&& self.coldStartTime > 0 | |
&& !self.eventSent) { | |
NSDictionary *properties = @{ | |
@"coldstart_time":@(self.coldStartTime), | |
@"setup_state_success_time":@(self.setupStateSuccessTime), | |
@"did_finish_launching_time":@(self.didFinishLaunchingTime), | |
@"did_become_active_time":@(self.didBecomeActiveTime) | |
}; | |
[self.ahoyReporter sendEventsWithEventName:BBM_COLD_START_TIME | |
action:nil | |
properties:properties | |
responseBlock:nil]; | |
self.eventSent = YES; | |
} | |
} | |
@end | |
////////////////////// | |
@interface ColdStartEventManagerTest : XCTestCase | |
@property (nonatomic, strong) ColdStartEventManager *sut; | |
@property (nonatomic, strong) id<AhoyReporterProtocol> ahoyReporter; | |
@end | |
@implementation ColdStartEventManagerTest | |
- (char *)filePath { return __FILE__; } | |
- (void)setUp { | |
[super setUp]; | |
self.ahoyReporter = OCMProtocolMock(@protocol(AhoyReporterProtocol)); | |
self.sut = [[ColdStartEventManager alloc] initWithAhoryReporter:self.ahoyReporter]; | |
} | |
- (void)tearDown { | |
self.ahoyReporter = nil; | |
self.sut = nil; | |
[super tearDown]; | |
} | |
- (void)testSetNonZeroTimeCombinations { | |
//Given: | |
CFTimeInterval coldStartTime = 1.23; | |
CFTimeInterval setupStateSuccessTime = 2.34; | |
CFTimeInterval didBecomeActiveTime = 3.45; | |
CFTimeInterval didFinishLaunchingTime = 0.0000000012; | |
NSDictionary *properties = @{@"coldstart_time":@(coldStartTime), | |
@"setup_state_success_time":@(setupStateSuccessTime), | |
@"did_finish_launching_time":@(didFinishLaunchingTime), | |
@"did_become_active_time":@(didBecomeActiveTime)}; | |
for (int i = 0; i < 16; i++) { | |
@autoreleasepool { | |
if (i != 15) { | |
OCMReject([self.ahoyReporter sendEventsWithEventName:[OCMArg any] action:[OCMArg any] properties:[OCMArg any] responseBlock:[OCMArg any]]); | |
} | |
//When: | |
if (i & (1 << ColdStartEventPhaseDidFinishLaunching)) { | |
[self.sut setTime:didFinishLaunchingTime forPhase:ColdStartEventPhaseDidFinishLaunching]; | |
} | |
if (i & (1 << ColdStartEventPhaseDidBecomeActive)) { | |
[self.sut setTime:didBecomeActiveTime forPhase:ColdStartEventPhaseDidBecomeActive]; | |
} | |
if (i & (1 << ColdStartEventPhaseSetupStateSuccess)) { | |
[self.sut setTime:setupStateSuccessTime forPhase:ColdStartEventPhaseSetupStateSuccess]; | |
} | |
if (i & (1 << ColdStartEventPhaseTotal)) { | |
[self.sut setTime:coldStartTime forPhase:ColdStartEventPhaseTotal]; | |
} | |
//Then: | |
if (i == 15) { | |
OCMVerify([self.ahoyReporter sendEventsWithEventName:BBM_COLD_START_TIME action:nil properties:properties responseBlock:nil]); | |
} | |
} | |
[self tearDown]; | |
[self setUp]; | |
} | |
} | |
- (void)testSetZeroTimeCombinations { | |
//Given: | |
CFTimeInterval coldStartTime = 1.23; | |
CFTimeInterval setupStateSuccessTime = 2.34; | |
CFTimeInterval didBecomeActiveTime = 3.45; | |
CFTimeInterval didFinishLaunchingTime = 4.56; | |
NSDictionary *properties = @{@"coldstart_time":@(coldStartTime), | |
@"setup_state_success_time":@(setupStateSuccessTime), | |
@"did_finish_launching_time":@(didFinishLaunchingTime), | |
@"did_become_active_time":@(didBecomeActiveTime)}; | |
for (int i = 0; i < 16; i++) { | |
@autoreleasepool { | |
if (i != 15) { | |
OCMReject([self.ahoyReporter sendEventsWithEventName:[OCMArg any] action:[OCMArg any] properties:[OCMArg any] responseBlock:[OCMArg any]]); | |
} | |
//When: | |
if (i & (1 << ColdStartEventPhaseDidFinishLaunching)) { | |
[self.sut setTime:didFinishLaunchingTime forPhase:ColdStartEventPhaseDidFinishLaunching]; | |
} else { | |
[self.sut setTime:0 forPhase:ColdStartEventPhaseDidFinishLaunching]; | |
} | |
if (i & (1 << ColdStartEventPhaseDidBecomeActive)) { | |
[self.sut setTime:didBecomeActiveTime forPhase:ColdStartEventPhaseDidBecomeActive]; | |
} else { | |
[self.sut setTime:0 forPhase:ColdStartEventPhaseDidBecomeActive]; | |
} | |
if (i & (1 << ColdStartEventPhaseSetupStateSuccess)) { | |
[self.sut setTime:setupStateSuccessTime forPhase:ColdStartEventPhaseSetupStateSuccess]; | |
} else { | |
[self.sut setTime:0 forPhase:ColdStartEventPhaseSetupStateSuccess]; | |
} | |
if (i & (1 << ColdStartEventPhaseTotal)) { | |
[self.sut setTime:coldStartTime forPhase:ColdStartEventPhaseTotal]; | |
} else { | |
[self.sut setTime:0 forPhase:ColdStartEventPhaseTotal]; | |
} | |
//Then: | |
if (i == 15) { | |
OCMVerify([self.ahoyReporter sendEventsWithEventName:BBM_COLD_START_TIME action:nil properties:properties responseBlock:nil]); | |
} | |
} | |
[self tearDown]; | |
[self setUp]; | |
} | |
} | |
- (void)testTimesLessThanZero { | |
//Given: | |
CFTimeInterval coldStartTime = 1.23; | |
CFTimeInterval setupStateSuccessTime = 2.34; | |
CFTimeInterval didBecomeActiveTime = 3.45; | |
CFTimeInterval didFinishLaunchingTime = 4.56; | |
NSDictionary *properties = @{@"coldstart_time":@(coldStartTime), | |
@"setup_state_success_time":@(setupStateSuccessTime), | |
@"did_finish_launching_time":@(didFinishLaunchingTime), | |
@"did_become_active_time":@(didBecomeActiveTime)}; | |
for (int i = 0; i < 16; i++) { | |
@autoreleasepool { | |
if (i != 15) { | |
OCMReject([self.ahoyReporter sendEventsWithEventName:[OCMArg any] action:[OCMArg any] properties:[OCMArg any] responseBlock:[OCMArg any]]); | |
} | |
//When: | |
if (i & (1 << ColdStartEventPhaseDidFinishLaunching)) { | |
[self.sut setTime:didFinishLaunchingTime forPhase:ColdStartEventPhaseDidFinishLaunching]; | |
} else { | |
[self.sut setTime:-1 forPhase:ColdStartEventPhaseDidFinishLaunching]; | |
} | |
if (i & (1 << ColdStartEventPhaseDidBecomeActive)) { | |
[self.sut setTime:didBecomeActiveTime forPhase:ColdStartEventPhaseDidBecomeActive]; | |
} else { | |
[self.sut setTime:-2 forPhase:ColdStartEventPhaseDidBecomeActive]; | |
} | |
if (i & (1 << ColdStartEventPhaseSetupStateSuccess)) { | |
[self.sut setTime:setupStateSuccessTime forPhase:ColdStartEventPhaseSetupStateSuccess]; | |
} else { | |
[self.sut setTime:-3 forPhase:ColdStartEventPhaseSetupStateSuccess]; | |
} | |
if (i & (1 << ColdStartEventPhaseTotal)) { | |
[self.sut setTime:coldStartTime forPhase:ColdStartEventPhaseTotal]; | |
} else { | |
[self.sut setTime:-4 forPhase:ColdStartEventPhaseTotal]; | |
} | |
//Then: | |
if (i == 15) { | |
OCMVerify([self.ahoyReporter sendEventsWithEventName:BBM_COLD_START_TIME action:nil properties:properties responseBlock:nil]); | |
} | |
} | |
[self tearDown]; | |
[self setUp]; | |
} | |
} | |
- (void)testEventIsFiredOnlyOnce { | |
//Given: | |
CFTimeInterval coldStartTime = 1.23; | |
CFTimeInterval setupStateSuccessTime = 2.34; | |
CFTimeInterval didBecomeActiveTime = 3.45; | |
CFTimeInterval didFinishLaunchingTime = 4.56; | |
__block NSUInteger count = 0; | |
[OCMStub([self.ahoyReporter sendEventsWithEventName:[OCMArg any] action:[OCMArg any] properties:[OCMArg any] responseBlock:nil]) andDo:^(NSInvocation *invocation) { | |
count++; | |
}]; | |
//When: | |
[self.sut setTime:didFinishLaunchingTime forPhase:ColdStartEventPhaseDidFinishLaunching]; | |
[self.sut setTime:didBecomeActiveTime forPhase:ColdStartEventPhaseDidBecomeActive]; | |
[self.sut setTime:setupStateSuccessTime forPhase:ColdStartEventPhaseSetupStateSuccess]; | |
[self.sut setTime:coldStartTime forPhase:ColdStartEventPhaseTotal]; | |
[self.sut setTime:coldStartTime forPhase:ColdStartEventPhaseTotal]; | |
//Then: | |
[self assertNumber:@(count) equalTo:@(1) line:__LINE__]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment