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

[MT-1437] Config.isEnabled implementation #316

Merged
merged 7 commits into from
Jan 5, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/pr-labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@main
- uses: actions/labeler@v4.3.0
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
92 changes: 90 additions & 2 deletions support/tests/test_tealium_core/TealiumModulesManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TealiumModulesManagerTests: XCTestCase {

override func setUp() {
super.setUp()
sharedConfig.shouldUseRemotePublishSettings = false
// Put setup code here. This method is called before the invocation of each test method in the class.
}

Expand Down Expand Up @@ -403,6 +404,91 @@ class TealiumModulesManagerTests: XCTestCase {
}
}
#endif

func testDisableLibraryRemovesAllModules() {
sharedConfig.shouldUseRemotePublishSettings = true
var defaultSettings = RemotePublishSettings()
let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings)
context.config.dispatchers = [Dispatchers.Collect]
TealiumQueues.backgroundSerialQueue.sync {
let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever)
XCTAssertNotEqual(modulesManager.dispatchers.count, 0)
XCTAssertNotEqual(modulesManager.collectors.count, 0)

defaultSettings.isEnabled = false
modulesManager.didUpdate(defaultSettings)
XCTAssertEqual(modulesManager.dispatchers.count, 0)
XCTAssertEqual(modulesManager.collectors.count, 0)
}
}

func testReEnableLibraryAddsAllModulesBack() {
sharedConfig.shouldUseRemotePublishSettings = true
var defaultSettings = RemotePublishSettings()
let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings)
context.config.dispatchers = [Dispatchers.Collect]
TealiumQueues.backgroundSerialQueue.sync {
let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever)
XCTAssertNotEqual(modulesManager.dispatchers.count, 0)
XCTAssertNotEqual(modulesManager.collectors.count, 0)

defaultSettings.isEnabled = false
modulesManager.didUpdate(defaultSettings)
XCTAssertEqual(modulesManager.dispatchers.count, 0)
XCTAssertEqual(modulesManager.collectors.count, 0)

defaultSettings.isEnabled = true
modulesManager.didUpdate(defaultSettings)
XCTAssertNotEqual(modulesManager.dispatchers.count, 0)
XCTAssertNotEqual(modulesManager.collectors.count, 0)
}
}

func testDisableLibraryStopsFutureTrackingCalls() {
let trackNotSent = expectation(description: "Track is not sent")
trackNotSent.isInverted = true
TealiumExpectations.expectations["sendTrack"] = trackNotSent
sharedConfig.shouldUseRemotePublishSettings = true
var defaultSettings = RemotePublishSettings()
let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings)
context.config.dispatchers = [Dispatchers.Collect]
TealiumQueues.backgroundSerialQueue.sync {
let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever)
let connectivity = ConnectivityModule(context: modulesManager.context, delegate: nil, diskStorage: nil) { _ in }
modulesManager.dispatchManager = DummyDispatchManagerSendTrack(dispatchers: nil, dispatchValidators: nil, dispatchListeners: nil, connectivityManager: connectivity, config: testTealiumConfig)
defaultSettings.isEnabled = false
modulesManager.didUpdate(defaultSettings)
modulesManager.sendTrack(TealiumTrackRequest(data: [:]))
}
waitForExpectations(timeout: 1.0)
}

