Skip to content

Commit

Permalink
update tests, remove deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
gdeluna-branch committed Sep 27, 2023
1 parent 87119f6 commit 10aa606
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ class BillingGooglePlayTests : BranchTest() {
Defines.RequestPath.TrackStandardEvent.path,
eventRequest.requestPath
)
Assert.assertTrue(eventRequest.isWaitingOnProcessToFinish)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.branch.referral;

import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;

import io.branch.referral.util.AdType;
import io.branch.referral.util.BRANCH_STANDARD_EVENT;
import io.branch.referral.util.BranchEvent;
Expand Down Expand Up @@ -82,14 +86,6 @@ public void testLogEvent() {

ServerRequest eventRequest = queue.peekAt(0);
Assert.assertEquals(Defines.RequestPath.TrackStandardEvent.getPath(), eventRequest.getRequestPath());
Assert.assertTrue(eventRequest.isWaitingOnProcessToFinish());

initSessionResumeActivity(new Runnable() {
@Override
public void run() {
Assert.assertEquals(2, queue.getSize());
}
}, null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,25 @@ public class BranchPreinstallFileTest extends BranchTest {
public void testResultSuccess() {
initBranchInstance();

final ServerRequestQueue queue = ServerRequestQueue.getInstance(getTestContext());
Assert.assertEquals(0, queue.getSize());
initSessionResumeActivity(new Runnable() {
@Override
public void run() {
Assert.assertEquals(1, queue.getSize());

String branchFileData = AssetUtils.readJsonFile(getTestContext(), "pre_install_apps.branch");
Assert.assertTrue(branchFileData.length() > 0);

JSONObject branchFileJson = null;
try {
branchFileJson = new JSONObject(branchFileData);
BranchPreinstall.getBranchFileContent(branchFileJson, branch, getTestContext());

ServerRequest initRequest = queue.peekAt(0);
doFinalUpdate(initRequest);
doFinalUpdateOnMainThread(initRequest);

Assert.assertTrue(hasV1InstallPreinstallCampaign(initRequest));
Assert.assertTrue(hasV1InstallPreinstallPartner(initRequest));
Assert.assertTrue(hasV1InstallPreinstallCustomData(initRequest));
} catch (Exception e) {
Assert.fail("parsing of test resources failed");
}
}
}, null);
String branchFileData = AssetUtils.readJsonFile(getTestContext(), "pre_install_apps.branch");
Assert.assertTrue(branchFileData.length() > 0);

JSONObject branchFileJson = null;
try {
branchFileJson = new JSONObject(branchFileData);
BranchPreinstall.getBranchFileContent(branchFileJson, branch, getTestContext());

ServerRequest initRequest = new ServerRequestRegisterInstall(getTestContext(), null, false);
doFinalUpdate(initRequest);
doFinalUpdateOnMainThread(initRequest);

Assert.assertTrue(hasV1InstallPreinstallCampaign(initRequest));
Assert.assertTrue(hasV1InstallPreinstallPartner(initRequest));
Assert.assertTrue(hasV1InstallPreinstallCustomData(initRequest));
}
catch (Exception e) {
Assert.fail("parsing of test resources failed");
}
}

@Test
Expand All @@ -57,33 +49,25 @@ public void testResultNullFile() {
public void testResultPackageNameNotPresent() {
initBranchInstance();

final ServerRequestQueue queue = ServerRequestQueue.getInstance(getTestContext());
Assert.assertEquals(0, queue.getSize());
initSessionResumeActivity(new Runnable() {
@Override
public void run() {
Assert.assertEquals(1, queue.getSize());

String branchFileData = AssetUtils
.readJsonFile(getTestContext(), "pre_install_apps_no_package.branch");
Assert.assertTrue(branchFileData.length() > 0);
try {
JSONObject branchFileJson = new JSONObject(branchFileData);
BranchPreinstall.getBranchFileContent(branchFileJson, branch,
getTestContext());

ServerRequest initRequest = queue.peekAt(0);
doFinalUpdate(initRequest);
doFinalUpdateOnMainThread(initRequest);

Assert.assertFalse(hasV1InstallPreinstallCampaign(initRequest));
Assert.assertFalse(hasV1InstallPreinstallPartner(initRequest));
Assert.assertFalse(hasV1InstallPreinstallCustomData(initRequest));
} catch (JSONException e) {
Assert.fail(e.getMessage());
}
}
}, null);
String branchFileData = AssetUtils
.readJsonFile(getTestContext(), "pre_install_apps_no_package.branch");
Assert.assertTrue(branchFileData.length() > 0);
try {
JSONObject branchFileJson = new JSONObject(branchFileData);
BranchPreinstall.getBranchFileContent(branchFileJson, branch,
getTestContext());

ServerRequest initRequest = new ServerRequestRegisterInstall(getTestContext(), null, false);
doFinalUpdate(initRequest);
doFinalUpdateOnMainThread(initRequest);

Assert.assertFalse(hasV1InstallPreinstallCampaign(initRequest));
Assert.assertFalse(hasV1InstallPreinstallPartner(initRequest));
Assert.assertFalse(hasV1InstallPreinstallCustomData(initRequest));
}
catch (JSONException e) {
Assert.fail(e.getMessage());
}
}

@Test
Expand All @@ -98,32 +82,24 @@ public void testAppLevelDataOverride() {
branch.setPreinstallPartner("partner1");
branch.setPreinstallCampaign("campaign1");

final ServerRequestQueue queue = ServerRequestQueue.getInstance(getTestContext());
Assert.assertEquals(0, queue.getSize());
initSessionResumeActivity(new Runnable() {
@Override
public void run() {
Assert.assertEquals(1, queue.getSize());

String branchFileData = AssetUtils.readJsonFile(getTestContext(), "pre_install_apps.branch");
Assert.assertTrue(branchFileData.length() > 0);
try {
JSONObject branchFileJson = new JSONObject(branchFileData);
BranchPreinstall.getBranchFileContent(branchFileJson, branch, getTestContext());

ServerRequest initRequest = queue.peekAt(0);
doFinalUpdate(initRequest);
doFinalUpdateOnMainThread(initRequest);

Assert.assertTrue(hasV1InstallPreinstallCampaign(initRequest));
Assert.assertTrue(hasV1InstallPreinstallPartner(initRequest));
Assert.assertTrue(hasV1InstallPreinstallCustomData(initRequest));
Assert.assertEquals(getPreinstallData(branch, PreinstallKey.partner.getKey()), "partner1");
} catch (JSONException e) {
Assert.fail(e.getMessage());
}
}
}, null);
String branchFileData = AssetUtils.readJsonFile(getTestContext(), "pre_install_apps.branch");
Assert.assertTrue(branchFileData.length() > 0);
try {
JSONObject branchFileJson = new JSONObject(branchFileData);
BranchPreinstall.getBranchFileContent(branchFileJson, branch, getTestContext());

ServerRequest initRequest = new ServerRequestRegisterInstall(getTestContext(), null, false);
doFinalUpdate(initRequest);
doFinalUpdateOnMainThread(initRequest);

Assert.assertTrue(hasV1InstallPreinstallCampaign(initRequest));
Assert.assertTrue(hasV1InstallPreinstallPartner(initRequest));
Assert.assertTrue(hasV1InstallPreinstallCustomData(initRequest));
Assert.assertEquals(getPreinstallData(branch, PreinstallKey.partner.getKey()), "partner1");
}
catch (JSONException e) {
Assert.fail(e.getMessage());
}
}

// Check to see if the preinstall campaign is available (V1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
Expand Down Expand Up @@ -99,15 +100,15 @@ protected void initBranchInstance(String branchKey) {

protected synchronized void initSessionResumeActivity(final Runnable pretest, final Runnable posttest) {
final CountDownLatch sessionLatch = new CountDownLatch(1);

activityScenario.onActivity(new ActivityScenario.ActivityAction<MockActivity>() {
@Override
public void perform(final MockActivity activity) {
BranchLogger.v("perform on thread " + Thread.currentThread().getName());
Branch.sessionBuilder(activity).withCallback(new Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(@Nullable JSONObject referringParams, @Nullable BranchError error) {
// this isn't really a test, just makes sure that we are indeed using `MockRemoteInterface` and getting success responses
BranchLogger.v(TAG + " onInitFinished, referringParams: " + referringParams + ", error: " + error);
BranchLogger.v(TAG + " onInitFinished, referringParams: " + referringParams + ", error: " + error + " on thread " + Thread.currentThread().getName());
Assert.assertNotNull(referringParams);
if (error != null) {
if (error.getErrorCode() != BranchError.ERR_BRANCH_REQ_TIMED_OUT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class BranchTestRequestUtil {
private static final String TAG = "BranchEventTestUtil";

// can be pretty short because we mock remote interface and don't actually make async calls from the SDK
public static final int TEST_REQUEST_TIMEOUT = 1000;
public static final int TEST_REQUEST_TIMEOUT = 2000;
public static final int TEST_INIT_SESSION_TIMEOUT = 15000;

// Dig out the variable for isStandardEvent from the BranchEvent object.
Expand Down Expand Up @@ -67,17 +67,6 @@ protected ServerRequest findRequestOnQueue(Context context, String key, String e
return null;
}

protected ServerRequest getLastRequestOnQueue(Context context, int minimumQueueSize) {
ServerRequestQueue queue = ServerRequestQueue.getInstance(context);

int size = queue.getSize();
if (size >= minimumQueueSize) {
return queue.peekAt(size - 1);
}

return null;
}

protected ServerRequest doFinalUpdate(ServerRequest request) {
Log.i("BranchSDK", "doFinalUpdate" + Thread.currentThread().getName());
request.updatePostData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.annotation.Nullable;
import androidx.test.annotation.UiThreadTest;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -61,26 +63,6 @@ public void testHardwareIdSimulatedInstall() {
Assert.assertEquals(uniqueId1, uniqueId2);
}

// TODO: Need to mock advertising ID providers
// Tests are written to assume a valid AID is obtainable from the system
@Test
public void testGAIDFetch() throws InterruptedException {
initBranchInstance();
Assert.assertNotNull(DeviceInfo.getInstance());

final CountDownLatch latch = new CountDownLatch(1);
//TODO DeviceInfo.getInstance().getSystemObserver().fetchAdId(getTestContext());

Assert.assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));

if(DeviceInfo.getInstance().getSystemObserver().getLATVal() == 0) {
Assert.assertFalse(DeviceInfo.isNullOrEmptyOrBlank(DeviceInfo.getInstance().getSystemObserver().getAID()));
}
else{
Assert.assertTrue(DeviceInfo.isNullOrEmptyOrBlank(DeviceInfo.getInstance().getSystemObserver().getAID()));
}
}

@Test
public void windowManagerAndDisplayManagerSameMetrics(){
DisplayManager displayManager = (DisplayManager) getTestContext().getSystemService(Context.DISPLAY_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -34,7 +33,7 @@ public void tearDown() throws InterruptedException {

@Test
public void testTimedOutInitSessionCallbackInvoked() throws InterruptedException {
setTimeouts(10,10000);
setTimeouts(1,1);
initSessionResumeActivity(null, new Runnable() {
@Override
public void run() {
Expand All @@ -49,12 +48,13 @@ public void testTimedOutLastAttributedTouchDataCallbackInvoked() throws Interrup
initSessionResumeActivity(null, new Runnable() {
@Override
public void run() {
setTimeouts(10,10);
setTimeouts(1,1);

final CountDownLatch lock1 = new CountDownLatch(1);
Branch.getInstance().getLastAttributedTouchData(new ServerRequestGetLATD.BranchLastAttributedTouchDataListener() {
@Override
public void onDataFetched(JSONObject jsonObject, BranchError error) {
BranchLogger.v("error is " + error);
Assert.assertEquals(BranchError.ERR_BRANCH_TASK_TIMEOUT, error.getErrorCode());
lock1.countDown();
}
Expand All @@ -74,7 +74,7 @@ public void testTimedOutGenerateShortUrlCallbackInvoked() throws InterruptedExce
initSessionResumeActivity(null, new Runnable() {
@Override
public void run() {
setTimeouts(10,10);
setTimeouts(1,1);

final CountDownLatch lock3 = new CountDownLatch(1);
BranchUniversalObject buo = new BranchUniversalObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public BranchResponse doRestfulGet(String url) throws BranchRemoteException {
@Override
public BranchResponse doRestfulPost(String url, JSONObject payload) throws BranchRemoteException {
try {
BranchLogger.v("sleeping for " + networkRequestDuration + " milliseconds");
Thread.sleep(networkRequestDuration);
} catch (InterruptedException e) {
BranchLogger.v("slept for " + networkRequestDuration + " milliseconds");
}
catch (InterruptedException e) {
e.printStackTrace();
}
BranchLogger.v(TAG + ", doRestfulPost, url: " + url + ", payload: " + payload);
Expand Down
21 changes: 2 additions & 19 deletions Branch-SDK/src/main/java/io/branch/referral/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -993,11 +993,6 @@ public JSONObject getFirstReferringParams() {
firstReferringParams = appendDebugParams(firstReferringParams);
return firstReferringParams;
}

@SuppressWarnings("WeakerAccess")
public void removeSessionInitializationDelay() {
requestQueue_.unlockProcessWait(ServerRequest.PROCESS_WAIT_LOCK.USER_SET_WAIT_LOCK);
}

/**
* <p>This function must be called from a non-UI thread! If Branch has no install link data,
Expand Down Expand Up @@ -1290,10 +1285,10 @@ private void initializeSession(ServerRequestInitSession initRequest, int delay)
}

if (delay > 0) {
initRequest.addProcessWaitLock(ServerRequest.PROCESS_WAIT_LOCK.USER_SET_WAIT_LOCK);

new Handler().postDelayed(new Runnable() {
@Override public void run() {
removeSessionInitializationDelay();
//TODO, put this on the init coroutine
}
}, delay);
}
Expand Down Expand Up @@ -1405,18 +1400,6 @@ ServerRequestInitSession getInstallOrOpenRequest(BranchReferralInitListener call
}
return request;
}

void onIntentReady(@NonNull Activity activity) {
setIntentState(Branch.INTENT_STATE.READY);
requestQueue_.unlockProcessWait(ServerRequest.PROCESS_WAIT_LOCK.INTENT_PENDING_WAIT_LOCK);

boolean grabIntentParams = activity.getIntent() != null && getInitState() != Branch.SESSION_STATE.INITIALISED;

if (grabIntentParams) {
Uri intentData = activity.getIntent().getData();
readAndStripParam(intentData, activity);
}
}

/**
* Notify Branch when network is available in order to process the next request in the queue.
Expand Down
Loading

0 comments on commit 10aa606

Please sign in to comment.