ConflictsResolveActivityIT.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. *
  3. * Nextcloud Android client application
  4. *
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2020 Tobias Kaminsky
  7. * Copyright (C) 2020 Nextcloud GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.ui.activity;
  23. import android.content.Intent;
  24. import com.nextcloud.client.account.UserAccountManagerImpl;
  25. import com.owncloud.android.AbstractIT;
  26. import com.owncloud.android.R;
  27. import com.owncloud.android.datamodel.FileDataStorageManager;
  28. import com.owncloud.android.datamodel.OCFile;
  29. import com.owncloud.android.db.OCUpload;
  30. import com.owncloud.android.ui.dialog.ConflictsResolveDialog;
  31. import com.owncloud.android.utils.FileStorageUtils;
  32. import com.owncloud.android.utils.ScreenshotTest;
  33. import org.junit.After;
  34. import org.junit.Rule;
  35. import org.junit.Test;
  36. import java.util.Objects;
  37. import androidx.fragment.app.DialogFragment;
  38. import androidx.test.espresso.intent.rule.IntentsTestRule;
  39. import static androidx.test.espresso.Espresso.onView;
  40. import static androidx.test.espresso.action.ViewActions.click;
  41. import static androidx.test.espresso.matcher.ViewMatchers.withId;
  42. import static androidx.test.espresso.matcher.ViewMatchers.withText;
  43. import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
  44. import static junit.framework.TestCase.assertTrue;
  45. import static org.junit.Assert.assertEquals;
  46. public class ConflictsResolveActivityIT extends AbstractIT {
  47. @Rule public IntentsTestRule<ConflictsResolveActivity> activityRule =
  48. new IntentsTestRule<>(ConflictsResolveActivity.class, true, false);
  49. private boolean returnCode;
  50. @Test
  51. @ScreenshotTest
  52. public void screenshotTextFiles() {
  53. OCFile newFile = new OCFile("/newFile.txt");
  54. newFile.setFileLength(56000);
  55. newFile.setModificationTimestamp(1522019340);
  56. newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt");
  57. OCFile existingFile = new OCFile("/newFile.txt");
  58. existingFile.setFileLength(1024000);
  59. existingFile.setModificationTimestamp(1582019340);
  60. FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver());
  61. storageManager.saveNewFile(existingFile);
  62. Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
  63. intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
  64. intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
  65. ConflictsResolveActivity sut = activityRule.launchActivity(intent);
  66. ConflictsResolveDialog dialog = ConflictsResolveDialog.newInstance(existingFile,
  67. newFile,
  68. UserAccountManagerImpl
  69. .fromContext(targetContext)
  70. .getUser()
  71. );
  72. dialog.showDialog(sut);
  73. getInstrumentation().waitForIdleSync();
  74. shortSleep();
  75. screenshot(Objects.requireNonNull(dialog.requireDialog().getWindow()).getDecorView());
  76. }
  77. // @Test
  78. // @ScreenshotTest // todo run without real server
  79. // public void screenshotImages() throws IOException {
  80. // FileDataStorageManager storageManager = new FileDataStorageManager(account,
  81. // targetContext.getContentResolver());
  82. //
  83. // OCFile newFile = new OCFile("/newFile.txt");
  84. // newFile.setFileLength(56000);
  85. // newFile.setModificationTimestamp(1522019340);
  86. // newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt");
  87. //
  88. // File image = getFile("image.jpg");
  89. //
  90. // assertTrue(new UploadFileRemoteOperation(image.getAbsolutePath(),
  91. // "/image.jpg",
  92. // "image/jpg",
  93. // "10000000").execute(client).isSuccess());
  94. //
  95. // assertTrue(new RefreshFolderOperation(storageManager.getFileByPath("/"),
  96. // System.currentTimeMillis(),
  97. // false,
  98. // true,
  99. // storageManager,
  100. // account,
  101. // targetContext
  102. // ).execute(client).isSuccess());
  103. //
  104. // OCFile existingFile = storageManager.getFileByPath("/image.jpg");
  105. //
  106. // Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
  107. // intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
  108. // intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
  109. //
  110. // ConflictsResolveActivity sut = activityRule.launchActivity(intent);
  111. //
  112. // ConflictsResolveDialog.OnConflictDecisionMadeListener listener = decision -> {
  113. //
  114. // };
  115. //
  116. // ConflictsResolveDialog dialog = ConflictsResolveDialog.newInstance(existingFile,
  117. // newFile,
  118. // UserAccountManagerImpl
  119. // .fromContext(targetContext)
  120. // .getUser()
  121. // );
  122. // dialog.showDialog(sut);
  123. // dialog.listener = listener;
  124. //
  125. // getInstrumentation().waitForIdleSync();
  126. // shortSleep();
  127. //
  128. // screenshot(Objects.requireNonNull(dialog.requireDialog().getWindow()).getDecorView());
  129. // }
  130. @Test
  131. public void cancel() {
  132. returnCode = false;
  133. OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt",
  134. "/newFile.txt",
  135. account.name);
  136. OCFile existingFile = new OCFile("/newFile.txt");
  137. existingFile.setFileLength(1024000);
  138. existingFile.setModificationTimestamp(1582019340);
  139. OCFile newFile = new OCFile("/newFile.txt");
  140. newFile.setFileLength(56000);
  141. newFile.setModificationTimestamp(1522019340);
  142. newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt");
  143. FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver());
  144. storageManager.saveNewFile(existingFile);
  145. Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
  146. intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
  147. intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
  148. intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
  149. ConflictsResolveActivity sut = activityRule.launchActivity(intent);
  150. sut.listener = decision -> {
  151. assertEquals(decision, ConflictsResolveDialog.Decision.CANCEL);
  152. returnCode = true;
  153. };
  154. getInstrumentation().waitForIdleSync();
  155. shortSleep();
  156. onView(withText("Cancel")).perform(click());
  157. assertTrue(returnCode);
  158. }
  159. @Test
  160. @ScreenshotTest
  161. public void keepExisting() {
  162. returnCode = false;
  163. OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt",
  164. "/newFile.txt",
  165. account.name);
  166. OCFile existingFile = new OCFile("/newFile.txt");
  167. existingFile.setFileLength(1024000);
  168. existingFile.setModificationTimestamp(1582019340);
  169. OCFile newFile = new OCFile("/newFile.txt");
  170. newFile.setFileLength(56000);
  171. newFile.setModificationTimestamp(1522019340);
  172. newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt");
  173. FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver());
  174. storageManager.saveNewFile(existingFile);
  175. Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
  176. intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
  177. intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
  178. intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
  179. ConflictsResolveActivity sut = activityRule.launchActivity(intent);
  180. sut.listener = decision -> {
  181. assertEquals(decision, ConflictsResolveDialog.Decision.KEEP_SERVER);
  182. returnCode = true;
  183. };
  184. getInstrumentation().waitForIdleSync();
  185. onView(withId(R.id.existing_checkbox)).perform(click());
  186. DialogFragment dialog = (DialogFragment) sut.getSupportFragmentManager().findFragmentByTag("conflictDialog");
  187. screenshot(Objects.requireNonNull(dialog.requireDialog().getWindow()).getDecorView());
  188. onView(withText("OK")).perform(click());
  189. assertTrue(returnCode);
  190. }
  191. @Test
  192. @ScreenshotTest
  193. public void keepNew() {
  194. returnCode = false;
  195. OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt",
  196. "/newFile.txt",
  197. account.name);
  198. OCFile existingFile = new OCFile("/newFile.txt");
  199. existingFile.setFileLength(1024000);
  200. existingFile.setModificationTimestamp(1582019340);
  201. existingFile.setRemoteId("123abc");
  202. OCFile newFile = new OCFile("/newFile.txt");
  203. newFile.setFileLength(56000);
  204. newFile.setModificationTimestamp(1522019340);
  205. newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt");
  206. FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver());
  207. storageManager.saveNewFile(existingFile);
  208. Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
  209. intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
  210. intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
  211. intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
  212. ConflictsResolveActivity sut = activityRule.launchActivity(intent);
  213. sut.listener = decision -> {
  214. assertEquals(decision, ConflictsResolveDialog.Decision.KEEP_LOCAL);
  215. returnCode = true;
  216. };
  217. getInstrumentation().waitForIdleSync();
  218. onView(withId(R.id.new_checkbox)).perform(click());
  219. DialogFragment dialog = (DialogFragment) sut.getSupportFragmentManager().findFragmentByTag("conflictDialog");
  220. screenshot(Objects.requireNonNull(dialog.requireDialog().getWindow()).getDecorView());
  221. onView(withText("OK")).perform(click());
  222. assertTrue(returnCode);
  223. }
  224. @Test
  225. @ScreenshotTest
  226. public void keepBoth() {
  227. returnCode = false;
  228. OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt",
  229. "/newFile.txt",
  230. account.name);
  231. OCFile existingFile = new OCFile("/newFile.txt");
  232. existingFile.setFileLength(1024000);
  233. existingFile.setModificationTimestamp(1582019340);
  234. OCFile newFile = new OCFile("/newFile.txt");
  235. newFile.setFileLength(56000);
  236. newFile.setModificationTimestamp(1522019340);
  237. newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt");
  238. FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver());
  239. storageManager.saveNewFile(existingFile);
  240. Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
  241. intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
  242. intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
  243. intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
  244. ConflictsResolveActivity sut = activityRule.launchActivity(intent);
  245. sut.listener = decision -> {
  246. assertEquals(decision, ConflictsResolveDialog.Decision.KEEP_BOTH);
  247. returnCode = true;
  248. };
  249. getInstrumentation().waitForIdleSync();
  250. onView(withId(R.id.existing_checkbox)).perform(click());
  251. onView(withId(R.id.new_checkbox)).perform(click());
  252. DialogFragment dialog = (DialogFragment) sut.getSupportFragmentManager().findFragmentByTag("conflictDialog");
  253. screenshot(Objects.requireNonNull(dialog.requireDialog().getWindow()).getDecorView());
  254. onView(withText("OK")).perform(click());
  255. assertTrue(returnCode);
  256. }
  257. @After
  258. public void after() {
  259. getStorageManager().deleteAllFiles();
  260. }
  261. }