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 <ndev@web.de>
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 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
This commit is contained in:
qwerty287 2022-04-07 20:58:09 +02:00 committed by 6543
parent 885bfc5d1f
commit 5b5919c1f6
7 changed files with 208 additions and 25 deletions

View File

@ -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"

View File

@ -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<MainActivity> a = ActivityScenario.launch(MainActivity.class);
a.close();
}
}

View File

@ -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());
}
}

View File

@ -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");
}
}

View File

@ -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)

View File

@ -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

View File

@ -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"));
}