Skip to content

Commit

Permalink
Merge branch 'gdeluna-branch/SDK-2104' of https://github.com/BranchMe…
Browse files Browse the repository at this point in the history
…trics/android-branch-deep-linking-attribution into gdeluna-branch/SDK-2104-2/AddMockInterfaceTests
  • Loading branch information
gdeluna-branch committed Oct 11, 2023
2 parents 9140010 + 2f29f7a commit 37f598a
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 153 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/unit-and-instrumented-tests-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
with:
name: unit-test-api-level-$API_MIN
path: ./Branch-SDK/build/

instrumented-test-api-level-min:
name: instrumented-test-api-level-$API_MIN
runs-on: macos-latest
Expand Down Expand Up @@ -66,6 +67,44 @@ jobs:
with:
name: instrumented-test-api-level-$API_MIN
path: ./Branch-SDK/build/

jacoco-test-coverage-api-level-min:
name: jacoco-test-coverage-api-level-$API_MIN
if: ${{ always() && contains(join(needs.*.result, ','), 'success') }}
needs: [unit-test-api-level-min, instrumented-test-api-level-min]
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3
# create an emulator with google apis, runs on java 8
- name: Create Android emulator
run: |
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --licenses
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install "system-images;android-"$API_MIN";google_apis;x86_64"
echo "no" | $ANDROID_HOME/tools/bin/avdmanager --verbose create avd --force --name test --package "system-images;android-"$API_MIN";google_apis;x86_64"
# boots and waits for the emulator to be ready
- name: Launch Emulator
run: |
echo "Starting emulator and waiting for boot to complete."
nohup $ANDROID_HOME/tools/emulator -avd test -no-audio -no-boot-anim -camera-back none -camera-front none -qemu -m 2048 2>&1 &
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do echo "waiting..."; sleep 1; done; input keyevent 82'
echo "Emulator has finished booting"
# repo's gradle is configured to run on java 17
- name: Setup java 17 for gradle
uses: actions/setup-java@v3
with:
distribution: ${{ env.JAVA_DISTRIBUTION }}
java-version: ${{ env.JAVA_VERSION }}
- name: Run Coverage
run: |
./gradlew :Branch-SDK:jacocoTestReport --info
- name: Upload Coverage Test Report
if: success()
uses: codecov/codecov-action@v3
with:
file: ./Branch-SDK/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml
fail_ci_if_error: true

unit-test-api-level-current:
name: unit-test-api-level-$API_CURRENT
runs-on: macos-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
Expand All @@ -26,6 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
setupDisableAdNetworkCalloutsSwitch();
setupPrepHelperView();
setupRetryEditText();
setupApiUrlText();
}

void setupRetryEditText() {
Expand All @@ -35,21 +35,40 @@ void setupRetryEditText() {

retryEditText.setText(Integer.toString(currentRetries));

retryEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
int retries = Integer.valueOf(textView.getText().toString());
PrefHelper.getInstance(SettingsActivity.this).setRetryCount(retries);

InputMethodManager imm = (InputMethodManager)getSystemService(SettingsActivity.this.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(retryEditText.getWindowToken(), 0);

Toast.makeText(getApplicationContext(), "Set Network Retries to " + retries, Toast.LENGTH_SHORT).show();
return true;
}
return false;
retryEditText.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_DONE) {
int retries = Integer.parseInt(textView.getText().toString());
PrefHelper.getInstance(SettingsActivity.this).setRetryCount(retries);

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(retryEditText.getWindowToken(), 0);

Toast.makeText(getApplicationContext(), "Set Network Retries to " + retries, Toast.LENGTH_SHORT).show();
return true;
}
return false;
});
}

void setupApiUrlText() {
final EditText apiUrlText = findViewById(R.id.api_url_text);
final PrefHelper prefHelper = PrefHelper.getInstance(this);
String currentApiUrl = prefHelper.getAPIBaseUrl();

apiUrlText.setText(currentApiUrl);

apiUrlText.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_DONE) {
String newApiUrl = textView.getText().toString();
Branch.getInstance().setAPIUrl(newApiUrl);

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(apiUrlText.getWindowToken(), 0);

Toast.makeText(getApplicationContext(), "Set API Base URL to " + newApiUrl, Toast.LENGTH_SHORT).show();
return true;
}
return false;
});
}