func testDisableLibraryDoesntStopRefreshingRemoteSettings() {
let refreshSettings = expectation(description: "Settings are refreshed")
refreshSettings.assertForOverFulfill = false // other events are also being sent so mulitple refresh are expected when running multiple tests at the same time
let notRefreshSettings = expectation(description: "Settings are not refreshed yet")
notRefreshSettings.isInverted = true
sharedConfig.shouldUseRemotePublishSettings = true
var defaultSettings = RemotePublishSettings()
let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings)
context.config.dispatchers = [Dispatchers.Collect]
TealiumQueues.backgroundSerialQueue.sync {
let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever)
modulesManager.remotePublishSettingsRetriever = MockPublishSettingsRetriever {
notRefreshSettings.fulfill()
}
defaultSettings.isEnabled = false
modulesManager.didUpdate(defaultSettings)
XCTAssertEqual(modulesManager.dispatchers.count, 0)
XCTAssertEqual(modulesManager.collectors.count, 0)
wait(for: [notRefreshSettings], timeout: 1.0)
modulesManager.remotePublishSettingsRetriever = MockPublishSettingsRetriever {
refreshSettings.fulfill()
}
modulesManager.sendTrack(TealiumTrackRequest(data: [:]))
wait(for: [refreshSettings], timeout: 1.0)
}
}
}

extension TealiumModulesManagerTests: ModuleDelegate {
Expand Down Expand Up @@ -496,13 +582,15 @@ class DummyDispatchManagerSendTrack: DispatchManagerProtocol {


struct MockPublishSettingsRetriever: TealiumPublishSettingsRetrieverProtocol {
init(cachedSettings: RemotePublishSettings? = nil) {
let onRefresh: () -> Void
init(cachedSettings: RemotePublishSettings? = nil, onRefresh: @escaping () -> Void = { }) {
sharedConfig.shouldUseRemotePublishSettings = true
self.onRefresh = onRefresh
self.cachedSettings = cachedSettings
}
var cachedSettings: RemotePublishSettings?

func refresh() {

onRefresh()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WKWebViewIntegrationTests: XCTestCase {

var expect: XCTestExpectation!
var module: TagManagementModule!
var config: TealiumConfig!
let config = TealiumConfig(account: "testAccount", profile: "testProfile", environment: "testEnv")
var mockTagmanagement: MockTagManagementWebView!
static var processPool = WKProcessPool()
static var wkConfig: WKWebViewConfiguration = {
Expand All @@ -34,7 +34,6 @@ class WKWebViewIntegrationTests: XCTestCase {
override func setUp() {
super.setUp()
userDefaults?.removePersistentDomain(forName: #file)
config = TealiumConfig(account: "testAccount", profile: "testProfile", environment: "testEnv")
}

func testWKWebViewInstance() {
Expand Down Expand Up @@ -204,8 +203,7 @@ class WKWebViewIntegrationTests: XCTestCase {
}
})
})

wait(for: [expect], timeout: 5.0)
wait(for: [expect], timeout: 10.0)
}

func testModuleWithQueryParamProviderChangesUrl() {
Expand Down
10 changes: 10 additions & 0 deletions tealium/core/ModulesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class ModulesManager {
}

self.setupDispatchers(context: context)
self.setupCollectors(config: newValue)
}
}
private var cachedTrackData: [String: Any]?
Expand Down Expand Up @@ -183,6 +184,7 @@ public class ModulesManager {
if self.config.shouldUseRemotePublishSettings == true {
self.remotePublishSettingsRetriever?.refresh()
}
guard config.isEnabled != false else { return }
let requestData = gatherTrackData(for: request.trackDictionary)
let newRequest = TealiumTrackRequest(data: requestData)
dispatchManager?.processTrack(newRequest)
Expand Down Expand Up @@ -256,6 +258,10 @@ extension ModulesManager {
}

func setupCollectors(config: TealiumConfig) {
guard context.config.isEnabled != false else {
collectors.removeAll()
return
}
collectorTypes.forEach { collector in
guard !collectors.contains(where: { type(of: $0) == collector }) else {
return
Expand All @@ -280,6 +286,10 @@ extension ModulesManager {
}

func setupDispatchers(context: TealiumContext) {
guard context.config.isEnabled != false else {
dispatchers.removeAll()
return
}
self.config.dispatchers?.forEach { dispatcherType in
guard !dispatchers.contains(where: { type(of: $0) == dispatcherType }) else {
return
Expand Down
Loading