/*
 * Copyright 2015 Johan Walles <johan.walles@gmail.com>
 *
 * 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 com.gmail.walles.johan.exactype;

import android.view.inputmethod.InputConnection;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

public class ExactypeOnDeleteHeldTest {
    private static class TestableExactype extends Exactype {
        private final InputConnection inputConnection;

        public TestableExactype(InputConnection inputConnection) {
            this.inputConnection = inputConnection;
        }

        @Override
        public InputConnection getCurrentInputConnection() {
            return inputConnection;
        }
    }

    private String deleteWord(String before) {
        InputConnection inputConnection = Mockito.mock(InputConnection.class);
        Mockito.stub(inputConnection.getSelectedText(0)).toReturn(null);

        Exactype exactype = new TestableExactype(inputConnection);
        exactype.onDeleteHeld();

        // Capture the parameters passed to deleteSurroundingText()
        ArgumentCaptor<Integer> beforeLength = ArgumentCaptor.forClass(Integer.class);
        Mockito.verify(inputConnection).deleteSurroundingText(
            beforeLength.capture(), Mockito.eq(0));

        String after = before.substring(0, before.length() - beforeLength.getValue());
        return after;
    }

    @Test
    public void testEmpty() {
        Assert.assertEquals("", deleteWord(""));
    }

    @Test
    public void testWithSelectedText() {
        InputConnection inputConnection = Mockito.mock(InputConnection.class);
        Mockito.stub(inputConnection.getSelectedText(0)).toReturn(null);

        Exactype exactype = new TestableExactype(inputConnection);
        exactype.onDeleteHeld();

        Mockito.verify(inputConnection).commitText("", 1);
    }
}

@@ -817,16 +817,100 @@
 Mockito;
+%0Aimport org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer;
 %0A%0Apublic
@@ -1352,16 +1352,22 @@
 eteWord(
+final 
 String b
@@ -1518,32 +1518,535 @@
 .toReturn(null);
+%0A        Mockito.when(%0A            inputConnection.getTextBeforeCursor(Mockito.anyInt(), Mockito.eq(0)))%0A            .thenAnswer(new Answer%3CString%3E()%0A            %7B%0A            @Override%0A            public String answer(InvocationOnMock invocation) %7B%0A                int n = (Integer)invocation.getArguments()%5B0%5D;%0A                if (n %3E before.length()) %7B%0A                    n = before.length();%0A                %7D%0A%0A                return before.substring(before.length() - n);%0A            %7D%0A        %7D);
 %0A%0A        Exacty
@@ -2511,32 +2511,459 @@
 n after;%0A    %7D%0A%0A
+    /**%0A     * After deleting a word, the cursor should be where the just-deleted word used to start.%0A     * %3Cp%3E%0A     * Basically we should divide the text into blocks of alphanumeric characters and%0A     * non-alphanumeric characters. Then, if the text ends with an alphanumeric block, delete that%0A     * one only. If the text ends with a non-alphanumeric block, delete that and the block before%0A     * it.%0A     * %3C/p%3E%0A     */%0A
     @Test%0A    pu
@@ -2980,13 +2980,18 @@
 test
-Empty
+DeleteWord
 () %7B
@@ -3039,16 +3039,961 @@
 rd(%22%22));
+%0A%0A        Assert.assertEquals(%22%22, deleteWord(%22a%22));%0A        Assert.assertEquals(%22%22, deleteWord(%22=%22));%0A%0A        Assert.assertEquals(%22%22, deleteWord(%22a %22));%0A        Assert.assertEquals(%22%22, deleteWord(%22= %22));%0A%0A        Assert.assertEquals(%22%22, deleteWord(%22aa%22));%0A        Assert.assertEquals(%22%22, deleteWord(%22aa %22));%0A%0A        Assert.assertEquals(%22a %22, deleteWord(%22a b%22));%0A        Assert.assertEquals(%22a %22, deleteWord(%22a b %22));%0A        Assert.assertEquals(%22a %22, deleteWord(%22a b  %22));%0A%0A        Assert.assertEquals(%22aa %22, deleteWord(%22aa bb%22));%0A        Assert.assertEquals(%22aa %22, deleteWord(%22aa bb %22));%0A        Assert.assertEquals(%22aa %22, deleteWord(%22aa bb  %22));%0A%0A        Assert.assertEquals(%22aa  %22, deleteWord(%22aa  bb%22));%0A        Assert.assertEquals(%22aa  %22, deleteWord(%22aa  bb %22));%0A        Assert.assertEquals(%22aa  %22, deleteWord(%22aa  bb  %22));%0A%0A        Assert.assertEquals(%2254.%22, deleteWord(%2254.32%22));%0A        Assert.assertEquals(%2254,%22, deleteWord(%2254,32%22));
 %0A    %7D%0A%0A
@@ -4026,12 +4026,14 @@
 test
-With
+Delete
 Sele

Complete the delete-word tests