-
Notifications
You must be signed in to change notification settings - Fork 244
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
Some fixes for view duration and added tests #347
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3ccee78
Some fixes for view duration and added tests
ijunaid 520ab8e
WIP : View tests
ijunaid 4586522
fixed views test to wait expectations by combining them
ijunaid 38bdb22
Validating recorded views and queued request for views
ijunaid 81dd221
View test finalized
ijunaid b1313a2
Added dummyTest
ijunaid c2f5e65
Updated test for views
ijunaid b02bbe9
Updated changelog for view fixes
ijunaid f267f7a
Updated changelog
ijunaid 150d545
Merge branch 'staging' into view-fixes-&-tests
turtledreams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
// | ||
// CountlyViewTrackingTests.swift | ||
// CountlyTests | ||
// | ||
// Copyright © 2024 Countly. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import Countly | ||
|
||
class CountlyViewTrackingTests: CountlyBaseTestCase { | ||
|
||
func checkPersistentValues() { | ||
let countlyPersistency = CountlyPersistency.sharedInstance() | ||
if(countlyPersistency != nil) { | ||
if let queuedRequests = CountlyPersistency.sharedInstance().value(forKey: "queuedRequests") as? NSMutableArray, | ||
let recordedEvents = CountlyPersistency.sharedInstance().value(forKey: "recordedEvents") as? NSMutableArray, | ||
let startedEvents = CountlyPersistency.sharedInstance().value(forKey: "startedEvents") as? NSMutableDictionary, | ||
let isQueueBeingModified = CountlyPersistency.sharedInstance().value(forKey: "isQueueBeingModified") as? Bool { | ||
print("Successfully access private properties.") | ||
|
||
|
||
} | ||
else { | ||
print("Failed to access private properties.") | ||
} | ||
} | ||
|
||
} | ||
|
||
func testViewForegroundBackground() { | ||
let config = createBaseConfig() | ||
// No Device ID provided during init | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startAutoStoppedView("TestAutoStoppedView") | ||
|
||
let pauseViewExpectation = XCTestExpectation(description: "Wait for pause view") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 5) { | ||
Countly.sharedInstance().views().pauseView(withID: viewID) | ||
pauseViewExpectation.fulfill() | ||
} | ||
|
||
let resumeViewExpectation = XCTestExpectation(description: "Wait for resume view") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 10) { // Delayed by 10 seconds | ||
Countly.sharedInstance().views().resumeView(withID: viewID) | ||
resumeViewExpectation.fulfill() | ||
} | ||
|
||
let bgExpectation = XCTestExpectation(description: "Wait for background notification") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 15) { // Delayed by 15 seconds | ||
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil) | ||
bgExpectation.fulfill() | ||
} | ||
|
||
let fgExpectation = XCTestExpectation(description: "Wait for active notification") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 20) { // Delayed by 20 seconds | ||
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) | ||
fgExpectation.fulfill() | ||
} | ||
|
||
let bgExpectation1 = XCTestExpectation(description: "Wait for second background notification") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 25) { // Delayed by 25 seconds | ||
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil) | ||
bgExpectation1.fulfill() | ||
} | ||
|
||
let fgExpectation1 = XCTestExpectation(description: "Wait for second active notification") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 30) { // Delayed by 30 seconds | ||
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) | ||
fgExpectation1.fulfill() | ||
} | ||
|
||
// Wait for all expectations or timeout | ||
wait(for: [pauseViewExpectation, resumeViewExpectation, bgExpectation, fgExpectation, bgExpectation1, fgExpectation1], timeout: 35) | ||
|
||
let viewID1 = Countly.sharedInstance().views().startView("startView") | ||
|
||
checkPersistentValues() | ||
Countly.sharedInstance().views().stopAllViews(nil); | ||
|
||
checkPersistentValues() | ||
} | ||
|
||
|
||
func testViewTrackingInit_CNR_AV() throws { | ||
let config = createBaseConfig() | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startAutoStoppedView("TestAutoStoppedView") | ||
|
||
XCTAssertNotNil(viewID, "Auto-stopped view should be started successfully.") | ||
|
||
let pauseViewExpectation = XCTestExpectation(description: "Wait for pause view") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 5) { | ||
Countly.sharedInstance().views().pauseView(withID: viewID) | ||
pauseViewExpectation.fulfill() | ||
} | ||
|
||
wait(for: [pauseViewExpectation], timeout: 10) | ||
} | ||
|
||
func testViewTrackingInit_CR_CNG_AV() throws { | ||
let config = createBaseConfig() | ||
config.requiresConsent = true | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startAutoStoppedView("TestAutoStoppedViewWithoutConsent") | ||
XCTAssertNil(viewID, "Auto-stopped view should not be started when consent is not given.") | ||
} | ||
|
||
func testViewTrackingInit_CR_CGV_AV() throws { | ||
let config = createBaseConfig() | ||
config.requiresConsent = true | ||
config.consents = [CLYConsent.viewTracking] | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startAutoStoppedView("TestAutoStoppedViewWithConsent") | ||
XCTAssertNotNil(viewID, "Auto-stopped view should be started when view tracking consent is given.") | ||
} | ||
|
||
func testManualViewTrackingInit_CNR_MV() throws { | ||
let config = createBaseConfig() | ||
config.manualSessionHandling = true | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startView("TestManualView") | ||
XCTAssertNotNil(viewID, "Manual view should be started successfully.") | ||
|
||
Countly.sharedInstance().views().stopView(withID: viewID) | ||
} | ||
|
||
func testManualViewTrackingInit_CR_CNG_MV() throws { | ||
let config = createBaseConfig() | ||
config.manualSessionHandling = true | ||
config.requiresConsent = true | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startView("TestManualViewWithoutConsent") | ||
XCTAssertNil(viewID, "Manual view should not be started when consent is not given.") | ||
} | ||
|
||
func testManualViewTrackingInit_CR_CGV_MV() throws { | ||
let config = createBaseConfig() | ||
config.manualSessionHandling = true | ||
config.requiresConsent = true | ||
config.consents = [CLYConsent.viewTracking] | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startView("TestManualViewWithConsent") | ||
XCTAssertNotNil(viewID, "Manual view should be started when view tracking consent is given.") | ||
|
||
Countly.sharedInstance().views().stopView(withID: viewID) | ||
} | ||
|
||
func testPauseAndResumeViewTracking() throws { | ||
let config = createBaseConfig() | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startAutoStoppedView("TestViewPauseResume") | ||
XCTAssertNotNil(viewID, "Auto-stopped view should be started successfully.") | ||
|
||
let pauseViewExpectation = XCTestExpectation(description: "Wait for pause view") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 2) { | ||
Countly.sharedInstance().views().pauseView(withID: viewID) | ||
pauseViewExpectation.fulfill() | ||
} | ||
|
||
let resumeViewExpectation = XCTestExpectation(description: "Wait for resume view") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 4) { | ||
Countly.sharedInstance().views().resumeView(withID: viewID) | ||
resumeViewExpectation.fulfill() | ||
} | ||
|
||
wait(for: [pauseViewExpectation, resumeViewExpectation], timeout: 10) | ||
} | ||
|
||
func testViewTrackingWithBackgroundAndForegroundNotifications() throws { | ||
let config = createBaseConfig() | ||
Countly.sharedInstance().start(with: config) | ||
|
||
let viewID = Countly.sharedInstance().views().startView("TestViewNotifications") | ||
|
||
let bgExpectation = XCTestExpectation(description: "Wait for background notification") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 5) { | ||
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil) | ||
bgExpectation.fulfill() | ||
} | ||
|
||
let fgExpectation = XCTestExpectation(description: "Wait for foreground notification") | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 10) { | ||
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) | ||
fgExpectation.fulfill() | ||
} | ||
|
||
wait(for: [bgExpectation, fgExpectation], timeout: 15) | ||
XCTAssertNotNil(viewID, "View should handle background and foreground notifications correctly.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addition of test comments would be great