Skip to content

Commit

Permalink
Merge pull request rakutentech#15 in ECO/alpha-ios-perftracking from …
Browse files Browse the repository at this point in the history
…~DONNIE.SMITH/perftracking_ds:bugfix/rem-20877-uicontroltests-queuefix to master

* commit '024c92636056601d23b36af57c55bb4b88f0c0fe':
  REM-20877 Add a flag `forceTrackingEnabled` to the TrackingManager API
  REM-20877 Move HostApp into Tests folder
  • Loading branch information
Smith, Donnie | Donnie | SSED committed Apr 18, 2017
2 parents 130f6fb + 024c926 commit eb87bed
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 51 deletions.
8 changes: 4 additions & 4 deletions RPerformanceTracking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
69194F731CED9D3500879A11 /* Tests */ = {
isa = PBXGroup;
children = (
5A8CF0D31E9CAECD006E7544 /* HostApp */,
5A8CF0D21E9CAECD006E7544 /* HostApp.app */,
69194F761CED9D3500879A11 /* Info.plist */,
5AB9C6631E8366300066C5CB /* EventWriterTests.m */,
6D3DBD491E824A9B00B97E89 /* SenderTests.m */,
Expand All @@ -124,8 +126,6 @@
children = (
69194F731CED9D3500879A11 /* Tests */,
69194F721CED9D3400879A11 /* Tests.xctest */,
5A8CF0D31E9CAECD006E7544 /* HostApp */,
5A8CF0D21E9CAECD006E7544 /* HostApp.app */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -323,7 +323,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_FILE = HostApp/Info.plist;
INFOPLIST_FILE = "$(SRCROOT)/Tests/HostApp/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
Expand Down Expand Up @@ -369,7 +369,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_FILE = HostApp/Info.plist;
INFOPLIST_FILE = "$(SRCROOT)/Tests/HostApp/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
Expand Down
1 change: 1 addition & 0 deletions RPerformanceTracking/Private/_RPTTrackingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RPT_EXPORT @interface _RPTTrackingManager : NSObject
@property (nonatomic, readonly) _RPTRingBuffer *ringBuffer;
@property (nonatomic, readonly) _RPTTracker *tracker;
@property (nonatomic, readonly) _RPTSender *sender;
@property (class, nonatomic) BOOL forceTrackingEnabled;

+ (instancetype)sharedInstance;

Expand Down
18 changes: 16 additions & 2 deletions RPerformanceTracking/Private/_RPTTrackingManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,32 @@ @interface _RPTTrackingManager()

/* RPT_EXPORT */ @implementation _RPTTrackingManager

static BOOL _forceTrackingEnabled = NO;

+ (BOOL)forceTrackingEnabled
{
return _forceTrackingEnabled;
}

+ (void)setForceTrackingEnabled:(BOOL)enabled
{
_forceTrackingEnabled = enabled;
}

- (instancetype)init
{
if ((self = [super init]))
{
if ((_configuration = [_RPTConfiguration loadConfiguration]))
if ((_configuration = [_RPTConfiguration loadConfiguration]) ||
_RPTTrackingManager.forceTrackingEnabled)
{
/*
* Should we activate tracking?
*/

double random_value = (double)arc4random() / 0x100000000;
if (random_value >= 1.0 - _configuration.activationRatio)
if ((random_value >= 1.0 - _configuration.activationRatio) ||
_RPTTrackingManager.forceTrackingEnabled)
{
// Activated!
_trackingData = [NSMutableDictionary.alloc initWithCapacity:TRACKING_DATA_LIMIT];
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 19 additions & 35 deletions Tests/UIControlTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#import "_RPTTracker.h"
#import "_RPTTrackingManager.h"
#import "_RPTRingBuffer.h"
#import "TestViewController.h"
#import "_RPTMetric.h"
#import "_RPTSender.h"
#import "TestViewController.h"

@interface UIControl ()
- (void)_rpt_sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;
Expand All @@ -16,42 +17,41 @@ @interface _RPTTrackingManager()
- (instancetype)init;
@end

/*
* We need HostApp for these unit tests, because the events are managed by UIApplication's object.
*/

@interface UIControlTests : XCTestCase
@property (nonatomic) _RPTTrackingManager *trackingManager;
@end

@implementation UIControlTests

/*
* We need HostApp for the unit tests, because the events are managed by UIApplication's object.
*/
static _RPTTrackingManager *_trackingManager = nil;

- (void)setUp
+ (void)setUp
{
[super setUp];
_RPTRingBuffer *ringBuffer = [_RPTRingBuffer.alloc initWithSize:512];
_RPTMetric *currentMetric = _RPTMetric.new;
_RPTTracker *tracker = [_RPTTracker.alloc initWithRingBuffer:ringBuffer currentMetric:currentMetric];
_trackingManager = [_RPTTrackingManager.alloc init];
_trackingManager.tracker = tracker;
_RPTTrackingManager.forceTrackingEnabled = YES;
_trackingManager = [_RPTTrackingManager sharedInstance];
_trackingManager.tracker = [_RPTTracker.alloc initWithRingBuffer:[_RPTRingBuffer.alloc initWithSize:512]
currentMetric:_RPTMetric.new];
}

- (void)tearDown
+ (void)tearDown
{
[_trackingManager.sender stop];
_trackingManager = nil;
[super tearDown];
}

- (void)testSwizzlingMethodIsAddedToClass
{
UIControl *control = [[UIControl alloc] init];
XCTAssert([control respondsToSelector:@selector(_rpt_sendAction:to:forEvent:)]);
XCTAssert([UIControl.new respondsToSelector:@selector(_rpt_sendAction:to:forEvent:)]);
}

- (void)testOriginalMethodIsPresentInClass
{
UIControl *control = [[UIControl alloc] init];
XCTAssert([control respondsToSelector:@selector(sendAction:to:forEvent:)]);
XCTAssert([UIControl.new respondsToSelector:@selector(sendAction:to:forEvent:)]);
}

- (void)testTargetActionMethodIsCalledWhenControlEventEndsMetric
Expand Down Expand Up @@ -92,10 +92,6 @@ - (void)testEndMetricMethodIsCalledWhenControlEventIsValueChanged
- (void)testEndMetricMethodIsNotCalledWhenControlEventDoesNotEndMetric
{
id mockTracker = OCMPartialMock(_trackingManager.tracker);

// Need to mock the trackingManager class, because the [TrackingManager sharedInstance] depends on random_value, so it makes the unit test is not stable.
id trackingManagerClassMock = OCMClassMock([_RPTTrackingManager class]);
OCMStub([trackingManagerClassMock sharedInstance]).andReturn(self.trackingManager);

OCMStub([mockTracker endMetric]).andDo(^(NSInvocation *invocation) {
XCTFail(@"endMetric should not be called");
Expand All @@ -106,7 +102,6 @@ - (void)testEndMetricMethodIsNotCalledWhenControlEventDoesNotEndMetric
[control sendActionsForControlEvents:UIControlEventTouchDown];

[mockTracker stopMocking];
[trackingManagerClassMock stopMocking];
}

#pragma mark - Helpers
Expand All @@ -127,43 +122,32 @@ - (void)assertThatEndMetricIsCalledForEvent:(UIControlEvents)event
{
id mockTracker = OCMPartialMock(_trackingManager.tracker);

// Need to mock the trackingManager class, because the [TrackingManager sharedInstance] depends on random_value, so it makes the unit test is not stable.
id trackingManagerClassMock = OCMClassMock([_RPTTrackingManager class]);
OCMStub([trackingManagerClassMock sharedInstance]).andReturn(self.trackingManager);

UIControl *control = [UIControl.alloc init];
[control addTarget:self action:@selector(targetActionMethod) forControlEvents:event];
[control sendActionsForControlEvents:event];

OCMVerify([mockTracker endMetric]);

[mockTracker stopMocking];
[trackingManagerClassMock stopMocking];
}

- (void)assertThatEndMetricIsCalledOnReceiptOfNotification:(NSString *)notification
{
id mockTracker = OCMPartialMock(_trackingManager.tracker);

// Need to mock the trackingManager class, because the [TrackingManager sharedInstance] depends on random_value, so it makes the unit test is not stable.
id trackingManagerClassMock = OCMClassMock([_RPTTrackingManager class]);
OCMStub([trackingManagerClassMock sharedInstance]).andReturn(self.trackingManager);

XCTestExpectation *expectNotification = [self expectationWithDescription:@"waitForNotification"];

[NSNotificationCenter.defaultCenter postNotificationName:notification object:nil];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[expectNotification fulfill];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

OCMVerify([mockTracker endMetric]);
[expectNotification fulfill];
});

[mockTracker stopMocking];
[trackingManagerClassMock stopMocking];

[self waitForExpectationsWithTimeout:2.5 handler:nil];
[self waitForExpectationsWithTimeout:2 handler:nil];
}

- (void)targetActionMethod
Expand Down
16 changes: 6 additions & 10 deletions Tests/UIViewControllerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,15 @@ - (void)testViewDidLoadMethodIsCalledWhenViewControllerIsInitWithNibName

- (void)testTrackingIdentifierIsValid
{
_RPTRingBuffer *ringBuffer = [_RPTRingBuffer.alloc initWithSize:MAX_MEASUREMENTS];
_RPTMetric *currentMetric = _RPTMetric.new;
_RPTTrackingManager *trackingManager = [_RPTTrackingManager.alloc init];
_RPTTracker *tracker = [_RPTTracker.alloc initWithRingBuffer:ringBuffer currentMetric:currentMetric];
trackingManager.tracker = tracker;

// Need to mock the trackingManager class, because the [TrackingManager sharedInstance] depends on random_value, so it makes the unit test is not stable.
id trackingManagerClassMock = OCMClassMock([_RPTTrackingManager class]);
OCMStub([trackingManagerClassMock sharedInstance]).andReturn(trackingManager);
_RPTRingBuffer *ringBuffer = [_RPTRingBuffer.alloc initWithSize:MAX_MEASUREMENTS];
_RPTMetric *currentMetric = _RPTMetric.new;
_RPTTrackingManager.forceTrackingEnabled = YES;
_RPTTrackingManager *trackingManager = [_RPTTrackingManager.alloc init];
_RPTTracker *tracker = [_RPTTracker.alloc initWithRingBuffer:ringBuffer currentMetric:currentMetric];
trackingManager.tracker = tracker;

UIViewController *presentedVC = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:self.testBundle];
[self.presentingViewController presentViewController:presentedVC animated:YES completion:nil];
XCTAssertTrue(presentedVC._rpt_trackingIdentifier != 0);
[trackingManagerClassMock stopMocking];
}
@end

0 comments on commit eb87bed

Please sign in to comment.