DialogFragmentIT.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.dialog;
  23. import android.accounts.Account;
  24. import android.accounts.AccountManager;
  25. import android.content.Intent;
  26. import android.os.Looper;
  27. import android.view.View;
  28. import android.view.ViewGroup;
  29. import android.widget.TextView;
  30. import com.google.gson.Gson;
  31. import com.nextcloud.client.account.RegisteredUser;
  32. import com.nextcloud.client.account.Server;
  33. import com.nextcloud.client.device.DeviceInfo;
  34. import com.nextcloud.ui.ChooseAccountDialogFragment;
  35. import com.owncloud.android.AbstractIT;
  36. import com.owncloud.android.MainApp;
  37. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  38. import com.owncloud.android.datamodel.FileDataStorageManager;
  39. import com.owncloud.android.datamodel.OCFile;
  40. import com.owncloud.android.lib.common.Creator;
  41. import com.owncloud.android.lib.common.DirectEditing;
  42. import com.owncloud.android.lib.common.Editor;
  43. import com.owncloud.android.lib.common.OwnCloudAccount;
  44. import com.owncloud.android.lib.common.accounts.AccountUtils;
  45. import com.owncloud.android.lib.resources.status.CapabilityBooleanType;
  46. import com.owncloud.android.lib.resources.status.OCCapability;
  47. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  48. import com.owncloud.android.lib.resources.users.Status;
  49. import com.owncloud.android.lib.resources.users.StatusType;
  50. import com.owncloud.android.ui.activity.FileDisplayActivity;
  51. import com.owncloud.android.ui.fragment.OCFileListBottomSheetActions;
  52. import com.owncloud.android.ui.fragment.OCFileListBottomSheetDialog;
  53. import com.owncloud.android.utils.MimeTypeUtil;
  54. import com.owncloud.android.utils.ScreenshotTest;
  55. import org.junit.Rule;
  56. import org.junit.Test;
  57. import java.net.URI;
  58. import java.util.ArrayList;
  59. import java.util.Collections;
  60. import java.util.Objects;
  61. import androidx.fragment.app.DialogFragment;
  62. import androidx.test.espresso.intent.rule.IntentsTestRule;
  63. import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
  64. public class DialogFragmentIT extends AbstractIT {
  65. @Rule public IntentsTestRule<FileDisplayActivity> activityRule =
  66. new IntentsTestRule<>(FileDisplayActivity.class, true, false);
  67. @Test
  68. @ScreenshotTest
  69. public void testRenameFileDialog() {
  70. if (Looper.myLooper() == null) {
  71. Looper.prepare();
  72. }
  73. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(new OCFile("/Test/"));
  74. showDialog(dialog);
  75. }
  76. @Test
  77. @ScreenshotTest
  78. public void testLoadingDialog() {
  79. LoadingDialog dialog = LoadingDialog.newInstance("Wait…");
  80. showDialog(dialog);
  81. }
  82. @Test
  83. @ScreenshotTest
  84. public void testRemoveFileDialog() {
  85. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(new OCFile("/Test.md"));
  86. showDialog(dialog);
  87. }
  88. @Test
  89. @ScreenshotTest
  90. public void testRemoveFilesDialog() {
  91. ArrayList<OCFile> toDelete = new ArrayList<>();
  92. toDelete.add(new OCFile("/Test.md"));
  93. toDelete.add(new OCFile("/Document.odt"));
  94. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(toDelete);
  95. showDialog(dialog);
  96. }
  97. @Test
  98. @ScreenshotTest
  99. public void testRemoveFolderDialog() {
  100. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(new OCFile("/Folder/"));
  101. showDialog(dialog);
  102. }
  103. @Test
  104. @ScreenshotTest
  105. public void testRemoveFoldersDialog() {
  106. ArrayList<OCFile> toDelete = new ArrayList<>();
  107. toDelete.add(new OCFile("/Folder/"));
  108. toDelete.add(new OCFile("/Documents/"));
  109. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(toDelete);
  110. showDialog(dialog);
  111. }
  112. @Test
  113. @ScreenshotTest
  114. public void testNewFolderDialog() {
  115. if (Looper.myLooper() == null) {
  116. Looper.prepare();
  117. }
  118. CreateFolderDialogFragment sut = CreateFolderDialogFragment.newInstance(new OCFile("/"));
  119. showDialog(sut);
  120. }
  121. @Test
  122. @ScreenshotTest
  123. public void testAccountChooserDialog() throws AccountUtils.AccountNotFoundException {
  124. AccountManager accountManager = AccountManager.get(targetContext);
  125. for (Account account : accountManager.getAccountsByType(MainApp.getAccountType(targetContext))) {
  126. accountManager.removeAccountExplicitly(account);
  127. }
  128. Account newAccount = new Account("test@https://server.com", MainApp.getAccountType(targetContext));
  129. accountManager.addAccountExplicitly(newAccount, "password", null);
  130. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_OC_BASE_URL, "https://server.com");
  131. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, "test");
  132. Account newAccount2 = new Account("user1@server.com", MainApp.getAccountType(targetContext));
  133. accountManager.addAccountExplicitly(newAccount2, "password", null);
  134. accountManager.setUserData(newAccount2, AccountUtils.Constants.KEY_OC_BASE_URL, "https://server.com");
  135. accountManager.setUserData(newAccount2, AccountUtils.Constants.KEY_USER_ID, "user1");
  136. accountManager.setUserData(newAccount2, AccountUtils.Constants.KEY_OC_VERSION, "20.0.0");
  137. FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(newAccount,
  138. targetContext.getContentResolver());
  139. OCCapability capability = new OCCapability();
  140. capability.setUserStatus(CapabilityBooleanType.TRUE);
  141. capability.setUserStatusSupportsEmoji(CapabilityBooleanType.TRUE);
  142. fileDataStorageManager.saveCapabilities(capability);
  143. ChooseAccountDialogFragment sut =
  144. ChooseAccountDialogFragment.newInstance(new RegisteredUser(newAccount,
  145. new OwnCloudAccount(newAccount, targetContext),
  146. new Server(URI.create("https://server.com"),
  147. OwnCloudVersion.nextcloud_20)));
  148. FileDisplayActivity activity = showDialog(sut);
  149. activity.runOnUiThread(() -> sut.setStatus(new Status(StatusType.DND,
  150. "Busy fixing 🐛…",
  151. "",
  152. -1),
  153. targetContext));
  154. shortSleep();
  155. screenshot(sut, "dnd");
  156. activity.runOnUiThread(() -> sut.setStatus(new Status(StatusType.ONLINE,
  157. "",
  158. "",
  159. -1),
  160. targetContext));
  161. shortSleep();
  162. screenshot(sut, "online");
  163. activity.runOnUiThread(() -> sut.setStatus(new Status(StatusType.ONLINE,
  164. "Let's have some fun",
  165. "🎉",
  166. -1),
  167. targetContext));
  168. shortSleep();
  169. screenshot(sut, "fun");
  170. activity.runOnUiThread(() -> sut.setStatus(new Status(StatusType.OFFLINE, "", "", -1), targetContext));
  171. shortSleep();
  172. screenshot(sut, "offline");
  173. activity.runOnUiThread(() -> sut.setStatus(new Status(StatusType.AWAY, "Vacation", "🌴", -1), targetContext));
  174. shortSleep();
  175. screenshot(sut, "away");
  176. }
  177. @Test
  178. @ScreenshotTest
  179. public void testAccountChooserDialogWithStatusDisabled() throws AccountUtils.AccountNotFoundException {
  180. AccountManager accountManager = AccountManager.get(targetContext);
  181. for (Account account : accountManager.getAccounts()) {
  182. accountManager.removeAccountExplicitly(account);
  183. }
  184. Account newAccount = new Account("test@https://server.com", MainApp.getAccountType(targetContext));
  185. accountManager.addAccountExplicitly(newAccount, "password", null);
  186. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_OC_BASE_URL, "https://server.com");
  187. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, "test");
  188. FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(newAccount,
  189. targetContext.getContentResolver());
  190. OCCapability capability = new OCCapability();
  191. capability.setUserStatus(CapabilityBooleanType.FALSE);
  192. fileDataStorageManager.saveCapabilities(capability);
  193. ChooseAccountDialogFragment sut =
  194. ChooseAccountDialogFragment.newInstance(new RegisteredUser(newAccount,
  195. new OwnCloudAccount(newAccount, targetContext),
  196. new Server(URI.create("https://server.com"),
  197. OwnCloudVersion.nextcloud_20)));
  198. showDialog(sut);
  199. }
  200. @Test
  201. @ScreenshotTest
  202. public void testBottomSheet() {
  203. if (Looper.myLooper() == null) {
  204. Looper.prepare();
  205. }
  206. OCFileListBottomSheetActions action = new OCFileListBottomSheetActions() {
  207. @Override
  208. public void createFolder() {
  209. }
  210. @Override
  211. public void uploadFromApp() {
  212. }
  213. @Override
  214. public void uploadFiles() {
  215. }
  216. @Override
  217. public void newDocument() {
  218. }
  219. @Override
  220. public void newSpreadsheet() {
  221. }
  222. @Override
  223. public void newPresentation() {
  224. }
  225. @Override
  226. public void directCameraUpload() {
  227. }
  228. @Override
  229. public void showTemplate(Creator creator, String headline) {
  230. }
  231. @Override
  232. public void createRichWorkspace() {
  233. }
  234. };
  235. DeviceInfo info = new DeviceInfo();
  236. OCFile ocFile = new OCFile("/test.md");
  237. Intent intent = new Intent(targetContext, FileDisplayActivity.class);
  238. FileDisplayActivity fda = activityRule.launchActivity(intent);
  239. // add direct editing info
  240. DirectEditing directEditing = new DirectEditing();
  241. directEditing.creators.put("1", new Creator("1",
  242. "text",
  243. "text file",
  244. ".md",
  245. "application/octet-stream",
  246. false));
  247. directEditing.creators.put("2", new Creator("2",
  248. "md",
  249. "markdown file",
  250. ".md",
  251. "application/octet-stream",
  252. false));
  253. directEditing.editors.put("text",
  254. new Editor("1",
  255. "Text",
  256. new ArrayList<>(Collections.singletonList(MimeTypeUtil.MIMETYPE_TEXT_MARKDOWN)),
  257. new ArrayList<>(),
  258. false));
  259. String json = new Gson().toJson(directEditing);
  260. new ArbitraryDataProvider(targetContext.getContentResolver()).storeOrUpdateKeyValue(user.getAccountName(),
  261. ArbitraryDataProvider.DIRECT_EDITING,
  262. json);
  263. // activate templates
  264. OCCapability capability = fda.getCapabilities();
  265. capability.setRichDocuments(CapabilityBooleanType.TRUE);
  266. capability.setRichDocumentsDirectEditing(CapabilityBooleanType.TRUE);
  267. capability.setRichDocumentsTemplatesAvailable(CapabilityBooleanType.TRUE);
  268. OCFileListBottomSheetDialog sut = new OCFileListBottomSheetDialog(fda,
  269. action,
  270. info,
  271. user,
  272. ocFile);
  273. fda.runOnUiThread(sut::show);
  274. waitForIdleSync();
  275. screenshot(sut.getWindow().getDecorView());
  276. }
  277. private FileDisplayActivity showDialog(DialogFragment dialog) {
  278. Intent intent = new Intent(targetContext, FileDisplayActivity.class);
  279. FileDisplayActivity sut = activityRule.launchActivity(intent);
  280. dialog.show(sut.getSupportFragmentManager(), "");
  281. getInstrumentation().waitForIdleSync();
  282. shortSleep();
  283. ViewGroup viewGroup = dialog.requireDialog().getWindow().findViewById(android.R.id.content);
  284. hideCursors(viewGroup);
  285. screenshot(Objects.requireNonNull(dialog.requireDialog().getWindow()).getDecorView());
  286. return sut;
  287. }
  288. private void hideCursors(ViewGroup viewGroup) {
  289. for (int i = 0; i < viewGroup.getChildCount(); i++) {
  290. View child = viewGroup.getChildAt(i);
  291. if (child instanceof ViewGroup) {
  292. hideCursors((ViewGroup) child);
  293. }
  294. if (child instanceof TextView) {
  295. ((TextView) child).setCursorVisible(false);
  296. }
  297. }
  298. }
  299. }