Skip to content

Commit

Permalink
feat: support ad position in original, rendering and mediation ad units
Browse files Browse the repository at this point in the history
  • Loading branch information
OlenaPostindustria committed Nov 27, 2024
1 parent 1759b68 commit e855f94
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 8 deletions.
6 changes: 6 additions & 0 deletions PrebidMobile/AdUnits/AdUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class AdUnit: NSObject, DispatcherDelegate {
set { adUnitConfig.setPbAdSlot(newValue) }
}

/// The position of the ad on the screen.
public var adPosition: AdPosition {
get { adUnitConfig.adPosition }
set { adUnitConfig.adPosition = newValue }
}

var adSizes: [CGSize] {
get { [adUnitConfig.adSize] + (adUnitConfig.additionalSizes ?? []) }
}
Expand Down
1 change: 1 addition & 0 deletions PrebidMobile/AdUnits/MultiformatAdUnit/PrebidAdUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public class PrebidAdUnit: NSObject {

adUnit.adUnitConfig.adConfiguration.isInterstitialAd = request.isInterstitial
adUnit.adUnitConfig.adConfiguration.isOptIn = request.isRewarded
adUnit.adUnitConfig.adPosition = request.adPosition

if request.isInterstitial || request.isRewarded {
adUnit.adUnitConfig.adPosition = .fullScreen
Expand Down
14 changes: 12 additions & 2 deletions PrebidMobile/AdUnits/MultiformatAdUnit/PrebidRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import Foundation
@objcMembers
public class PrebidRequest: NSObject {

// MARK: - Public properties

/// The position of the ad on the screen.
public var adPosition: AdPosition = .undefined

// MARK: - Internal properties

private(set) var bannerParameters: BannerParameters?
Expand All @@ -45,8 +50,13 @@ public class PrebidRequest: NSObject {
/// - nativeParameters: The native parameters for the ad request.
/// - isInterstitial: Indicates if the request is for an interstitial ad.
/// - isRewarded: Indicates if the request is for a rewarded ad.
public init(bannerParameters: BannerParameters? = nil, videoParameters: VideoParameters? = nil,
nativeParameters: NativeParameters? = nil, isInterstitial: Bool = false, isRewarded: Bool = false) {
public init(
bannerParameters: BannerParameters? = nil,
videoParameters: VideoParameters? = nil,
nativeParameters: NativeParameters? = nil,
isInterstitial: Bool = false,
isRewarded: Bool = false
) {
self.bannerParameters = bannerParameters
self.videoParameters = videoParameters
self.nativeParameters = nativeParameters
Expand Down
17 changes: 13 additions & 4 deletions PrebidMobile/PrebidMobileRendering/AdPosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,25 @@ import Foundation
public enum AdPosition: Int {

/// 0 Unknown
case undefined = 0
case undefined = 0

/// 1 Above the Fold
case aboveTheFold = 1

/// 2 Locked (i.e. fixed position)
case locked = 2

/// 3 Below the Fold
case belowTheFold = 3

/// 4 Header
case header = 4
case header = 4

/// 5 Footer
case footer = 5
case footer = 5

/// 6 Sidebar
case sidebar = 6
case sidebar = 6

/// 7 Full Screen
case fullScreen = 7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public class BaseInterstitialAdUnit :
set { adUnitConfig.adFormats = newValue }
}

/// The position of the ad on the screen.
public var adPosition: AdPosition {
get { adUnitConfig.adPosition }
set { adUnitConfig.adPosition = newValue }
}

/// The ORTB (OpenRTB) configuration string for the ad unit.
@objc public var ortbConfig: String? {
get { adUnitConfig.ortbConfig }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class MediationBaseInterstitialAdUnit : NSObject {
get { adUnitConfig.adConfiguration.videoParameters }
}

/// The position of the ad on the screen.
public var adPosition: AdPosition {
get { adUnitConfig.adPosition }
set { adUnitConfig.adPosition = newValue }
}

/// Indicates whether the video ad is muted.
public var isMuted: Bool {
get { adUnitConfig.adConfiguration.videoControlsConfig.isMuted }
Expand Down
7 changes: 5 additions & 2 deletions PrebidMobileTests/AdUnitTests/AdUnitSuccessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AdUnitSuccessorTests: XCTestCase {
Prebid.shared.useExternalClickthroughBrowser = false
}

//MARK: - BannerAdUnit
// MARK: - BannerAdUnit

func testBannerAdUnitCreation() {
//when
Expand All @@ -51,7 +51,8 @@ class AdUnitSuccessorTests: XCTestCase {
bannerBasedAdUnits.forEach { checkBannerParametersHelper($0) }
}

//MARK: - InterstitialAdUnit
// MARK: - InterstitialAdUnit

func testInterstitialAdUnitCreation() {
//when
let adUnit = InterstitialAdUnit(configId: Constants.configID1)
Expand Down Expand Up @@ -86,6 +87,7 @@ class AdUnitSuccessorTests: XCTestCase {
}

//MARK: - VideoAdUnit

func testVideoAdUnitCreation() {
//when
let adUnit = VideoAdUnit(configId: Constants.configID1, size: CGSize(width: Constants.width1, height: Constants.height1))
Expand All @@ -95,6 +97,7 @@ class AdUnitSuccessorTests: XCTestCase {
}

//MARK: - VideoInterstitialAdUnit

func testVideoInterstitialAdUnitCreation() {
//when
let adUnit = VideoInterstitialAdUnit(configId: Constants.configID1)
Expand Down
20 changes: 20 additions & 0 deletions PrebidMobileTests/AdUnitTests/AdUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,24 @@ class AdUnitTests: XCTestCase {
let objects2 = adUnit.getUserData()!
XCTAssertEqual(0, objects2.count)
}

func testAdUnitSetAdPosition() {
let adUnit = AdUnit(
configId: "test",
size: CGSize(width: 300, height: 250),
adFormats: [.banner, .video]
)

let adUnitConfig = adUnit.adUnitConfig

adUnit.adPosition = .header

XCTAssertEqual(adUnit.adPosition, adUnitConfig.adPosition)
XCTAssertEqual(adUnitConfig.adPosition, .header)

adUnit.adPosition = .footer

XCTAssertEqual(adUnit.adPosition, adUnitConfig.adPosition)
XCTAssertEqual(adUnitConfig.adPosition, .footer)
}
}
15 changes: 15 additions & 0 deletions PrebidMobileTests/AdUnitTests/PrebidAdUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,19 @@ class PrebidAdUnitTests: XCTestCase {

waitForExpectations(timeout: 5, handler: nil)
}

func testSetAdPosition() {
let request = PrebidRequest(bannerParameters: BannerParameters())
request.adPosition = .header

let adUnit = PrebidAdUnit(configId: "test-config-id")
adUnit.fetchDemand(request: request, completion: { _ in })

XCTAssertEqual(adUnit.getConfiguration().adPosition, .header)

request.adPosition = .footer
adUnit.fetchDemand(request: request, completion: { _ in })

XCTAssertEqual(adUnit.getConfiguration().adPosition, .footer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ class BaseInterstitialAdUnitTest: XCTestCase {
adUnit.closeButtonPosition = .topLeft
XCTAssertEqual(adUnit.adUnitConfig.adConfiguration.videoControlsConfig.closeButtonPosition, .topLeft)
}

func testSetAdPosition() {
let adUnit = BaseInterstitialAdUnit(configID: "test")
let adUnitConfig = adUnit.adUnitConfig

adUnit.adPosition = .header

XCTAssertEqual(adUnit.adPosition, adUnitConfig.adPosition)
XCTAssertEqual(adUnitConfig.adPosition, .header)

adUnit.adPosition = .footer

XCTAssertEqual(adUnit.adPosition, adUnitConfig.adPosition)
XCTAssertEqual(adUnitConfig.adPosition, .footer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,22 @@ class MediationInterstitialAdUnitTest: XCTestCase {
waitForExpectations(timeout: 0.1)
}

func testSetAdPosition() {
let adUnit = MediationBaseInterstitialAdUnit(
configId: "test",
mediationDelegate: MockEmptyPrebidMediationDelegate()
)

let adUnitConfig = adUnit.adUnitConfig

adUnit.adPosition = .header

XCTAssertEqual(adUnit.adPosition, adUnitConfig.adPosition)
XCTAssertEqual(adUnitConfig.adPosition, .header)

adUnit.adPosition = .footer

XCTAssertEqual(adUnit.adPosition, adUnitConfig.adPosition)
XCTAssertEqual(adUnitConfig.adPosition, .footer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ fileprivate let HBKeywordPrefix = "hb_"
@objc var localExtras: [AnyHashable : Any]?
}

class MockEmptyPrebidMediationDelegate: PrebidMediationDelegate {
func setUpAdObject(with values: [String : Any]) -> Bool { true }
func cleanUpAdObject() {}
func getAdView() -> UIView? { UIView(frame: .zero)}
}

class MockMediationUtils: PrebidMediationDelegate {

let adObject: MockAdObject
Expand Down

0 comments on commit e855f94

Please sign in to comment.