Skip to main content
summaryrefslogtreecommitdiffstats
blob: f2015a609a27706b5bec2288efb605a5c7f6f0b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2010 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.tasks.tests.ui;

import junit.framework.TestCase;

import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.mylyn.internal.provisional.commons.ui.WorkbenchUtil;
import org.eclipse.mylyn.internal.tasks.core.RepositoryQuery;
import org.eclipse.mylyn.internal.tasks.core.TaskCategory;
import org.eclipse.mylyn.internal.tasks.core.TaskList;
import org.eclipse.mylyn.internal.tasks.core.TaskTask;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.tests.TaskTestUtil;
import org.eclipse.mylyn.tasks.tests.connector.MockRepositoryConnector;
import org.eclipse.mylyn.tasks.ui.ITasksUiConstants;

/**
 * @author Steffen Pingel
 */
public class TaskListViewTest extends TestCase {

	private TaskRepository repository;

	@Override
	protected void setUp() throws Exception {
		TaskTestUtil.resetTaskListAndRepositories();
		repository = TaskTestUtil.createMockRepository();
	}

	@Override
	protected void tearDown() throws Exception {
		TaskTestUtil.resetTaskListAndRepositories();
	}

	public void testSelectedAndFocusTaskMultiple() {
		TaskList taskList = TasksUiPlugin.getTaskList();
		TaskCategory category1 = new TaskCategory(taskList.getUniqueHandleIdentifier());
		taskList.addCategory(category1);

		TaskTask task1 = TaskTestUtil.createMockTask("task1");
		TaskTask task2 = TaskTestUtil.createMockTask("task2");

		taskList.addTask(task1, category1);
		taskList.addTask(task2, category1);

		TaskListView view = (TaskListView) WorkbenchUtil.showViewInActiveWindow(ITasksUiConstants.ID_VIEW_TASKS);
		view.refresh();

		TreeSelection selection = new TreeSelection(new TreePath(new Object[] { category1, task1 }));
		// select multiple
		view.getViewer().setSelection(new StructuredSelection(new Object[] { task1, task2 }));
		view.selectedAndFocusTask(task1);
		// make sure only a single task is selected
		assertEquals(toString(selection), toString(((TreeSelection) view.getViewer().getSelection())));
	}

	public void testSelectedAndFocusTaskRestore() {
		TaskList taskList = TasksUiPlugin.getTaskList();
		TaskCategory category1 = new TaskCategory(taskList.getUniqueHandleIdentifier());
		taskList.addCategory(category1);
		// create a query since tasks can only be in one category
		RepositoryQuery category2 = new RepositoryQuery(MockRepositoryConnector.CONNECTOR_KIND,
				taskList.getUniqueHandleIdentifier());
		taskList.addQuery(category2);

		TaskTask task1 = TaskTestUtil.createMockTask("task1");

		taskList.addTask(task1, category1);
		taskList.addTask(task1, category2);

		TaskListView view = (TaskListView) WorkbenchUtil.showViewInActiveWindow(ITasksUiConstants.ID_VIEW_TASKS);
		view.refresh();

		TreeSelection selection = new TreeSelection(new TreePath(new Object[] { category1, task1 }));
		view.getViewer().setSelection(selection);
		view.selectedAndFocusTask(task1);
		assertEquals(toString(selection), toString(((TreeSelection) view.getViewer().getSelection())));

		// select different path and restore original path
		view.getViewer().setSelection(new TreeSelection(new TreePath(new Object[] { category2, task1 })));
		view.selectedAndFocusTask(task1);
		assertEquals(selection, view.getViewer().getSelection());
	}

	private String toString(TreeSelection s1) {
		StringBuilder sb = new StringBuilder();
		for (TreePath path : s1.getPaths()) {
			sb.append("[");
			for (int i = 0; i < path.getSegmentCount(); i++) {
				sb.append(path.getSegment(i));
				sb.append(", ");
			}
			sb.setLength(sb.length() - 2);
			sb.append("]");
		}
		return sb.toString();
	}

}

Back to the top