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

Add locale parameter to RMF survey builder #1105

Merged
merged 4 commits into from
Nov 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ public struct DefaultRemoteMessagingSurveyURLBuilder: RemoteMessagingSurveyActio
private let statisticsStore: StatisticsStore
private let vpnActivationDateStore: VPNActivationDateProviding
private let subscription: Subscription?
private let localeIdentifier: String

public init(statisticsStore: StatisticsStore, vpnActivationDateStore: VPNActivationDateProviding, subscription: Subscription?) {
public init(statisticsStore: StatisticsStore,
vpnActivationDateStore: VPNActivationDateProviding,
subscription: Subscription?,
localeIdentifier: String = Locale.current.identifier) {
self.statisticsStore = statisticsStore
self.vpnActivationDateStore = vpnActivationDateStore
self.subscription = subscription
self.localeIdentifier = localeIdentifier
}

// swiftlint:disable:next cyclomatic_complexity
Expand Down Expand Up @@ -72,6 +77,9 @@ public struct DefaultRemoteMessagingSurveyURLBuilder: RemoteMessagingSurveyActio
let daysSinceInstall = Calendar.current.numberOfDaysBetween(installDate, and: Date()) {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: String(describing: daysSinceInstall)))
}
case .locale:
let formattedLocale = LocaleMatchingAttribute.localeIdentifierAsJsonFormat(localeIdentifier)
queryItems.append(URLQueryItem(name: parameter.rawValue, value: formattedLocale))
case .privacyProStatus:
if let privacyProStatusSurveyParameter = subscription?.privacyProStatusSurveyParameter {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: privacyProStatusSurveyParameter))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum RemoteMessagingSurveyActionParameter: String, CaseIterable {
case atbVariant = "var"
case daysInstalled = "delta"
case hardwareModel = "mo"
case locale = "locale"
case osVersion = "osv"
case privacyProStatus = "ppro_status"
case privacyProPlatform = "ppro_platform"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//
// DefaultRemoteMessagingSurveyURLBuilderTests.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import XCTest
import BrowserServicesKitTestsUtils
import RemoteMessagingTestsUtils
@testable import Subscription
@testable import RemoteMessaging

class DefaultRemoteMessagingSurveyURLBuilderTests: XCTestCase {
samsymons marked this conversation as resolved.
Show resolved Hide resolved

func testAddingATBParameter() {
let builder = buildRemoteMessagingSurveyURLBuilder(atb: "v456-7")
let baseURL = URL(string: "https://duckduckgo.com")!
let finalURL = builder.add(parameters: [.atb], to: baseURL)

XCTAssertEqual(finalURL.absoluteString, "https://duckduckgo.com?atb=v456-7")
}

func testAddingATBVariantParameter() {
let builder = buildRemoteMessagingSurveyURLBuilder(variant: "test-variant")
let baseURL = URL(string: "https://duckduckgo.com")!
let finalURL = builder.add(parameters: [.atbVariant], to: baseURL)

XCTAssertEqual(finalURL.absoluteString, "https://duckduckgo.com?var=test-variant")
}

func testAddingLocaleParameter() {
let builder = buildRemoteMessagingSurveyURLBuilder(locale: Locale(identifier: "en_NZ"))
let baseURL = URL(string: "https://duckduckgo.com")!
let finalURL = builder.add(parameters: [.locale], to: baseURL)

XCTAssertEqual(finalURL.absoluteString, "https://duckduckgo.com?locale=en-NZ")
}

func testAddingPrivacyProParameters() {
let builder = buildRemoteMessagingSurveyURLBuilder()
let baseURL = URL(string: "https://duckduckgo.com")!
let finalURL = builder.add(parameters: [.privacyProStatus, .privacyProPlatform, .privacyProPlatform], to: baseURL)

XCTAssertEqual(finalURL.absoluteString, "https://duckduckgo.com?ppro_status=auto_renewable&ppro_platform=apple&ppro_platform=apple")
}

func testAddingVPNUsageParameters() {
let builder = buildRemoteMessagingSurveyURLBuilder(vpnDaysSinceActivation: 10, vpnDaysSinceLastActive: 5)
let baseURL = URL(string: "https://duckduckgo.com")!
let finalURL = builder.add(parameters: [.vpnFirstUsed, .vpnLastUsed], to: baseURL)

XCTAssertEqual(finalURL.absoluteString, "https://duckduckgo.com?vpn_first_used=10&vpn_last_used=5")
}

func testAddingParametersToURLThatAlreadyHasThem() {
let builder = buildRemoteMessagingSurveyURLBuilder(vpnDaysSinceActivation: 10, vpnDaysSinceLastActive: 5)
let baseURL = URL(string: "https://duckduckgo.com?param=test")!
let finalURL = builder.add(parameters: [.vpnFirstUsed, .vpnLastUsed], to: baseURL)

XCTAssertEqual(finalURL.absoluteString, "https://duckduckgo.com?param=test&vpn_first_used=10&vpn_last_used=5")
}

private func buildRemoteMessagingSurveyURLBuilder(
atb: String = "v123-4",
variant: String = "var",
vpnDaysSinceActivation: Int = 2,
vpnDaysSinceLastActive: Int = 1,
locale: Locale = Locale(identifier: "en_US")
) -> DefaultRemoteMessagingSurveyURLBuilder {

let mockStatisticsStore = MockStatisticsStore()
mockStatisticsStore.atb = atb
mockStatisticsStore.variant = variant

let vpnActivationDateStore = MockVPNActivationDateStore(
daysSinceActivation: vpnDaysSinceActivation,
daysSinceLastActive: vpnDaysSinceLastActive
)

let subscription = DDGSubscription(productId: "product-id",
name: "product-name",
billingPeriod: .monthly,
startedAt: Date(timeIntervalSince1970: 1000),
expiresOrRenewsAt: Date(timeIntervalSince1970: 2000),
platform: .apple,
status: .autoRenewable)

return DefaultRemoteMessagingSurveyURLBuilder(
statisticsStore: mockStatisticsStore,
vpnActivationDateStore: vpnActivationDateStore,
subscription: subscription,
localeIdentifier: locale.identifier)
}

}

private class MockVPNActivationDateStore: VPNActivationDateProviding {

var _daysSinceActivation: Int
var _daysSinceLastActive: Int

init(daysSinceActivation: Int, daysSinceLastActive: Int) {
self._daysSinceActivation = daysSinceActivation
self._daysSinceLastActive = daysSinceLastActive
}

func daysSinceActivation() -> Int? {
return _daysSinceActivation
}

func daysSinceLastActive() -> Int? {
return _daysSinceLastActive
}

}
Loading