Add ICommitMessageProvider2 for caret positioning in commit messages
[egit/eclipse.git] / org.eclipse.egit.ui.test / src / org / eclipse / egit / ui / common / StagingViewTester.java
blob665a9fbda68d2c858149f301f3cbe5eaf1d80456
1 /*******************************************************************************
2 * Copyright (C) 2011, 2017 Jens Baumgart <jens.baumgart@sap.com> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.egit.ui.common;
11 import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
15 import java.util.concurrent.TimeUnit;
17 import org.eclipse.egit.ui.Activator;
18 import org.eclipse.egit.ui.JobFamilies;
19 import org.eclipse.egit.ui.UIPreferences;
20 import org.eclipse.egit.ui.internal.UIText;
21 import org.eclipse.egit.ui.internal.staging.StagingView;
22 import org.eclipse.egit.ui.test.ContextMenuHelper;
23 import org.eclipse.egit.ui.test.JobJoiner;
24 import org.eclipse.egit.ui.test.TestUtil;
25 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
26 import org.eclipse.swtbot.swt.finder.widgets.SWTBotStyledText;
27 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarToggleButton;
28 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
30 public class StagingViewTester {
32 private SWTBotView stagingView;
34 public StagingViewTester(SWTBotView view) {
35 stagingView = view;
38 public static StagingViewTester openStagingView() throws Exception {
39 // This is needed so that we can find staging entries by full path.
40 Activator.getDefault().getPreferenceStore()
41 .setValue(UIPreferences.STAGING_VIEW_FILENAME_MODE, false);
43 SWTBotView view = TestUtil.showView(StagingView.VIEW_ID);
44 TestUtil.joinJobs(org.eclipse.egit.core.JobFamilies.INDEX_DIFF_CACHE_UPDATE);
45 TestUtil.processUIEvents();
47 return new StagingViewTester(view);
50 public void setAuthor(String author) {
51 stagingView.bot().textWithLabel(UIText.StagingView_Author)
52 .setText(author);
55 public void setCommitter(String committer) {
56 stagingView.bot().textWithLabel(UIText.StagingView_Committer)
57 .setText(committer);
60 public void setCommitMessage(String message) {
61 stagingView.bot().styledTextWithLabel(UIText.StagingView_CommitMessage)
62 .setText(message);
65 public void stageFile(String path) {
66 SWTBotTree unstagedTree = stagingView.bot().tree(0);
68 TestUtil.waitUntilTreeHasNodeContainsText(stagingView.bot(),
69 unstagedTree, path, 10000);
71 TestUtil.getNode(unstagedTree.getAllItems(), path).select();
73 JobJoiner jobJoiner = JobJoiner.startListening(
74 org.eclipse.egit.core.JobFamilies.INDEX_DIFF_CACHE_UPDATE, 30,
75 TimeUnit.SECONDS);
77 ContextMenuHelper.clickContextMenu(unstagedTree,
78 UIText.StagingView_StageItemMenuLabel);
80 jobJoiner.join();
83 public void commit() throws Exception {
84 JobJoiner jobJoiner = JobJoiner.startListening(JobFamilies.COMMIT, 30,
85 TimeUnit.SECONDS);
86 stagingView.bot().button(UIText.StagingView_Commit).click();
87 jobJoiner.join();
90 public void assertCommitEnabled(boolean expectEnabled) {
91 boolean actual = stagingView.bot().button(UIText.StagingView_Commit)
92 .isEnabled();
93 if (expectEnabled)
94 assertTrue("Expected Commit button to be enabled", actual);
95 else
96 assertFalse("Expected Commit button to be disabled", actual);
99 public void setAmend(boolean amend) {
100 SWTBotToolbarToggleButton button = stagingView.bot()
101 .toolbarToggleButtonWithTooltip(
102 UIText.StagingView_Ammend_Previous_Commit);
103 selectToolbarToggle(button, amend);
106 public boolean getAmend() {
107 SWTBotToolbarToggleButton button = stagingView.bot()
108 .toolbarToggleButtonWithTooltip(
109 UIText.StagingView_Ammend_Previous_Commit);
110 return button.isChecked();
113 public void setInsertChangeId(boolean insertChangeId) {
114 SWTBotToolbarToggleButton button = stagingView.bot()
115 .toolbarToggleButtonWithTooltip(
116 UIText.StagingView_Add_Change_ID);
117 selectToolbarToggle(button, insertChangeId);
120 public boolean getInsertChangeId() {
121 SWTBotToolbarToggleButton button = stagingView.bot()
122 .toolbarToggleButtonWithTooltip(
123 UIText.StagingView_Add_Change_ID);
124 return button.isChecked();
127 public void setSignedOff(boolean signedOff) {
128 SWTBotToolbarToggleButton button = stagingView.bot()
129 .toolbarToggleButtonWithTooltip(
130 UIText.StagingView_Add_Signed_Off_By);
131 selectToolbarToggle(button, signedOff);
134 public boolean getSignedOff() {
135 SWTBotToolbarToggleButton button = stagingView.bot()
136 .toolbarToggleButtonWithTooltip(
137 UIText.StagingView_Add_Signed_Off_By);
138 return button.isChecked();
141 private void selectToolbarToggle(SWTBotToolbarToggleButton button,
142 boolean select) {
143 if (select) {
144 if (!button.isChecked())
145 button.select();
146 } else {
147 if (button.isChecked())
148 button.deselect();
152 public String getCommitMessage() {
153 return stagingView.bot()
154 .styledTextWithLabel(UIText.StagingView_CommitMessage)
155 .getText();
158 public int getCaretPosition() {
159 SWTBotStyledText commitMessageArea = stagingView.bot().styledTextWithLabel(UIText.StagingView_CommitMessage);
160 Integer pos = syncExec(() -> Integer
161 .valueOf(commitMessageArea.widget.getCaretOffset()));
162 return pos == null ? -1 : pos.intValue();