-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCR_ProfileIdFunctionalTests.m
346 lines (269 loc) · 11.5 KB
/
CR_ProfileIdFunctionalTests.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// CR_ProfileIdFunctionalTests.m
// CriteoPublisherSdkTests
//
// Copyright © 2018-2020 Criteo. 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 <FunctionalObjC/FBLFunctional.h>
#import <XCTest/XCTest.h>
#import <OCMock.h>
#import "Criteo+Testing.h"
#import "Criteo+Internal.h"
#import "CR_Config.h"
#import "CR_DependencyProvider+Testing.h"
#import "CR_IntegrationRegistry.h"
#import "CR_NetworkCaptor.h"
#import "CR_TestAdUnits.h"
#import "CR_ThreadManager+Waiter.h"
#import "NSURL+Testing.h"
#import "CRBannerView.h"
#import "CRBannerView+Internal.h"
#import "CRInterstitial+Internal.h"
#import "CRNativeLoader+Internal.h"
#import "CR_URLOpenerMock.h"
#import "DFPRequestClasses.h"
#import "CRContextData.h"
@interface CR_ProfileIdFunctionalTests : XCTestCase
@property(strong, nonatomic) Criteo *criteo;
@property(strong, nonatomic) CR_DependencyProvider *dependencyProvider;
@end
@implementation CR_ProfileIdFunctionalTests
- (void)setUp {
[super setUp];
self.dependencyProvider =
CR_DependencyProvider.new.withIsolatedUserDefaults.withWireMockConfiguration
.withListenedNetworkManager.withIsolatedFeedbackStorage.withIsolatedNotificationCenter
.withShortLiveBidTimeBudget.withConsentGiven;
[self resetCriteo];
}
- (void)tearDown {
CR_ThreadManager *threadManager = self.criteo.dependencyProvider.threadManager;
[threadManager waiter_waitIdle];
[super tearDown];
}
#pragma mark - Prefetch
- (void)testPrefetch_GivenSdkUsedForTheFirstTime_UseFallbackProfileId {
[self prepareCriteoForGettingBid];
NSDictionary *request = [self cdbRequest];
XCTAssertEqualObjects(request[@"profileId"], @(CR_IntegrationFallback));
}
- (void)testPrefetch_GivenUsedSdk_UseLastProfileId {
[self.dependencyProvider.integrationRegistry declare:CR_IntegrationInHouse];
[self prepareCriteoAndGetBid];
NSDictionary *request = [self cdbRequest];
XCTAssertEqualObjects(request[@"profileId"], @(CR_IntegrationInHouse));
}
#pragma mark - Config
- (void)testRemoteConfig_GivenSdkUsedForTheFirstTime_UseFallbackProfileId {
[self prepareCriteoForGettingBid];
NSDictionary *request = [self configRequest];
XCTAssertEqualObjects(request[@"rtbProfileId"], @(CR_IntegrationFallback));
}
- (void)testRemoteConfig_GivenUsedSdk_UseLastProfileId {
[self.dependencyProvider.integrationRegistry declare:CR_IntegrationInHouse];
[self prepareCriteoAndGetBid];
[self resetCriteo];
[self.dependencyProvider.integrationRegistry declare:CR_IntegrationInHouse];
[self prepareCriteoForGettingBid];
NSDictionary *request = [self configRequest];
XCTAssertEqualObjects(request[@"rtbProfileId"], @(CR_IntegrationInHouse));
}
#pragma mark - CSM
- (void)testCsm_GivenPrefetchWithSdkUsedForTheFirstTime_UseFallbackProfileId {
CRBannerAdUnit *adUnit = [self prepareCriteoAndGetBid];
[self.criteo.testing_networkCaptor clear];
// Get another bid to send CSM of previous call
[self getBidWithAdUnit:adUnit];
NSDictionary *request = [self csmRequest];
XCTAssertEqualObjects(request[@"profile_id"], @(CR_IntegrationFallback));
}
- (void)
testCsm_GivenIntegrationSpecificBidConsumedWithSdkUsedForTheFirstTime_UseIntegrationProfileId {
CRBannerAdUnit *adUnit = [self prepareCriteoAndGetBid];
[self resetCriteo];
[self.dependencyProvider.integrationRegistry declare:CR_IntegrationInHouse];
[self prepareCriteoForGettingBidWithAdUnits:@[ adUnit ]];
[self getBidWithAdUnit:adUnit];
[self.criteo.testing_networkCaptor clear];
// Get another bid to send CSM of previous call
[self getBidWithAdUnit:adUnit];
NSDictionary *request = [self csmRequest];
XCTAssertEqualObjects(request[@"profile_id"], @(CR_IntegrationInHouse));
}
#pragma mark - Standalone
- (void)validateStandaloneTest {
[self waitForIdleState];
NSDictionary *request = [self cdbRequest];
XCTAssertEqualObjects(request[@"profileId"], @(CR_IntegrationStandalone));
}
- (void)testStandaloneBanner_GivenAnyPreviousIntegration_UseStandaloneProfileId {
CRBannerAdUnit *adUnit = [CR_TestAdUnits preprodBanner320x50];
[self prepareUsedSdkWithInHouse:adUnit];
CRBannerView *bannerView = [[CRBannerView alloc] initWithAdUnit:adUnit criteo:self.criteo];
[bannerView loadAdWithContext:self.contextData];
[self validateStandaloneTest];
}
- (void)testStandaloneInterstitial_GivenAnyPreviousIntegration_UseStandaloneProfileId {
CRInterstitialAdUnit *adUnit = [CR_TestAdUnits preprodInterstitial];
[self prepareUsedSdkWithInHouse:adUnit];
CRInterstitial *interstitial = [[CRInterstitial alloc] initWithAdUnit:adUnit criteo:self.criteo];
[interstitial loadAdWithContext:self.contextData];
[self validateStandaloneTest];
}
- (void)testStandaloneNative_GivenAnyPreviousIntegration_UseStandaloneProfileId {
CRNativeAdUnit *adUnit = [CR_TestAdUnits preprodNative];
[self prepareUsedSdkWithInHouse:adUnit];
CRNativeLoader *nativeLoader =
[[CRNativeLoader alloc] initWithAdUnit:adUnit
criteo:self.criteo
urlOpener:[[CR_URLOpenerMock alloc] init]];
nativeLoader.delegate = OCMProtocolMock(@protocol(CRNativeLoaderDelegate));
[nativeLoader loadAdWithContext:self.contextData];
[self validateStandaloneTest];
}
#pragma mark - InHouse
- (void)testInHouseBanner_GivenAnyPreviousIntegration_UseInHouseProfileId {
CRBannerAdUnit *adUnit = [CR_TestAdUnits preprodBanner320x50];
CRBid *bid = [self prepareCriteoAndGetBidWithAdUnit:adUnit];
CRBannerView *bannerView = [[CRBannerView alloc] initWithAdUnit:adUnit criteo:self.criteo];
[bannerView loadAdWithBid:bid]; // declares integration
// Get another bid to request with declared integration
[self expectIntegrationType:CR_IntegrationInHouse whenGettingBidForAdUnit:adUnit];
}
- (void)testInHouseInterstitial_GivenAnyPreviousIntegration_UseInHouseProfileId {
CRInterstitialAdUnit *adUnit = [CR_TestAdUnits preprodInterstitial];
CRBid *bid = [self prepareCriteoAndGetBidWithAdUnit:adUnit];
CRInterstitial *interstitial = [[CRInterstitial alloc] initWithAdUnit:adUnit criteo:self.criteo];
[interstitial loadAdWithBid:bid]; // declares integration
// Get another bid to request with declared integration
[self expectIntegrationType:CR_IntegrationInHouse whenGettingBidForAdUnit:adUnit];
}
- (void)testInHouseNative_GivenAnyPreviousIntegration_UseInHouseProfileId {
CRNativeAdUnit *adUnit = [CR_TestAdUnits preprodNative];
CRBid *bid = [self prepareCriteoAndGetBidWithAdUnit:adUnit];
CRNativeLoader *nativeLoader = [[CRNativeLoader alloc] initWithAdUnit:adUnit criteo:self.criteo];
[nativeLoader loadAdWithBid:bid]; // declares integration
// Get another bid to request with declared integration
[self expectIntegrationType:CR_IntegrationInHouse whenGettingBidForAdUnit:adUnit];
}
#pragma mark - AppBidding
- (void)expectAppBiddingIntegrationType:(CR_IntegrationType)expectedType
afterEnrichingAdObject:(id)adObject
forAdUnit:(CRAdUnit *)adUnit {
// First call declares integration
[self enrichAdObject:adObject forAdUnit:adUnit];
// Get another bid to request with declared integration
[self.criteo.testing_networkCaptor clear];
[self expectIntegrationType:expectedType whenGettingBidForAdUnit:adUnit];
}
- (void)testCustomAppBidding_GivenAnyPreviousIntegration_UseCustomAppBiddingProfileId {
CRBannerAdUnit *adUnit = [CR_TestAdUnits preprodBanner320x50];
[self prepareUsedSdkWithInHouse:adUnit];
[self expectAppBiddingIntegrationType:CR_IntegrationCustomAppBidding
afterEnrichingAdObject:NSMutableDictionary.new
forAdUnit:adUnit];
}
- (void)testGamAppBidding_GivenAnyPreviousIntegration_UseCustomAppBiddingProfileId {
CRBannerAdUnit *adUnit = [CR_TestAdUnits preprodBanner320x50];
[self prepareUsedSdkWithInHouse:adUnit];
[self expectAppBiddingIntegrationType:CR_IntegrationGamAppBidding
afterEnrichingAdObject:GAMRequest.new
forAdUnit:adUnit];
}
#pragma mark - Private
- (void)prepareUsedSdkWithInHouse:(CRAdUnit *)adUnit {
[self prepareCriteoAndGetBidWithAdUnit:adUnit];
[self resetCriteo];
[self prepareCriteoForGettingBidWithAdUnits:@[ adUnit ]];
[self.criteo.testing_networkCaptor clear];
}
- (void)resetCriteo {
self.criteo = [[Criteo alloc] initWithDependencyProvider:self.dependencyProvider];
}
- (void)prepareCriteoForGettingBidWithAdUnits:(NSArray *)adUnits {
[self.criteo testing_registerWithAdUnits:adUnits];
[self waitForIdleState];
}
- (CRBannerAdUnit *)prepareCriteoForGettingBid {
CRBannerAdUnit *adUnit = [CR_TestAdUnits preprodBanner320x50];
[self prepareCriteoForGettingBidWithAdUnits:@[ adUnit ]];
return adUnit;
}
- (CRBid *)prepareCriteoAndGetBidWithAdUnit:(CRAdUnit *)adUnit {
[self prepareCriteoForGettingBidWithAdUnits:@[ adUnit ]];
return [self getBidWithAdUnit:adUnit];
}
- (CRBannerAdUnit *)prepareCriteoAndGetBid {
CRBannerAdUnit *adUnit = [CR_TestAdUnits preprodBanner320x50];
[self prepareCriteoAndGetBidWithAdUnit:adUnit];
return adUnit;
}
- (CRBid *)getBidWithAdUnit:(CRAdUnit *)adUnit {
[self.criteo.testing_networkCaptor clear];
__block CRBid *bid;
[self.criteo loadBidForAdUnit:adUnit
withContext:self.contextData
responseHandler:^(CRBid *bid_) {
bid = bid_;
}];
[self waitForIdleState];
return bid;
}
- (void)waitForIdleState {
[self.dependencyProvider.threadManager waiter_waitIdle];
}
- (NSDictionary *)requestPassingTest:(BOOL (^)(CR_HttpContent *))predicate {
NSArray<CR_HttpContent *> *requests = self.criteo.testing_networkCaptor.allRequests;
NSUInteger index = [requests
indexOfObjectPassingTest:^BOOL(CR_HttpContent *httpContent, NSUInteger idx, BOOL *stop) {
return predicate(httpContent);
}];
CR_HttpContent *request = (index != NSNotFound) ? requests[index] : nil;
return request.requestBody;
}
- (NSDictionary *)cdbRequest {
return [self requestPassingTest:^BOOL(CR_HttpContent *httpContent) {
return [httpContent.url testing_isBidUrlWithConfig:self.criteo.config];
}];
}
- (NSDictionary *)configRequest {
return [self requestPassingTest:^BOOL(CR_HttpContent *httpContent) {
return [httpContent.url testing_isConfigUrlWithConfig:self.criteo.config];
}];
}
- (NSDictionary *)csmRequest {
return [self requestPassingTest:^BOOL(CR_HttpContent *httpContent) {
return [httpContent.url testing_isFeedbackMessageUrlWithConfig:self.criteo.config];
}];
}
- (void)enrichAdObject:(id)adObject forAdUnit:(CRAdUnit *)adUnit {
[self.criteo loadBidForAdUnit:adUnit
withContext:CRContextData.new
responseHandler:^(CRBid *bid) {
[self.criteo enrichAdObject:adObject withBid:bid];
}];
[self waitForIdleState];
}
- (void)expectIntegrationType:(CR_IntegrationType)expectedType
whenGettingBidForAdUnit:(CRAdUnit *)adUnit {
[self.criteo.testing_networkCaptor clear];
[self getBidWithAdUnit:adUnit];
NSDictionary *request = [self cdbRequest];
XCTAssertEqualObjects(request[@"profileId"], @(expectedType));
}
- (CRContextData *)contextData {
return CRContextData.new;
}
@end