DialogFragmentIT.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Manifest;
  24. import android.accounts.Account;
  25. import android.accounts.AccountManager;
  26. import android.content.Intent;
  27. import android.os.Looper;
  28. import com.nextcloud.client.account.RegisteredUser;
  29. import com.nextcloud.client.account.Server;
  30. import com.nextcloud.ui.ChooseAccountDialogFragment;
  31. import com.owncloud.android.AbstractIT;
  32. import com.owncloud.android.MainApp;
  33. import com.owncloud.android.datamodel.OCFile;
  34. import com.owncloud.android.lib.common.OwnCloudAccount;
  35. import com.owncloud.android.lib.common.accounts.AccountUtils;
  36. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  37. import com.owncloud.android.ui.activity.FileDisplayActivity;
  38. import com.owncloud.android.utils.ScreenshotTest;
  39. import org.junit.Rule;
  40. import org.junit.Test;
  41. import java.net.URI;
  42. import java.util.ArrayList;
  43. import java.util.Objects;
  44. import androidx.fragment.app.DialogFragment;
  45. import androidx.test.espresso.intent.rule.IntentsTestRule;
  46. import androidx.test.rule.GrantPermissionRule;
  47. import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
  48. public class DialogFragmentIT extends AbstractIT {
  49. @Rule public IntentsTestRule<FileDisplayActivity> activityRule =
  50. new IntentsTestRule<>(FileDisplayActivity.class, true, false);
  51. @Rule
  52. public final GrantPermissionRule permissionRule = GrantPermissionRule.grant(
  53. Manifest.permission.WRITE_EXTERNAL_STORAGE);
  54. @Test
  55. @ScreenshotTest
  56. public void testRenameFileDialog() {
  57. if (Looper.myLooper() == null) {
  58. Looper.prepare();
  59. }
  60. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(new OCFile("/Test/"));
  61. showDialog(dialog);
  62. }
  63. @Test
  64. @ScreenshotTest
  65. public void testLoadingDialog() {
  66. LoadingDialog dialog = LoadingDialog.newInstance("Wait…");
  67. showDialog(dialog);
  68. }
  69. @Test
  70. @ScreenshotTest
  71. public void testRemoveFileDialog() {
  72. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(new OCFile("/Test.md"));
  73. showDialog(dialog);
  74. }
  75. @Test
  76. @ScreenshotTest
  77. public void testRemoveFilesDialog() {
  78. ArrayList<OCFile> toDelete = new ArrayList<>();
  79. toDelete.add(new OCFile("/Test.md"));
  80. toDelete.add(new OCFile("/Document.odt"));
  81. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(toDelete);
  82. showDialog(dialog);
  83. }
  84. @Test
  85. @ScreenshotTest
  86. public void testRemoveFolderDialog() {
  87. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(new OCFile("/Folder/"));
  88. showDialog(dialog);
  89. }
  90. @Test
  91. @ScreenshotTest
  92. public void testRemoveFoldersDialog() {
  93. ArrayList<OCFile> toDelete = new ArrayList<>();
  94. toDelete.add(new OCFile("/Folder/"));
  95. toDelete.add(new OCFile("/Documents/"));
  96. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(toDelete);
  97. showDialog(dialog);
  98. }
  99. @Test
  100. @ScreenshotTest
  101. public void testNewFolderDialog() {
  102. if (Looper.myLooper() == null) {
  103. Looper.prepare();
  104. }
  105. CreateFolderDialogFragment sut = CreateFolderDialogFragment.newInstance(new OCFile("/"));
  106. showDialog(sut);
  107. }
  108. @Test
  109. @ScreenshotTest
  110. public void testAccountChooserDialog() throws AccountUtils.AccountNotFoundException {
  111. AccountManager accountManager = AccountManager.get(targetContext);
  112. for (Account account : accountManager.getAccounts()) {
  113. accountManager.removeAccountExplicitly(account);
  114. }
  115. Account newAccount = new Account("test@https://server.com", MainApp.getAccountType(targetContext));
  116. accountManager.addAccountExplicitly(newAccount, "password", null);
  117. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_OC_BASE_URL, "https://server.com");
  118. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, "test");
  119. Account newAccount2 = new Account("user1@server.com", MainApp.getAccountType(targetContext));
  120. accountManager.addAccountExplicitly(newAccount2, "password", null);
  121. accountManager.setUserData(newAccount2, AccountUtils.Constants.KEY_OC_BASE_URL, "https://server.com");
  122. accountManager.setUserData(newAccount2, AccountUtils.Constants.KEY_USER_ID, "user1");
  123. accountManager.setUserData(newAccount2, AccountUtils.Constants.KEY_OC_VERSION, "19.0.0.0");
  124. ChooseAccountDialogFragment sut =
  125. ChooseAccountDialogFragment.newInstance(new RegisteredUser(newAccount,
  126. new OwnCloudAccount(newAccount, targetContext),
  127. new Server(URI.create("https://server.com"),
  128. OwnCloudVersion.nextcloud_19)));
  129. showDialog(sut);
  130. }
  131. private void showDialog(DialogFragment dialog) {
  132. Intent intent = new Intent(targetContext, FileDisplayActivity.class);
  133. FileDisplayActivity sut = activityRule.launchActivity(intent);
  134. dialog.show(sut.getSupportFragmentManager(), "");
  135. getInstrumentation().waitForIdleSync();
  136. shortSleep();
  137. screenshot(Objects.requireNonNull(dialog.requireDialog().getWindow()).getDecorView());
  138. }
  139. }