Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NOTES="$2"

# Update constant in codebase
sed -i '' 's/NSString \*const kMParticleSDKVersion = @".*/NSString *const kMParticleSDKVersion = @"'"$VERSION"'";/' mParticle-Apple-SDK/MPIConstants.m
sed -i '' 's/let kMParticleSDKVersion = ".*/let kMParticleSDKVersion = "'"$VERSION"'"/' mParticle-Apple-SDK/MPConstants.swift

# Update framework plist file
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $VERSION" Framework/Info.plist
Expand Down
9 changes: 5 additions & 4 deletions UnitTests/MPURLRequestBuilderTests.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import "mParticle.h"
#import "MPURLRequestBuilder.h"
#import "MPStateMachine.h"
#import "MPIConstants.h"
Expand All @@ -14,18 +15,18 @@
#import "MPMessage.h"
#import "MPBaseTestCase.h"
#import "MPKitConfiguration.h"
#import "MParticleWebView.h"
#import "MPExtensionProtocol.h"
#import "MPURL.h"
#import "MPUpload.h"
#import "mParticle.h"
#import "MParticleSwift.h"


@interface MParticle ()

+ (dispatch_queue_t)messageQueue;
@property (nonatomic, strong) MPStateMachine_PRIVATE *stateMachine;
@property (nonatomic, strong) MPKitContainer_PRIVATE *kitContainer_PRIVATE;
@property (nonatomic, strong) MParticleWebView *webView;
@property (nonatomic, strong) MParticleWebView_PRIVATE *webView;

@end