Expand Down
22 changes: 22 additions & 0 deletions Branch-SDK-TestBed/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
style="@style/testbed_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="API Base URL" />

<EditText
android:id="@+id/api_url_text"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="text"
android:maxLines="1"
android:hint="api2.branch.io" />

</LinearLayout>

<TextView
android:id="@+id/prefhelper_text_view"
style="@style/testbed_text_view"
Expand Down
30 changes: 30 additions & 0 deletions Branch-SDK/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ plugins {
`maven-publish`
signing
id("org.gradle.test-retry") version "1.5.3"
id("jacoco")
}
val coroutinesVersion = "1.6.4"
jacoco {
toolVersion = "0.8.10"
}

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to "*.jar")))
Expand Down Expand Up @@ -93,6 +97,7 @@ android {

debug {
enableUnitTestCoverage = true
enableAndroidTestCoverage = true
buildConfigField("long", "VERSION_CODE", VERSION_CODE)
buildConfigField("String", "VERSION_NAME", VERSION_NAME.wrapInQuotes())
}
Expand Down Expand Up @@ -275,5 +280,30 @@ tasks {
retry {
maxRetries.set(3)
}
configure<JacocoTaskExtension> {
isIncludeNoLocationClasses = true
excludes = listOf("jdk.internal.*")
}
}
}

