Skip to content

Commit

Permalink
Merge pull request #29 from prebid/small-changes
Browse files Browse the repository at this point in the history
Clean up changes for 0.1.0 release
  • Loading branch information
nhedley authored Sep 27, 2017
2 parents 50bd22c + e156ea4 commit baac3ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
15 changes: 11 additions & 4 deletions sdk/PrebidMobile/PBBidManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,18 @@ - (void)registerAdUnit:(PBAdUnit *)adUnit {
if (adUnit.adSizes == nil && adUnit.adType == PBAdUnitTypeBanner) {
@throw [PBException exceptionWithName:PBAdUnitNoSizeException];
}
// Check if adunit already exists, if so remove it
if ([_adUnits containsObject:adUnit]) {
// Check if ad unit already exists, if so remove it
NSMutableArray *adUnitsToRemove = [[NSMutableArray alloc] init];
for (PBAdUnit *existingAdUnit in _adUnits) {
if ([existingAdUnit.identifier isEqualToString:adUnit.identifier]) {
[adUnitsToRemove addObject:existingAdUnit];
}
}
for (PBAdUnit *adUnit in adUnitsToRemove) {
[_adUnits removeObject:adUnit];
}

// Finish registration of ad unit by adding it to adUnits
[_adUnits addObject:adUnit];
PBLogDebug(@"AdUnit %@ is registered with Prebid Mobile", adUnit.identifier);
}
Expand Down Expand Up @@ -249,8 +257,7 @@ - (void)checkForBidsExpired {
}

- (BOOL)isBidReady:(NSString *)identifier {
if ([[_bidsMap allKeys] containsObject:identifier] &&
[_bidsMap objectForKey:identifier] != nil &&
if ([_bidsMap objectForKey:identifier] != nil &&
[[_bidsMap objectForKey:identifier] count] > 0) {
PBLogDebug(@"Bid is ready for ad unit with identifier %@", identifier);
return YES;
Expand Down
25 changes: 24 additions & 1 deletion sdk/PrebidMobileTests/PrebidMobileCoreTests/PBBidManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ @interface PBBidManager (Testing)
- (void)startNewAuction:(PBAdUnit *)adUnit;
- (void)saveBidResponses:(nonnull NSArray<PBBidResponse *> *)bidResponse;
- (void)checkForBidsExpired;
- (void)registerAdUnit:(PBAdUnit *)adUnit;
- (NSArray<PBBidResponse *> *)getBids:(PBAdUnit *)adUnit;

@end
Expand Down Expand Up @@ -110,6 +111,21 @@ - (void)testRegisterBannerAdUnitNoSizeException {
XCTAssertNil(returnedAdUnit);
}

- (void)testRegisterAdUnitWithSameIdentifier {
PBAdUnit *returnedUnit = nil;
PBAdUnit *bannerAdUnit1 = [[PBBannerAdUnit alloc] initWithAdUnitIdentifier:@"sameid" andConfigId:@"0b33e7ae-cf61-4003-8404-0711eea6e673"];
[bannerAdUnit1 addSize:CGSizeMake(320, 50)];
PBAdUnit *bannerAdUnit2 = [[PBBannerAdUnit alloc] initWithAdUnitIdentifier:@"sameid" andConfigId:@"0b33e7ae-cf61-4003-8404-0711eea6e673"];
[bannerAdUnit2 addSize:CGSizeMake(320, 50)];

[[PBBidManager sharedInstance] registerAdUnits:@[bannerAdUnit1, bannerAdUnit2] withAccountId:self.accountId];
returnedUnit = [[PBBidManager sharedInstance] adUnitByIdentifier:[bannerAdUnit1 identifier]];

XCTAssertNotNil(returnedUnit);
// Test that the second banner ad unit is the one that is registered, not the first banner ad unit
XCTAssertEqualObjects(returnedUnit, bannerAdUnit2);
}

#pragma mark - Test winning bid for ad unit

- (void)testWinningBidForAdUnit {
Expand Down Expand Up @@ -250,9 +266,16 @@ - (void)testStartNewAuction {

- (void)testCheckForBidsExpired {
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
// This test is a bit confusing. The first ad unit is registered to initialize Prebid and the ad units set using the public API.
PBAdUnit *adUnitTest = [[PBBannerAdUnit alloc] initWithAdUnitIdentifier:@"bmt21" andConfigId:@"0b33e7ae-cf61-4003-8404-0711eea6e673"];
[adUnitTest addSize:CGSizeMake(320, 50)];
[[PBBidManager sharedInstance] registerAdUnits:@[adUnitTest] withAccountId:self.accountId];

// We initialize this ad unit using the private function to ensure a request doesn't go out and the timeToExpireAfter doesn't get reset
// to 4 min and 30 seconds instead of 1 second for testing.
PBAdUnit *adUnit = [[PBBannerAdUnit alloc] initWithAdUnitIdentifier:@"bmt12" andConfigId:@"0b33e7ae-cf61-4003-8404-0711eea6e673"];
[adUnit addSize:CGSizeMake(320, 50)];
[[PBBidManager sharedInstance] registerAdUnits:@[adUnit] withAccountId:self.accountId];
[[PBBidManager sharedInstance] registerAdUnit:adUnit];

NSDictionary *testAdServerTargeting = @{@"hb_pb":@"4.14", @"hb_cache_id":@"0000-0000-000-0000", @"hb_bidder": @"appnexus"};
PBBidResponse *bidResponse = [PBBidResponse bidResponseWithAdUnitId:adUnit.identifier adServerTargeting:testAdServerTargeting];
Expand Down

0 comments on commit baac3ca

Please sign in to comment.