public void testSaveContextOnCommitForExistingTask() throws Exception {
    myTaskManager.getState().saveContextOnCommit = true;

    assertEquals(1, myTaskManager.getLocalTasks().size());

    Task task = myRepository.findTask("TEST-001");
    assertNotNull(task);
    assertEquals(
        1, myChangeListManager.getChangeListsCopy().size()); // default change list should be here
    activateAndCreateChangelist(task);
    myChangeListManager.waitUntilRefreshed();

    assertEquals(2, myTaskManager.getLocalTasks().size());
    List<LocalChangeList> copy = myChangeListManager.getChangeListsCopy();
    assertEquals(copy.toString(), 2, copy.size());
    LocalTask localTask = myTaskManager.getActiveTask();
    List<ChangeListInfo> changelists = localTask.getChangeLists();
    ChangeListInfo info = changelists.get(0);
    LocalChangeList changeList = myChangeListManager.getChangeList(info.id);
    assertNotNull(changeList);

    CommitChangeListDialog.commitChanges(
        getProject(), Collections.<Change>emptyList(), changeList, null, changeList.getName());

    assertEquals(2, myTaskManager.getLocalTasks().size()); // no extra task created
    assertEquals(2, myChangeListManager.getChangeListsCopy().size());

    assertEquals(
        localTask, myTaskManager.getAssociatedTask(changeList)); // association should survive
  }
  public void testRemoveChangelistViaVcsAction() throws Exception {
    Task task = myRepository.findTask("TEST-001");
    assertNotNull(task);
    activateAndCreateChangelist(task);
    myChangeListManager.waitUntilRefreshed();

    LocalTask defaultTask = myTaskManager.findTask(LocalTaskImpl.DEFAULT_TASK_ID);
    assertNotNull(defaultTask);
    activateAndCreateChangelist(defaultTask);
    myChangeListManager.waitUntilRefreshed();
    assertEquals(defaultTask, myTaskManager.getActiveTask());

    LocalTask anotherTask = myTaskManager.findTask("TEST-001");
    assertNotNull(anotherTask);

    LocalChangeList defaultChangeList =
        myChangeListManager.findChangeList(LocalChangeList.DEFAULT_NAME);
    assertNotNull(defaultChangeList);
    LocalChangeList anotherChangeList = myChangeListManager.findChangeList("TEST-001 Summary");
    assertNotNull(anotherChangeList);
    removeChangeList(anotherChangeList);

    assertEquals(1, anotherTask.getChangeLists().size());
    assertEquals(1, defaultTask.getChangeLists().size());
    assertEquals(1, myChangeListManager.getChangeListsCopy().size());

    assertEquals(defaultTask, myTaskManager.getAssociatedTask(defaultChangeList));
    assertEquals(LocalChangeList.DEFAULT_NAME, defaultChangeList.getName());
  }
  public void testInitialState() {
    assertEquals(1, myTaskManager.getLocalTasks().size());
    final LocalTask defaultTask = myTaskManager.getLocalTasks().get(0);
    assertEquals(defaultTask, myTaskManager.getActiveTask());
    assertTrue(defaultTask.isDefault());

    assertEquals(1, myChangeListManager.getChangeListsCopy().size());
    assertEquals(1, defaultTask.getChangeLists().size());

    assertEquals(
        defaultTask,
        myTaskManager.getAssociatedTask(myChangeListManager.getChangeListsCopy().get(0)));
    assertEquals(
        defaultTask.getChangeLists().get(0).id,
        myChangeListManager.getChangeListsCopy().get(0).getId());
    assertEquals(
        defaultTask.getChangeLists().get(0),
        new ChangeListInfo(myChangeListManager.getChangeListsCopy().get(0)));
  }
  public void testTrackContext() {
    myTaskManager.getState().trackContextForNewChangelist = true;

    addChangeList("New Changelist", "");
    assertEquals(2, myTaskManager.getLocalTasks().size());
    assertEquals(2, myChangeListManager.getChangeListsCopy().size());
    LocalChangeList newChangeList = myChangeListManager.findChangeList("New Changelist");
    assertNotNull(newChangeList);
    LocalTask newTask = myTaskManager.getAssociatedTask(newChangeList);
    assertNotNull(newTask);
    assertEquals("New Changelist", newTask.getSummary());

    myTaskManager.getState().trackContextForNewChangelist = false;
  }
  public void testSaveContextOnCommit() throws Exception {
    myTaskManager.getState().saveContextOnCommit = true;

    assertEquals(1, myTaskManager.getLocalTasks().size());
    assertEquals(1, myChangeListManager.getChangeListsCopy().size());

    LocalChangeList changeList = addChangeList("New Changelist", "");

    assertEquals(1, myTaskManager.getLocalTasks().size());
    assertEquals(2, myChangeListManager.getChangeListsCopy().size());

    CommitChangeListDialog.commitChanges(
        getProject(), Collections.<Change>emptyList(), changeList, null, changeList.getName());

    assertEquals(2, myTaskManager.getLocalTasks().size()); // extra task created
    assertEquals(2, myChangeListManager.getChangeListsCopy().size());

    assertTrue(
        ContainerUtil.exists(
            myTaskManager.getLocalTasks(),
            task -> {
              return task.getSummary().equals("New Changelist");
            }));
  }
  public void testSwitchingTasks() throws Exception {
    final LocalTask defaultTask = myTaskManager.getLocalTasks().get(0);

    Task task = myRepository.findTask("TEST-001");
    assertNotNull(task);
    myTaskManager.activateTask(task, false);

    assertEquals(2, myTaskManager.getLocalTasks().size());

    LocalTask localTask = myTaskManager.getActiveTask();
    assertEquals(task, localTask);

    assertEquals(0, localTask.getChangeLists().size());
    assertEquals(1, defaultTask.getChangeLists().size());
    assertEquals(1, myChangeListManager.getChangeListsCopy().size());
    assertEquals(
        defaultTask,
        myTaskManager.getAssociatedTask(myChangeListManager.getChangeListsCopy().get(0)));

    myTaskManager.activateTask(defaultTask, false);

    assertEquals(0, localTask.getChangeLists().size());
    assertEquals(1, defaultTask.getChangeLists().size());
    assertEquals(1, myChangeListManager.getChangeListsCopy().size());
    assertEquals(
        defaultTask,
        myTaskManager.getAssociatedTask(myChangeListManager.getChangeListsCopy().get(0)));

    activateAndCreateChangelist(localTask);

    assertEquals(1, localTask.getChangeLists().size());
    assertEquals(1, defaultTask.getChangeLists().size());
    assertEquals(2, myChangeListManager.getChangeListsCopy().size());

    LocalChangeList activeChangeList = myChangeListManager.getDefaultChangeList();
    LocalChangeList anotherChangeList =
        myChangeListManager
            .getChangeListsCopy()
            .get(1 - myChangeListManager.getChangeListsCopy().indexOf(activeChangeList));

    assertNotNull(activeChangeList);
    assertEquals(localTask, myTaskManager.getAssociatedTask(activeChangeList));
    assertEquals("TEST-001 Summary", activeChangeList.getName());

    assertEquals(defaultTask, myTaskManager.getAssociatedTask(anotherChangeList));
    assertEquals(LocalChangeList.DEFAULT_NAME, anotherChangeList.getName());

    myTaskManager.activateTask(defaultTask, false);
    myChangeListManager.waitUntilRefreshed();

    assertEquals(1, localTask.getChangeLists().size());
    assertEquals(1, defaultTask.getChangeLists().size());
    assertEquals(2, myChangeListManager.getChangeListsCopy().size());

    activeChangeList = myChangeListManager.getDefaultChangeList();
    anotherChangeList =
        myChangeListManager
            .getChangeListsCopy()
            .get(1 - myChangeListManager.getChangeListsCopy().indexOf(activeChangeList));

    assertNotNull(activeChangeList);
    assertEquals(defaultTask, myTaskManager.getAssociatedTask(activeChangeList));
    assertEquals(LocalChangeList.DEFAULT_NAME, activeChangeList.getName());

    assertEquals(localTask, myTaskManager.getAssociatedTask(anotherChangeList));
    assertEquals("TEST-001 Summary", anotherChangeList.getName());
  }