Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0dbb9f681190fa80c772f88dd03fd78c12a41d39 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 * Copyright (c) 2010 Red Hat Inc. 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
 *******************************************************************************/
package org.eclipse.linuxtools.changelog.ui.tests.swtbot;

import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
import static org.hamcrest.core.AllOf.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ContextMenuHelper;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ProjectExplorer;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ProjectExplorerTreeItemAppearsCondition;
import org.eclipse.linuxtools.changelog.ui.tests.utils.SVNProject;
import org.eclipse.linuxtools.changelog.ui.tests.utils.SVNProjectCreatedCondition;
import org.eclipse.linuxtools.changelog.ui.tests.utils.TableAppearsCondition;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.IEditorReference;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * UI tests for creating changelogs from SVN history (commit messages).
 *
 */
@RunWith(SWTBotJunit4ClassRunner.class)
public class CreateChangeLogFromHistorySWTBotTest {

	private static SWTWorkbenchBot bot;
	private static SWTBotTree projectExplorerViewTree;
	private IProject  project;
	private SVNProject subversionProject;
	// The name of the test project, we create
	private final String PROJECT_NAME = "org.eclipse.linuxtools.changelog.tests";
	// An available SVN repo
	private final String SVN_PROJECT_URL = "svn://dev.eclipse.org/svnroot/technology/" +
		"org.eclipse.linuxtools/changelog/trunk";

	@BeforeClass
	public static void beforeClass() throws Exception {
		// delay click speed
		//System.setProperty("org.eclipse.swtbot.playback.delay", "200");
		bot = new SWTWorkbenchBot();
		try {
			bot.viewByTitle("Welcome").close();
			// hide Subclipse Usage stats popup if present/installed
			bot.shell("Subclipse Usage").activate();
			bot.button("Cancel").click();
		} catch (WidgetNotFoundException e) {
			// ignore
		}
		// Make sure project explorer is open and tree available
		ProjectExplorer.openView();
		projectExplorerViewTree = ProjectExplorer.getTree();
	}

	@Before
	public void setUp() throws Exception {
		// Do an SVN checkout of the changelog.tests plugin
		subversionProject = new SVNProject(bot);
		project = subversionProject.setProjectName(PROJECT_NAME).setRepoURL(SVN_PROJECT_URL).checkoutProject();
		bot.waitUntil(new SVNProjectCreatedCondition(PROJECT_NAME));
		ProjectExplorer.openView();
	}

	@After
	public void tearDown() throws Exception {
		this.project.delete(true, null);
		// discard existing repo from previous test runs
		try {
			subversionProject.discardRepositoryLocation();
		} catch (WidgetNotFoundException e) {
			// Ignore case if repository not existing
		}
	}

	/**
	 * Create changelog from SVN history (commit messages).
	 *
	 * @throws Exception
	 */
	@Test
	public void canPrepareChangeLogFromSVNHistory() throws Exception {
		// select ChangeLog file
		String teamProviderString = "[changelog/trunk/" + PROJECT_NAME + "]";
		SWTBotTreeItem projectItem = ProjectExplorer.expandProject(projectExplorerViewTree, PROJECT_NAME, teamProviderString);
		long oldTimeout = SWTBotPreferences.TIMEOUT;
		SWTBotPreferences.TIMEOUT = 5000;
		bot.waitUntil(new ProjectExplorerTreeItemAppearsCondition(projectExplorerViewTree, PROJECT_NAME, teamProviderString, "ChangeLog"));
		SWTBotPreferences.TIMEOUT = oldTimeout;
		SWTBotTreeItem changeLogItem = ProjectExplorer.getProjectItem(projectItem, "ChangeLog");
		changeLogItem.select();

		// open history for ChangeLog file
		clickOnShowHistory(projectExplorerViewTree);
		SWTBot historyViewBot = bot.viewByTitle("History").bot();
		// wait for SVN revision table to appear
		oldTimeout = SWTBotPreferences.TIMEOUT;
		SWTBotPreferences.TIMEOUT = 3 * 5000;
		historyViewBot.waitUntil(new TableAppearsCondition());
		SWTBotPreferences.TIMEOUT = oldTimeout;
		SWTBotTable historyTable = historyViewBot.table();
		historyTable.select(0); // select the first row

		// right-click => Generate Changelog...
		clickOnGenerateChangeLog(historyTable);
		bot.waitUntil(Conditions.shellIsActive("Generate ChangeLog"));
		SWTBotShell shell = bot.shell("Generate ChangeLog");

		SWTBot generateChangelogBot = shell.bot();
		generateChangelogBot.radio("Clipboard").click();
		generateChangelogBot.button("OK").click();

		// create and open a new file for pasting
		String pasteFile = "newFile";
		IFile newFile = project.getFile(new Path(pasteFile));
		newFile.create(new ByteArrayInputStream("".getBytes()) /* empty content */, false, null);
		project.refreshLocal(IResource.DEPTH_INFINITE, null);

		assertNotNull(project.findMember(new Path(pasteFile)));

		ProjectExplorer.expandProject(projectExplorerViewTree, PROJECT_NAME,
				teamProviderString).expandNode(pasteFile).select().doubleClick();
		Matcher<IEditorReference> editorMatcher = allOf(
				IsInstanceOf.instanceOf(IEditorReference.class),
				withPartName(pasteFile)
				);
		bot.waitUntil(Conditions.waitForEditor(editorMatcher));
		oldTimeout = SWTBotPreferences.TIMEOUT;
		SWTBotPreferences.TIMEOUT = oldTimeout;
		SWTBotEditor swtBoteditor = bot.activeEditor();
		assertEquals(pasteFile, swtBoteditor.getTitle());
		SWTBotEclipseEditor eclipseEditor = swtBoteditor.toTextEditor();

		// go to beginning of editor
		eclipseEditor.selectRange(0, 0, 0);
		// paste
		eclipseEditor.pressShortcut(Keystrokes.CTRL, KeyStroke.getInstance("V"));
		swtBoteditor.save();
		// make sure some changelog like text was pasted
		String text = eclipseEditor.getText();
		assertTrue(!text.equals(""));
	}

	/**
	 * Helper method for right-clicking => Generate ChangeLog in History
	 * view table.
	 *
	 * Pre: History view table row selected.
	 */
	private void clickOnGenerateChangeLog(SWTBotTable table) {
		String menuItem = "Generate ChangeLog...";
		ContextMenuHelper.clickContextMenu(table, menuItem);
	}

	/**
	 * Helper method for right-click => Team => Show History.
	 */
	private void clickOnShowHistory(SWTBotTree tree) {
		String menuItem = "Team";
		String subMenuItem = "Show History";
		ContextMenuHelper.clickContextMenu(tree, menuItem, subMenuItem);
	}

}

Back to the top