tasks.create<JacocoReport>("jacocoTestReport") {
group = "Reporting"
description = "Generate Jacoco code coverage reports after running tests."
dependsOn("testDebugUnitTest","createDebugCoverageReport")
reports {
xml.required.set(true)
html.required.set(true)
}
sourceDirectories.setFrom("${project.projectDir}/src/main/java")
classDirectories.setFrom("${project.buildDir}/intermediates/javac/debug/classes")
executionData.setFrom(
fileTree(project.buildDir) {
include(
"jacoco/testDebugUnitTest.exec",
"outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec",
"outputs/code_coverage/debugAndroidTest/connected/**/*.ec",
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ public void testGetAPIBaseUrl() {

@Test
public void testSetAPIUrl_Example() {
PrefHelper.setAPIUrl("https://www.example.com/");
prefHelper.setAPIUrl("https://www.example.com/");
String actual = prefHelper.getAPIBaseUrl();
Assert.assertEquals("https://www.example.com/", actual);
}

@Test
public void testSetAPIUrl_InvalidHttp() {
PrefHelper.setAPIUrl("http://www.example.com/");
prefHelper.setAPIUrl("http://www.example.com/");
assertDefaultURL();
}

@Test
public void testSetAPIUrl_InvalidNull() {
PrefHelper.setAPIUrl(null);
prefHelper.setAPIUrl(null);
assertDefaultURL();
}

@Test
public void testSetAPIUrl_InvalidEmpty() {
PrefHelper.setAPIUrl("");
prefHelper.setAPIUrl("");
assertDefaultURL();
}

Expand Down
17 changes: 11 additions & 6 deletions Branch-SDK/src/main/java/io/branch/referral/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,11 @@ public static void expectDelayedSessionInitialization(boolean expectDelayedInit)
* <p>Sets a custom base URL for all calls to the Branch API. Requires https.</p>
* @param url The {@link String} URL base URL that the Branch API uses.
*/
public static void setAPIUrl(String url) {
PrefHelper.setAPIUrl(url);
public void setAPIUrl(String url) {
if (prefHelper_ != null && url.length() > 0) {
prefHelper_.setAPIUrl(url);
}
}

/**
* <p>Sets a custom CDN base URL.</p>
* @param url The {@link String} base URL for CDN endpoints.
Expand Down Expand Up @@ -971,9 +972,13 @@ public void logout() {
* @param callback An instance of {@link io.branch.referral.Branch.LogoutStatusListener} to callback with the logout operation status.
*/
public void logout(LogoutStatusListener callback) {
ServerRequest req = new ServerRequestLogout(context_, callback);
if (!req.constructError_ && !req.handleErrors(context_)) {
requestQueue_.handleNewRequest(req);
prefHelper_.setIdentity(PrefHelper.NO_STRING_VALUE);
prefHelper_.clearUserValues();
//On Logout clear the link cache and all pending requests
linkCache_.clear();
requestQueue_.clear();
if (callback != null) {
callback.onLogoutFinished(true, null);
}
}

Expand Down
1 change: 0 additions & 1 deletion Branch-SDK/src/main/java/io/branch/referral/Defines.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ public enum RequestPath {
RegisterInstall("v1/install"),
RegisterOpen("v1/open"),
IdentifyUser("v1/profile"),
Logout("v1/logout"),
ContentEvent("v1/content-events"),
TrackStandardEvent("v2/event/standard"),
TrackCustomEvent("v2/event/custom"),
Expand Down
15 changes: 5 additions & 10 deletions Branch-SDK/src/main/java/io/branch/referral/PrefHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public class PrefHelper {

static final String KEY_REFERRING_URL_QUERY_PARAMETERS = "bnc_referringUrlQueryParameters";
static final String KEY_ANON_ID = "bnc_anon_id";
private static final String KEY_BASE_URL = "bnc_base_url";

/**
* Internal static variable of own type {@link PrefHelper}. This variable holds the single
Expand Down Expand Up @@ -156,11 +157,6 @@ public class PrefHelper {
*/
private final JSONObject secondaryRequestMetadata = new JSONObject();

/**
* Branch Custom server url. Used by clients that want to proxy all requests.
*/
private static String customServerURL_ = null;

/**
* Branch Custom server url. Used by clients that want to proxy all CDN requests.
*/
Expand Down Expand Up @@ -207,16 +203,15 @@ static void shutDown() {
// Reset all of the statics.
enableLogging_ = false;
prefHelper_ = null;
customServerURL_ = null;
customCDNBaseURL_ = null;
}

/**
* <p>Sets a custom base URL for all calls to the Branch API. Requires https.</p>
* @param url The {@link String} URL base URL that the Branch API uses.
*/
static void setAPIUrl(String url) {
customServerURL_ = url;
public void setAPIUrl(String url) {
setString(KEY_BASE_URL, url);
}

/**
Expand All @@ -227,8 +222,8 @@ static void setAPIUrl(String url) {
* API uses.
*/
public String getAPIBaseUrl() {
if (URLUtil.isHttpsUrl(customServerURL_)) {
return customServerURL_;
if (URLUtil.isHttpsUrl(getString(KEY_BASE_URL))) {
return getString(KEY_BASE_URL);
}

if (Build.VERSION.SDK_INT >= 20) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,6 @@ private static ServerRequest getExtendedServerRequest(String requestPath, JSONOb
extendedReq = new ServerRequestCreateUrl(Defines.RequestPath.GetURL, post, context);
} else if (requestPath.equalsIgnoreCase(Defines.RequestPath.IdentifyUser.getPath())) {
extendedReq = new ServerRequestIdentifyUserRequest(Defines.RequestPath.IdentifyUser, post, context);
} else if (requestPath.equalsIgnoreCase(Defines.RequestPath.Logout.getPath())) {
extendedReq = new ServerRequestLogout(Defines.RequestPath.Logout, post, context);
} else if (requestPath.equalsIgnoreCase(Defines.RequestPath.RegisterInstall.getPath())) {
extendedReq = new ServerRequestRegisterInstall(Defines.RequestPath.RegisterInstall, post, context, initiatedByClient);
} else if (requestPath.equalsIgnoreCase(Defines.RequestPath.RegisterOpen.getPath())) {
Expand Down
Loading

0 comments on commit 37f598a

Please sign in to comment.