Skip to content

Commit

Permalink
Fix failing Unit Tests
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit d06ca1af7826799877dbf6a6f3cd30815c574354
Author: Abhas Vohra <[email protected]>
Date:   Thu Feb 27 21:05:55 2020 +0530

    Interstitial/CSR Fix

commit 99da1944bda2bac7cf695dd2703e3941ce6b9b97
Author: Abhas Vohra <[email protected]>
Date:   Thu Feb 27 20:21:32 2020 +0530

    Fix

commit 2f66ddb96a99cadcd55c8143b8d5cf45fd49914e
Author: Abhas Vohra <[email protected]>
Date:   Thu Feb 27 19:26:14 2020 +0530

    Fix

commit c5d52eb3baf8a26187432cbcc5e3597b4ccac1d5
Author: Abhas Vohra <[email protected]>
Date:   Thu Feb 27 18:42:33 2020 +0530

    Fix Unit tests
  • Loading branch information
avohraa committed Feb 28, 2020
1 parent 1e51ae1 commit 7f7c349
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 18 deletions.
1 change: 1 addition & 0 deletions sdk/test/com/appnexus/opensdk/AdFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void testRequestExecutorForBackgroundTasks() {
SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
adFetcher.start();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void testRequestExecutorForBackgroundTasks() {
assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
adRequest = new UTAdRequest(this);
adRequest.execute();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
Expand Down
22 changes: 22 additions & 0 deletions sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;
import org.robolectric.shadows.ShadowLooper;

import static android.os.Looper.getMainLooper;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotSame;
import static junit.framework.Assert.assertTrue;
import static org.robolectric.Shadows.shadowOf;

/**
* This tests AdViewRequestManager
Expand Down Expand Up @@ -87,6 +91,18 @@ public void testNotifyURLForVideo() throws Exception {

request = server.takeRequest(); // Discard the first request since its a HTTP Post for /ut/v3 ad request call
request = server.takeRequest();

waitForTasks();
// execute main ad request
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();

ShadowLooper shadowLooper = shadowOf(getMainLooper());
if (!shadowLooper.isIdle()) {
shadowLooper.idle();
}
RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable();

assertNotifyURL(request);
}

Expand All @@ -102,6 +118,12 @@ private void executeUTRequest() {
// execute main ad request
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();

ShadowLooper shadowLooper = shadowOf(getMainLooper());
if (!shadowLooper.isIdle()) {
shadowLooper.idle();
}
RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable();
}


Expand Down
1 change: 1 addition & 0 deletions sdk/test/com/appnexus/opensdk/BannerAdViewLoadAdTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void testRequestExecutorForBackgroundTasks() {
SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
bannerAdView.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void testRequestExecutorForBackgroundTasks() {
SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
bannerAdView.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
Expand Down
7 changes: 7 additions & 0 deletions sdk/test/com/appnexus/opensdk/BaseNativeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 21)
Expand All @@ -35,6 +36,8 @@ public class BaseNativeTest extends BaseRoboTest implements NativeAdRequestListe
protected NativeAdResponse response;

protected boolean adLoaded, adFailed;
NativeAdResponse nativeAdResponse;
ResultCode failErrorCode;


@Override
Expand All @@ -43,6 +46,8 @@ public void setup() {

adLoaded = false;
adFailed = false;
nativeAdResponse = null;
failErrorCode = null;

adRequest = new NativeAdRequest(activity, "0");
adRequest.setListener(this);
Expand All @@ -64,13 +69,15 @@ public void assertCallbacks(boolean success) {
@Override
public void onAdLoaded(NativeAdResponse response) {
adLoaded = true;
nativeAdResponse = response;
this.response = response;
Clog.w(TestUtil.testLogTag, "BaseNativeTest onAdLoaded");
}

@Override
public void onAdFailed(ResultCode errorcode) {
adFailed = true;
failErrorCode = errorcode;
Clog.w(TestUtil.testLogTag, "BaseNativeTest onAdFailed");

}
Expand Down
13 changes: 13 additions & 0 deletions sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowLog;
import org.robolectric.shadows.ShadowLooper;

import static android.os.Looper.getMainLooper;
import static com.appnexus.opensdk.AdActivity.ACTIVITY_TYPE_INTERSTITIAL;
import static com.appnexus.opensdk.AdActivity.INTENT_KEY_ACTIVITY_TYPE;
import static com.appnexus.opensdk.InterstitialAdView.INTENT_KEY_AUTODISMISS_DELAY;
Expand Down Expand Up @@ -116,5 +119,15 @@ private void executeInterstitialRequest() {
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();

waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();

// ShadowLooper shadowLooper = shadowOf(getMainLooper());
// if (!shadowLooper.isIdle()) {
// shadowLooper.idle();
// }
// RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable();
}
}
18 changes: 16 additions & 2 deletions sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void assertResponseURL(int curRequestPositionInQueue, ResultCode errorCo
sanitizer.setAllowUnregisteredParamaters(true);
sanitizer.parseUrl(response_url);

int reasonVal = Integer.parseInt(sanitizer.getValue("reason"));
int reasonVal = Integer.parseInt(sanitizer.getValue("reason").replace("_HTTP/1.1", ""));

assertEquals(reasonVal, errorCode.ordinal());

Expand Down Expand Up @@ -125,18 +125,30 @@ private RecordedRequest takeNoAdURLRequestFromQueue(int position){
private void executeUTRequest() {
requestManager.execute();
// execute main ad request
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
}

private void executeAndAssertResponseURL(int positionInQueue, ResultCode errorCode,boolean checkLatency) {
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();

waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertResponseURL(positionInQueue, errorCode, checkLatency);
}

private void executeAndAssertNoAdURL(int positionInQueue) {
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();

waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
Expand Down Expand Up @@ -167,7 +179,9 @@ private void executeSSMRequest(){
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();


waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
}

// Verify that a successful mediation response,
Expand Down
54 changes: 38 additions & 16 deletions sdk/test/com/appnexus/opensdk/NativeRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import java.util.HashMap;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -108,8 +110,10 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
HttpUrl impbus = server.url("/");
UTConstants.REQUEST_BASE_URL_UT = impbus.toString();
NativeAdRequestListener listener = new NativeAdRequestListener() {

@Override
public void onAdLoaded(NativeAdResponse response) {
nativeAdResponse = response;
((MockFBNativeBannerAdResponse) response).logImpression();
((MockFBNativeBannerAdResponse) response).clickAd();
}
Expand All @@ -118,15 +122,16 @@ public void onAdLoaded(NativeAdResponse response) {
public void onAdFailed(ResultCode errorcode) {
}
};
NativeAdRequestListener listenerSpy = spy(listener);
adRequest.setListener(listenerSpy);
adRequest.setListener(listener);
adRequest.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(listenerSpy).onAdLoaded(any(MockFBNativeBannerAdResponse.class));
assertNotNull(nativeAdResponse);
assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse);
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Expand All @@ -140,57 +145,74 @@ public void onAdFailed(ResultCode errorcode) {
@Test
public void requestNativeGetsMediationNoFillThenCSR() {
server.enqueue(new MockResponse().setBody(TestResponsesUT.mediationNoFillThenCSRSuccessfull()).setResponseCode(200));
NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(adListener);
// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(this);
adRequest.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class));
assertNotNull(nativeAdResponse);
assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse);
// verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class));
}

@Test
public void requestNativeGetsCSRNofillThenMediation() {
server.enqueue(new MockResponse().setBody(TestResponsesUT.csrNoFillThenMediationSuccessfull()).setResponseCode(200));
NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(adListener);
// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(this);
assertNull(nativeAdResponse);
adRequest.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(adListener).onAdLoaded(any(BaseNativeAdResponse.class));
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertNotNull(nativeAdResponse);
assertTrue(nativeAdResponse instanceof BaseNativeAdResponse);
// verify(adListener).onAdLoaded(any(BaseNativeAdResponse.class));
}

@Test
public void requestNativeCSRNofill() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.csrNativeNofill()));
NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(adListener);
// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(this);
adRequest.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(adListener).onAdFailed(ResultCode.UNABLE_TO_FILL);
assertNull(nativeAdResponse);
assertAdFailed(true);
assertEquals(ResultCode.UNABLE_TO_FILL, failErrorCode);
// verify(adListener).onAdFailed(ResultCode.UNABLE_TO_FILL);
}

@Test
public void requestNativeCSRSuccessful() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.csrNativeSuccessful()));
NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(adListener);
// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class);
adRequest.setListener(this);
adRequest.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class));
assertNotNull(nativeAdResponse);
assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse);
// verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class));
}

@Test
Expand Down Expand Up @@ -247,4 +269,4 @@ public void tearDown() {
response.destroy();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public void testRequestExecutorForBackgroundTasks() {
SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
adRequest.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();

Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());
Expand Down

0 comments on commit 7f7c349

Please sign in to comment.