-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathRCNUserDefaultsManagerTests.m
290 lines (242 loc) · 13.1 KB
/
RCNUserDefaultsManagerTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2019 Google
*
* 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/XCTest.h>
@import FirebaseRemoteConfig;
static NSTimeInterval RCNUserDefaultsSampleTimeStamp = 0;
static NSString* const AppName = @"testApp";
static NSString* const FQNamespace1 = @"testNamespace1:testApp";
static NSString* const FQNamespace2 = @"testNamespace2:testApp";
static NSMutableDictionary<NSString*, NSString*>* customSignals1 = nil;
static NSMutableDictionary<NSString*, NSString*>* customSignals2 = nil;
@interface RCNUserDefaultsManagerTests : XCTestCase
@end
@implementation RCNUserDefaultsManagerTests
- (void)setUp {
[super setUp];
[[NSUserDefaults standardUserDefaults]
removePersistentDomainForName:[NSBundle mainBundle].bundleIdentifier];
RCNUserDefaultsSampleTimeStamp = [[NSDate date] timeIntervalSince1970];
customSignals1 = [[NSMutableDictionary alloc] initWithDictionary:@{
@"signal1" : @"stringValue",
}];
customSignals2 = [[NSMutableDictionary alloc] initWithDictionary:@{
@"signal2" : @"stringValue2",
}];
}
- (void)testUserDefaultsEtagWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setLastETag:@"eTag1"];
XCTAssertEqualObjects([manager lastETag], @"eTag1");
[manager setLastETag:@"eTag2"];
XCTAssertEqualObjects([manager lastETag], @"eTag2");
}
- (void)testUserDefaultsLastFetchTimeWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setLastFetchTime:RCNUserDefaultsSampleTimeStamp];
XCTAssertEqual([manager lastFetchTime], RCNUserDefaultsSampleTimeStamp);
[manager setLastFetchTime:RCNUserDefaultsSampleTimeStamp - 1000];
XCTAssertEqual([manager lastFetchTime], RCNUserDefaultsSampleTimeStamp - 1000);
}
- (void)testUserDefaultsLastETagUpdateTimeWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setLastETagUpdateTime:RCNUserDefaultsSampleTimeStamp];
XCTAssertEqual([manager lastETagUpdateTime], RCNUserDefaultsSampleTimeStamp);
[manager setLastETagUpdateTime:RCNUserDefaultsSampleTimeStamp - 1000];
XCTAssertEqual([manager lastETagUpdateTime], RCNUserDefaultsSampleTimeStamp - 1000);
}
- (void)testUserDefaultsLastFetchStatusWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setLastFetchStatus:@"Success"];
XCTAssertEqualObjects([manager lastFetchStatus], @"Success");
[manager setLastFetchStatus:@"Error"];
XCTAssertEqualObjects([manager lastFetchStatus], @"Error");
}
- (void)testUserDefaultsisClientThrottledWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setIsClientThrottledWithExponentialBackoff:YES];
XCTAssertEqual([manager isClientThrottledWithExponentialBackoff], YES);
[manager setIsClientThrottledWithExponentialBackoff:NO];
XCTAssertEqual([manager isClientThrottledWithExponentialBackoff], NO);
}
- (void)testUserDefaultsThrottleEndTimeWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 7.0];
XCTAssertEqual([manager throttleEndTime], RCNUserDefaultsSampleTimeStamp - 7.0);
[manager setThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 8.0];
XCTAssertEqual([manager throttleEndTime], RCNUserDefaultsSampleTimeStamp - 8.0);
}
- (void)testUserDefaultsCurrentThrottlingRetryIntervalWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setCurrentThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 1.0];
XCTAssertEqual([manager currentThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 1.0);
[manager setCurrentThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 2.0];
XCTAssertEqual([manager currentThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 2.0);
}
- (void)testUserDefaultsTemplateVersionWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setLastFetchedTemplateVersion:@"1"];
XCTAssertEqual([manager lastFetchedTemplateVersion], @"1");
}
- (void)testUserDefaultsActiveTemplateVersionWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setLastActiveTemplateVersion:@"1"];
XCTAssertEqual([manager lastActiveTemplateVersion], @"1");
}
- (void)testUserDefaultsRealtimeThrottleEndTimeWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setRealtimeThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 7.0];
XCTAssertEqual([manager realtimeThrottleEndTime], RCNUserDefaultsSampleTimeStamp - 7.0);
[manager setRealtimeThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 8.0];
XCTAssertEqual([manager realtimeThrottleEndTime], RCNUserDefaultsSampleTimeStamp - 8.0);
}
- (void)testUserDefaultsCurrentRealtimeThrottlingRetryIntervalWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setCurrentRealtimeThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 1.0];
XCTAssertEqual([manager currentRealtimeThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 1.0);
[manager setCurrentRealtimeThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 2.0];
XCTAssertEqual([manager currentRealtimeThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 2.0);
}
- (void)testUserDefaultsCustomSignalsWriteAndRead {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setCustomSignals:customSignals1];
XCTAssertEqualObjects([manager customSignals], customSignals1);
[manager setCustomSignals:customSignals2];
XCTAssertEqualObjects([manager customSignals], customSignals2);
}
- (void)testUserDefaultsForMultipleNamespaces {
RCNUserDefaultsManager* manager1 =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
RCNUserDefaultsManager* manager2 =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace2];
/// Last ETag.
[manager1 setLastETag:@"eTag1ForNamespace1"];
[manager2 setLastETag:@"eTag1ForNamespace2"];
XCTAssertEqualObjects([manager1 lastETag], @"eTag1ForNamespace1");
XCTAssertEqualObjects([manager2 lastETag], @"eTag1ForNamespace2");
/// Last fetch time.
[manager1 setLastFetchTime:RCNUserDefaultsSampleTimeStamp - 1000.0];
[manager2 setLastFetchTime:RCNUserDefaultsSampleTimeStamp - 7000.0];
XCTAssertEqual([manager1 lastFetchTime], RCNUserDefaultsSampleTimeStamp - 1000);
XCTAssertEqual([manager2 lastFetchTime], RCNUserDefaultsSampleTimeStamp - 7000);
/// Last fetch status.
[manager1 setLastFetchStatus:@"Success"];
[manager2 setLastFetchStatus:@"Error"];
XCTAssertEqualObjects([manager1 lastFetchStatus], @"Success");
XCTAssertEqualObjects([manager2 lastFetchStatus], @"Error");
/// Is client throttled.
[manager1 setIsClientThrottledWithExponentialBackoff:YES];
[manager2 setIsClientThrottledWithExponentialBackoff:NO];
XCTAssertEqual([manager1 isClientThrottledWithExponentialBackoff], YES);
XCTAssertEqual([manager2 isClientThrottledWithExponentialBackoff], NO);
/// Throttle end time.
[manager1 setThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 7.0];
[manager2 setThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 8.0];
XCTAssertEqual([manager1 throttleEndTime], RCNUserDefaultsSampleTimeStamp - 7.0);
XCTAssertEqual([manager2 throttleEndTime], RCNUserDefaultsSampleTimeStamp - 8.0);
/// Throttling retry interval.
[manager1 setCurrentThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 1.0];
[manager2 setCurrentThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 2.0];
XCTAssertEqual([manager1 currentThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 1.0);
XCTAssertEqual([manager2 currentThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 2.0);
/// Realtime throttle end time.
[manager1 setRealtimeThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 7.0];
[manager2 setRealtimeThrottleEndTime:RCNUserDefaultsSampleTimeStamp - 8.0];
XCTAssertEqual([manager1 realtimeThrottleEndTime], RCNUserDefaultsSampleTimeStamp - 7.0);
XCTAssertEqual([manager2 realtimeThrottleEndTime], RCNUserDefaultsSampleTimeStamp - 8.0);
/// Realtime throttling retry interval.
[manager1 setCurrentRealtimeThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 1.0];
[manager2 setCurrentRealtimeThrottlingRetryIntervalSeconds:RCNUserDefaultsSampleTimeStamp - 2.0];
XCTAssertEqual([manager1 currentRealtimeThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 1.0);
XCTAssertEqual([manager2 currentRealtimeThrottlingRetryIntervalSeconds],
RCNUserDefaultsSampleTimeStamp - 2.0);
/// Realtime retry count;
[manager1 setRealtimeRetryCount:1];
[manager2 setRealtimeRetryCount:2];
XCTAssertEqual([manager1 realtimeRetryCount], 1);
XCTAssertEqual([manager2 realtimeRetryCount], 2);
/// Fetch template version.
[manager1 setLastFetchedTemplateVersion:@"1"];
[manager2 setLastFetchedTemplateVersion:@"2"];
XCTAssertEqualObjects([manager1 lastFetchedTemplateVersion], @"1");
XCTAssertEqualObjects([manager2 lastFetchedTemplateVersion], @"2");
/// Active template version.
[manager1 setLastActiveTemplateVersion:@"1"];
[manager2 setLastActiveTemplateVersion:@"2"];
XCTAssertEqualObjects([manager1 lastActiveTemplateVersion], @"1");
XCTAssertEqualObjects([manager2 lastActiveTemplateVersion], @"2");
/// Custom Signals
[manager1 setCustomSignals:customSignals1];
[manager2 setCustomSignals:customSignals2];
XCTAssertEqualObjects([manager1 customSignals], customSignals1);
XCTAssertEqualObjects([manager2 customSignals], customSignals2);
}
- (void)testUserDefaultsReset {
RCNUserDefaultsManager* manager =
[[RCNUserDefaultsManager alloc] initWithAppName:AppName
bundleID:[NSBundle mainBundle].bundleIdentifier
namespace:FQNamespace1];
[manager setLastETag:@"testETag"];
[manager resetUserDefaults];
XCTAssertNil([manager lastETag]);
}
@end