Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b6095f1a34425319ba4a065bce958bdea01b70b (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
/**
 * Copyright (c) 2009, 2014 THALES GLOBAL SERVICES
 * 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:
 *      Obeo - Initial API and implementation
 */
package org.eclipse.sirius.tests.swtbot.support.api.view;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.junit.Assert;

/**
 * Utility class to open and get the Eclipse views.
 * 
 * @author mchauvin
 */
public class DesignerViews {

    private static final String OUTLINE = "Outline";

    private static final String WINDOW = "Window";

    private static final String SHOW_VIEW = "Show View";

    private final SWTWorkbenchBot bot;

    /**
     * Construct a new instance.
     * 
     * @param bot
     *            the workbench bot
     */
    public DesignerViews(final SWTWorkbenchBot bot) {
        this.bot = bot;
    }

    /**
     * Open a view with the Eclipse API.
     * 
     * @param viewId
     *            The id of the view to open.
     * @param viewTitle
     *            The title of the view
     * @return The SWTBotView
     */
    public SWTBotView openViewByAPI(final String viewId, String viewTitle) {
        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
            @Override
            public void run() {
                try {
                    PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewId);
                } catch (PartInitException e) {
                    Assert.fail("Unable to open errorLog view : " + e.getMessage());
                }
            }
        });
        return bot.viewByTitle(viewTitle);
    }

    /**
     * Open a view.
     * 
     * @param categoryName
     *            The name of the category in the list of views
     * @param viewName
     *            The name of the label in the list of views and the name of the
     *            opened view.
     * @return The SWTBotView
     */
    public SWTBotView openView(String categoryName, String viewName) {
        return openView(categoryName, viewName, viewName);
    }

    /**
     * Open a view.
     * 
     * @param categoryName
     *            The name of the category in the list of views
     * @param labelInList
     *            The name of the label in the list of views
     * @param viewTitle
     *            The title of the view
     * @return The SWTBotView
     */
    public SWTBotView openView(String categoryName, String labelInList, String viewTitle) {
        bot.menu(DesignerViews.WINDOW).menu(DesignerViews.SHOW_VIEW).menu("Other...").click();
        bot.waitUntil(Conditions.shellIsActive("Show View"));

        bot.text().setText(labelInList);
        // Wait that the filter is apply on the tree and expand all the needed
        // nodes
        bot.sleep(500);
        // The node is already expanded (so don't use expand method)
        bot.tree().getTreeItem(categoryName).getNode(labelInList).select();
        final SWTBotButton okButton = bot.button("OK");
        bot.waitUntil(new OKButtonEnabledCondition(okButton));
        okButton.click();
        return bot.viewByTitle(viewTitle);
    }

    /**
     * Open the outline view.
     * 
     * @return the opened outline view
     */
    public SiriusOutlineView openOutlineView() {

        bot.menu(DesignerViews.WINDOW).menu(DesignerViews.SHOW_VIEW).menu(DesignerViews.OUTLINE).click();
        return getOutlineView();
    }

    /**
     * Get the already opened outline view.
     * 
     * @return the outline view
     */
    public SiriusOutlineView getOutlineView() {
        return new SiriusOutlineView(bot, bot.viewByTitle(DesignerViews.OUTLINE));
    }

    /**
     * A class to wait until a button is enabled.
     * 
     * @author mchauvin
     */
    private class OKButtonEnabledCondition extends DefaultCondition {

        private final SWTBotButton okButton;

        public OKButtonEnabledCondition(final SWTBotButton okButton) {
            this.okButton = okButton;
        }

        @Override
        public String getFailureMessage() {
            return "ok button is not enabled";
        }

        @Override
        public boolean test() throws Exception {
            return okButton.isEnabled();
        }
    }
}

Back to the top