Skip to content

Commit fe6fcfa

Browse files
committed
display receipt: add a (failing) non regression test for "isOptOut"
the current implementation is broken
1 parent 8e4ffe9 commit fe6fcfa

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

BatchExtension.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
512540D6254AD54000F01090 /* Writer+Optionals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 512540D5254AD54000F01090 /* Writer+Optionals.swift */; };
11+
5141F8EA258A6CB300170375 /* SharedDefaultsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5141F8DE258A211900170375 /* SharedDefaultsTests.swift */; };
1112
514E86CE2362EBEC00E42FF4 /* Consts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514E86C82362EBEC00E42FF4 /* Consts.swift */; };
1213
514E86CF2362EBEC00E42FF4 /* RichNotificationHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514E86CA2362EBEC00E42FF4 /* RichNotificationHelper.swift */; };
1314
514E86D02362EBEC00E42FF4 /* BAENotificationServiceExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 514E86CC2362EBEC00E42FF4 /* BAENotificationServiceExtension.m */; };
@@ -40,6 +41,7 @@
4041

4142
/* Begin PBXFileReference section */
4243
512540D5254AD54000F01090 /* Writer+Optionals.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Writer+Optionals.swift"; sourceTree = "<group>"; };
44+
5141F8DE258A211900170375 /* SharedDefaultsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedDefaultsTests.swift; sourceTree = "<group>"; };
4345
514E86C82362EBEC00E42FF4 /* Consts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Consts.swift; sourceTree = "<group>"; };
4446
514E86CA2362EBEC00E42FF4 /* RichNotificationHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RichNotificationHelper.swift; sourceTree = "<group>"; };
4547
514E86CC2362EBEC00E42FF4 /* BAENotificationServiceExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BAENotificationServiceExtension.m; sourceTree = "<group>"; };
@@ -154,6 +156,7 @@
154156
children = (
155157
51F7ED00236092FF00E01CD2 /* BatchExtensionTests.swift */,
156158
51F7ED02236092FF00E01CD2 /* Info.plist */,
159+
5141F8DE258A211900170375 /* SharedDefaultsTests.swift */,
157160
);
158161
path = Tests;
159162
sourceTree = "<group>";
@@ -314,6 +317,7 @@
314317
isa = PBXSourcesBuildPhase;
315318
buildActionMask = 2147483647;
316319
files = (
320+
5141F8EA258A6CB300170375 /* SharedDefaultsTests.swift in Sources */,
317321
51F7ED01236092FF00E01CD2 /* BatchExtensionTests.swift in Sources */,
318322
);
319323
runOnlyForDeploymentPostprocessing = 0;

Sources/Swift/Display Receipt/DisplayReceiptCacheHelper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import Foundation
99

1010
internal struct DisplayReceiptCacheHelper {
1111

12-
private var appInformationProvider: AppInformationProvider = AppInformationProviderDefaultImpl()
12+
private let appInformationProvider: AppInformationProvider
13+
14+
init(appInformationProvider injectedAppInfoProvider: AppInformationProvider = AppInformationProviderDefaultImpl()) {
15+
self.appInformationProvider = injectedAppInfoProvider
16+
}
1317

1418
func makeCoordinator() -> NSFileCoordinator {
1519
return NSFileCoordinator(filePresenter: nil)

Tests/SharedDefaultsTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// SharedDefaultsTests.swift
3+
// BatchExtension
4+
//
5+
// Copyright © 2020 Batch. All rights reserved.
6+
//
7+
8+
import XCTest
9+
@testable import BatchExtension
10+
11+
class SharedDefaultsTests: XCTestCase {
12+
13+
private let suiteName = "SharedDefaultsTests"
14+
private let sharedOptOutKey = "batch_shared_optout"
15+
private var defaults: UserDefaults!
16+
private var appInformationProvider: CustomDefaultAppInformationProvider!
17+
18+
override func setUp() {
19+
super.setUp()
20+
UserDefaults().removePersistentDomain(forName: suiteName)
21+
defaults = UserDefaults(suiteName: suiteName)!
22+
appInformationProvider = CustomDefaultAppInformationProvider(defaults: defaults)
23+
}
24+
25+
func testOptOutReading() throws {
26+
let cacheHelper = DisplayReceiptCacheHelper(appInformationProvider: appInformationProvider)
27+
// Default value, when key is missing, should be "true"
28+
XCTAssertTrue(try cacheHelper.isOptOut())
29+
30+
defaults.set(true, forKey: sharedOptOutKey)
31+
XCTAssertTrue(try cacheHelper.isOptOut())
32+
33+
defaults.set(false, forKey: sharedOptOutKey)
34+
XCTAssertFalse(try cacheHelper.isOptOut())
35+
}
36+
}
37+
38+
private struct CustomDefaultAppInformationProvider: AppInformationProvider {
39+
40+
var defaults: UserDefaults
41+
42+
func sharedDefaults() throws -> UserDefaults {
43+
return defaults
44+
}
45+
}

0 commit comments

Comments
 (0)