Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BrowserWidgetBot.java « bots « ui « integration « test « studio « directory « apache « org « java « main « src « test.integration.ui « tests - github.com/apache/directory-studio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7fa0aa86414aaadebc8a7611004c1b37d0f5d45e (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.directory.studio.test.integration.ui.bots;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.directory.studio.ldapbrowser.core.BrowserCoreMessages;
import org.apache.directory.studio.test.integration.ui.utils.JobWatcher;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;


/**
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
class BrowserWidgetBot
{
    private SWTBot bot;

    BrowserWidgetBot( SWTBot bot )
    {
        this.bot = bot;
    }


    boolean existsEntry( String... path )
    {
        // ensure the parent exists
        String[] parentPath = new String[path.length - 1];
        System.arraycopy( path, 0, parentPath, 0, parentPath.length );
        getEntry( false, parentPath );

        // check if the child exists
        try
        {
            getEntry( false, path );
            return true;
        }
        catch ( WidgetNotFoundException e )
        {
            return false;
        }
    }


    void selectEntry( boolean wait, String... path )
    {
        SWTBotTreeItem entry = getEntry( true, path );
        select( entry, wait );
    }


    void selectChildrenOfEntry( String[] children, String... path )
    {
        SWTBotTreeItem entry = getEntry( true, path );
        entry.select( children );
    }


    ReferralDialogBot selectEntryExpectingReferralDialog( String... path )
    {
        SWTBotTreeItem entry = getEntry( true, path );
        select( entry, false );
        return new ReferralDialogBot();
    }


    void expandEntry( String... path )
    {
        SWTBotTreeItem entry = getEntry( true, path );
        expand( entry, true, null );
    }


    void waitForEntry( String... path )
    {
        getEntry( true, path );
    }


    ReferralDialogBot expandEntryExpectingReferralDialog( String... path )
    {
        SWTBotTreeItem entry = getEntry( false, path );
        expand( entry, false, null );
        return new ReferralDialogBot();
    }


    String getSelectedEntry()
    {
        return getTree().selection().get( 0 ).get( 0 );
    }


    private SWTBotTreeItem getEntry( boolean wait, String... path )
    {
        SWTBotTree browserTree = bot.tree();
        List<String> pathList = new ArrayList<String>( Arrays.asList( path ) );
        SWTBotTreeItem entry = null;

        while ( !pathList.isEmpty() )
        {
            String node = pathList.remove( 0 );

            if ( entry == null )
            {
                node = adjustNodeName( browserTree, node );
                entry = browserTree.getTreeItem( node );
            }
            else
            {
                entry = getChild( entry, node, wait );
            }

            if ( !pathList.isEmpty() )
            {
                // expand entry and wait till
                // - children are displayed
                // - next child is visible
                final String nextNode = !pathList.isEmpty() ? pathList.get( 0 ) : null;
                expand( entry, true, nextNode );
            }
        }

        return entry;
    }


    private void expand( final SWTBotTreeItem entry, boolean wait, final String nextNode )
    {
        UIThreadRunnable.asyncExec( bot.getDisplay(), new VoidResult()
        {
            public void run()
            {
                if ( !entry.isExpanded() )
                {
                    entry.expand();
                }
            }
        } );

        if ( wait )
        {
            bot.waitUntil( new DefaultCondition()
            {
                public boolean test() throws Exception
                {
                    return !entry.getNodes().contains( "Fetching Entries..." )
                        && !entry.getNodes().contains( "Opening Connection..." );
                }


                public String getFailureMessage()
                {
                    return "Could not find entry " + entry.getText() + " -> " + nextNode;
                }
            } );
        }
    }


    private void select( final SWTBotTreeItem entry, boolean wait )
    {
        if ( !bot.tree().isEnabled() )
        {
            bot.waitUntil( Conditions.widgetIsEnabled( bot.tree() ) );
        }
        if ( wait )
        {
            JobWatcher watcher = new JobWatcher( BrowserCoreMessages.jobs__init_entries_title_attonly,
                "Open Entry Editor" );
            entry.click();
            entry.select();
            watcher.waitUntilDone();
        }
        else
        {
            entry.click();
            entry.select();
        }
    }


    private SWTBotTreeItem getChild( SWTBotTreeItem entry, String nodeName, boolean wait )
    {
        // adjust current path, because the label is decorated with the number of children
        if ( wait )
        {

            bot.waitUntil( new DefaultCondition()
            {

                @Override
                public boolean test() throws Exception
                {
                    String adjustedNodeName = adjustNodeName( entry, nodeName );
                    return adjustedNodeName != null;
                }


                @Override
                public String getFailureMessage()
                {
                    return "Node " + nodeName + " not found";
                }
            } );
        }

        String adjustedNodeName = adjustNodeName( entry, nodeName );
        return entry.getNode( adjustedNodeName );
    }


    private String adjustNodeName( SWTBotTreeItem entry, String nodeName )
    {
        List<String> nodes = entry.getNodes();
        for ( String node : nodes )
        {
            if ( matches( node, nodeName ) )
            {
                return node;
            }
        }
        return null;
    }


    private String adjustNodeName( SWTBotTree tree, String nodeName )
    {
        SWTBotTreeItem[] allItems = tree.getAllItems();
        for ( SWTBotTreeItem item : allItems )
        {
            String node = item.getText();
            if ( matches( node, nodeName ) )
            {
                return node;
            }
        }
        return nodeName;
    }


    private boolean matches( String candidate, String needle )
    {
        Pattern pattern = Pattern.compile( "(.*) \\(\\d+\\+?\\)" );
        Matcher candidateMatcher = pattern.matcher( candidate );
        Matcher needleMatcher = pattern.matcher( needle );
        if ( candidateMatcher.matches() && !needleMatcher.matches() )
        {
            candidate = candidateMatcher.group( 1 );
        }
        return candidate.toUpperCase().equals( needle.toUpperCase() );
    }


    SWTBotTree getTree()
    {
        return bot.tree();
    }


    public void waitUntilEntryIsSelected( String label )
    {
        bot.waitUntil( new DefaultCondition()
        {

            @Override
            public boolean test() throws Exception
            {
                String selectedEntry = getSelectedEntry();
                return selectedEntry.equals( label );
            }


            @Override
            public String getFailureMessage()
            {
                return "Entry " + label + " was not selected, but " + getSelectedEntry();
            }
        } );
    }

}