remove metrics tests

This commit is contained in:
Justin R. Miller
2015-10-27 10:45:10 -07:00
parent 48d6d039ed
commit 1421755434
2 changed files with 0 additions and 276 deletions

View File

@@ -1,272 +0,0 @@
#import "MGLMapboxEvents.h"
#import "KIFTestActor+MapboxGL.h"
#import <KIF/KIF.h>
#import <OCMock/OCMock.h>
#import "Mapbox.h"
#import "OHHTTPStubs.h"
const NSUInteger MGLMaximumEventsPerFlush = 20;
const NSTimeInterval MGLFlushInterval = 60;
@interface MGLMapboxEvents (Testing)
- (NSString *)appBundleId;
- (NSString *)token;
- (BOOL)isPaused;
- (NSURLSession *)session;
- (void)pushEvent:(NSString *)event withAttributes:(NSDictionary *)attributeDictionary;
- (NSMutableArray *)eventQueue;
- (void)postEvents:(NSArray *)events;
- (NSTimer *)timer;
- (void)flush;
- (void)startTimer;
@end
@interface MetricsTests : KIFTestCase
@end
@implementation MetricsTests
- (void)beforeAll {
[tester acknowledgeSystemAlert];
}
- (void)beforeEach {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MGLMapboxMetricsEnabled"];
[MGLMapboxEvents resumeMetricsCollection];
[MGLAccountManager setAccessToken:@"pk.eyJ1IjoianVzdGluIiwiYSI6IlpDbUJLSUEifQ.4mG8vhelFMju6HpIY-Hi5A"];
}
- (void)afterEach {
[MGLMapboxEvents pauseMetricsCollection];
}
- (void)pushFakeEvent {
[MGLMapboxEvents pushEvent:@"test_event" withAttributes:@{ @"foo" : @"one", @"bar" : @(2) }];
}
- (void)testAppBundleID {
XCTAssertEqualObjects([[MGLMapboxEvents sharedManager] appBundleId], @"com.mapbox.Mapbox-GL-Tests");
}
- (void)testResumeMetricsCollection {
[MGLMapboxEvents resumeMetricsCollection];
XCTAssertFalse([[MGLMapboxEvents sharedManager] isPaused]);
XCTAssertNotNil([[MGLMapboxEvents sharedManager] session]);
}
- (void)testPauseMetricsCollection {
[MGLMapboxEvents pauseMetricsCollection];
XCTAssertTrue([[MGLMapboxEvents sharedManager] isPaused]);
XCTAssertNil([[MGLMapboxEvents sharedManager] session]);
}
- (void)testFlushAtThreshold {
NSUInteger startCount = [[[MGLMapboxEvents sharedManager] eventQueue] count];
XCTestExpectation *queueItemsExpectation = [self expectationWithDescription:@"queue should contain events"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([[[MGLMapboxEvents sharedManager] eventQueue] count] > 0) {
[queueItemsExpectation fulfill];
break;
}
}
});
for (NSUInteger i = 0; i < (MGLMaximumEventsPerFlush - startCount - 1); i++) {
[self pushFakeEvent];
}
[self waitForExpectationsWithTimeout:1.0 handler:nil];
XCTestExpectation *emptyQueueExpectation = [self expectationWithDescription:@"queue should be empty"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([[[MGLMapboxEvents sharedManager] eventQueue] count] == 0) {
[emptyQueueExpectation fulfill];
break;
}
}
});
[self pushFakeEvent];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
- (void)testTimerDestroyedAfterFlush {
XCTestExpectation *emptyQueueExpectation = [self expectationWithDescription:@"queue should be empty"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([[[MGLMapboxEvents sharedManager] eventQueue] count] == 0) {
[emptyQueueExpectation fulfill];
break;
}
}
});
[[MGLMapboxEvents sharedManager] flush];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
XCTAssertNil([[MGLMapboxEvents sharedManager] timer]);
}
- (void)testTimerCreatedAfterEventPushed {
XCTestExpectation *emptyQueueExpectation = [self expectationWithDescription:@"queue should be empty"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([[[MGLMapboxEvents sharedManager] eventQueue] count] == 0) {
[emptyQueueExpectation fulfill];
break;
}
}
});
[[MGLMapboxEvents sharedManager] flush];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
id eventsMock = [OCMockObject partialMockForObject:[MGLMapboxEvents sharedManager]];
[[[eventsMock expect] andForwardToRealObject] startTimer];
[self pushFakeEvent];
[eventsMock verifyWithDelay:2.0];
XCTAssertEqual([[[MGLMapboxEvents sharedManager] eventQueue] count], 1);
XCTAssertNotNil([[MGLMapboxEvents sharedManager] timer]);
XCTAssertEqual(MGLFlushInterval, [[[MGLMapboxEvents sharedManager] timer] timeInterval]);
}
- (void)testTimerFiresFlush {
XCTestExpectation *emptyQueueExpectation = [self expectationWithDescription:@"queue should be empty"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([[[MGLMapboxEvents sharedManager] eventQueue] count] == 0) {
[emptyQueueExpectation fulfill];
break;
}
}
});
[[MGLMapboxEvents sharedManager] flush];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
[self pushFakeEvent];
id eventsMock = [OCMockObject partialMockForObject:[MGLMapboxEvents sharedManager]];
[[eventsMock expect] flush];
[eventsMock verifyWithDelay:MGLFlushInterval];
}
- (void)testFlushPostsEvents {
NSArray *events = [NSArray arrayWithArray:[[MGLMapboxEvents sharedManager] eventQueue]];
id eventsMock = [OCMockObject partialMockForObject:[MGLMapboxEvents sharedManager]];
[[eventsMock expect] postEvents:events];
[[MGLMapboxEvents sharedManager] flush];
[eventsMock verifyWithDelay:2.0];
XCTAssertTrue([[[MGLMapboxEvents sharedManager] eventQueue] count] == 0);
}
- (void)testPostEventsNetworkRequest {
XCTestExpectation *requestExpectation = [self expectationWithDescription:@"posting events generates network request"];
XCTestExpectation *contentTypeExpectation = [self expectationWithDescription:@"events are posted as JSON"];
XCTestExpectation *userAgentExpectation = [self expectationWithDescription:@"events are posted with proper user agent"];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.path hasPrefix:@"/events/v1"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
if ([request.URL.absoluteString isEqualToString:[@"https://api.tiles.mapbox.com/events/v1?access_token=" stringByAppendingString:[MGLAccountManager accessToken]]]) {
[requestExpectation fulfill];
}
if ([request.allHTTPHeaderFields[@"Content-Type"] isEqualToString:@"application/json"]) {
[contentTypeExpectation fulfill];
}
if ([request.allHTTPHeaderFields[@"User-Agent"] isEqualToString:[NSString stringWithFormat:@"%@/1.0/1.0 MapboxEventsiOS/1.0", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]]]) {
[userAgentExpectation fulfill];
}
return nil;
}];
[self pushFakeEvent];
[MGLMapboxEvents flush];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
- (void)testPushEventAddsToEventQueue {
XCTestExpectation *queueSizeIncreaseExpectation = [self expectationWithDescription:@"event queue size increase"];
NSUInteger beforeCount = [[[MGLMapboxEvents sharedManager] eventQueue] count];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([[[MGLMapboxEvents sharedManager] eventQueue] count] == (beforeCount + 1)) {
[queueSizeIncreaseExpectation fulfill];
break;
}
}
});
[self pushFakeEvent];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
- (void)testOptOutUpfrontDisablesMetrics {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"MGLMapboxMetricsEnabled"];
NSUInteger startingCount = [[[MGLMapboxEvents sharedManager] eventQueue] count];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([[[MGLMapboxEvents sharedManager] eventQueue] count] > startingCount) {
XCTFail(@"queue should not receive events when opted-out");
break;
}
}
});
[self pushFakeEvent];
[tester waitForTimeInterval:1.0];
}
- (void)testOptOutAtRuntimeDisablesMetrics {
XCTAssertTrue([[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsEnabled"]);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.path hasPrefix:@"/events/v1"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
XCTFail(@"opt-out should not post events");
return nil;
}];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"MGLMapboxMetricsEnabled"];
[self pushFakeEvent];
[[MGLMapboxEvents sharedManager] flush];
[tester waitForTimeInterval:2.0];
}
@end

