Skip to content

Commit

Permalink
Fix inverted hiding condition in IndexWindowFilter
Browse files Browse the repository at this point in the history
Issue: fixes #379
  • Loading branch information
mlopatkin committed May 15, 2024
1 parent bfea9b1 commit b068f03
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ public int hashCode() {
public boolean equals(Object obj) {
return super.equals(obj) && obj instanceof ToggleFilter;
}

public static ToggleFilter show(Predicate<? super LogRecord> predicate) {
return new ToggleFilter(FilteringMode.SHOW, true, predicate);
}

public static ToggleFilter hide(Predicate<? super LogRecord> predicate) {
return new ToggleFilter(FilteringMode.HIDE, true, predicate);
}

public static ToggleFilter index(Predicate<? super LogRecord> predicate) {
return new ToggleFilter(FilteringMode.WINDOW, true, predicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected HideFilter copy(boolean enabled) {

@Override
public boolean test(LogRecord logRecord) {
return filterPredicate.test(logRecord);
return !filterPredicate.test(logRecord);
}
}

Expand Down
19 changes: 2 additions & 17 deletions test/name/mlopatkin/andlogview/filters/FilterChainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package name.mlopatkin.andlogview.filters;

import static name.mlopatkin.andlogview.filters.FilteringMode.HIDE;
import static name.mlopatkin.andlogview.filters.FilteringMode.SHOW;
import static name.mlopatkin.andlogview.filters.ToggleFilter.hide;
import static name.mlopatkin.andlogview.filters.ToggleFilter.show;
import static name.mlopatkin.andlogview.test.TestData.MATCH_ALL;
import static name.mlopatkin.andlogview.test.TestData.MATCH_FIRST;
import static name.mlopatkin.andlogview.test.TestData.RECORD1;
Expand All @@ -25,13 +25,9 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import name.mlopatkin.andlogview.logmodel.LogRecord;

import org.junit.Before;
import org.junit.Test;

import java.util.function.Predicate;

public class FilterChainTest {
private FilterChain chain;

Expand Down Expand Up @@ -98,15 +94,4 @@ public void testSetEnabled() throws Exception {
assertTrue(chain.shouldShow(RECORD2));
}

private static PredicateFilter show(Predicate<LogRecord> p) {
return filter(SHOW, p);
}

private static PredicateFilter hide(Predicate<LogRecord> p) {
return filter(HIDE, p);
}

private static PredicateFilter filter(FilteringMode mode, Predicate<LogRecord> p) {
return new ToggleFilter(mode, true, p);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2024 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package name.mlopatkin.andlogview.ui.filterdialog;

import static name.mlopatkin.andlogview.filters.ToggleFilter.show;
import static name.mlopatkin.andlogview.logmodel.LogRecordUtils.forTag;

import static org.assertj.core.api.Assertions.assertThat;

import name.mlopatkin.andlogview.filters.FilterChain;
import name.mlopatkin.andlogview.filters.FilteringMode;
import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.utils.Try;

import com.google.common.collect.ImmutableList;

import org.junit.jupiter.api.Test;

import java.util.function.Predicate;

class IndexWindowFilterTest {
@Test
void modelShowsOnlyMatchingRecords() {
var filter = createFilterForTags("ActivityManager");
assertThat(asPredicate(filter))
.accepts(forTag("ActivityManager"))
.rejects(forTag("PackageManager"));
}

@Test
void nonMatchingRecordsCannotReappearBecauseOfChildFilters() {
var filter = createFilterForTags("ActivityManager");
filter.getFilters()
.addFilter(show(r -> true));
assertThat(asPredicate(filter))
.accepts(forTag("ActivityManager"))
.rejects(forTag("PackageManager"));
}

private IndexWindowFilter createFilterForTags(String... tags) {
return Try.ofCallable(
() -> new IndexWindowFilter(true,
new FilterFromDialogData()
.setMode(FilteringMode.WINDOW)
.setTags(ImmutableList.copyOf(tags))))
.get();
}

private Predicate<LogRecord> asPredicate(IndexWindowFilter filter) {
var filterChain = new FilterChain();
filterChain.setModel(filter.getFilters());
return filterChain::shouldShow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package name.mlopatkin.andlogview.ui.filters;

import static name.mlopatkin.andlogview.filters.ToggleFilter.hide;
import static name.mlopatkin.andlogview.filters.ToggleFilter.index;
import static name.mlopatkin.andlogview.filters.ToggleFilter.show;

import static org.assertj.core.api.Assertions.assertThat;

import name.mlopatkin.andlogview.filters.ColoringToggleFilter;
import name.mlopatkin.andlogview.filters.Filter;
import name.mlopatkin.andlogview.filters.FilterModel;
import name.mlopatkin.andlogview.filters.FilteringMode;
import name.mlopatkin.andlogview.filters.ToggleFilter;
import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.logmodel.LogRecordPredicates;
import name.mlopatkin.andlogview.logmodel.LogRecordUtils;
Expand Down Expand Up @@ -93,22 +95,10 @@ void skipsUnsupportedFiltersUponLoad() {
assertThat(filter.getHighlightColor(recordWithTag("PackageManager"))).isNull();
}

private Filter show(Predicate<? super LogRecord> predicate) {
return new ToggleFilter(FilteringMode.SHOW, true, predicate);
}

private Filter hide(Predicate<? super LogRecord> predicate) {
return new ToggleFilter(FilteringMode.HIDE, true, predicate);
}

private Filter color(Color color, Predicate<? super LogRecord> predicate) {
return new ColoringToggleFilter(color, true, predicate);
}

private Filter index(Predicate<? super LogRecord> predicate) {
return new ToggleFilter(FilteringMode.WINDOW, true, predicate);
}

private FilterModel createModel(Filter... filters) {
return FilterModel.create(Arrays.asList(filters));
}
Expand Down

0 comments on commit b068f03

Please sign in to comment.