From 5b5919c1f66617ee5afa4eadb74848666bf03bd9 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 7 Apr 2022 20:58:09 +0200 Subject: [PATCH] Add some basic instrumentation tests (#1105) Add some tests to test Android-only features: `MainActivityTest` (just start the activity), `AppUtilTest` (some Android-related methods), `ToastyTest` (only to test if toasts are working). Actually, this doesn't really test something, but just as a first step. https://codeberg.org/gitnex/GitNex/issues/1098 I think it's REALLY hard to write tests that really work well in testing features. Starting activities is possible, but it's hard to test it. Also, this will run with the same database and tinydb the real app uses - it's hard for us to make this usable, we have to replace the DB and TinyDB and then add values again. Co-authored-by: qwerty287 Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1105 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 Co-committed-by: qwerty287 --- app/build.gradle | 2 +- .../gitnex/activities/MainActivityTest.java | 23 ++++ .../org/main/gitnex/helpers/AppUtilTest.java | 111 ++++++++++++++++++ .../org/main/gitnex/helpers/ToastyTest.java | 49 ++++++++ .../java/org/mian/gitnex/helpers/AppUtil.java | 2 +- .../org/mian/gitnex/helpers/AppUtilTest.java | 20 ++-- .../mian/gitnex/helpers/PathsHelperTest.java | 26 ++-- 7 files changed, 208 insertions(+), 25 deletions(-) create mode 100644 app/src/androidTest/java/org/main/gitnex/activities/MainActivityTest.java create mode 100644 app/src/androidTest/java/org/main/gitnex/helpers/AppUtilTest.java create mode 100644 app/src/androidTest/java/org/main/gitnex/helpers/ToastyTest.java diff --git a/app/build.gradle b/app/build.gradle index bbeb959b..309e6482 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -64,7 +64,7 @@ dependencies { implementation "androidx.legacy:legacy-support-v4:1.0.0" implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test:runner:1.4.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2' implementation "com.google.code.gson:gson:2.9.0" diff --git a/app/src/androidTest/java/org/main/gitnex/activities/MainActivityTest.java b/app/src/androidTest/java/org/main/gitnex/activities/MainActivityTest.java new file mode 100644 index 00000000..0749b377 --- /dev/null +++ b/app/src/androidTest/java/org/main/gitnex/activities/MainActivityTest.java @@ -0,0 +1,23 @@ +package org.main.gitnex.activities; + +import androidx.test.core.app.ActivityScenario; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mian.gitnex.activities.MainActivity; +import static org.junit.Assert.*; + +/** + * @author qwerty287 + */ +@RunWith(AndroidJUnit4.class) +public class MainActivityTest { + + @Test + public void activityTest() { + + ActivityScenario a = ActivityScenario.launch(MainActivity.class); + a.close(); + } + +} diff --git a/app/src/androidTest/java/org/main/gitnex/helpers/AppUtilTest.java b/app/src/androidTest/java/org/main/gitnex/helpers/AppUtilTest.java new file mode 100644 index 00000000..41c53df2 --- /dev/null +++ b/app/src/androidTest/java/org/main/gitnex/helpers/AppUtilTest.java @@ -0,0 +1,111 @@ +package org.main.gitnex.helpers; + +import android.content.Context; +import android.net.Uri; +import android.view.View; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mian.gitnex.helpers.AppUtil; +import static org.junit.Assert.*; + +/** + * @author qwerty287 + */ +@RunWith(AndroidJUnit4.class) +public class AppUtilTest { + + @Test + public void getAppBuildNoTest() { + + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals(425, AppUtil.getAppBuildNo(context)); + } + + @Test + public void getAppVersionTest() { + + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("4.3.0", AppUtil.getAppVersion(context)); + } + + @Test + public void isProTest() { + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertFalse(AppUtil.isPro(context)); // tests use a custom package -> always false + } + + @Test + public void encodeBase64Test() { + assertEquals("SGVsbG8gV29ybGQh\n", AppUtil.encodeBase64("Hello World!")); + assertEquals("R2l0TmV4\n", AppUtil.encodeBase64("GitNex")); + assertEquals("Q29kZWJlcmc=\n", AppUtil.encodeBase64("Codeberg")); + assertEquals("R2l0ZWE=\n", AppUtil.encodeBase64("Gitea")); + + assertNotEquals("123\n", AppUtil.encodeBase64("Hello World!")); + assertNotEquals("234\n", AppUtil.encodeBase64("GitNex")); + assertNotEquals("345\n", AppUtil.encodeBase64("Codeberg")); + assertNotEquals("456\n", AppUtil.encodeBase64("Gitea")); + } + + @Test + public void decodeBase64Test() { + assertEquals("Hello World!", AppUtil.decodeBase64("SGVsbG8gV29ybGQh\n")); + assertEquals("GitNex", AppUtil.decodeBase64("R2l0TmV4\n")); + assertEquals("Codeberg", AppUtil.decodeBase64("Q29kZWJlcmc=\n")); + assertEquals("Gitea", AppUtil.decodeBase64("R2l0ZWE=\n")); + + assertNotEquals("helloworld", AppUtil.decodeBase64("SGVsbG8gV29ybGQh\n")); + assertNotEquals("gitnex", AppUtil.decodeBase64("R2l0TmV4\n")); + assertNotEquals("123codeberg", AppUtil.decodeBase64("Q29kZWJlcmc=\n")); + assertNotEquals("gitea123", AppUtil.decodeBase64("R2l0ZWE=\n")); + } + + @Test + public void setMultiVisibilityTest() { + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + View v1 = new View(context); + View v2 = new View(context); + View v3 = new View(context); + View v4 = new View(context); + + AppUtil.setMultiVisibility(View.GONE, v1, v2, v3, v4); + assertEquals(View.GONE, v1.getVisibility()); + assertEquals(View.GONE, v2.getVisibility()); + assertEquals(View.GONE, v3.getVisibility()); + assertEquals(View.GONE, v4.getVisibility()); + + AppUtil.setMultiVisibility(View.VISIBLE, v2); + assertEquals(View.GONE, v1.getVisibility()); + assertEquals(View.VISIBLE, v2.getVisibility()); + assertEquals(View.GONE, v3.getVisibility()); + assertEquals(View.GONE, v4.getVisibility()); + + AppUtil.setMultiVisibility(View.INVISIBLE, v4); + assertEquals(View.GONE, v1.getVisibility()); + assertEquals(View.VISIBLE, v2.getVisibility()); + assertEquals(View.GONE, v3.getVisibility()); + assertEquals(View.INVISIBLE, v4.getVisibility()); + } + + @Test + public void getUriFromGitUrlTest() { + assertEquals("https://git@codeberg.org/gitnex/GitNex", AppUtil.getUriFromGitUrl("ssh://git@codeberg.org:gitnex/GitNex").toString()); + assertEquals("https://codeberg.org/gitnex/GitNex", AppUtil.getUriFromGitUrl("codeberg.org:gitnex/GitNex").toString()); + assertEquals("ssh://git@codeberg.org/gitnex/GitNex", AppUtil.getUriFromGitUrl("ssh://git@codeberg.org/gitnex/GitNex").toString()); + assertEquals("https://git@codeberg.org/gitnex/GitNex.git", AppUtil.getUriFromGitUrl("ssh://git@codeberg.org:gitnex/GitNex.git").toString()); + assertEquals("https://codeberg.org/gitnex/GitNex.git", AppUtil.getUriFromGitUrl("codeberg.org:gitnex/GitNex.git").toString()); + assertEquals("https://codeberg.org/gitnex/GitNex.git", AppUtil.getUriFromGitUrl("https://codeberg.org/gitnex/GitNex.git").toString()); + assertEquals("https://gitnex.com", AppUtil.getUriFromGitUrl("https://gitnex.com").toString()); + assertEquals("https://gitnex.com:3000", AppUtil.getUriFromGitUrl("https://gitnex.com:3000").toString()); + } + + @Test + public void changeSchemeTest() { + assertEquals("https://codeberg.org/gitnex/GitNex", AppUtil.changeScheme(Uri.parse("ssh://codeberg.org/gitnex/GitNex"), "https").toString()); + assertEquals("https://gitnex.com", AppUtil.changeScheme(Uri.parse("http://gitnex.com"), "https").toString()); + assertEquals("ssh://codeberg.org/gitnex/GitNex", AppUtil.changeScheme(Uri.parse("http://codeberg.org/gitnex/GitNex"), "ssh").toString()); + } +} + diff --git a/app/src/androidTest/java/org/main/gitnex/helpers/ToastyTest.java b/app/src/androidTest/java/org/main/gitnex/helpers/ToastyTest.java new file mode 100644 index 00000000..ca0b94de --- /dev/null +++ b/app/src/androidTest/java/org/main/gitnex/helpers/ToastyTest.java @@ -0,0 +1,49 @@ +package org.main.gitnex.helpers; + +import android.content.Context; +import android.os.Looper; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mian.gitnex.helpers.Toasty; + +/** + * Class test if Toasts are working, no assertions, just crash tests. + * + * @author qwerty287 + */ +@RunWith(AndroidJUnit4.class) +public class ToastyTest { + + @BeforeClass + public static void prepare() { + Looper.prepare(); + } + + @Test + public void infoTest() { + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + Toasty.info(context, "GitNex info test"); + } + + @Test + public void warningTest() { + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + Toasty.warning(context, "GitNex warning test"); + } + + @Test + public void errorTest() { + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + Toasty.error(context, "GitNex error test"); + } + + @Test + public void successTest() { + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + Toasty.success(context, "GitNex success test"); + } + +} diff --git a/app/src/main/java/org/mian/gitnex/helpers/AppUtil.java b/app/src/main/java/org/mian/gitnex/helpers/AppUtil.java index 55c9944d..23d64f1b 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/AppUtil.java +++ b/app/src/main/java/org/mian/gitnex/helpers/AppUtil.java @@ -405,7 +405,7 @@ public class AppUtil { public static Uri getUriFromGitUrl(String url) { Uri uri = Uri.parse(url); String host = uri.getHost(); - if(host != null) { + if(host != null && !host.contains(":")) { return uri; } // must be a Git SSH URL now (old rcp standard) diff --git a/app/src/test/java/org/mian/gitnex/helpers/AppUtilTest.java b/app/src/test/java/org/mian/gitnex/helpers/AppUtilTest.java index 98c1332d..468f0a3a 100644 --- a/app/src/test/java/org/mian/gitnex/helpers/AppUtilTest.java +++ b/app/src/test/java/org/mian/gitnex/helpers/AppUtilTest.java @@ -1,7 +1,7 @@ package org.mian.gitnex.helpers; import org.junit.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; /** * @author qwerty287 @@ -20,19 +20,19 @@ public class AppUtilTest { @Test public void checkStringsWithAlphaNumeric() { - assertEquals(AppUtil.checkStringsWithAlphaNumeric("string"), true); - assertEquals(AppUtil.checkStringsWithAlphaNumeric("123"), true); - assertEquals(AppUtil.checkStringsWithAlphaNumeric("123 with string"), false); - assertEquals(AppUtil.checkStringsWithAlphaNumeric("string 123"), false); - assertEquals(AppUtil.checkStringsWithAlphaNumeric("string-123"), false); + assertTrue(AppUtil.checkStringsWithAlphaNumeric("string")); + assertTrue(AppUtil.checkStringsWithAlphaNumeric("123")); + assertFalse(AppUtil.checkStringsWithAlphaNumeric("123 with string")); + assertFalse(AppUtil.checkStringsWithAlphaNumeric("string 123")); + assertFalse(AppUtil.checkStringsWithAlphaNumeric("string-123")); } @Test public void checkIntegers() { - assertEquals(AppUtil.checkIntegers("string"), false); - assertEquals(AppUtil.checkIntegers("123"), true); - assertEquals(AppUtil.checkIntegers("123 with string"), false); - assertEquals(AppUtil.checkIntegers("string 123"), false); + assertFalse(AppUtil.checkIntegers("string")); + assertTrue(AppUtil.checkIntegers("123")); + assertFalse(AppUtil.checkIntegers("123 with string")); + assertFalse(AppUtil.checkIntegers("string 123")); } @Test diff --git a/app/src/test/java/org/mian/gitnex/helpers/PathsHelperTest.java b/app/src/test/java/org/mian/gitnex/helpers/PathsHelperTest.java index 3443ee61..accbf2ff 100644 --- a/app/src/test/java/org/mian/gitnex/helpers/PathsHelperTest.java +++ b/app/src/test/java/org/mian/gitnex/helpers/PathsHelperTest.java @@ -12,21 +12,21 @@ public class PathsHelperTest { @Test public void testJoin() { - assertEquals(PathsHelper.join("test", "/test", "test/", "/test/"), "/test/test/test/test/"); - assertEquals(PathsHelper.join("test", "test", "test", "test"), "/test/test/test/test/"); - assertEquals(PathsHelper.join("/test", "/test", "/test", "/test"), "/test/test/test/test/"); - assertEquals(PathsHelper.join("/test/", "/test/", "test/", "/test/"), "/test/test/test/test/"); - assertEquals(PathsHelper.join("test", "test", "/test", "/test"), "/test/test/test/test/"); - assertEquals(PathsHelper.join("test/", "test", "/test", "/test"), "/test/test/test/test/"); + assertEquals("/test/test/test/test/", PathsHelper.join("test", "/test", "test/", "/test/")); + assertEquals("/test/test/test/test/", PathsHelper.join("test", "test", "test", "test")); + assertEquals("/test/test/test/test/", PathsHelper.join("/test", "/test", "/test", "/test")); + assertEquals("/test/test/test/test/", PathsHelper.join("/test/", "/test/", "test/", "/test/")); + assertEquals("/test/test/test/test/", PathsHelper.join("test", "test", "/test", "/test")); + assertEquals("/test/test/test/test/", PathsHelper.join("test/", "test", "/test", "/test")); - assertEquals(PathsHelper.join("test/test/test/test"), "/test/test/test/test/"); - assertEquals(PathsHelper.join("/test/test/test/test"), "/test/test/test/test/"); - assertEquals(PathsHelper.join("test/test/test/test/"), "/test/test/test/test/"); + assertEquals("/test/test/test/test/", PathsHelper.join("test/test/test/test")); + assertEquals("/test/test/test/test/", PathsHelper.join("/test/test/test/test")); + assertEquals("/test/test/test/test/", PathsHelper.join("test/test/test/test/")); - assertEquals(PathsHelper.join("test"), "/test/"); - assertEquals(PathsHelper.join("test/"), "/test/"); - assertEquals(PathsHelper.join("/test/"), "/test/"); - assertEquals(PathsHelper.join("/test"), "/test/"); + assertEquals("/test/", PathsHelper.join("test")); + assertEquals("/test/", PathsHelper.join("test/")); + assertEquals("/test/", PathsHelper.join("/test/")); + assertEquals("/test/", PathsHelper.join("/test")); }