Skip to content

Commit

Permalink
Merge pull request #48 from amberin/shared-test-code
Browse files Browse the repository at this point in the history
Shared test code
  • Loading branch information
amberin authored Aug 12, 2024
2 parents e1e4df3 + b5d5f09 commit ed80d66
Show file tree
Hide file tree
Showing 63 changed files with 3,357 additions and 846 deletions.
43 changes: 39 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,29 @@ on:
workflow_dispatch:

jobs:
test:

localUnitTests:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Gradle Build
run: ./gradlew build

- name: Gradle Test
run: ./gradlew test

instrumentedTests:
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -39,8 +61,14 @@ jobs:
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
uses: gradle/actions/setup-gradle@v4

- name: AVD cache
uses: actions/cache@v4
Expand Down Expand Up @@ -74,13 +102,20 @@ jobs:
~/.android/adb*
key: avd-${{ matrix.api-level }}

- name: Add Dropbox API credentials
- name: Add Dropbox API credentials (for DropboxRepo tests)
shell: bash
run: |
echo "dropbox.refresh_token = \"${{ secrets.DROPBOX_REFRESH_TOKEN }}\"" >> app.properties
echo "dropbox.app_key = \"${{ secrets.DROPBOX_APP_KEY }}\"" >> app.properties
- name: Run tests
- name: Add thegood.cloud credentials (for WebdavRepo tests)
shell: bash
run: |
echo "webdav.repo_url = \"${{ secrets.WEBDAV_TEST_REPO_URL }}\"" >> app.properties
echo "webdav.username = \"${{ secrets.WEBDAV_TEST_USERNAME }}\"" >> app.properties
echo "webdav.password = \"${{ secrets.WEBDAV_TEST_PASSWORD }}\"" >> app.properties
- name: Run instrumented tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
Expand Down
28 changes: 22 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ android {
viewBinding true
}

// testOptions {
// execution 'ANDROIDX_TEST_ORCHESTRATOR'
// }
testOptions {
unitTests {
includeAndroidResources = true
}
}

