//   Copyright 2012 Georg-August-Universität Göttingen, Germany
//
//   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 de.ugoe.cs.autoquest.usability.rules.patterns.visitors.endswith;

import static de.ugoe.cs.autoquest.usability.tasktree.filters.EventTypeFilter.MOUSE_CLICK;
import static de.ugoe.cs.autoquest.usability.tasktree.filters.EventTypeFilter.TEXT_INPUT;
import static de.ugoe.cs.autoquest.usability.tasktree.filters.TaskTypeFilter.ITERATION;
import static de.ugoe.cs.autoquest.usability.tasktree.filters.TaskTypeFilter.SELECTION;
import static de.ugoe.cs.autoquest.usability.tasktree.filters.TaskTypeFilter.SEQUENCE;
import static org.fest.assertions.api.Assertions.assertThat;

import org.junit.Test;

import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree;
import de.ugoe.cs.autoquest.usability.rules.patterns.UsagePattern;
import de.ugoe.cs.autoquest.usability.rules.patterns.UsagePatternBuilder;
import de.ugoe.cs.autoquest.usability.testutil.GenerateTaskTreeUtil;

/**
 * <p>
 * TODO comment
 * </p>
 * 
 * @author Alexander Deicke
 */
public class EndsWithPatternTest {

    @Test
    public void sequence_starts_with_positive_1() {
        // Given
        String spec = "Sequence {" +
                      "  EventTask target2 {}" +
                      "  EventTask target3 {}" +
                      "  Selection {" +
                      "    Sequence {" +
                      "      TextInput target4 {}" +
                      "      MouseClick target2 {}" +
                      "    }" +
                      "    Sequence {" +
                      "      TextInput target4 {}" +
                      "      EventTask target2 {}" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SELECTION).startsWith(TEXT_INPUT).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SEQUENCE).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void sequence_starts_with_positive_2() {
        // Given
        String spec = "Selection {" +
                      "  EventTask target1 {}" +
                      "  Sequence {" +
                      "    Iteration {" +
                      "      Sequence {" +
                      "        TextInput target4 {}" +
                      "        MouseClick target2 {}" +
                      "        TextInput target4 {}" +
                      "      }" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(ITERATION).startsWith(TEXT_INPUT).contains(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SEQUENCE).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void sequence_starts_with_positive_3() {
        // Given
        String spec =  "Sequence {" +
                       "  Optional {" +
                       "    Sequence {" +
                       "      EventTask target4 {}" +
                       "      EventTask target3 {}" +
                       "    }" +
                       "  }" +
                       "  EventTask target1 {}" +
                       "  EventTask target2 {}" +
                       "  EventTask target3 {}" +
                       "  EventTask target4 {}" +
                       "  EventTask target6 {}" +
                       "  EventTask target7 {}" +
                       "  Sequence {" +
                       "    MouseClick target5 {}" +
                       "    TextInput target4 {}" +
                       "    MouseClick target2 {}" +
                       "  }" +
                       "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec); 
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern startsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SEQUENCE).startsWith(startsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
   
    @Test
    public void sequence_starts_with_negative_1() {
        // Given
        String spec = "Sequence {" + 
                      "  MouseClick () {}" + 
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SELECTION).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SEQUENCE).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }
    
    @Test
    public void sequence_starts_with_negative_2() {
        // Given
        String spec = "Selection {" +
                      "  Sequence {" +
                      "    EventTask target1 {}" +
                      "    EventTask target2 {}" +
                      "  }" +
                      "  Iteration {" +
                      "    Sequence {" +
                      "      MouseClick target5 {}" +
                      "      EventTask target4 {}" +
                      "      EventTask target2 {}" +
                      "    }" +
                      "  }" +
                      "  Sequence {" +
                      "    EventTask target5 {}" +
                      "    EventTask target6 {}" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(ITERATION).startsWith(MOUSE_CLICK).contains(TEXT_INPUT).build();
        UsagePattern pattern = patternBuilder.concernedNode(SEQUENCE).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }
    
    @Test
    public void sequence_starts_with_negative_3() {
        // Given
        String spec = "Sequence {" +
                      "  Selection {" +
                      "    Sequence {" +
                      "      MouseClick target4 {}" +
                      "      EventTask target2 {}" +
                      "    }" +
                      "    Sequence {" +
                      "      TextInput target4 {}" +
                      "      TextInput target2 {}" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(MOUSE_CLICK).endsWith(TEXT_INPUT).build();
        UsagePattern pattern = patternBuilder.concernedNode(SEQUENCE).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }
    
    @Test
    public void iteration_starts_with_positive_1() {
        // Given
        String spec = "Iteration {" +
        	      "  Sequence {" + 
                      "    MouseClick () {}" +
                      "    TextInput () {}" +
                      "  }" + 
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(MOUSE_CLICK).endsWith(TEXT_INPUT).build();
        UsagePattern pattern = patternBuilder.concernedNode(ITERATION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void iteration_starts_with_positive_2() {
        // Given
        String spec = "Iteration {" +
                      "  Selection {" +
                      "    Sequence {" +
                      "      MouseClick target4 {}" +
                      "      TextInput target2 {}" +
                      "      MouseClick target 4 {}" +
                      "    }" +
                      "    Sequence {" +
                      "      TextInput target4 {}" +
                      "      TextInput target2 {}" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SELECTION).startsWith(MOUSE_CLICK).contains(TEXT_INPUT).build();
        UsagePattern pattern = patternBuilder.concernedNode(ITERATION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void iteration_starts_with_positive_3() {
        // Given
        String spec = "Iteration {" +
                      "  Selection {" +
                      "    EventTask target1 {}" +
                      "    Sequence {" +
                      "      MouseClick target2 {}" +
                      "      MouseClick target3 {}" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(ITERATION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void iteration_starts_with_negative_1() {
        // Given
        String spec = "Iteration {" + 
                      "  MouseClick () {}" + 
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(ITERATION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }
    
    @Test
    public void iteration_starts_with_negative_2() {
        // Given
        String spec = "Iteration {" +
                      "  Selection {" +
                      "    Sequence {" +
                      "      MouseClick target2 {}" +
                      "      MouseClick target3 {}" +
                      "      TextInput target4 {}" + 
                      "    }" +
                      "    Optional {" +
                      "      EventTask target2 {}" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SELECTION).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(ITERATION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }
    
    @Test
    public void iteration_starts_with_negative_3() {
        // Given
        String spec = "Iteration {" +
                      "  Selection {" +
                      "    EventTask target1 {}" +
                      "    Sequence {" +
                      "      EventTask target2 {}" +
                      "      EventTask target3 {}" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(ITERATION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }
    
    @Test
    public void selection_starts_with_positive_1() {
        // Given
        String spec = "Selection {" + 
                      "  Sequence {" +
                      "    MouseClick target2 {}" +
                      "    MouseClick target3 {}" +
                      "  }" +
                      "  MouseClick () {}" + 
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SELECTION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void selection_starts_with_positive_2() {
        // Given
        String spec = "Sequence {" +
                      "  Optional {" +
                      "    Sequence {" +
                      "      EventTask target4 {}" +
                      "      EventTask target3 {}" +
                      "    }" +
                      "  }" +
                      "  EventTask target1 {}" +
                      "  EventTask target2 {}" +
                      "  EventTask target3 {}" +
                      "  EventTask target4 {}" +
                      "  Selection {" +
                      "    TextInput target5 {}" +
                      "    Selection {" +
                      "      MouseClick target4 {}" +
                      "      MouseClick target2 {}" +
                      "    }" +
                      "  }" +
                      "  EventTask target6 {}" +
                      "  EventTask target7 {}" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SELECTION).startsWith(MOUSE_CLICK).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SELECTION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void selection_starts_with_positive_3() {
        // Given
        String spec = "Iteration {" +
                      "  Selection {" +
                      "    Iteration {" +
                      "      Sequence {" +
                      "        TextInput target3 {}" +
                      "        MouseClick target4 {}" +
                      "      }" +
                      "    }" +
                      "    Sequence {" +
                      "      TextInput target5 {}" +
                      "      EventTask target6 {}" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(ITERATION).startsWith(TEXT_INPUT).contains(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SELECTION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isTrue();
    }
    
    @Test
    public void selection_starts_with_negative_1() {
        // Given
        String spec = "Selection {" + 
                      "  DragAndDrop () {}" + 
                      "  MouseClick () {}" + 
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(SEQUENCE).startsWith(TEXT_INPUT).contains(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SELECTION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }
    
    @Test
    public void selection_starts_with_negative_2() {
        // Given
        String spec = "Iteration {" +
                      "  Selection {" +
                      "    Iteration {" +
                      "      Sequence {"+
                      "        TextInput target3 {}" +
                      "        TextInput target4 {}" +
                      "      }" +
                      "    }" +
                      "    Iteration {" +
                      "      Sequence {"+
                      "        MouseClick target5 {}" +
                      "        TextInput target6 {}" +
                      "      }" +
                      "    }" +
                      "  }" +
                      "}";
        ITaskTree taskTree = GenerateTaskTreeUtil.getTaskTreeFromSpec(spec);
        UsagePatternBuilder patternBuilder = new UsagePatternBuilder();
        UsagePattern endsWithPattern = patternBuilder.concernedNode(ITERATION).startsWith(TEXT_INPUT).endsWith(MOUSE_CLICK).build();
        UsagePattern pattern = patternBuilder.concernedNode(SELECTION).endsWith(endsWithPattern).build();
        // When
        boolean patternContained = pattern.containedIn(taskTree);
        // Then
        assertThat(patternContained).isFalse();
    }

}