View File

@@ -14,7 +14,6 @@
DD043366196DBBE000E6F39D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DD043365196DBBE000E6F39D /* main.m */; };
DD0580E81ACB628200B112C9 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD0580E71ACB628200B112C9 /* IOKit.framework */; };
DD0581041ACB661200B112C9 /* Headers in Resources */ = {isa = PBXBuildFile; fileRef = DD0581031ACB661200B112C9 /* Headers */; };
DD0E6F671B01806600DC035A /* MetricsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0E6F661B01806600DC035A /* MetricsTests.m */; };
DD0E6F841B0190E200DC035A /* libOCMock.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DD0E6F701B0190E200DC035A /* libOCMock.a */; };
DD0E6F981B01B68E00DC035A /* OHHTTPStubs.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0E6F8E1B01B68E00DC035A /* OHHTTPStubs.m */; };
DD0E6F991B01B68E00DC035A /* OHHTTPStubs+NSURLSessionConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0E6F8F1B01B68E00DC035A /* OHHTTPStubs+NSURLSessionConfiguration.m */; };
@@ -89,7 +88,6 @@
DD0580E71ACB628200B112C9 /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/IOKit.framework; sourceTree = DEVELOPER_DIR; };
DD0580EF1ACB62BE00B112C9 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
DD0581031ACB661200B112C9 /* Headers */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Headers; path = ../../build/ios/pkg/static/Headers; sourceTree = SOURCE_ROOT; };
DD0E6F661B01806600DC035A /* MetricsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MetricsTests.m; sourceTree = SOURCE_ROOT; };
DD0E6F701B0190E200DC035A /* libOCMock.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libOCMock.a; path = OCMock/libOCMock.a; sourceTree = SOURCE_ROOT; };
DD0E6F721B0190E200DC035A /* NSNotificationCenter+OCMAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSNotificationCenter+OCMAdditions.h"; sourceTree = "<group>"; };
DD0E6F731B0190E200DC035A /* OCMArg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCMArg.h; sourceTree = "<group>"; };
@@ -283,7 +281,6 @@
DDBD016B196DC4740033959E /* KIFTestActor+MapboxGL.h */,
DDBD016A196DC4740033959E /* KIFTestActor+MapboxGL.m */,
DDBD0168196DC4740033959E /* MapViewTests.m */,
DD0E6F661B01806600DC035A /* MetricsTests.m */,
DDBD0167196DC46B0033959E /* Supporting Files */,
DDBD014D196DC3B00033959E /* KIF */,
DD0E6F6B1B01906600DC035A /* OCMock */,
@@ -471,7 +468,6 @@
DDBD016D196DC4740033959E /* KIFTestActor+MapboxGL.m in Sources */,
DD0E6F991B01B68E00DC035A /* OHHTTPStubs+NSURLSessionConfiguration.m in Sources */,
DD0E6F9A1B01B68E00DC035A /* OHHTTPStubsResponse.m in Sources */,
DD0E6F671B01806600DC035A /* MetricsTests.m in Sources */,
DDBD016C196DC4740033959E /* MapViewTests.m in Sources */,
DD0E6F9D1B01B68E00DC035A /* OHPathHelpers.m in Sources */,
);