package org.mozilla.focus.webkit.matcher;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mozilla.focus.webkit.matcher.Trie.WhiteListTrie;
import org.mozilla.focus.webkit.matcher.util.FocusString;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.*;

@RunWith(RobolectricTestRunner.class)
public class TrieTest {

    @Test
    public void findNode() throws Exception {
        final Trie trie = Trie.createRootNode();

        assertNull(trie.findNode(FocusString.create("hello")));

        final Trie putNode = trie.put(FocusString.create("hello"));
        final Trie foundNode = trie.findNode(FocusString.create("hello"));

        assertNotNull(putNode);
        assertNotNull(foundNode);
        assertEquals(putNode, foundNode);

        // Substring matching: only works in one direction
        assertNull(trie.findNode(FocusString.create("hell")));
        assertNotNull(trie.findNode(FocusString.create("hellohello")));

        trie.put(FocusString.create("hellohello"));

        // Ensure both old and new overlapping strings can still be found
        assertNotNull(trie.findNode(FocusString.create("hello")));
        assertNotNull(trie.findNode(FocusString.create("hellohello")));
        assertNull(trie.findNode(FocusString.create("hell")));
        assertNull(trie.findNode(FocusString.create("hella")));
    }

    @Test
    public void testWhiteListTrie() {
        final WhiteListTrie trie;

        {
            final Trie whitelist = Trie.createRootNode();

            whitelist.put(FocusString.create("abc"));

            trie = WhiteListTrie.createRootNode();
            trie.putWhiteList(FocusString.create("def"), whitelist);
        }

        assertNull(trie.findNode(FocusString.create("abc")));

        // In practice EntityList uses it's own search in order to cover all possible matching notes
        // (e.g. in case we have separate whitelists for mozilla.org and foo.mozilla.org), however
        // we don't need to test that here yet.
        final WhiteListTrie foundWhitelist = (WhiteListTrie) trie.findNode(FocusString.create("def"));
        assertNotNull(foundWhitelist);

        assertNotNull(foundWhitelist.whitelist.findNode(FocusString.create("abc")));
    }
}
@@ -814,35 +814,67 @@
 ng: 
-only works in one direction
+doesn't happen (except for subdomains, we test those later)
 %0A   
@@ -940,35 +940,32 @@
 %0A        assertN
-otN
 ull(trie.findNod
@@ -987,29 +987,25 @@
 reate(%22hello
-hello
+p
 %22)));%0A%0A     
@@ -1256,32 +1256,69 @@
 %22hellohello%22)));
+%0A%0A        // These still don't match:
 %0A        assertN
@@ -1426,10 +1426,544 @@
 hell
-a%22
+op%22)));%0A%0A        // Domain specific / partial domain tests:%0A        trie.put(FocusString.create(%22foo.com%22).reverse());%0A%0A        // Domain and subdomain can be found%0A        assertNotNull(trie.findNode(FocusString.create(%22foo.com%22).reverse()));%0A        assertNotNull(trie.findNode(FocusString.create(%22bar.foo.com%22).reverse()));%0A        // But other domains with some overlap don't match%0A        assertNull(trie.findNode(FocusString.create(%22bar-foo.com%22).reverse()));%0A        assertNull(trie.findNode(FocusString.create(%22oo.com%22).reverse(
 )));

Update Trie tests to reflect partial domain fixes (#360)