Skip to content

Commit 94557e5

Browse files
authored
Fixed lifecycle issue and added tests (#117)
* Added Build.sh * Fixed lifecycle issue and added tests
1 parent c9d0c71 commit 94557e5

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

Build.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/zsh
2+
3+
# Set Project name
4+
PROJECT_NAME="Segment-Package"
5+
6+
# set framework folder name
7+
FRAMEWORK_FOLDER_NAME="${PROJECT_NAME}_XCFramework"
8+
9+
# set framework name or read it from project by this variable
10+
FRAMEWORK_NAME="Segment"
11+
12+
#xcframework path
13+
FRAMEWORK_PATH="${HOME}/Desktop/${FRAMEWORK_FOLDER_NAME}/${FRAMEWORK_NAME}.xcframework"
14+
15+
# set path for iOS simulator archive
16+
SIMULATOR_ARCHIVE_PATH="${HOME}/Desktop/${FRAMEWORK_FOLDER_NAME}/simulator.xcarchive"
17+
18+
# set path for iOS device archive
19+
IOS_DEVICE_ARCHIVE_PATH="${HOME}/Desktop/${FRAMEWORK_FOLDER_NAME}/iOS.xcarchive"
20+
21+
rm -rf "${HOME}/Desktop/${FRAMEWORK_FOLDER_NAME}"
22+
echo "Deleted ${FRAMEWORK_FOLDER_NAME}"
23+
24+
mkdir "${HOME}/Desktop/${FRAMEWORK_FOLDER_NAME}"
25+
echo "Created ${FRAMEWORK_FOLDER_NAME}"
26+
27+
echo "Archiving ${FRAMEWORK_NAME}"
28+
29+
xcodebuild archive -scheme ${PROJECT_NAME} -destination="iOS Simulator" -archivePath "${SIMULATOR_ARCHIVE_PATH}" -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
30+
31+
xcodebuild archive -scheme ${PROJECT_NAME} -destination="iOS" -archivePath "${IOS_DEVICE_ARCHIVE_PATH}" -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
32+
33+
#Creating XCFramework
34+
xcodebuild -create-xcframework -framework ${SIMULATOR_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework -framework ${IOS_DEVICE_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework -output "${FRAMEWORK_PATH}"
35+
rm -rf "${SIMULATOR_ARCHIVE_PATH}"
36+
rm -rf "${IOS_DEVICE_ARCHIVE_PATH}"
37+
open "${HOME}/Desktop/${FRAMEWORK_FOLDER_NAME}"

Segment.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
967C40E3258D4DAF008EB0B6 /* Metrics_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40E2258D4DAF008EB0B6 /* Metrics_Tests.swift */; };
5959
9692724E25A4E5B7009B5298 /* Startup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692724D25A4E5B7009B5298 /* Startup.swift */; };
6060
9692726825A583A6009B5298 /* SegmentDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692726725A583A6009B5298 /* SegmentDestination.swift */; };
61+
96A9668927BC137F00078F8B /* iOSLifecycle_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96A9668827BC137F00078F8B /* iOSLifecycle_Tests.swift */; };
6162
96C33A9C25880A5E00F3D538 /* SegmentLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33A9B25880A5E00F3D538 /* SegmentLog.swift */; };
6263
96C33AB1258961F500F3D538 /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33AB0258961F500F3D538 /* Settings.swift */; };
6364
96C95B16271DE22700C3EB9A /* LogTarget_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C95B15271DE22600C3EB9A /* LogTarget_Tests.swift */; };
@@ -158,6 +159,7 @@
158159
967C40ED259A7311008EB0B6 /* HTTPClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPClient.swift; sourceTree = "<group>"; };
159160
9692724D25A4E5B7009B5298 /* Startup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Startup.swift; sourceTree = "<group>"; };
160161
9692726725A583A6009B5298 /* SegmentDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentDestination.swift; sourceTree = "<group>"; };
162+
96A9668827BC137F00078F8B /* iOSLifecycle_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSLifecycle_Tests.swift; sourceTree = "<group>"; };
161163
96C33A9B25880A5E00F3D538 /* SegmentLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentLog.swift; sourceTree = "<group>"; };
162164
96C33AB0258961F500F3D538 /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; };
163165
96C95B15271DE22600C3EB9A /* LogTarget_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogTarget_Tests.swift; sourceTree = "<group>"; };
@@ -383,6 +385,7 @@
383385
4621082D2609206D00EBC4A8 /* Support */,
384386
96DBF37A26F39B5500724B0B /* Timeline_Tests.swift */,
385387
OBJ_13 /* XCTestManifests.swift */,
388+
96A9668827BC137F00078F8B /* iOSLifecycle_Tests.swift */,
386389
);
387390
name = "Segment-Tests";
388391
path = "Tests/Segment-Tests";
@@ -588,6 +591,7 @@
588591
4658175425BA4C20006B2809 /* HTTPClient_Tests.swift in Sources */,
589592
46210811260538BE00EBC4A8 /* KeyPath_Tests.swift in Sources */,
590593
967C40E3258D4DAF008EB0B6 /* Metrics_Tests.swift in Sources */,
594+
96A9668927BC137F00078F8B /* iOSLifecycle_Tests.swift in Sources */,
591595
96C95B16271DE22700C3EB9A /* LogTarget_Tests.swift in Sources */,
592596
A31A16512576C47400C9CDDF /* JSON_Tests.swift in Sources */,
593597
46FE4D1D25A7A850003A7362 /* Storage_Tests.swift in Sources */,

