add unit tests for status filter predicate

This commit is contained in:
sk 2023-05-26 17:02:39 +02:00
parent ab7489a049
commit 6abfe6ddd7
3 changed files with 107 additions and 13 deletions

View File

@ -0,0 +1,81 @@
package org.joinmastodon.android.utils;
import static org.joinmastodon.android.model.Filter.FilterAction.*;
import static org.joinmastodon.android.model.Filter.FilterContext.*;
import static org.junit.Assert.*;
import org.joinmastodon.android.model.Filter;
import org.joinmastodon.android.model.Status;
import org.junit.Test;
import java.time.Instant;
import java.util.EnumSet;
import java.util.List;
public class StatusFilterPredicateTest {
private static final Filter hideMeFilter = new Filter(), warnMeFilter = new Filter();
private static final List<Filter> allFilters = List.of(hideMeFilter, warnMeFilter);
private static final Status
hideInHomePublic = Status.ofFake(null, "hide me, please", Instant.now()),
warnInHomePublic = Status.ofFake(null, "display me with a warning", Instant.now());
static {
hideMeFilter.phrase = "hide me";
hideMeFilter.filterAction = HIDE;
hideMeFilter.context = EnumSet.of(PUBLIC, HOME);
warnMeFilter.phrase = "warning";
warnMeFilter.filterAction = WARN;
warnMeFilter.context = EnumSet.of(PUBLIC, HOME);
}
@Test
public void testHide() {
assertFalse("should not pass because matching filter applies to given context",
new StatusFilterPredicate(allFilters, HOME).test(hideInHomePublic));
}
@Test
public void testHideRegardlessOfContext() {
assertTrue("filters without context should always pass",
new StatusFilterPredicate(allFilters, null).test(hideInHomePublic));
}
@Test
public void testHideInDifferentContext() {
assertTrue("should pass because matching filter does not apply to given context",
new StatusFilterPredicate(allFilters, THREAD).test(hideInHomePublic));
}
@Test
public void testHideWithWarningText() {
assertTrue("should pass because matching filter is for warnings",
new StatusFilterPredicate(allFilters, HOME).test(warnInHomePublic));
}
@Test
public void testWarn() {
assertFalse("should not pass because filter applies to given context",
new StatusFilterPredicate(allFilters, HOME, WARN).test(warnInHomePublic));
}
@Test
public void testWarnRegardlessOfContext() {
assertTrue("filters without context should always pass",
new StatusFilterPredicate(allFilters, null, WARN).test(warnInHomePublic));
}
@Test
public void testWarnInDifferentContext() {
assertTrue("should pass because filter does not apply to given context",
new StatusFilterPredicate(allFilters, THREAD, WARN).test(warnInHomePublic));
}
@Test
public void testWarnWithHideText() {
assertTrue("should pass because matching filter is for hiding",
new StatusFilterPredicate(allFilters, HOME, WARN).test(hideInHomePublic));
}
}

View File

@ -50,6 +50,8 @@ public class Status extends BaseModel implements DisplayItemsParent, Searchable{
public long favouritesCount; public long favouritesCount;
public long repliesCount; public long repliesCount;
public Instant editedAt; public Instant editedAt;
// might not be provided (by older mastodon servers),
// so megalodon will use the locally cached filters if filtered == null
public List<FilterResult> filtered; public List<FilterResult> filtered;
public String url; public String url;
@ -180,7 +182,6 @@ public class Status extends BaseModel implements DisplayItemsParent, Searchable{
s.mentions = List.of(); s.mentions = List.of();
s.tags = List.of(); s.tags = List.of();
s.emojis = List.of(); s.emojis = List.of();
s.filtered = List.of();
return s; return s;
} }

View File

@ -17,11 +17,28 @@ public class StatusFilterPredicate implements Predicate<Status>{
/** /**
* @param context null makes the predicate pass automatically * @param context null makes the predicate pass automatically
* @param action defines what the predicate should check:
* status should not be hidden or should not display with warning
*/ */
public StatusFilterPredicate(List<Filter> filters, Filter.FilterContext context){ public StatusFilterPredicate(List<Filter> filters, Filter.FilterContext context, Filter.FilterAction action){
this.filters=filters; this.filters = filters;
this.context = context; this.context = context;
this.action = Filter.FilterAction.HIDE; this.action = action;
}
public StatusFilterPredicate(List<Filter> filters, Filter.FilterContext context){
this(filters, context, Filter.FilterAction.HIDE);
}
/**
* @param context null makes the predicate pass automatically
* @param action defines what the predicate should check:
* status should not be hidden or should not display with warning
*/
public StatusFilterPredicate(String accountID, Filter.FilterContext context, Filter.FilterAction action){
filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(context)).collect(Collectors.toList());
this.context = context;
this.action = action;
} }
/** /**
@ -32,16 +49,11 @@ public class StatusFilterPredicate implements Predicate<Status>{
} }
/** /**
* @param context null makes the predicate pass automatically * @return whether the status should be displayed without being hidden/warned about.
* @param action defines what the predicate should check: * will always return true if the context is null.
* should not be hidden or should not display with warning * true = display this status,
* false = filter this status
*/ */
public StatusFilterPredicate(String accountID, Filter.FilterContext context, Filter.FilterAction action){
filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(context)).collect(Collectors.toList());
this.context = context;
this.action = action;
}
@Override @Override
public boolean test(Status status){ public boolean test(Status status){
if (context == null) return true; if (context == null) return true;