-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCRBannerViewDelegateTests.m
257 lines (211 loc) · 9.89 KB
/
CRBannerViewDelegateTests.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
//
// CRBannerViewDelegateTests.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 <XCTest/XCTest.h>
#import <OCMock.h>
#import "CRBannerView.h"
#import "Criteo.h"
#import "CRBannerView+Internal.h"
#import "Criteo+Internal.h"
#import "CRBid+Internal.h"
#import "CR_CdbBid.h"
#import "NSError+Criteo.h"
#import "CRInterstitialAdUnit.h"
#import "CRBannerViewDelegateMock.h"
#import "CR_URLOpenerMock.h"
#import "XCTestCase+Criteo.h"
#import "CR_DependencyProvider.h"
#import "CR_DependencyProvider+Testing.h"
#import "CRContextData.h"
@interface CRBannerViewDelegateTests : XCTestCase {
WKNavigationResponse *validNavigationResponse;
}
@property(nonatomic, strong) CR_CacheAdUnit *expectedCacheAdUnit;
@property(nonatomic, strong) CRBannerAdUnit *adUnit;
@property(nonatomic, strong) CRContextData *contextData;
@property(strong, nonatomic) CR_URLOpenerMock *urlOpener;
@property(strong, nonatomic) Criteo *criteo;
@property(strong, nonatomic) CRBannerViewDelegateMock *delegate;
@end
@implementation CRBannerViewDelegateTests
- (void)setUp {
self.expectedCacheAdUnit = [[CR_CacheAdUnit alloc] initWithAdUnitId:@"123"
size:CGSizeMake(47.0f, 57.0f)
adUnitType:CRAdUnitTypeBanner];
self.adUnit = [[CRBannerAdUnit alloc] initWithAdUnitId:@"123" size:CGSizeMake(47.0f, 57.0f)];
self.contextData = CRContextData.new;
self.urlOpener = [[CR_URLOpenerMock alloc] init];
CR_DependencyProvider *dependencyProvider = CR_DependencyProvider.testing_dependencyProvider;
self.criteo = OCMPartialMock([Criteo.alloc initWithDependencyProvider:dependencyProvider]);
self.delegate = [[CRBannerViewDelegateMock alloc] init];
}
- (WKNavigationResponse *)validNavigationResponse {
if (!validNavigationResponse) {
validNavigationResponse = OCMStrictClassMock([WKNavigationResponse class]);
NSHTTPURLResponse *response = OCMStrictClassMock([NSHTTPURLResponse class]);
OCMStub(response.statusCode).andReturn(200);
OCMStub(validNavigationResponse.response).andReturn(response);
}
return validNavigationResponse;
}
- (CR_CdbBid *)bidWithDisplayURL:(NSString *)displayURL {
return [[CR_CdbBid alloc] initWithZoneId:@123
placementId:@"placementId"
cpm:@"4.2"
currency:@"₹😀"
width:@47.0f
height:@57.0f
ttl:26
creative:@"THIS IS USELESS LEGACY"
displayUrl:displayURL
isVideo:NO
isRewarded:NO
insertTime:[NSDate date]
nativeAssets:nil
impressionId:nil
skAdNetworkParameters:nil];
}
- (void)testBannerDidReceiveAd {
WKWebView *realWebView = [WKWebView new];
[self mockCriteoWithAdUnit:self.expectedCacheAdUnit respondBid:[self bidWithDisplayURL:@"test"]];
CRBannerView *bannerView = [self bannerViewWithWebView:realWebView];
bannerView.delegate = self.delegate;
[bannerView loadAdWithContext:self.contextData];
[self cr_waitShortlyForExpectations:@[ self.delegate.didReceiveAdExpectation ]];
}
// test banner fail when an empty bid is returned
- (void)testBannerAdFetchFail {
self.delegate.expectedError = [NSError cr_errorWithCode:CRErrorCodeNoFill];
[self mockCriteoWithAdUnit:self.expectedCacheAdUnit respondBid:nil];
CRBannerView *bannerView = [self bannerViewWithWebView:nil];
bannerView.delegate = self.delegate;
[bannerView loadAdWithContext:self.contextData];
[self cr_waitShortlyForExpectations:@[ self.delegate.didFailToReceiveAdWithErrorExpectation ]];
}
- (void)testBannerWillLeaveApplicationAndWasClicked {
WKNavigationAction *mockNavigationAction = OCMStrictClassMock([WKNavigationAction class]);
OCMStub(mockNavigationAction.navigationType).andReturn(WKNavigationTypeLinkActivated);
WKFrameInfo *mockFrame = OCMStrictClassMock([WKFrameInfo class]);
OCMStub(mockNavigationAction.sourceFrame).andReturn(mockFrame);
OCMStub([mockFrame isMainFrame]).andReturn(YES);
NSURL *url = [[NSURL alloc] initWithString:@"123"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
OCMStub(mockNavigationAction.request).andReturn(request);
CRBannerView *bannerView = [self bannerViewWithWebView:nil];
bannerView.delegate = self.delegate;
[bannerView webView:nil
decidePolicyForNavigationAction:mockNavigationAction
decisionHandler:^(WKNavigationActionPolicy decisionHandler){
}];
[self cr_waitShortlyForExpectations:@[
self.delegate.wasClickedExpectation, self.delegate.willLeaveApplicationExpectation
]];
}
// test no delegate method called when webView navigation fails
- (void)testNoDelegateWhenWebViewFailsToNavigate {
[self.delegate invertAllExpectations];
CRBannerView *bannerView = [self bannerViewWithWebView:nil];
bannerView.delegate = self.delegate;
[bannerView webView:nil didFailNavigation:nil withError:nil];
[self cr_waitShortlyForExpectations:self.delegate.allExpectations];
}
// test no delegate method called when webView load fails
- (void)testNoDelegateWhenWebViewFailsToLoad {
[self.delegate invertAllExpectations];
CRBannerView *bannerView = [self bannerViewWithWebView:nil];
bannerView.delegate = self.delegate;
[bannerView webView:nil didFailProvisionalNavigation:nil withError:nil];
[self cr_waitShortlyForExpectations:self.delegate.allExpectations];
}
// test no delegate method called when HTTP error
- (void)testNoDelegateWhenHTTPError {
[self.delegate invertAllExpectations];
WKNavigationResponse *navigationResponse = OCMStrictClassMock([WKNavigationResponse class]);
NSHTTPURLResponse *response = OCMStrictClassMock([NSHTTPURLResponse class]);
OCMStub(response.statusCode).andReturn(404);
OCMStub(navigationResponse.response).andReturn(response);
CRBannerView *bannerView = [self bannerViewWithWebView:nil];
bannerView.delegate = self.delegate;
[bannerView webView:nil
decidePolicyForNavigationResponse:navigationResponse
decisionHandler:^(WKNavigationResponsePolicy decisionHandler){
}];
[self cr_waitShortlyForExpectations:self.delegate.allExpectations];
}
- (void)testNoDelegateWhenNoHttpResponse {
WKWebView *realWebView = [WKWebView new];
[self mockCriteoWithAdUnit:self.expectedCacheAdUnit respondBid:[self bidWithDisplayURL:@"-"]];
CRBannerView *bannerView = [self bannerViewWithWebView:realWebView];
bannerView.delegate = self.delegate;
[bannerView loadAdWithContext:self.contextData];
[self cr_waitShortlyForExpectations:@[ self.delegate.didReceiveAdExpectation ]];
}
- (void)testInterstitialFailWithMissingAdUnit {
CRBannerView *bannerView = [[CRBannerView alloc] initWithAdUnit:nil criteo:nil];
id<CRBannerViewDelegate> delegate = OCMStrictProtocolMock(@protocol(CRBannerViewDelegate));
bannerView.delegate = delegate;
OCMExpect([delegate banner:bannerView
didFailToReceiveAdWithError:[OCMArg checkWithBlock:^BOOL(NSError *error) {
return error.code == CRErrorCodeInvalidParameter;
}]]);
[bannerView loadAdWithContext:self.contextData];
OCMVerifyAllWithDelay(delegate, 1);
}
#pragma mark inhouseSpecificTests
- (void)testBannerLoadFailWhenBidIsNil {
WKWebView *mockWebView = [WKWebView new];
CRBannerView *bannerView = [self bannerViewWithWebView:mockWebView];
CRBid *bid = nil;
id<CRBannerViewDelegate> mockBannerViewDelegate =
OCMStrictProtocolMock(@protocol(CRBannerViewDelegate));
bannerView.delegate = mockBannerViewDelegate;
OCMStub([mockBannerViewDelegate banner:bannerView didFailToReceiveAdWithError:[OCMArg any]]);
[bannerView loadAdWithBid:bid];
self.delegate.expectedError = [NSError cr_errorWithCode:CRErrorCodeNoFill];
bannerView.delegate = self.delegate;
[bannerView loadAdWithBid:bid];
[self cr_waitForExpectations:@[ self.delegate.didFailToReceiveAdWithErrorExpectation ]];
}
- (void)testBannerDidLoadForValidBid {
WKWebView *mockWebView = [WKWebView new];
CR_CdbBid *cdbBid = [self bidWithDisplayURL:@"test"];
CRBid *bid = [[CRBid alloc] initWithCdbBid:cdbBid adUnit:self.adUnit];
CRBannerView *bannerView = [self bannerViewWithWebView:mockWebView];
bannerView.delegate = self.delegate;
[bannerView loadAdWithBid:bid];
[self cr_waitForExpectations:@[ self.delegate.didReceiveAdExpectation ]];
}
#pragma mark - Private
- (CRBannerView *)bannerViewWithWebView:(WKWebView *)webView {
return [[CRBannerView alloc] initWithFrame:CGRectMake(13.0f, 17.0f, 47.0f, 57.0f)
criteo:self.criteo
webView:webView
adUnit:self.adUnit
urlOpener:self.urlOpener];
}
- (void)mockCriteoWithAdUnit:(CR_CacheAdUnit *)adUnit respondBid:(CR_CdbBid *)bid {
OCMStub([self.criteo loadCdbBidForAdUnit:adUnit
withContext:self.contextData
responseHandler:[OCMArg any]])
.andDo(^(NSInvocation *invocation) {
CR_CdbBidResponseHandler handler;
[invocation getArgument:&handler atIndex:4];
handler(bid);
});
}
@end