diff --git a/screenshots/com.owncloud.android.ui.preview.PreviewTextFileFragmentTest_displayJavaSnippetFile.png b/screenshots/com.owncloud.android.ui.preview.PreviewTextFileFragmentTest_displayJavaSnippetFile.png new file mode 100644 index 0000000000..76f480f1bd Binary files /dev/null and b/screenshots/com.owncloud.android.ui.preview.PreviewTextFileFragmentTest_displayJavaSnippetFile.png differ diff --git a/screenshots/com.owncloud.android.ui.preview.PreviewTextFileFragmentTest_displaySimpleTextFile.png b/screenshots/com.owncloud.android.ui.preview.PreviewTextFileFragmentTest_displaySimpleTextFile.png new file mode 100644 index 0000000000..67f6dc6053 Binary files /dev/null and b/screenshots/com.owncloud.android.ui.preview.PreviewTextFileFragmentTest_displaySimpleTextFile.png differ diff --git a/src/androidTest/assets/java.md b/src/androidTest/assets/java.md new file mode 100644 index 0000000000..817cad1dca --- /dev/null +++ b/src/androidTest/assets/java.md @@ -0,0 +1,23 @@ +Java: + +```java +private String getAppProcessName() { + String processName = ""; + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { + ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE); + final int ownPid = android.os.Process.myPid(); + final List processes = manager.getRunningAppProcesses(); + if (processes != null) { + for (ActivityManager.RunningAppProcessInfo info : processes) { + if (info.pid == ownPid) { + processName = info.processName; + break; + } + } + } + } else { + processName = Application.getProcessName(); + } + return processName; + } +``` diff --git a/src/androidTest/java/com/owncloud/android/AbstractIT.java b/src/androidTest/java/com/owncloud/android/AbstractIT.java index 25fc902922..79142ec5b9 100644 --- a/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -19,6 +19,7 @@ import com.owncloud.android.utils.FileStorageUtils; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.io.FileUtils; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.runner.RunWith; @@ -26,10 +27,13 @@ import org.junit.runner.RunWith; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; +import static androidx.test.InstrumentationRegistry.getInstrumentation; + /** * Common base for all integration tests @@ -39,7 +43,7 @@ import androidx.test.platform.app.InstrumentationRegistry; public abstract class AbstractIT { protected static OwnCloudClient client; - static Account account; + protected static Account account; protected static Context targetContext; @BeforeClass @@ -133,4 +137,16 @@ public abstract class AbstractIT { e.printStackTrace(); } } + + protected File getFile(String filename) throws IOException { + InputStream inputStream = getInstrumentation().getContext().getAssets().open(filename); + File temp = File.createTempFile("file", "file"); + FileUtils.copyInputStreamToFile(inputStream, temp); + + return temp; + } + + protected void waitForIdleSync() { + InstrumentationRegistry.getInstrumentation().waitForIdleSync(); + } } diff --git a/src/androidTest/java/com/owncloud/android/ui/preview/PreviewTextFileFragmentTest.java b/src/androidTest/java/com/owncloud/android/ui/preview/PreviewTextFileFragmentTest.java new file mode 100644 index 0000000000..ad7f04bce4 --- /dev/null +++ b/src/androidTest/java/com/owncloud/android/ui/preview/PreviewTextFileFragmentTest.java @@ -0,0 +1,85 @@ +/* + * + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2020 Tobias Kaminsky + * Copyright (C) 2020 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.ui.preview; + +import android.Manifest; + +import com.facebook.testing.screenshot.Screenshot; +import com.owncloud.android.AbstractIT; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.ui.activity.FileDisplayActivity; +import com.owncloud.android.utils.FileStorageUtils; +import com.owncloud.android.utils.MimeTypeUtil; + +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import androidx.test.espresso.intent.rule.IntentsTestRule; +import androidx.test.rule.GrantPermissionRule; + +public class PreviewTextFileFragmentTest extends AbstractIT { + @Rule public IntentsTestRule activityRule = new IntentsTestRule<>(FileDisplayActivity.class, + true, + false); + + @Rule + public final GrantPermissionRule permissionRule = GrantPermissionRule.grant( + Manifest.permission.WRITE_EXTERNAL_STORAGE); + + @Test + public void displaySimpleTextFile() throws InterruptedException { + FileDisplayActivity sut = activityRule.launchActivity(null); + + Thread.sleep(3000); + + File file = new File(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt"); + OCFile test = new OCFile("/text.md"); + test.setMimeType(MimeTypeUtil.MIMETYPE_TEXT_MARKDOWN); + test.setStoragePath(file.getAbsolutePath()); + sut.startTextPreview(test, false); + + Thread.sleep(3000); + + Screenshot.snapActivity(sut).record(); + } + + @Test + public void displayJavaSnippetFile() throws IOException, InterruptedException { + FileDisplayActivity sut = activityRule.launchActivity(null); + + Thread.sleep(3000); + + File file = getFile("java.md"); + OCFile test = new OCFile("/java.md"); + test.setMimeType(MimeTypeUtil.MIMETYPE_TEXT_MARKDOWN); + test.setStoragePath(file.getAbsolutePath()); + sut.startTextPreview(test, false); + + Thread.sleep(3000); + + Screenshot.snapActivity(sut).record(); + } +}