Skip to content

Commit 8102b71

Browse files
committed
[tests] add logging tests
1 parent bfeda76 commit 8102b71

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
3C5117172B15C31E00563465 /* OSUserState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5117162B15C31E00563465 /* OSUserState.swift */; };
9393
3C5501402E09CF0100E77DF7 /* OSCopyOnWriteSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */; };
9494
3C5501412E09CF0100E77DF7 /* OSCopyOnWriteSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */; };
95+
3C5501432E09F3D900E77DF7 /* LoggingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5501422E09F3D900E77DF7 /* LoggingTests.swift */; };
9596
3C5929E32CAD9EC50020D6FF /* OneSignalUserManagerImpl+OSLoggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5929E22CAD9EC50020D6FF /* OneSignalUserManagerImpl+OSLoggable.swift */; };
9697
3C5929E52CAE523E0020D6FF /* MockUserJwtInvalidatedListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5929E42CAE523E0020D6FF /* MockUserJwtInvalidatedListener.swift */; };
9798
3C62999F2BEEA34800649187 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */; };
@@ -1283,6 +1284,7 @@
12831284
3C5117162B15C31E00563465 /* OSUserState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSUserState.swift; sourceTree = "<group>"; };
12841285
3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSCopyOnWriteSet.h; sourceTree = "<group>"; };
12851286
3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSCopyOnWriteSet.m; sourceTree = "<group>"; };
1287+
3C5501422E09F3D900E77DF7 /* LoggingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingTests.swift; sourceTree = "<group>"; };
12861288
3C5929E22CAD9EC50020D6FF /* OneSignalUserManagerImpl+OSLoggable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OneSignalUserManagerImpl+OSLoggable.swift"; sourceTree = "<group>"; };
12871289
3C5929E42CAE523E0020D6FF /* MockUserJwtInvalidatedListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockUserJwtInvalidatedListener.swift; sourceTree = "<group>"; };
12881290
3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
@@ -2251,6 +2253,7 @@
22512253
isa = PBXGroup;
22522254
children = (
22532255
3CC063A62B6D7A8E002BB07F /* OneSignalCoreTests.swift */,
2256+
3C5501422E09F3D900E77DF7 /* LoggingTests.swift */,
22542257
3C24B0EB2BD09D7A0052E771 /* OneSignalCoreObjCTests.m */,
22552258
3C24B0EA2BD09D790052E771 /* OneSignalCoreTests-Bridging-Header.h */,
22562259
);
@@ -4288,6 +4291,7 @@
42884291
files = (
42894292
3CC063A72B6D7A8E002BB07F /* OneSignalCoreTests.swift in Sources */,
42904293
3C24B0EC2BD09D7A0052E771 /* OneSignalCoreObjCTests.m in Sources */,
4294+
3C5501432E09F3D900E77DF7 /* LoggingTests.swift in Sources */,
42914295
);
42924296
runOnlyForDeploymentPostprocessing = 0;
42934297
};
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2025 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
18+
with services provided by OneSignal.
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
import XCTest
29+
import OneSignalCore
30+
31+
class TestLogListener: NSObject, OSLogListener {
32+
var calls: [String] = []
33+
let callback: ((OneSignalLogEvent) -> Void)?
34+
35+
init(_ callback: ((OneSignalLogEvent) -> Void)? = nil) {
36+
self.callback = callback
37+
}
38+
39+
func onLogEvent(_ event: OneSignalLogEvent) {
40+
calls.append(event.entry)
41+
guard let callback = callback else { return }
42+
callback(event)
43+
}
44+
}
45+
46+
final class LoggingTests: XCTestCase {
47+
override func setUpWithError() throws {
48+
OneSignalLog.setLogLevel(.LL_NONE)
49+
}
50+
51+
override func tearDownWithError() throws {
52+
// Put teardown code here. This method is called after the invocation of each test method in the class.
53+
}
54+
55+
func testAddListener() throws {
56+
// Given
57+
let listener = TestLogListener()
58+
OneSignalLog.debug().__add(listener)
59+
60+
// When
61+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test")
62+
63+
// Then
64+
XCTAssertEqual(listener.calls, ["DEBUG: test"])
65+
}
66+
67+
func testAddListenerTwice() throws {
68+
// Given
69+
let listener = TestLogListener()
70+
OneSignalLog.debug().__add(listener)
71+
OneSignalLog.debug().__add(listener)
72+
73+
// When
74+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test")
75+
76+
// Then
77+
XCTAssertEqual(listener.calls, ["DEBUG: test"])
78+
}
79+
80+
func testRemoveListener() throws {
81+
// Given
82+
let listener = TestLogListener()
83+
OneSignalLog.debug().__add(listener)
84+
OneSignalLog.debug().__remove(listener)
85+
86+
// When
87+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test")
88+
89+
// Then
90+
XCTAssertEqual(listener.calls, [])
91+
}
92+
93+
func testRemoveListenerTwice() throws {
94+
// Given
95+
let listener = TestLogListener()
96+
OneSignalLog.debug().__add(listener)
97+
OneSignalLog.debug().__remove(listener)
98+
OneSignalLog.debug().__remove(listener)
99+
100+
// When
101+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test")
102+
103+
// Then
104+
XCTAssertEqual(listener.calls, [])
105+
}
106+
107+
func testAddListenerNested() throws {
108+
// Given
109+
let nestedListener = TestLogListener()
110+
let firstListener = TestLogListener({ _ in
111+
OneSignalLog.debug().__add(nestedListener)
112+
})
113+
114+
OneSignalLog.debug().__add(firstListener)
115+
116+
// When
117+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test")
118+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test2")
119+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test3")
120+
121+
// Then
122+
XCTAssertEqual(nestedListener.calls, ["DEBUG: test2", "DEBUG: test3"])
123+
}
124+
125+
func testRemoveListenerNested() throws {
126+
// Given
127+
var calls: [String] = []
128+
var listener: OSLogListener?
129+
130+
listener = TestLogListener({ event in
131+
calls.append(event.entry)
132+
OneSignalLog.debug().__remove(listener!)
133+
})
134+
135+
OneSignalLog.debug().__add(listener!)
136+
137+
// When
138+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test")
139+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "test2")
140+
141+
// Then
142+
XCTAssertEqual(calls, ["DEBUG: test"])
143+
}
144+
}

0 commit comments

Comments
 (0)