/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * 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.intellij.ui;

import com.intellij.testFramework.UsefulTestCase;
import com.intellij.ui.components.JBList;

import java.util.Arrays;

public class FinderRecursivePanelListModelMergeTest extends UsefulTestCase {

  public void testListModelMerge1() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 0, 0, "a");
  }

  public void testListModelMerge2() {
    assertMerge(new String[0], "a", "b", "c", "d");
  }

  public void testListModelMerge3() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 0, -1 /* nothing */);
  }

  public void testListModelMerge4() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 2, 2, "a", "b", "c", "d");
  }

  public void _testListModelMerge5() {
    assertMerge(new String[]{"a", "b", "c", "d"}, "d", "c", "b", "a");
  }

  public void testListModelMerge6() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 2, 1, "b", "c");
  }

  public void testListModelMerge7() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 1, 0, "b", "c", "d", "e");
  }

  public void _testListModelMerge8() {
    assertMerge(new String[]{"a", "a", "b", "b"}, "b", "a", "b", "a");
  }

  public void testListModelMerge9() {
    assertMerge(new String[]{"a", "b"}, 1, 3, "e", "d", "a", "b");
  }

  public void testListModelMerge10() {
    assertMerge(new String[]{"a", "b"}, 0, -1, "c", "d");
  }

  private static void assertMerge(String[] initialItems,
                                  int initialSelectionIdx,
                                  int selectionIndexAfterMerge,
                                  String... itemsToMerge) {
    CollectionListModel<String> model = new CollectionListModel<String>();
    model.add(Arrays.asList(initialItems));
    JBList list = new JBList(model);
    list.setSelectedIndex(initialSelectionIdx);

    FinderRecursivePanel.mergeListItems(model, Arrays.asList(itemsToMerge));
    assertEquals(itemsToMerge.length, model.getSize());
    for (int i = 0; i < itemsToMerge.length; i++) {
      assertEquals("idx:" + i + " " + toString(model.getItems(), ","), itemsToMerge[i], model.getElementAt(i));
    }
    assertEquals(toString(model.getItems(), ","), selectionIndexAfterMerge, list.getSelectedIndex());
  }

  private static void assertMerge(String[] items, String... newItems) {
    assertMerge(items, -1, -1, newItems);
  }
}
@@ -835,31 +835,39 @@
 oid test
-ListModelMerge1
+SelectionKeptSingleItem
 () %7B%0A   
@@ -952,23 +952,41 @@
 test
-ListModelMerge2
+NoSelectionAfterReplacingAllItems
 () %7B
@@ -1065,23 +1065,26 @@
 test
-ListModelMerge3
+NoSelectionNoItems
 () %7B
@@ -1183,23 +1183,35 @@
 test
-ListModelMerge4
+SelectionKeptIdenticalItems
 () %7B
@@ -1430,23 +1430,44 @@
 test
-ListModelMerge6
+SelectionItemKeptItemsLessAndMovedUp
 () %7B
@@ -1561,23 +1561,37 @@
 test
-ListModelMerge7
+SelectionItemKeptItemsMovedUp
 () %7B
@@ -1810,23 +1810,39 @@
 test
-ListModelMerge9
+SelectionItemKeptItemsMovedDown
 () %7B
@@ -1936,24 +1936,32 @@
 test
-ListModelMerge10
+NoSelectionDisjointItems
 () %7B
@@ -2016,32 +2016,173 @@
 %22c%22, %22d%22);%0A  %7D%0A%0A
+  private static void assertMerge(String%5B%5D initialItems, String... itemsToMerge) %7B%0A    assertMerge(initialItems, -1, -1, itemsToMerge);%0A  %7D%0A%0A
   private static
@@ -2913,16 +2913,16 @@
 ;%0A    %7D%0A
+
     asse
@@ -3022,125 +3022,6 @@
 %0A  %7D
-%0A%0A  private static void assertMerge(String%5B%5D items, String... newItems) %7B%0A    assertMerge(items, -1, -1, newItems);%0A  %7D
 %0A%7D

test names