buildTypes {
release {
Expand All @@ -57,7 +59,11 @@ android {
debug {
buildConfigField "boolean", "LOG_DEBUG", "true"

// Set BuildConfig fields used by tests
buildConfigField "String", "DROPBOX_REFRESH_TOKEN", gradle.ext.appProperties.getProperty("dropbox.refresh_token", '""')
buildConfigField "String", "WEBDAV_REPO_URL", gradle.ext.appProperties.getProperty("webdav.repo_url", '""')
buildConfigField "String", "WEBDAV_USERNAME", gradle.ext.appProperties.getProperty("webdav.username", '""')
buildConfigField "String", "WEBDAV_PASSWORD", gradle.ext.appProperties.getProperty("webdav.password", '""')
}
}

Expand Down Expand Up @@ -91,12 +97,12 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = 11
jvmTarget = 17
}

packagingOptions {
Expand Down Expand Up @@ -149,7 +155,15 @@ dependencies {

implementation "androidx.work:work-runtime-ktx:$versions.android_workmanager"

testImplementation "androidx.test:runner:$versions.android_test"
testImplementation "androidx.test:rules:$versions.android_test"
testImplementation "androidx.test.ext:junit:$versions.android_test_ext_junit"
testImplementation "junit:junit:$versions.junit"
testImplementation 'org.robolectric:robolectric:4.13'
testImplementation "io.github.atetzner:webdav-embedded-server:0.2.1"
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.8.2")
testImplementation "org.mockito:mockito-core:2.28.2"
testImplementation "org.mockito.kotlin:mockito-kotlin:5.1.0"

// AndroidX Test
androidTestImplementation "androidx.test.espresso:espresso-core:$versions.android_test_espresso"
Expand Down Expand Up @@ -209,6 +223,8 @@ dependencies {
implementation("androidx.biometric:biometric-ktx:$versions.biometric_ktx") {
because 'Protect SSH key with biometric prompt'
}
testImplementation(project(":shared-test"))
androidTestImplementation(project(":shared-test"))
}

repositories {
Expand Down
2 changes: 0 additions & 2 deletions app/src/androidTest/java/com/orgzly/android/OrgzlyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.Manifest;
import android.app.Activity;
import android.app.UiAutomation;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
Expand All @@ -20,7 +19,6 @@
import com.orgzly.android.repos.RepoFactory;
import com.orgzly.android.util.UserTimeFormatter;
import com.orgzly.org.datetime.OrgDateTime;
import com.orgzly.test.BuildConfig;

import org.junit.After;
import org.junit.Before;
Expand Down
40 changes: 40 additions & 0 deletions app/src/androidTest/java/com/orgzly/android/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import android.net.Uri;

import com.orgzly.BuildConfig;
import com.orgzly.android.data.DataRepository;
import com.orgzly.android.data.DbRepoBookRepository;
import com.orgzly.android.db.entity.BookAction;
import com.orgzly.android.db.entity.BookView;
import com.orgzly.android.db.entity.Repo;
import com.orgzly.android.prefs.AppPreferences;
import com.orgzly.android.repos.RepoType;
import com.orgzly.android.repos.RepoWithProps;
import com.orgzly.android.repos.SyncRepo;
Expand All @@ -18,6 +20,10 @@
import com.orgzly.android.sync.SyncUtils;
import com.orgzly.android.util.MiscUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assume;

import java.io.File;
import java.io.IOException;
import java.util.Map;
Expand All @@ -40,12 +46,21 @@ public SyncRepo repoInstance(RepoType type, String url) {
return dataRepository.getRepoInstance(13, type, url);
}

public SyncRepo repoInstance(RepoType type, String url, Long id) {
return dataRepository.getRepoInstance(id, type, url);
}

public Repo setupRepo(RepoType type, String url) {
long id = dataRepository.createRepo(new RepoWithProps(new Repo(0, type, url)));

return dataRepository.getRepo(id);
}

public Repo setupRepo(RepoType type, String url, Map<String, String> props) {
long id = dataRepository.createRepo(new RepoWithProps(new Repo(0, type, url), props));
return dataRepository.getRepo(id);
}

public void deleteRepo(String url) {
Repo repo = dataRepository.getRepo(url);
if (repo != null) {
Expand Down Expand Up @@ -149,4 +164,29 @@ public Map<String, BookNamesake> sync() {

return null;
}

public Map<String, BookNamesake> syncOrThrow() throws Exception {
Map<String, BookNamesake> nameGroups = SyncUtils.groupAllNotebooksByName(dataRepository);

for (BookNamesake group : nameGroups.values()) {
BookAction action = SyncUtils.syncNamesake(dataRepository, group);
dataRepository.setBookLastActionAndSyncStatus(
group.getBook().getBook().getId(), action, group.getStatus().toString());
}

return nameGroups;
}

public void dropboxTestPreflight() throws JSONException {
Assume.assumeTrue(BuildConfig.IS_DROPBOX_ENABLED);
Assume.assumeTrue(BuildConfig.DROPBOX_APP_KEY.length() > 0);
Assume.assumeTrue(BuildConfig.DROPBOX_REFRESH_TOKEN.length() > 0);

JSONObject mockSerializedDbxCredential = new JSONObject();
mockSerializedDbxCredential.put("access_token", "dummy");
mockSerializedDbxCredential.put("expires_at", System.currentTimeMillis());
mockSerializedDbxCredential.put("refresh_token", BuildConfig.DROPBOX_REFRESH_TOKEN);
mockSerializedDbxCredential.put("app_key", BuildConfig.DROPBOX_APP_KEY);
AppPreferences.dropboxSerializedCredential(App.getAppContext(), mockSerializedDbxCredential.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class BookPrefaceTest : OrgzlyTest() {
private fun setPrefaceSetting(@StringRes id: Int) {
onActionItemClick(R.id.activity_action_settings, R.string.settings)

clickSetting("prefs_screen_notebooks", R.string.pref_title_notebooks)
clickSetting("pref_key_preface_in_book", R.string.preface_in_book)
clickSetting(R.string.pref_title_notebooks)
clickSetting(R.string.preface_in_book)

onView(withText(id)).perform(click())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ public void testSetDeadlineTimeForNewNote() {
onView(withId(R.id.date_picker_button)).perform(click());
onView(withClassName(equalTo(DatePicker.class.getName()))).perform(setDate(2014, 4, 1));
onView(withText(android.R.string.ok)).perform(click());
SystemClock.sleep(100);
onView(withText(R.string.set)).perform(click());
onView(withId(R.id.deadline_button)).check(matches(withText(userDateTime("<2014-04-01 Tue>"))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class BooksSortOrderTest : OrgzlyTest() {

private fun setBooksSortOrder(@StringRes id: Int) {
onActionItemClick(R.id.activity_action_settings, R.string.settings)
clickSetting("prefs_screen_notebooks", R.string.pref_title_notebooks)
clickSetting("pref_key_notebooks_sort_order", R.string.sort_order)
clickSetting(R.string.pref_title_notebooks)
clickSetting(R.string.sort_order)
onData(hasToString(context.getString(id))).perform(click())
pressBack()
pressBack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,17 @@ public void testNewNote() {

private void enableCreatedAt() {
onActionItemClick(R.id.activity_action_settings, R.string.settings);
clickSetting("prefs_screen_sync", R.string.sync);
clickSetting("pref_key_is_created_at_added", R.string.use_created_at_property);
clickSetting(R.string.sync);
clickSetting(R.string.use_created_at_property);
onView(withText(R.string.yes)).perform(click());
pressBack();
pressBack();
}

private void changeCreatedAtProperty(String propName) {
onActionItemClick(R.id.activity_action_settings, R.string.settings);
clickSetting("prefs_screen_sync", R.string.sync);
clickSetting("pref_key_created_at_property", R.string.created_at_property);
clickSetting(R.string.sync);
clickSetting(R.string.created_at_property);
onView(instanceOf(EditText.class)).perform(replaceTextCloseKeyboard(propName));
onView(withText(android.R.string.ok)).perform(click());
onView(withText(R.string.yes)).perform(click());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public void testClearDatabaseWithFragmentsInBackStack() {
onView(allOf(withText("book-two"), isDisplayed())).perform(click());
onView(withText("Note #2.")).perform(click());
onActionItemClick(R.id.activity_action_settings, R.string.settings);
clickSetting("prefs_screen_app", R.string.app);
clickSetting("pref_key_clear_database", R.string.clear_database);
clickSetting(R.string.app);
clickSetting(R.string.clear_database);
onView(withText(R.string.ok)).perform(click());
pressBack();
pressBack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ class NoteFragmentTest : OrgzlyTest() {

/* Change lowest priority to A. */
onActionItemClick(R.id.activity_action_settings, R.string.settings)
clickSetting("prefs_screen_notebooks", R.string.pref_title_notebooks)
clickSetting("pref_key_min_priority", R.string.lowest_priority)
clickSetting(R.string.pref_title_notebooks)
clickSetting(R.string.lowest_priority)
onData(hasToString(containsString("A"))).perform(click())
pressBack()
pressBack()
Expand All @@ -431,8 +431,8 @@ class NoteFragmentTest : OrgzlyTest() {

/* Change lowest priority to C. */
onActionItemClick(R.id.activity_action_settings, R.string.settings)
clickSetting("prefs_screen_notebooks", R.string.pref_title_notebooks)
clickSetting("pref_key_min_priority", R.string.lowest_priority)
clickSetting(R.string.pref_title_notebooks)
clickSetting(R.string.lowest_priority)
onData(hasToString(containsString("C"))).perform(click())
pressBack()
pressBack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public void testClickingNote() {
defaultSetUp();

onView(allOf(withText("book-two"), isDisplayed())).perform(click());
SystemClock.sleep(200);
searchForTextCloseKeyboard("b.book-two Note");
onView(withId(R.id.fragment_query_search_view_flipper)).check(matches(isDisplayed()));
onNotesInSearch().check(matches(recyclerViewItemCount(29)));
Expand Down Expand Up @@ -384,6 +385,7 @@ public void testInheritedAndOwnTag() {
scenario = ActivityScenario.launch(MainActivity.class);

onView(allOf(withText("notebook-1"), isDisplayed())).perform(click());
SystemClock.sleep(200);
searchForTextCloseKeyboard("t.tag1 t.tag2");
onView(withId(R.id.fragment_query_search_view_flipper)).check(matches(isDisplayed()));
onNotesInSearch().check(matches(recyclerViewItemCount(3)));
Expand Down Expand Up @@ -615,6 +617,7 @@ public void testSearchForTagOrTag() {
scenario = ActivityScenario.launch(MainActivity.class);

onView(allOf(withText("notebook"), isDisplayed())).perform(click());
SystemClock.sleep(200);
searchForTextCloseKeyboard("tn.a or tn.b");
onView(withId(R.id.fragment_query_search_view_flipper)).check(matches(isDisplayed()));
onNotesInSearch().check(matches(recyclerViewItemCount(2)));
Expand Down Expand Up @@ -723,6 +726,7 @@ public void testSearchWithState() {
scenario = ActivityScenario.launch(MainActivity.class);

onView(allOf(withText("notebook"), isDisplayed())).perform(click());
SystemClock.sleep(200);
searchForTextCloseKeyboard(".it.none");
onView(withId(R.id.fragment_query_search_view_flipper)).check(matches(isDisplayed()));
onNotesInSearch().check(matches(recyclerViewItemCount(3)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public void testDisplayedContentInBook() {
.check(matches(allOf(withText(containsString("Content for [a-1]")), isDisplayed())));

onActionItemClick(R.id.activity_action_settings, R.string.settings);
clickSetting("prefs_screen_notebooks", R.string.pref_title_notebooks);
clickSetting("pref_key_is_notes_content_displayed_in_list", R.string.display_content);
clickSetting(R.string.pref_title_notebooks);
clickSetting(R.string.display_content);
pressBack();
pressBack();

Expand All @@ -96,8 +96,8 @@ public void testDisplayedContentInBook() {

private void setDefaultPriority(String priority) {
onActionItemClick(R.id.activity_action_settings, R.string.settings);
clickSetting("prefs_screen_notebooks", R.string.pref_title_notebooks);
clickSetting("pref_key_default_priority", R.string.default_priority);
clickSetting(R.string.pref_title_notebooks);
clickSetting(R.string.default_priority);
onData(hasToString(containsString(priority))).perform(click());
pressBack();
pressBack();
Expand Down
Loading

0 comments on commit ed80d66

Please sign in to comment.