AbstractIT.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.owncloud.android;
  2. import android.accounts.Account;
  3. import android.accounts.AccountManager;
  4. import android.accounts.AuthenticatorException;
  5. import android.accounts.OperationCanceledException;
  6. import android.app.Activity;
  7. import android.content.ActivityNotFoundException;
  8. import android.content.Context;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import com.facebook.testing.screenshot.Screenshot;
  12. import com.nextcloud.client.account.UserAccountManager;
  13. import com.nextcloud.client.account.UserAccountManagerImpl;
  14. import com.owncloud.android.datamodel.FileDataStorageManager;
  15. import com.owncloud.android.lib.common.OwnCloudClient;
  16. import com.owncloud.android.lib.common.OwnCloudClientFactory;
  17. import com.owncloud.android.lib.common.accounts.AccountUtils;
  18. import com.owncloud.android.utils.FileStorageUtils;
  19. import org.apache.commons.httpclient.HttpStatus;
  20. import org.apache.commons.httpclient.methods.GetMethod;
  21. import org.apache.commons.io.FileUtils;
  22. import org.junit.Assert;
  23. import org.junit.BeforeClass;
  24. import org.junit.runner.RunWith;
  25. import java.io.File;
  26. import java.io.FileWriter;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import androidx.test.espresso.contrib.DrawerActions;
  30. import androidx.test.espresso.intent.rule.IntentsTestRule;
  31. import androidx.test.ext.junit.runners.AndroidJUnit4;
  32. import androidx.test.platform.app.InstrumentationRegistry;
  33. import static androidx.test.InstrumentationRegistry.getInstrumentation;
  34. import static androidx.test.espresso.Espresso.onView;
  35. import static androidx.test.espresso.matcher.ViewMatchers.withId;
  36. /**
  37. * Common base for all integration tests
  38. */
  39. @RunWith(AndroidJUnit4.class)
  40. public abstract class AbstractIT {
  41. protected static OwnCloudClient client;
  42. protected static Account account;
  43. protected static Context targetContext;
  44. @BeforeClass
  45. public static void beforeAll() {
  46. try {
  47. targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
  48. Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments();
  49. Uri baseUrl = Uri.parse(arguments.getString("TEST_SERVER_URL"));
  50. String loginName = arguments.getString("TEST_SERVER_USERNAME");
  51. String password = arguments.getString("TEST_SERVER_PASSWORD");
  52. Account temp = new Account(loginName + "@" + baseUrl, MainApp.getAccountType(targetContext));
  53. UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
  54. if (!accountManager.exists(temp)) {
  55. AccountManager platformAccountManager = AccountManager.get(targetContext);
  56. platformAccountManager.addAccountExplicitly(temp, password, null);
  57. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION,
  58. Integer.toString(UserAccountManager.ACCOUNT_VERSION));
  59. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_VERSION, "14.0.0.0");
  60. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, baseUrl.toString());
  61. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName); // same as userId
  62. }
  63. final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
  64. account = userAccountManager.getAccountByName(loginName + "@" + baseUrl);
  65. if (account == null) {
  66. throw new ActivityNotFoundException();
  67. }
  68. client = OwnCloudClientFactory.createOwnCloudClient(account, targetContext);
  69. createDummyFiles();
  70. waitForServer(client, baseUrl);
  71. } catch (OperationCanceledException e) {
  72. e.printStackTrace();
  73. } catch (AuthenticatorException e) {
  74. e.printStackTrace();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. } catch (AccountUtils.AccountNotFoundException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. FileDataStorageManager getStorageManager() {
  82. return new FileDataStorageManager(account, targetContext.getContentResolver());
  83. }
  84. private static void createDummyFiles() throws IOException {
  85. new File(FileStorageUtils.getSavePath(account.name)).mkdirs();
  86. createFile("empty.txt", 0);
  87. createFile("nonEmpty.txt", 100);
  88. createFile("chunkedFile.txt", 500000);
  89. }
  90. private static void createFile(String name, int iteration) throws IOException {
  91. File file = new File(FileStorageUtils.getSavePath(account.name) + File.separator + name);
  92. file.createNewFile();
  93. FileWriter writer = new FileWriter(file);
  94. for (int i = 0; i < iteration; i++) {
  95. writer.write("123123123123123123123123123\n");
  96. }
  97. writer.flush();
  98. writer.close();
  99. }
  100. private static void waitForServer(OwnCloudClient client, Uri baseUrl) {
  101. GetMethod get = new GetMethod(baseUrl + "/status.php");
  102. try {
  103. int i = 0;
  104. while (client.executeMethod(get) != HttpStatus.SC_OK && i < 3) {
  105. System.out.println("wait…");
  106. Thread.sleep(60 * 1000);
  107. i++;
  108. }
  109. if (i == 3) {
  110. Assert.fail("Server not ready!");
  111. }
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. } catch (InterruptedException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. protected File getFile(String filename) throws IOException {
  119. InputStream inputStream = getInstrumentation().getContext().getAssets().open(filename);
  120. File temp = File.createTempFile("file", "file");
  121. FileUtils.copyInputStreamToFile(inputStream, temp);
  122. return temp;
  123. }
  124. protected void waitForIdleSync() {
  125. InstrumentationRegistry.getInstrumentation().waitForIdleSync();
  126. }
  127. protected void openDrawer(IntentsTestRule activityRule) throws InterruptedException {
  128. Activity sut = activityRule.launchActivity(null);
  129. Thread.sleep(3000);
  130. onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
  131. waitForIdleSync();
  132. Screenshot.snapActivity(sut).record();
  133. }
  134. }