forked from criteo/ios-publisher-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCR_CacheManagerTests.m
224 lines (189 loc) · 8.29 KB
/
CR_CacheManagerTests.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
//
// CR_CacheManagerTests.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 <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
#import <OCMock.h>
#import "Criteo.h"
#import "Criteo+Internal.h"
#import "CR_CacheAdUnit.h"
#import "CR_CacheManager.h"
#import "CR_DeviceInfo.h"
#import "CR_CdbBidBuilder.h"
#import "CR_DependencyProvider.h"
@interface CR_CacheManagerTests : XCTestCase
@property(strong) CR_CacheManager *cacheManager;
@property(strong) CR_CacheAdUnit *adUnit;
@property(strong) CR_CdbBid *testBid;
@property(strong) NSDictionary *assetsDict;
@property(strong) NSDictionary *productDict1;
@property(strong) NSDictionary *productDict2;
@property(strong) NSDictionary *impressionPixelDict1;
@property(strong) NSDictionary *impressionPixelDict2;
@property(strong) NSDictionary *advertiserDict;
@property(strong) NSDictionary *privacyDict;
@property(strong) CR_DeviceInfo *originDeviceInfo;
@end
@implementation CR_CacheManagerTests
- (void)setUp {
self.cacheManager = [[CR_CacheManager alloc] init];
self.productDict1 = @{
@"title" : @"\"Stripe Pima Dress\" - $99",
@"description" : @"We're All About Comfort.",
@"price" : @"$99",
@"clickUrl" : @"https://cat.sv.us.criteo.com/delivery/ckn.php?",
@"callToAction" : @"scipio",
@"image" :
@{@"url" : @"https://pix.us.criteo.net/img/img?", @"height" : @(501), @"width" : @(502)}
};
self.productDict2 = @{
@"title" : @"\"Just a Dress\" - $9999",
@"description" : @"We're NOT About Comfort.",
@"price" : @"$9999",
@"clickUrl" : @"https://cat.sv.us.criteo.com/delivery/ckn2.php?",
@"callToAction" : @"Buy this blinkin dress",
@"image" :
@{@"url" : @"https://pix.us.criteo.net/img/img2?", @"height" : @(401), @"width" : @(402)}
};
self.impressionPixelDict1 = @{@"url" : @"https://cat.sv.us.criteo.com/delivery/lgn.php?"};
self.impressionPixelDict2 = @{@"url" : @"https://cat.sv.us.criteo.com/delivery2/lgn.php?"};
self.advertiserDict = @{
@"description" : @"The Company Store",
@"domain" : @"thecompanystore.com",
@"logo" :
@{@"url" : @"https://pix.us.criteo.net/img/img?", @"height" : @(200), @"width" : @(300)},
@"logoClickUrl" : @"https://cat.sv.us.criteo.com/delivery/ckn.php?"
};
self.privacyDict = @{
@"optoutClickUrl" : @"https://privacy.us.criteo.com/adcenter?",
@"optoutImageUrl" : @"https://static.criteo.net/flash/icon/nai_small.png",
@"longLegalText" : @"Blah dee blah blah"
};
self.assetsDict = @{
@"products" : @[ self.productDict1, self.productDict2 ],
@"privacy" : self.privacyDict,
@"advertiser" : self.advertiserDict,
@"impressionPixels" : @[ self.impressionPixelDict1, self.impressionPixelDict2 ]
};
}
- (void)testGetBidWithinTtl {
[self createAdUnitAndTestBidWithSize:CGSizeMake(200, 100)
adUnitType:CRAdUnitTypeBanner
nativeAssets:nil];
[self.cacheManager setBid:self.testBid];
CR_CdbBid *retrievedBid = [self.cacheManager getBidForAdUnit:self.adUnit];
XCTAssertNotNil(retrievedBid);
XCTAssertEqualObjects(self.adUnit.adUnitId, retrievedBid.placementId);
}
- (void)testSetBidForBanner {
[self createAdUnitAndTestBidWithSize:CGSizeMake(320, 50)
adUnitType:CRAdUnitTypeBanner
nativeAssets:nil];
CR_CacheAdUnit *newAdUnit = [self.cacheManager setBid:self.testBid];
XCTAssertEqualObjects(self.adUnit, newAdUnit);
XCTAssertTrue([[self.cacheManager getBidForAdUnit:self.adUnit] isEqual:self.testBid]);
}
- (void)testSetBidForInterstitial {
CR_DeviceInfo *deviceInfo = [self mockDeviceInfo];
OCMStub(deviceInfo.screenSize).andReturn(CGSizeMake(320, 50));
[self createAdUnitAndTestBidWithSize:CGSizeMake(320, 50)
adUnitType:CRAdUnitTypeInterstitial
nativeAssets:nil];
[self.cacheManager setBid:self.testBid];
XCTAssertTrue([[self.cacheManager getBidForAdUnit:self.adUnit] isEqual:self.testBid]);
OCMStub(deviceInfo.screenSize).andReturn(CGSizeMake(50, 320));
[self.cacheManager setBid:self.testBid];
XCTAssertTrue([[self.cacheManager getBidForAdUnit:self.adUnit] isEqual:self.testBid]);
Criteo.sharedCriteo.dependencyProvider.deviceInfo = _originDeviceInfo;
}
- (void)testSetBidForNative {
CR_NativeAssets *nativeAssets = [[CR_NativeAssets alloc] initWithDict:self.assetsDict];
[self createAdUnitAndTestBidWithSize:CGSizeMake(2, 2)
adUnitType:CRAdUnitTypeNative
nativeAssets:nativeAssets];
[self.cacheManager setBid:self.testBid];
CR_CdbBid *cachedTestBid = [self.cacheManager getBidForAdUnit:self.adUnit];
XCTAssertEqualObjects(self.testBid, cachedTestBid);
}
- (void)testSetNullBid_ShouldNotSetBid {
CR_CacheAdUnit *adUnit = [self.cacheManager setBid:nil];
XCTAssertNil([self.cacheManager getBidForAdUnit:adUnit]);
XCTAssertNil(adUnit);
}
- (void)testSetBidForRewarded {
CR_DeviceInfo *deviceInfo = [self mockDeviceInfo];
OCMStub(deviceInfo.screenSize).andReturn(CGSizeMake(320, 50));
[self createAdUnitAndTestBidWithSize:CGSizeMake(320, 50)
adUnitType:CRAdUnitTypeRewarded
nativeAssets:nil];
[self.cacheManager setBid:self.testBid];
XCTAssertTrue([[self.cacheManager getBidForAdUnit:self.adUnit] isEqual:self.testBid]);
OCMStub(deviceInfo.screenSize).andReturn(CGSizeMake(50, 320));
[self.cacheManager setBid:self.testBid];
XCTAssertTrue([[self.cacheManager getBidForAdUnit:self.adUnit] isEqual:self.testBid]);
Criteo.sharedCriteo.dependencyProvider.deviceInfo = _originDeviceInfo;
}
- (void)testSetInvalidBid_ShouldNotSetBid {
NSDictionary *badAssetsDict = @{
@"products" : @[ self.productDict1, self.productDict2 ],
@"privacy" : self.privacyDict,
@"advertiser" : self.advertiserDict,
@"impressionPixels" : @[]
};
CR_NativeAssets *badNativeAssets = [[CR_NativeAssets alloc] initWithDict:badAssetsDict];
[self createAdUnitAndTestBidWithSize:CGSizeMake(320, 50)
adUnitType:CRAdUnitTypeNative
nativeAssets:badNativeAssets];
CR_CacheAdUnit *newAdUnit = [self.cacheManager setBid:self.testBid];
XCTAssertNil([self.cacheManager getBidForAdUnit:self.adUnit]);
XCTAssertNil(newAdUnit);
}
- (void)testSetBidWithInvalidAdUnit_ShouldNotSetBid {
[self createAdUnitAndTestBidWithSize:CGSizeMake(0, 0)
adUnitType:CRAdUnitTypeBanner
nativeAssets:nil];
CR_CacheAdUnit *newAdUnit = [self.cacheManager setBid:self.testBid];
XCTAssertNil([self.cacheManager getBidForAdUnit:self.adUnit]);
XCTAssertNil(newAdUnit);
}
#pragma mark - Private methods
- (void)createAdUnitAndTestBidWithSize:(CGSize)size
adUnitType:(CRAdUnitType)adUnitType
nativeAssets:(CR_NativeAssets *)nativeAssets {
self.adUnit = [[CR_CacheAdUnit alloc] initWithAdUnitId:@"a_test_placement"
size:size
adUnitType:adUnitType];
self.testBid = CR_CdbBidBuilder.new.zoneId(1)
.adUnit(self.adUnit)
.cpm(@"0.0312")
.currency(@"USD")
.ttl(200)
.creative(nil)
.displayUrl(@"https://someUrl.com")
.insertTime([NSDate date])
.nativeAssets(nativeAssets)
.impressionId(nil)
.build;
}
- (CR_DeviceInfo *)mockDeviceInfo {
_originDeviceInfo = Criteo.sharedCriteo.dependencyProvider.deviceInfo;
CR_DeviceInfo *deviceInfo = OCMPartialMock(_originDeviceInfo);
Criteo.sharedCriteo.dependencyProvider.deviceInfo = deviceInfo;
return deviceInfo;
}
@end