Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f72a860701dfcc4b76a796c607bd9b365617e7f5 (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
/*******************************************************************************
 * Copyright (c) 2010, 2019 Red Hat Inc. and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
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.assertNull;

import java.io.ByteArrayInputStream;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ChangeLogTestProject;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ProjectExplorer;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ProjectExplorerTreeItemAppearsCondition;
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.keyboard.Keystrokes;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
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.Test;

/**
 * UI tests for formatting ChangeLog files.
 */
public class FormatChangeLogSWTBotTest extends AbstractSWTBotTest {

    private ChangeLogTestProject project;
    // The name of the test project, we create
    private final String PROJECT_NAME = "org.eclipse.linuxtools.changelog.ui.formattestproject";

    @Before
    public void setUp() throws Exception {
        project = new ChangeLogTestProject(PROJECT_NAME);
		projectExplorerViewTree = ProjectExplorer.getTree();
    }

    @After
    public void tearDown() throws Exception {
        this.project.getTestProject().delete(true, null);
    }

    /**
     * Simple test for ChangeLog formatting.
     *
     * @throws Exception
     */
    @Test
    public void canFormatChangeLogFile() throws Exception {
        // add a ChangeLog file
        assertNull(project.getTestProject().findMember(new Path("/ChangeLog")));
        final String changelogContent = "2010-12-14  Severin Gehwolf  <sgehwolf@redhat.com>\n\n" +
            "\tAdded org.eclipse.linuxtools.changelog.tests.ui plug-in.\n" +
            "\t* .classpath: New file.\n" +
            "\t* .project: New file.\n" +
            "\t* .settings/org.eclipse.jdt.core.prefs: New file.\n" +
            "\t* build.properties: New file.\n" +
            "\t* src/log4j.xml: New file.\n" +
            "\t* src/org/eclipse/linuxtools/changelog/tests/ui/utils/ContextMenuHelper.java: New file.\n" +
            "\t* src/org/eclipse/linuxtools/changelog/tests/ui/utils/ProjectExplorer.java: New file.\n" +
            "\t* src/org/eclipse/linuxtools/changelog/tests/ui/utils/ProjectExplorerTreeItemAppearsCondition.java: New file.\n";
        project.addFileToProject("/", "ChangeLog", new ByteArrayInputStream(changelogContent.getBytes()));
        assertNotNull(project.getTestProject().findMember(new Path("/ChangeLog")));

        // select ChangeLog file
        String teamProviderString = "n/a";
        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.doubleClick(); // should open ChangeLog file

        oldTimeout = SWTBotPreferences.TIMEOUT;
        SWTBotPreferences.TIMEOUT = 3 * 5000;
        // Wait for ChangeLog editor to open
        Matcher<IEditorReference> editorMatcher = allOf(
                IsInstanceOf.instanceOf(IEditorReference.class),
                withPartName("ChangeLog")
                );
        bot.waitUntil(Conditions.waitForEditor(editorMatcher));
        SWTBotEditor swtBoteditor = bot.activeEditor();
        assertEquals("ChangeLog", swtBoteditor.getTitle());

        SWTBotEclipseEditor swtBotEclipseEditor = swtBoteditor.toTextEditor();

        // Add two extra lines after the first date line
        swtBotEclipseEditor.insertText(1, 0, "\n\n");
        // Should have 3 empty lines between date-line and first file entry
        swtBotEclipseEditor.selectRange(1, 0, 3);

        // format: ESC CTRL+F
        swtBotEclipseEditor.pressShortcut(Keystrokes.ESC);
        swtBotEclipseEditor.pressShortcut(Keystrokes.CTRL, KeyStroke.getInstance("F"));
        swtBoteditor.save();
    }

}

Back to the top