Expand Down Expand Up @@ -370,7 +371,7 @@ - (void)testInvalidURLs {

- (void)testEventRequest {
MParticle *sharedInstance = [MParticle sharedInstance];
MParticleWebView *webview = sharedInstance.webView;
MParticleWebView_PRIVATE *webview = sharedInstance.webView;
NSString *agent = @"Example resolved agent";

id mockWebView = OCMPartialMock(webview);
Expand Down
7 changes: 3 additions & 4 deletions UnitTests/MParticleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#import "MPSession.h"
#import "MPBackendController.h"
#import "MPURLRequestBuilder.h"
#import "MParticleWebView.h"
#import "MPPersistenceController.h"
#import "MPIUserDefaults.h"
#import "MPURL.h"
Expand All @@ -26,7 +25,7 @@ + (dispatch_queue_t)messageQueue;
@property (nonatomic, strong) MParticleOptions *options;
- (BOOL)isValidBridgeName:(NSString *)bridgeName;
- (void)handleWebviewCommand:(NSString *)command dictionary:(NSDictionary *)dictionary;
@property (nonatomic, strong) MParticleWebView *webView;
@property (nonatomic, strong) MParticleWebView_PRIVATE *webView;
@end

@interface MParticleUser ()
Expand Down Expand Up @@ -980,7 +979,7 @@ - (void)testSetATTStatusRemoveIDFA {
}

- (void)testUserAgentDefault {
id mockWebView = OCMClassMock([MParticleWebView class]);
id mockWebView = OCMClassMock([MParticleWebView_PRIVATE class]);
#if TARGET_OS_IOS == 1
[[[mockWebView stub] andReturn:@"Example resolved agent"] userAgent];
#else
Expand All @@ -1003,7 +1002,7 @@ - (void)testUserAgentDefault {

- (void)testUserAgentCustom {
NSString *customAgent = @"Foo 1.2.3 Like Bar";
id mockWebView = OCMClassMock([MParticleWebView class]);
id mockWebView = OCMClassMock([MParticleWebView_PRIVATE class]);
[[[mockWebView stub] andReturn:customAgent] userAgent];
id mockMParticle = OCMPartialMock([MParticle sharedInstance]);
[[[mockMParticle stub] andReturn:mockWebView] webView];
Expand Down
67 changes: 15 additions & 52 deletions UnitTests/MParticleWebViewTests.m
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import "MParticleWebView.h"
#import "mParticle.h"
#import "MPApplication.h"
#import "MParticleSwift.h"
#import <UIKit/UIKit.h>

@interface MPApplication_PRIVATE ()
+ (void)setMockApplication:(id)mockApplication;
@end

@interface MParticleWebView ()

- (void)evaluateAgent;
- (BOOL)canAndShouldCollect;

@interface MParticleWebView_PRIVATE ()
@property (nonatomic) NSDate *initializedDate;
@property (nonatomic) NSString *resolvedAgent;
@property (nonatomic) NSString *resolvedUserAgent;
@property (nonatomic, assign) BOOL isCollecting;
@property (nonatomic, assign) int retryCount;

#if TARGET_OS_IOS == 1
@property (nonatomic) WKWebView *webView;
#endif

@end

@interface MParticleWebViewTests : XCTestCase

@property (nonatomic, strong) MParticleWebView *webView;

@property (nonatomic, strong) MParticleWebView_PRIVATE *webView;
@end

@implementation MParticleWebViewTests

- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
_webView = [[MParticleWebView alloc] init];
dispatch_queue_t messageQueue = dispatch_queue_create("com.mparticle.messageQueue", DISPATCH_QUEUE_SERIAL);
_webView = [[MParticleWebView_PRIVATE alloc] initWithMessageQueue:messageQueue];
}

- (void)tearDown {
Expand All @@ -48,84 +36,59 @@ - (void)testInit {
}

- (void)testUserAgentCustom {
[_webView startWithCustomUserAgent:@"Test User Agent" shouldCollect:NO defaultAgentOverride:nil];
[_webView startWithCustomUserAgent:@"Test User Agent" shouldCollect:NO defaultUserAgentOverride:nil];
XCTAssertEqualObjects(_webView.userAgent, @"Test User Agent");
}

- (void)testUserAgentDisabled {
[_webView startWithCustomUserAgent:nil shouldCollect:NO defaultAgentOverride:nil];
[_webView startWithCustomUserAgent:nil shouldCollect:NO defaultUserAgentOverride:nil];
NSString *defaultAgent = [NSString stringWithFormat:@"mParticle Apple SDK/%@", MParticle.sharedInstance.version];
XCTAssertEqualObjects(_webView.userAgent, defaultAgent);
}

- (void)testUserAgentDefaultOverride {
[_webView startWithCustomUserAgent:nil shouldCollect:NO defaultAgentOverride:@"Test User Agent"];
[_webView startWithCustomUserAgent:nil shouldCollect:NO defaultUserAgentOverride:@"Test User Agent"];
NSString *defaultAgent = [NSString stringWithFormat:@"mParticle Apple SDK/%@", MParticle.sharedInstance.version];
XCTAssertNotEqualObjects(_webView.userAgent, defaultAgent);
XCTAssertEqualObjects(_webView.userAgent, @"Test User Agent");
}

- (void)testUserAgentCapture {
MParticleWebView *mockWebView = OCMPartialMock(_webView);
#if TARGET_OS_IOS == 1
[[(id)mockWebView expect] evaluateAgent];
#else
[[(id)mockWebView reject] evaluateAgent];
#endif
[mockWebView startWithCustomUserAgent:nil shouldCollect:YES defaultAgentOverride:nil];
[(id)mockWebView verify];
}

- (void)testShouldCollectResolved {
_webView.resolvedAgent = @"Test User Agent";
_webView.resolvedUserAgent = @"Test User Agent";
XCTAssertFalse([_webView shouldDelayUpload:5]);
}

- (void)testShouldCollectPending {
_webView.resolvedAgent = nil;
_webView.resolvedUserAgent = nil;
_webView.isCollecting = YES;
_webView.initializedDate = [NSDate date];
XCTAssertTrue([_webView shouldDelayUpload:5]);
}

- (void)testShouldCollectNoDate {
_webView.resolvedAgent = nil;
_webView.resolvedUserAgent = nil;
_webView.isCollecting = YES;
_webView.initializedDate = nil;
XCTAssertFalse([_webView shouldDelayUpload:5]);
}

- (void)testShouldCollectTooLong {
_webView.resolvedAgent = nil;
_webView.resolvedUserAgent = nil;
_webView.isCollecting = YES;
_webView.initializedDate = [NSDate dateWithTimeIntervalSinceNow:-6];
XCTAssertFalse([_webView shouldDelayUpload:5]);
}

- (void)testShouldCollectTimeLeft {
_webView.resolvedAgent = nil;
_webView.resolvedUserAgent = nil;
_webView.isCollecting = YES;
_webView.initializedDate = [NSDate dateWithTimeIntervalSinceNow:-4];
XCTAssertTrue([_webView shouldDelayUpload:5]);
}

- (void)testOriginalDefaultAgent {
NSString *defaultAgent = [NSString stringWithFormat:@"mParticle Apple SDK/%@", MParticle.sharedInstance.version];
XCTAssertEqualObjects(_webView.originalDefaultAgent, defaultAgent);
}

- (void)testBackgroundCollection {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this test and the other removed for any particular reason? I'm not against it as they seem to be more testing mocking and Apple code but I'm curious if that's your reson

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesh, so the 2 tests I removed were because OCMock wasn't properly detecting the call of the method (the "expect") even though it was 100% being called, confirmed via breakpoints. Seems to be some incompatibility with OCMock and Swift. In any case, when I looked closer at what was really being tested, it was just testing that a method was called that is literally always called, it wasn't really testing the logic.

Basically on iOS evaluateAgent is always called after calling startWithCustomUserAgent and on tvOS it never is, so this and the other test I removed ultimately were just testing that the #if os(iOS) worked, which isn't really useful.

id mockApplication = OCMClassMock([UIApplication class]);
OCMStub([mockApplication applicationState]).andReturn(UIApplicationStateBackground);
[MPApplication_PRIVATE setMockApplication:mockApplication];
MParticleWebView *mockWebView = OCMPartialMock(_webView);
#if TARGET_OS_IOS == 1
[[(id)mockWebView expect] evaluateAgent];
#else
[[(id)mockWebView reject] evaluateAgent];
#endif
[mockWebView startWithCustomUserAgent:nil shouldCollect:YES defaultAgentOverride:nil];
[(id)mockWebView verify];
XCTAssertEqualObjects(_webView.originalDefaultUserAgent, defaultAgent);
}

@end
Loading
Loading