Skip to content

Commit 83c8dfe

Browse files
joesusfacebook-github-bot
authored andcommitted
Backfill Unit Tests AppEventsConfiguration
Summary: $title Reviewed By: jamestouri Differential Revision: D26886787 fbshipit-source-id: de1bc19de13d5f6a05d1a230dcb4eccb02ade712
1 parent 9978667 commit 83c8dfe

4 files changed

Lines changed: 168 additions & 5 deletions

File tree

FBSDKCoreKit/FBSDKCoreKitTests/FBSDKCoreKitTests-Bridging-Header.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#import "FBSDKServerConfigurationFixtures.h"
3131
#import "FBSDKSettingsProtocol.h"
3232
#import "FBSDKTestCase.h"
33+
#import "FBSDKTestCoder.h"
3334
#import "UserDefaultsSpy.h"
3435
#import "FakeBundle.h"
3536
// URLSession Abstraction

FBSDKCoreKit/FBSDKCoreKitTests/Internal/AppEvents/FBSDKAppEventsConfigurationTests.swift

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,133 @@ class FBSDKAppEventsConfigurationTests: XCTestCase {
5555
config = Fixtures.config(with: ["event_collection_enabled": true])
5656
XCTAssertTrue(config.eventCollectionEnabled, "Event collection enabled should be settable")
5757
}
58+
59+
func testCodingSecurity() {
60+
XCTAssertTrue(AppEventsConfiguration.supportsSecureCoding, "Should support secure coding")
61+
}
62+
63+
func testCreatingWithMissingDictionary() {
64+
config = AppEventsConfiguration(json: nil)
65+
XCTAssertEqual(
66+
config,
67+
AppEventsConfiguration.default(),
68+
"Should use the default config when creating with a missing dictionary"
69+
)
70+
}
71+
72+
func testCreatingWithEmptyDictionary() {
73+
config = AppEventsConfiguration(json: [:])
74+
XCTAssertEqual(
75+
config,
76+
AppEventsConfiguration.default(),
77+
"Should use the default config when creating with an empty dictionary"
78+
)
79+
}
80+
81+
func testCreatingWithMissingTopLevelKey() {
82+
config = AppEventsConfiguration(
83+
json: RawAppEventsConfigurationResponseFixtures.validMissingTopLevelKey
84+
)
85+
XCTAssertEqual(
86+
config,
87+
AppEventsConfiguration.default(),
88+
"Should use the default config when creating with a missing top level key"
89+
)
90+
}
91+
92+
func testCreatingWithInvalidValues() {
93+
config = AppEventsConfiguration(
94+
json: RawAppEventsConfigurationResponseFixtures.invalidValues
95+
)
96+
XCTAssertEqual(
97+
config.defaultATEStatus,
98+
.unspecified,
99+
"Should use the correct default for ate status"
100+
)
101+
XCTAssertTrue(
102+
config.advertiserIDCollectionEnabled,
103+
"Should use the correct default for ad ID collection"
104+
)
105+
XCTAssertFalse(
106+
config.eventCollectionEnabled,
107+
"Should use the correct default for event collection"
108+
)
109+
}
110+
111+
func testCreatingWithValidValues() {
112+
config = AppEventsConfiguration(json: RawAppEventsConfigurationResponseFixtures.valid)
113+
XCTAssertEqual(
114+
config.defaultATEStatus,
115+
.disallowed,
116+
"Should use the provided value for the default ate status"
117+
)
118+
XCTAssertFalse(
119+
config.advertiserIDCollectionEnabled,
120+
"Should use the provided value for ad ID collection"
121+
)
122+
XCTAssertTrue(
123+
config.eventCollectionEnabled,
124+
"Should use the provided value for event collection"
125+
)
126+
}
127+
128+
// MARK: Coding
129+
130+
func testEncoding() {
131+
let coder = TestCoder()
132+
config = AppEventsConfiguration.default()
133+
config.encode(with: coder)
134+
135+
XCTAssertEqual(
136+
coder.encodedObject["default_ate_status"] as? Int,
137+
Int(config.defaultATEStatus.rawValue),
138+
"Should encode the expected default ate status value as an integer"
139+
)
140+
XCTAssertEqual(
141+
coder.encodedObject["advertiser_id_collection_enabled"] as? Bool,
142+
config.advertiserIDCollectionEnabled,
143+
"Should encode the expected advertiser ID collection enabled value"
144+
)
145+
XCTAssertEqual(
146+
coder.encodedObject["event_collection_enabled"] as? Bool,
147+
config.eventCollectionEnabled,
148+
"Should encode the expected eventCollectionEnabled value"
149+
)
150+
}
151+
152+
func testDecoding() {
153+
let coder = TestCoder()
154+
_ = AppEventsConfiguration(coder: coder)
155+
156+
config.encode(with: coder)
157+
158+
XCTAssertEqual(
159+
coder.decodedObject["default_ate_status"] as? String,
160+
"decodeIntegerForKey",
161+
"Initializing from a decoder should attempt to decode an int for the ate status key"
162+
)
163+
XCTAssertEqual(
164+
coder.decodedObject["advertiser_id_collection_enabled"] as? String,
165+
"decodeBoolForKey",
166+
"Initializing from a decoder should attempt to decode an int for the advertiser ID collection enabled key"
167+
)
168+
XCTAssertEqual(
169+
coder.decodedObject["event_collection_enabled"] as? String,
170+
"decodeBoolForKey",
171+
"Initializing from a decoder should attempt to decode an int for the event collection enabled key"
172+
)
173+
}
174+
}
175+
176+
extension AppEventsConfiguration {
177+
// swiftlint:disable:next override_in_extension
178+
open override func isEqual(_ object: Any?) -> Bool {
179+
if let other = object as? AppEventsConfiguration {
180+
return advertiserIDCollectionEnabled == other.advertiserIDCollectionEnabled &&
181+
eventCollectionEnabled == other.eventCollectionEnabled &&
182+
defaultATEStatus == other.defaultATEStatus
183+
} else {
184+
return super.isEqual(object)
185+
}
186+
}
58187
}

