fix sorting of synced folder: NPE if both enabled, but one has null folder name

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2019-10-15 09:56:55 +02:00
parent a730311486
commit 1b0d548c69
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
2 changed files with 194 additions and 2 deletions

View file

@ -297,14 +297,21 @@ public class SyncedFoldersActivity extends FileActivity implements SyncedFolderA
} else if (f2 == null) {
return 1;
} else if (f1.isEnabled() && f2.isEnabled()) {
if (f1.getFolderName() == null) {
return -1;
}
if (f2.getFolderName() == null) {
return 1;
}
return f1.getFolderName().toLowerCase(Locale.getDefault()).compareTo(
f2.getFolderName().toLowerCase(Locale.getDefault()));
} else if (f1.getFolderName() == null && f2.getFolderName() == null) {
return 0;
} else if (f1.isEnabled()) {
return -1;
} else if (f2.isEnabled()) {
return 1;
} else if (f1.getFolderName() == null && f2.getFolderName() == null) {
return 0;
} else if (f1.getFolderName() == null) {
return -1;
} else if (f2.getFolderName() == null) {

View file

@ -0,0 +1,185 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.owncloud.android.ui.activity;
import com.owncloud.android.datamodel.MediaFolderType;
import com.owncloud.android.datamodel.SyncedFolderDisplayItem;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import third_parties.daveKoeller.AlphanumComparator;
import static org.junit.Assert.assertTrue;
public class SyncedFoldersActivityTest {
@Test
public void regular() {
SyncedFolderDisplayItem[] sortedArray = {
create("Folder1", true),
create("Folder2", true),
};
assertTrue(sortAndTest(Arrays.asList(sortedArray)));
}
@Test
public void withNull() {
SyncedFolderDisplayItem[] sortedArray = {
null,
null,
create("Folder1", true),
create("Folder2", true),
};
assertTrue(sortAndTest(Arrays.asList(sortedArray)));
}
@Test
public void withNullAndEnableStatus() {
SyncedFolderDisplayItem[] sortedArray = {
null,
null,
create("Folder1", true),
create("Folder2", true),
create("Folder3", true),
create("Folder4", true),
create("Folder5", false),
create("Folder6", false),
create("Folder7", false),
create("Folder8", false),
};
assertTrue(sortAndTest(Arrays.asList(sortedArray)));
}
@Test
public void withNullFolderName() {
SyncedFolderDisplayItem[] sortedArray = {
null,
null,
create("Folder1", true),
create(null, false),
create("Folder2", false),
create("Folder3", false),
create("Folder4", false),
create("Folder5", false),
};
assertTrue(sortAndTest(Arrays.asList(sortedArray)));
}
@Test
public void withNullFolderNameAllEnabled() {
SyncedFolderDisplayItem[] sortedArray = {
null,
null,
create(null, true),
create("Folder1", true),
create("Folder2", true),
create("Folder3", true),
create("Folder4", true),
};
assertTrue(sortAndTest(Arrays.asList(sortedArray)));
}
private List<SyncedFolderDisplayItem> shuffle(List<SyncedFolderDisplayItem> list) {
List<SyncedFolderDisplayItem> shuffled = new ArrayList<>(list);
Collections.shuffle(shuffled);
return shuffled;
}
private boolean sortAndTest(List<SyncedFolderDisplayItem> sortedList) {
List<SyncedFolderDisplayItem> unsortedList = shuffle(sortedList);
return test(sortedList, SyncedFoldersActivity.sortSyncedFolderItems(unsortedList));
}
private List<SyncedFolderDisplayItem> sort(List<SyncedFolderDisplayItem> sortedList) {
List<SyncedFolderDisplayItem> unsortedList = shuffle(sortedList);
Collections.sort(unsortedList, new AlphanumComparator<>());
return unsortedList;
}
private boolean test(List<SyncedFolderDisplayItem> target, List<SyncedFolderDisplayItem> actual) {
for (int i = 0; i < target.size(); i++) {
boolean compare;
compare = target.get(i) == (actual.get(i));
if (!compare) {
System.out.println("target:");
for (SyncedFolderDisplayItem item : target) {
if (item == null) {
System.out.println("null");
} else {
System.out.println(item.getFolderName() + " " + item.isEnabled());
}
}
System.out.println();
System.out.println("actual:");
for (SyncedFolderDisplayItem item : actual) {
if (item == null) {
System.out.println("null");
} else {
System.out.println(item.getFolderName() + " " + item.isEnabled());
}
}
return false;
}
}
return true;
}
private SyncedFolderDisplayItem create(String folderName, Boolean enabled) {
return new SyncedFolderDisplayItem(1,
"localPath",
"remotePath",
true,
true,
true,
"test@nextcloud.com",
1,
enabled,
new ArrayList<String>(),
folderName,
2,
MediaFolderType.IMAGE);
}
}