Skip to content

Commit

Permalink
refactor: Port MParticleWebView to Swift (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
einsteinx2 authored Dec 11, 2024
1 parent 488c9e3 commit 6cc3db7
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 244 deletions.
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 {
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

0 comments on commit 6cc3db7

Please sign in to comment.