FBSDKCoreKit/FBSDKCoreKitTests/Internal/AppEvents/RawAppEventsConfigurationResponseFixtures.swift

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,46 @@ class RawAppEventsConfigurationResponseFixtures: NSObject {
2525
static let defaultATEStatus = "default_ate_status"
2626
static let advertiserIDCollectionEnabled = "advertiser_id_collection_enabled"
2727
static let eventCollectionEnabled = "event_collection_enabled"
28+
static let topLevel = "app_events_config"
2829
}
2930

30-
/// Provides a dictionary with well-known keys and random values for a network provided app events configuration
31-
class var random: [AnyHashable: Any] {
31+
static var valid: [String: Any] {
3232
return [
33-
Keys.defaultATEStatus: Fuzzer.random,
34-
Keys.advertiserIDCollectionEnabled: Fuzzer.random,
35-
Keys.eventCollectionEnabled: Fuzzer.random,
33+
Keys.topLevel: [
34+
Keys.defaultATEStatus: 1,
35+
Keys.advertiserIDCollectionEnabled: false,
36+
Keys.eventCollectionEnabled: true
37+
]
38+
]
39+
}
40+
41+
static var validMissingTopLevelKey: [String: Any] {
42+
return [
43+
Keys.defaultATEStatus: 1,
44+
Keys.advertiserIDCollectionEnabled: 1,
45+
Keys.eventCollectionEnabled: 1,
46+
]
47+
}
48+
49+
static var invalidValues: [String: Any] {
50+
return [
51+
Keys.topLevel: [
52+
Keys.defaultATEStatus: "foo",
53+
Keys.advertiserIDCollectionEnabled: "bar",
54+
Keys.eventCollectionEnabled: "baz"
55+
]
56+
]
57+
}
58+
59+
/// Provides a dictionary with well-known keys and random values for a network provided app events configuration
60+
static var random: Any {
61+
let response = [
62+
Keys.topLevel: [
63+
Keys.defaultATEStatus: Fuzzer.random,
64+
Keys.advertiserIDCollectionEnabled: Fuzzer.random,
65+
Keys.eventCollectionEnabled: Fuzzer.random,
66+
]
3667
]
68+
return Fuzzer.randomize(json: response)
3769
}
3870
}

FBSDKCoreKit/FBSDKCoreKitTests/Internal/Helpers/FBSDKTestCoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

23+
NS_SWIFT_NAME(TestCoder)
2324
@interface FBSDKTestCoder : NSCoder
2425

2526
@property (nonatomic) NSMutableDictionary<NSString *, id> *encodedObject;

0 commit comments

Comments
 (0)