Sources/Segment/Plugins/Platforms/iOS/iOSLifecycleEvents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {
2929
let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
3030
let currentBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String
3131

32-
if previousBuild != nil {
32+
if previousBuild == nil {
3333
analytics?.track(name: "Application Installed", properties: [
3434
"version": currentVersion ?? "",
3535
"build": currentBuild ?? ""

Tests/Segment-Tests/Support/TestUtilities.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class OutputReaderPlugin: Plugin {
107107
let type: PluginType
108108
var analytics: Analytics?
109109

110+
var events = [RawEvent]()
110111
var lastEvent: RawEvent? = nil
111112

112113
init() {
@@ -116,6 +117,7 @@ class OutputReaderPlugin: Plugin {
116117
func execute<T>(event: T?) -> T? where T : RawEvent {
117118
lastEvent = event
118119
if let t = lastEvent as? TrackEvent {
120+
events.append(t)
119121
print("EVENT: \(t.event)")
120122
}
121123
return event
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import XCTest
2+
@testable import Segment
3+
4+
#if os(iOS)
5+
final class iOSLifecycle_Tests: XCTestCase {
6+
7+
func testInstallEventCreation() {
8+
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
9+
let outputReader = OutputReaderPlugin()
10+
analytics.add(plugin: outputReader)
11+
12+
let iosLifecyclePlugin = iOSLifecycleEvents()
13+
analytics.add(plugin: iosLifecyclePlugin)
14+
15+
waitUntilStarted(analytics: analytics)
16+
17+
UserDefaults.standard.setValue(nil, forKey: "SEGBuildKeyV2")
18+
19+
// This is a hack that needs to be dealt with
20+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 2))
21+
22+
iosLifecyclePlugin.application(nil, didFinishLaunchingWithOptions: nil)
23+
24+
let trackEvent: TrackEvent? = outputReader.events.first as? TrackEvent
25+
XCTAssertTrue(trackEvent?.event == "Application Installed")
26+
XCTAssertTrue(trackEvent?.type == "track")
27+
}
28+
29+
func testInstallEventUpdated() {
30+
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
31+
let outputReader = OutputReaderPlugin()
32+
analytics.add(plugin: outputReader)
33+
34+
let iosLifecyclePlugin = iOSLifecycleEvents()
35+
analytics.add(plugin: iosLifecyclePlugin)
36+
37+
waitUntilStarted(analytics: analytics)
38+
39+
UserDefaults.standard.setValue("1337", forKey: "SEGBuildKeyV2")
40+
41+
// This is a hack that needs to be dealt with
42+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 2))
43+
44+
iosLifecyclePlugin.application(nil, didFinishLaunchingWithOptions: nil)
45+
46+
let trackEvent: TrackEvent? = outputReader.events.first as? TrackEvent
47+
XCTAssertTrue(trackEvent?.event == "Application Updated")
48+
XCTAssertTrue(trackEvent?.type == "track")
49+
}
50+
51+
func testInstallEventOpened() {
52+
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
53+
let outputReader = OutputReaderPlugin()
54+
analytics.add(plugin: outputReader)
55+
56+
let iosLifecyclePlugin = iOSLifecycleEvents()
57+
analytics.add(plugin: iosLifecyclePlugin)
58+
59+
waitUntilStarted(analytics: analytics)
60+
61+
iosLifecyclePlugin.application(nil, didFinishLaunchingWithOptions: nil)
62+
63+
let trackEvent: TrackEvent? = outputReader.events.last as? TrackEvent
64+
XCTAssertTrue(trackEvent?.event == "Application Opened")
65+
XCTAssertTrue(trackEvent?.type == "track")
66+
}
67+
}
68+
69+
#endif

0 commit comments

Comments
 (0)