DownloadIT.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2020 Tobias Kaminsky
  6. * Copyright (C) 2020 Nextcloud GmbH
  7. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  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 <http://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android;
  23. import android.net.Uri;
  24. import com.owncloud.android.datamodel.OCFile;
  25. import com.owncloud.android.db.OCUpload;
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  27. import com.owncloud.android.operations.DownloadFileOperation;
  28. import com.owncloud.android.operations.RefreshFolderOperation;
  29. import com.owncloud.android.operations.RemoveFileOperation;
  30. import com.owncloud.android.utils.FileStorageUtils;
  31. import org.junit.After;
  32. import org.junit.Test;
  33. import java.io.File;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertNotNull;
  36. import static org.junit.Assert.assertNotSame;
  37. import static org.junit.Assert.assertTrue;
  38. /**
  39. * Tests related to file uploads
  40. */
  41. public class DownloadIT extends AbstractOnServerIT {
  42. private static final String FOLDER = "/testUpload/";
  43. @After
  44. public void after() {
  45. RemoteOperationResult result = new RefreshFolderOperation(getStorageManager().getFileByPath("/"),
  46. System.currentTimeMillis() / 1000L,
  47. false,
  48. true,
  49. getStorageManager(),
  50. user,
  51. targetContext)
  52. .execute(client);
  53. // cleanup only if folder exists
  54. if (result.isSuccess() && getStorageManager().getFileByDecryptedRemotePath(FOLDER) != null) {
  55. new RemoveFileOperation(getStorageManager().getFileByDecryptedRemotePath(FOLDER),
  56. false,
  57. user,
  58. false,
  59. targetContext,
  60. getStorageManager())
  61. .execute(client);
  62. }
  63. }
  64. @Test
  65. public void verifyDownload() {
  66. OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt",
  67. FOLDER + "nonEmpty.txt",
  68. account.name);
  69. uploadOCUpload(ocUpload);
  70. OCUpload ocUpload2 = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/nonEmpty.txt",
  71. FOLDER + "nonEmpty2.txt",
  72. account.name);
  73. uploadOCUpload(ocUpload2);
  74. refreshFolder("/");
  75. refreshFolder(FOLDER);
  76. OCFile file1 = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty.txt");
  77. OCFile file2 = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty2.txt");
  78. verifyDownload(file1, file2);
  79. assertTrue(new DownloadFileOperation(user, file1, targetContext).execute(client).isSuccess());
  80. assertTrue(new DownloadFileOperation(user, file2, targetContext).execute(client).isSuccess());
  81. refreshFolder(FOLDER);
  82. file1 = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty.txt");
  83. file2 = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty2.txt");
  84. verifyDownload(file1, file2);
  85. }
  86. private void verifyDownload(OCFile file1, OCFile file2) {
  87. assertNotNull(file1);
  88. assertNotNull(file2);
  89. assertNotSame(file1.getStoragePath(), file2.getStoragePath());
  90. assertTrue(new File(file1.getStoragePath()).exists());
  91. assertTrue(new File(file2.getStoragePath()).exists());
  92. // test against hardcoded path to make sure that it is correct
  93. assertEquals("/storage/emulated/0/Android/media/com.nextcloud.client/nextcloud/" +
  94. Uri.encode(account.name, "@") + "/testUpload/nonEmpty.txt",
  95. file1.getStoragePath());
  96. assertEquals("/storage/emulated/0/Android/media/com.nextcloud.client/nextcloud/" +
  97. Uri.encode(account.name, "@") + "/testUpload/nonEmpty2.txt",
  98. file2.getStoragePath());
  99. }
  100. }