Skip to content

Commit

Permalink
feat(multi-format): fix tests #393
Browse files Browse the repository at this point in the history
Remove m prefix for Interstitial classes.
  • Loading branch information
ValentinPostindustria committed Mar 28, 2022
1 parent 1cc7d24 commit c724ef8
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ public abstract class BaseInterstitialAdUnit {

protected AdUnitConfiguration configuration;

private BidLoader mBidLoader;
private BidResponse mBidResponse;
private InterstitialController mInterstitialController;
private InterstitialAdUnitState mInterstitialAdUnitState = READY_FOR_LOAD;
private BidLoader bidLoader;
private BidResponse bidResponse;
private InterstitialController interstitialController;
private InterstitialAdUnitState interstitialAdUnitState = READY_FOR_LOAD;

private final WeakReference<Context> mWeakContext;
private final BidRequesterListener mBidRequesterListener = createBidRequesterListener();
private final InterstitialControllerListener mControllerListener = createInterstitialControllerListener();
private final WeakReference<Context> weakContext;
private final BidRequesterListener bidRequesterListener = createBidRequesterListener();
private final InterstitialControllerListener controllerListener = createInterstitialControllerListener();

protected BaseInterstitialAdUnit(Context context) {
mWeakContext = new WeakReference<>(context);
weakContext = new WeakReference<>(context);
}


Expand All @@ -71,17 +71,17 @@ protected BaseInterstitialAdUnit(Context context) {
* Executes ad loading if no request is running.
*/
public void loadAd() {
if (mBidLoader == null) {
if (bidLoader == null) {
LogUtil.error(TAG, "loadAd: Failed. BidLoader is not initialized.");
return;
}

if (!isAdLoadAllowed()) {
LogUtil.debug(TAG, "loadAd: Skipped. InterstitialAdUnitState is: " + mInterstitialAdUnitState);
LogUtil.debug(TAG, "loadAd: Skipped. InterstitialAdUnitState is: " + interstitialAdUnitState);
return;
}

mBidLoader.load();
bidLoader.load();
}

/**
Expand All @@ -100,16 +100,16 @@ public void show() {
return;
}

switch (mInterstitialAdUnitState) {
switch (interstitialAdUnitState) {
case READY_TO_DISPLAY_GAM:
showGamAd();
break;
case READY_TO_DISPLAY_PREBID:
mInterstitialController.show();
interstitialController.show();
break;
default:
notifyErrorListener(new AdException(AdException.INTERNAL_ERROR,
"show(): Encountered an invalid mInterstitialAdUnitState - " + mInterstitialAdUnitState
"show(): Encountered an invalid mInterstitialAdUnitState - " + interstitialAdUnitState
));
}
}
Expand Down Expand Up @@ -174,11 +174,11 @@ public void setPbAdSlot(String adSlot) {
* Cleans up resources when destroyed.
*/
public void destroy() {
if (mBidLoader != null) {
mBidLoader.destroy();
if (bidLoader != null) {
bidLoader.destroy();
}
if (mInterstitialController != null) {
mInterstitialController.destroy();
if (interstitialController != null) {
interstitialController.destroy();
}
}

Expand All @@ -192,64 +192,64 @@ protected void init(AdUnitConfiguration adUnitConfiguration) {
}

protected void loadPrebidAd() {
if (mInterstitialController == null) {
if (interstitialController == null) {
notifyErrorListener(new AdException(AdException.INTERNAL_ERROR,
"InterstitialController is not defined. Unable to process bid."
));
return;
}

mInterstitialController.loadAd(configuration, mBidResponse);
interstitialController.loadAd(configuration, bidResponse);
}

@Nullable
protected Context getContext() {
return mWeakContext.get();
return weakContext.get();
}

protected boolean isBidInvalid() {
return mBidResponse == null || mBidResponse.getWinningBid() == null;
return bidResponse == null || bidResponse.getWinningBid() == null;
}

protected void changeInterstitialAdUnitState(InterstitialAdUnitState state) {
mInterstitialAdUnitState = state;
interstitialAdUnitState = state;
}

private void initPrebidRenderingSdk() {
PrebidMobile.setApplicationContext(getContext(), () -> {});
}

private void initBidLoader() {
mBidLoader = new BidLoader(getContext(), configuration, mBidRequesterListener);
bidLoader = new BidLoader(getContext(), configuration, bidRequesterListener);
}

private void initInterstitialController() {
try {
mInterstitialController = new InterstitialController(getContext(), mControllerListener);
interstitialController = new InterstitialController(getContext(), controllerListener);
} catch (AdException e) {
notifyErrorListener(e);
}
}

private Bid getWinnerBid() {
return mBidResponse != null ? mBidResponse.getWinningBid() : null;
return bidResponse != null ? bidResponse.getWinningBid() : null;
}

public BidResponse getBidResponse() {
return mBidResponse;
return bidResponse;
}

private boolean isAuctionWinnerReadyToDisplay() {
return mInterstitialAdUnitState == READY_TO_DISPLAY_PREBID || mInterstitialAdUnitState == READY_TO_DISPLAY_GAM;
return interstitialAdUnitState == READY_TO_DISPLAY_PREBID || interstitialAdUnitState == READY_TO_DISPLAY_GAM;
}

private boolean isAdLoadAllowed() {
return mInterstitialAdUnitState == READY_FOR_LOAD;
return interstitialAdUnitState == READY_FOR_LOAD;
}

@VisibleForTesting
final InterstitialAdUnitState getAdUnitState() {
return mInterstitialAdUnitState;
return interstitialAdUnitState;
}

public void addContent(ContentObject content) {
Expand All @@ -261,15 +261,15 @@ private BidRequesterListener createBidRequesterListener() {
return new BidRequesterListener() {
@Override
public void onFetchCompleted(BidResponse response) {
mBidResponse = response;
bidResponse = response;

changeInterstitialAdUnitState(LOADING);
requestAdWithBid(getWinnerBid());
}

@Override
public void onError(AdException exception) {
mBidResponse = null;
bidResponse = null;
requestAdWithBid(null);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@
public class RewardedAdUnit extends BaseInterstitialAdUnit {
private static final String TAG = RewardedAdUnit.class.getSimpleName();

private final RewardedEventHandler mEventHandler;
private final RewardedEventHandler eventHandler;

@Nullable
private RewardedAdUnitListener mRewardedAdUnitListener;
@Nullable private RewardedAdUnitListener rewardedAdUnitListener;

@Nullable
private Object mUserReward;
@Nullable private Object userReward;

//region ==================== Listener implementation

private final RewardedVideoEventListener mEventListener = new RewardedVideoEventListener() {
private final RewardedVideoEventListener eventListener = new RewardedVideoEventListener() {
@Override
public void onPrebidSdkWin() {
if (isBidInvalid()) {
changeInterstitialAdUnitState(READY_FOR_LOAD);
notifyErrorListener(new AdException(AdException.INTERNAL_ERROR, "WinnerBid is null when executing onPrebidSdkWin."));
notifyErrorListener(new AdException(
AdException.INTERNAL_ERROR,
"WinnerBid is null when executing onPrebidSdkWin."
));
return;
}

Expand All @@ -58,7 +59,7 @@ public void onPrebidSdkWin() {

@Override
public void onAdServerWin(Object userReward) {
mUserReward = userReward;
RewardedAdUnit.this.userReward = userReward;
changeInterstitialAdUnitState(READY_TO_DISPLAY_GAM);
notifyAdEventListener(AdListenerEvent.AD_LOADED);
}
Expand Down Expand Up @@ -92,17 +93,17 @@ public void onAdDisplayed() {

@Override
public void onUserEarnedReward() {
if (mRewardedAdUnitListener != null) {
mRewardedAdUnitListener.onUserEarnedReward(RewardedAdUnit.this);
if (rewardedAdUnitListener != null) {
rewardedAdUnitListener.onUserEarnedReward(RewardedAdUnit.this);
}
}
};
//endregion ==================== Listener implementation

public RewardedAdUnit(Context context, String configId, RewardedEventHandler eventHandler) {
super(context);
mEventHandler = eventHandler;
mEventHandler.setRewardedEventListener(mEventListener);
this.eventHandler = eventHandler;
this.eventHandler.setRewardedEventListener(eventListener);

AdUnitConfiguration adUnitConfiguration = new AdUnitConfiguration();
adUnitConfiguration.setConfigId(configId);
Expand All @@ -119,72 +120,75 @@ public RewardedAdUnit(Context context, String configId) {
@Override
public void loadAd() {
super.loadAd();
mUserReward = null;
userReward = null;
}

@Override
public void destroy() {
super.destroy();
if (mEventHandler != null) {
mEventHandler.destroy();
if (eventHandler != null) {
eventHandler.destroy();
}
}

//region ==================== getters and setters
public void setRewardedAdUnitListener(
@Nullable
RewardedAdUnitListener rewardedAdUnitListener) {
mRewardedAdUnitListener = rewardedAdUnitListener;
this.rewardedAdUnitListener = rewardedAdUnitListener;
}

@Nullable
public Object getUserReward() {
return mUserReward;
return userReward;
}
//endregion ==================== getters and setters

@Override
void requestAdWithBid(
@Nullable
Bid bid) {
mEventHandler.requestAdWithBid(bid);
eventHandler.requestAdWithBid(bid);
}

@Override
void showGamAd() {
mEventHandler.show();
eventHandler.show();
}

@Override
void notifyAdEventListener(AdListenerEvent adListenerEvent) {
if (mRewardedAdUnitListener == null) {
LogUtil.debug(TAG, "notifyAdEventListener: Failed. AdUnitListener is null. Passed listener event: " + adListenerEvent);
if (rewardedAdUnitListener == null) {
LogUtil.debug(
TAG,
"notifyAdEventListener: Failed. AdUnitListener is null. Passed listener event: " + adListenerEvent
);
return;
}

switch (adListenerEvent) {
case AD_CLOSE:
mRewardedAdUnitListener.onAdClosed(RewardedAdUnit.this);
rewardedAdUnitListener.onAdClosed(RewardedAdUnit.this);
break;
case AD_LOADED:
mRewardedAdUnitListener.onAdLoaded(RewardedAdUnit.this);
rewardedAdUnitListener.onAdLoaded(RewardedAdUnit.this);
break;
case AD_DISPLAYED:
mRewardedAdUnitListener.onAdDisplayed(RewardedAdUnit.this);
rewardedAdUnitListener.onAdDisplayed(RewardedAdUnit.this);
break;
case AD_CLICKED:
mRewardedAdUnitListener.onAdClicked(RewardedAdUnit.this);
rewardedAdUnitListener.onAdClicked(RewardedAdUnit.this);
break;
case USER_RECEIVED_PREBID_REWARD:
mRewardedAdUnitListener.onUserEarnedReward(RewardedAdUnit.this);
rewardedAdUnitListener.onUserEarnedReward(RewardedAdUnit.this);
break;
}
}

@Override
void notifyErrorListener(AdException exception) {
if (mRewardedAdUnitListener != null) {
mRewardedAdUnitListener.onAdFailed(RewardedAdUnit.this, exception);
if (rewardedAdUnitListener != null) {
rewardedAdUnitListener.onAdFailed(RewardedAdUnit.this, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.robolectric.annotation.Config;

import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;

import static junit.framework.Assert.assertEquals;
Expand Down Expand Up @@ -73,7 +73,7 @@ public void testBannerAdUnitCreation() {

assertEquals(1, configuration.getSizes().size());
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(Collections.singletonList(AdFormat.BANNER), configuration.getAdFormats());
assertEquals(EnumSet.of(AdFormat.BANNER), configuration.getAdFormats());

assertEquals(0, adUnit.configuration.getAutoRefreshDelay());
adUnit.setAutoRefreshInterval(30);
Expand Down Expand Up @@ -113,7 +113,7 @@ public void testInterstitialAdUnitCreation() throws Exception {
InterstitialAdUnit adUnit = new InterstitialAdUnit(testConfigId);
AdUnitConfiguration configuration = adUnit.getConfiguration();
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(Collections.singletonList(AdFormat.INTERSTITIAL), configuration.getAdFormats());
assertEquals(EnumSet.of(AdFormat.INTERSTITIAL), configuration.getAdFormats());
}


Expand All @@ -122,7 +122,7 @@ public void testAdvancedInterstitialAdUnitCreation() {
InterstitialAdUnit adUnit = new InterstitialAdUnit(testConfigId, height, 70);
AdUnitConfiguration configuration = (AdUnitConfiguration) adUnit.getConfiguration();

assertEquals(Collections.singletonList(AdFormat.INTERSTITIAL), configuration.getAdFormats());
assertEquals(EnumSet.of(AdFormat.INTERSTITIAL), configuration.getAdFormats());
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(height, configuration.getMinSizePercentage().getWidth());
assertEquals(70, configuration.getMinSizePercentage().getHeight());
Expand All @@ -137,7 +137,7 @@ public void testVideoAdUnitCreation() {
assertEquals(width, size.getWidth());
assertEquals(height, size.getHeight());
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(Collections.singletonList(AdFormat.VAST), configuration.getAdFormats());
assertEquals(EnumSet.of(AdFormat.VAST), configuration.getAdFormats());
}

@Test
Expand All @@ -146,15 +146,15 @@ public void testVideoInterstitialAdUnitCreation() {
AdUnitConfiguration configuration = (AdUnitConfiguration) adUnit.getConfiguration();

assertEquals(testConfigId, configuration.getConfigId());
assertEquals(Collections.singletonList(AdFormat.VAST), configuration.getAdFormats());
assertEquals(EnumSet.of(AdFormat.VAST), configuration.getAdFormats());
}

@Test
public void testRewardedVideoAdUnitCreation() {
RewardedVideoAdUnit adUnit = new RewardedVideoAdUnit(testConfigId);
AdUnitConfiguration configuration = adUnit.getConfiguration();
assertEquals(testConfigId, configuration.getConfigId());
assertEquals(Collections.singletonList(AdFormat.VAST), configuration.getAdFormats());
assertEquals(EnumSet.of(AdFormat.VAST), configuration.getAdFormats());
}

@Test
Expand Down
Loading

0 comments on commit c724ef8

Please sign in to comment.