Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous Fixes #920

Merged
merged 8 commits into from
Jul 13, 2020
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
24 changes: 15 additions & 9 deletions Analytics.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
A34CC6DD249AAEBB00C5E368 /* SEGState.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CC6DB249AAEBA00C5E368 /* SEGState.m */; };
A34CC6DE249AAEBB00C5E368 /* SEGState.h in Headers */ = {isa = PBXBuildFile; fileRef = A34CC6DC249AAEBB00C5E368 /* SEGState.h */; settings = {ATTRIBUTES = (Private, ); }; };
A352176023AD5825005B07F6 /* SEGMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = A352175F23AD5825005B07F6 /* SEGMacros.h */; settings = {ATTRIBUTES = (Private, ); }; };
A3BECDD024BCE1E3009E2BD3 /* ObjcUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = A3BECDCF24BCE1E3009E2BD3 /* ObjcUtils.m */; };
A3BECDD324BCFACA009E2BD3 /* ObjcUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = A3BECDCF24BCE1E3009E2BD3 /* ObjcUtils.m */; };
E4ACAD51DB827114D8626C74 /* Pods_AnalyticsTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5238C0454BF56A36A536C5C1 /* Pods_AnalyticsTests.framework */; };
EA88A5981DED7608009FB66A /* SEGSerializableValue.h in Headers */ = {isa = PBXBuildFile; fileRef = EA88A5971DED7608009FB66A /* SEGSerializableValue.h */; settings = {ATTRIBUTES = (Public, ); }; };
EA8F09741E24C5C600B8B93F /* MiddlewareTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA8F09731E24C5C600B8B93F /* MiddlewareTests.swift */; };
Expand Down Expand Up @@ -133,6 +135,8 @@
A34CC6DB249AAEBA00C5E368 /* SEGState.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SEGState.m; sourceTree = "<group>"; };
A34CC6DC249AAEBB00C5E368 /* SEGState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SEGState.h; sourceTree = "<group>"; };
A352175F23AD5825005B07F6 /* SEGMacros.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SEGMacros.h; sourceTree = "<group>"; };
A3BECDCE24BCE1E3009E2BD3 /* ObjcUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ObjcUtils.h; sourceTree = "<group>"; };
A3BECDCF24BCE1E3009E2BD3 /* ObjcUtils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ObjcUtils.m; sourceTree = "<group>"; };
B0D1E9E4405FD8386DCCD150 /* Pods-AnalyticsTestsTVOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnalyticsTestsTVOS.release.xcconfig"; path = "Target Support Files/Pods-AnalyticsTestsTVOS/Pods-AnalyticsTestsTVOS.release.xcconfig"; sourceTree = "<group>"; };
EA88A5971DED7608009FB66A /* SEGSerializableValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SEGSerializableValue.h; sourceTree = "<group>"; };
EA8F09731E24C5C600B8B93F /* MiddlewareTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MiddlewareTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -396,6 +400,8 @@
EADEB8E21DECD335005322DA /* NSData+SEGGUNZIPP.h */,
EADEB8E31DECD335005322DA /* NSData+SEGGUNZIPP.m */,
EADEB8E41DECD335005322DA /* TestUtils.swift */,
A3BECDCE24BCE1E3009E2BD3 /* ObjcUtils.h */,
A3BECDCF24BCE1E3009E2BD3 /* ObjcUtils.m */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -463,7 +469,7 @@
9D8CE58F23EE014E00197D0C /* Sources */,
9D8CE59F23EE014E00197D0C /* Frameworks */,
9D8CE5A223EE014E00197D0C /* Resources */,
D2ED3899A800F2659A9CF6C2 /* [CP] Embed Pods Frameworks */,
F59284A917BD27204F7B1F64 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -501,7 +507,7 @@
EADEB8661DECD0EF005322DA /* Sources */,
EADEB8671DECD0EF005322DA /* Frameworks */,
EADEB8681DECD0EF005322DA /* Resources */,
87F85ADB13C707FFFF3F27B0 /* [CP] Embed Pods Frameworks */,
7B56FD82419D9D8206680C38 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -532,7 +538,7 @@
};
EADEB8691DECD0EF005322DA = {
CreatedOnToolsVersion = 8.1;
LastSwiftMigration = 1130;
LastSwiftMigration = 1150;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -582,20 +588,18 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
87F85ADB13C707FFFF3F27B0 /* [CP] Embed Pods Frameworks */ = {
7B56FD82419D9D8206680C38 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-AnalyticsTests/Pods-AnalyticsTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Nocilla-iOS/Nocilla.framework",
"${BUILT_PRODUCTS_DIR}/SwiftTryCatch-iOS/SwiftTryCatch.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Nocilla.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftTryCatch.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -646,20 +650,18 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
D2ED3899A800F2659A9CF6C2 /* [CP] Embed Pods Frameworks */ = {
F59284A917BD27204F7B1F64 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-AnalyticsTestsTVOS/Pods-AnalyticsTestsTVOS-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Nocilla-tvOS/Nocilla.framework",
"${BUILT_PRODUCTS_DIR}/SwiftTryCatch-tvOS/SwiftTryCatch.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Nocilla.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftTryCatch.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand All @@ -680,6 +682,7 @@
9D8CE59423EE014E00197D0C /* SerializationTests.m in Sources */,
9D8CE59523EE014E00197D0C /* HTTPClientTest.swift in Sources */,
9D8CE59623EE014E00197D0C /* AnalyticsTests.swift in Sources */,
A3BECDD324BCFACA009E2BD3 /* ObjcUtils.m in Sources */,
9D8CE59723EE014E00197D0C /* FileStorageTest.swift in Sources */,
9D8CE59823EE014E00197D0C /* AnalyticsUtilTests.swift in Sources */,
9D8CE59923EE014E00197D0C /* StoreKitTrackerTests.swift in Sources */,
Expand Down Expand Up @@ -734,6 +737,7 @@
A31958EF2385AC3A00A47EFA /* SerializationTests.m in Sources */,
EADEB8EE1DECD335005322DA /* HTTPClientTest.swift in Sources */,
EADEB8F31DECD335005322DA /* AnalyticsTests.swift in Sources */,
A3BECDD024BCE1E3009E2BD3 /* ObjcUtils.m in Sources */,
EADEB8F11DECD335005322DA /* FileStorageTest.swift in Sources */,
F61EB4AF1F996DEF0038C8C0 /* AnalyticsUtilTests.swift in Sources */,
EAA542801EB4382100945DA7 /* StoreKitTrackerTests.swift in Sources */,
Expand Down Expand Up @@ -969,6 +973,7 @@
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OBJC_BRIDGING_HEADER = "AnalyticsTests/AnalyticsTests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -982,6 +987,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "AnalyticsTests/AnalyticsTests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
3 changes: 3 additions & 0 deletions Analytics/Classes/SEGAnalytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ NS_SWIFT_NAME(Analytics)
/** Returns the anonymous ID of the current user. */
- (NSString *)getAnonymousId;

/** Returns the registered device token of this device */
- (NSString *)getDeviceToken;

/** Returns the configuration used to create the analytics client. */
- (SEGAnalyticsConfiguration *)configuration;

Expand Down
5 changes: 5 additions & 0 deletions Analytics/Classes/SEGAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ - (NSString *)getAnonymousId
return [SEGState sharedInstance].userInfo.anonymousId;
}

- (NSString *)getDeviceToken
{
return [SEGState sharedInstance].context.deviceToken;
}

- (NSDictionary *)bundledIntegrations
{
return [self.integrationsManager.registeredIntegrations copy];
Expand Down
4 changes: 2 additions & 2 deletions Analytics/Classes/SEGAnalyticsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
NS_SWIFT_NAME(ApplicationProtocol)
@protocol SEGApplicationProtocol <NSObject>
@property (nullable, nonatomic, assign) id<UIApplicationDelegate> delegate;
- (UIBackgroundTaskIdentifier)seg_beginBackgroundTaskWithName:(nullable NSString *)taskName expirationHandler:(void (^__nullable)(void))handler;
- (void)seg_endBackgroundTask:(UIBackgroundTaskIdentifier)identifier;
- (NSUInteger)seg_beginBackgroundTaskWithName:(nullable NSString *)taskName expirationHandler:(void (^__nullable)(void))handler;
- (void)seg_endBackgroundTask:(NSUInteger)identifier;
@end


Expand Down
24 changes: 11 additions & 13 deletions Analytics/Classes/SEGSegmentIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ - (id)initWithAnalytics:(SEGAnalytics *)analytics httpClient:(SEGHTTPClient *)ht
self.fileStorage = fileStorage;
self.userDefaultsStorage = userDefaultsStorage;
self.apiURL = [SEGMENT_API_BASE URLByAppendingPathComponent:@"import"];
self.userId = [self getUserId];
self.reachability = [SEGReachability reachabilityWithHostname:@"google.com"];
[self.reachability startNotifier];
self.serialQueue = seg_dispatch_queue_create_specific("io.segment.analytics.segmentio", DISPATCH_QUEUE_SERIAL);
self.backgroundTaskQueue = seg_dispatch_queue_create_specific("io.segment.analytics.backgroundTask", DISPATCH_QUEUE_SERIAL);
self.flushTaskID = UIBackgroundTaskInvalid;

// load traits from disk.
// load traits & user from disk.
[self loadUserId];
[self loadTraits];

[self dispatchBackground:^{
Expand Down Expand Up @@ -150,10 +150,14 @@ - (NSString *)description
return [NSString stringWithFormat:@"<%p:%@, %@>", self, self.class, self.configuration.writeKey];
}

- (void)saveUserId:(NSString *)userId
- (NSString *)userId
{
return [SEGState sharedInstance].userInfo.userId;
}

- (void)setUserId:(NSString *)userId
{
[self dispatchBackground:^{
self.userId = userId;
[SEGState sharedInstance].userInfo.userId = userId;
#if TARGET_OS_TV
[self.userDefaultsStorage setString:userId forKey:SEGUserIdKey];
Expand All @@ -169,11 +173,6 @@ - (NSDictionary *)traits
}

- (void)setTraits:(NSDictionary *)traits
{
[self saveTraits:traits];
}

- (void)saveTraits:(NSDictionary *)traits
{
[self dispatchBackground:^{
[SEGState sharedInstance].userInfo.traits = traits;
Expand All @@ -190,8 +189,8 @@ - (void)saveTraits:(NSDictionary *)traits
- (void)identify:(SEGIdentifyPayload *)payload
{
[self dispatchBackground:^{
[self saveUserId:payload.userId];
[self saveTraits:payload.traits];
self.userId = payload.userId;
self.traits = payload.traits;
}];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -447,7 +446,7 @@ - (NSUInteger)maxBatchSize
return 100;
}

- (NSString *)getUserId
- (void)loadUserId
{
NSString *result = nil;
#if TARGET_OS_TV
Expand All @@ -456,7 +455,6 @@ - (NSString *)getUserId
result = [self.fileStorage stringForKey:kSEGUserIdFilename];
#endif
[SEGState sharedInstance].userInfo.userId = result;
return result;
}

- (void)persistQueue
Expand Down
2 changes: 1 addition & 1 deletion Analytics/Internal/SEGState.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ - (void)setDeviceToken:(NSString *)deviceToken
{
[state setValueWithBlock: ^{
self->_deviceToken = [deviceToken copy];
[self updateStaticContext];
}];
[self updateStaticContext];
bsneed marked this conversation as resolved.
Show resolved Hide resolved
}

@end
Expand Down
4 changes: 1 addition & 3 deletions AnalyticsTests/AnalyticsTests-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@
#import <Analytics/SEGScreenReporting.h>

#import "NSData+SEGGUNZIPP.h"
// Temp hack. We should fix the LSNocilla podspec to make this header publicly available
#import "LSMatcher.h"

#import "UIViewController+SegScreenTest.h"
#import "ObjcUtils.h"
31 changes: 24 additions & 7 deletions AnalyticsTests/AnalyticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//


import Analytics
@testable import Analytics
import XCTest

class AnalyticsTests: XCTestCase {
Expand Down Expand Up @@ -143,9 +143,9 @@ class AnalyticsTests: XCTestCase {

func testFiresApplicationOpenedForAppLaunchingEvent() {
testMiddleware.swallowEvent = true
NotificationCenter.default.post(name: .UIApplicationDidFinishLaunching, object: testApplication, userInfo: [
UIApplicationLaunchOptionsKey.sourceApplication: "testApp",
UIApplicationLaunchOptionsKey.url: "test://test",
NotificationCenter.default.post(name: UIApplication.didFinishLaunchingNotification, object: testApplication, userInfo: [
UIApplication.LaunchOptionsKey.sourceApplication: "testApp",
UIApplication.LaunchOptionsKey.url: "test://test",
])
let event = testMiddleware.lastContext?.payload as? TrackPayload
XCTAssertEqual(event?.event, "Application Opened")
Expand All @@ -156,22 +156,22 @@ class AnalyticsTests: XCTestCase {

func testFiresApplicationEnterForeground() {
testMiddleware.swallowEvent = true
NotificationCenter.default.post(name: .UIApplicationWillEnterForeground, object: testApplication)
NotificationCenter.default.post(name: UIApplication.willEnterForegroundNotification, object: testApplication)
let event = testMiddleware.lastContext?.payload as? TrackPayload
XCTAssertEqual(event?.event, "Application Opened")
XCTAssertEqual(event?.properties?["from_background"] as? Bool, true)
}

func testFiresApplicationDuringEnterBackground() {
testMiddleware.swallowEvent = true
NotificationCenter.default.post(name: .UIApplicationDidEnterBackground, object: testApplication)
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: testApplication)
let event = testMiddleware.lastContext?.payload as? TrackPayload
XCTAssertEqual(event?.event, "Application Backgrounded")
}

func testFlushesWhenApplicationBackgroundIsFired() {
analytics.track("test")
NotificationCenter.default.post(name: .UIApplicationDidEnterBackground, object: testApplication)
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: testApplication)

expectUntil(2.0, expression: self.testApplication.backgroundTasks.count == 1)
expectUntil(2.0, expression: self.testApplication.backgroundTasks[0].isEnded == false)
Expand Down Expand Up @@ -259,4 +259,21 @@ class AnalyticsTests: XCTestCase {
integration?.test_fileStorage()?.resetAll()
XCTAssert(integration?.test_queue()?.isEmpty ?? false)
}

func testDeviceTokenRegistration() {
func getStringFrom(token: Data) -> String {
return token.reduce("") { $0 + String(format: "%02.2hhx", $1) }
bsneed marked this conversation as resolved.
Show resolved Hide resolved
}

let deviceToken = GenerateUUIDString()
let data = deviceToken.data(using: .utf8)
if let data = data {
analytics.registeredForRemoteNotifications(withDeviceToken: data)
let deviceTokenString = getStringFrom(token: data)
XCTAssertTrue(deviceTokenString == analytics.getDeviceToken())

} else {
XCTAssertNotNil(data)
}
}
}
7 changes: 3 additions & 4 deletions AnalyticsTests/AutoScreenReportingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

import Foundation
import SwiftTryCatch
@testable import Analytics
import XCTest

Expand Down Expand Up @@ -64,7 +63,7 @@ class AutoScreenReportingTests: XCTestCase {
class CustomContainerViewController: UIViewController, SEGScreenReporting {
var selectedIndex: Int = 0
var seg_mainViewController: UIViewController? {
return childViewControllers[selectedIndex]
return children[selectedIndex]
}
}

Expand All @@ -73,7 +72,7 @@ class AutoScreenReportingTests: XCTestCase {
let containerVC = CustomContainerViewController()
rootVC.present(containerVC, animated: false)
[UIViewController(), expectedVC, UIViewController()].forEach { child in
containerVC.addChildViewController(child)
containerVC.addChild(child)
}
containerVC.selectedIndex = 1
let actualVC = UIViewController.seg_topViewController(rootVC)
Expand All @@ -86,7 +85,7 @@ class AutoScreenReportingTests: XCTestCase {
let containerVC = UIViewController()
rootVC.present(containerVC, animated: false)
[expectedVC, UIViewController(), UIViewController()].forEach { child in
containerVC.addChildViewController(child)
containerVC.addChild(child)
}
let actualVC = UIViewController.seg_topViewController(rootVC)
XCTAssertEqual(actualVC, expectedVC)
Expand Down
7 changes: 2 additions & 5 deletions AnalyticsTests/ContextTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright © 2016 Segment. All rights reserved.
//

import SwiftTryCatch
import Analytics
import XCTest

Expand All @@ -24,11 +23,9 @@ class ContextTests: XCTestCase {
var context: Context?
var exception: NSException?

SwiftTryCatch.tryRun({
exception = objc_tryCatch {
context = Context()
}, catchRun: { e in
exception = e
}, finallyRun: nil)
}

XCTAssertNil(context)
XCTAssertNotNil(exception)
Expand Down
Loading