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

SwtResourcesTest.java « 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: 5a76bec725325bf0394efd8a270e3d175f9daeef (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
/*
 *  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;


import static org.apache.directory.studio.test.integration.junit5.TestFixture.MISC_DN;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import org.apache.directory.studio.test.integration.junit5.LdapServersSource;
import org.apache.directory.studio.test.integration.junit5.TestLdapServer;
import org.apache.directory.studio.test.integration.ui.bots.DeleteDialogBot;
import org.apache.directory.studio.test.integration.ui.bots.NewEntryWizardBot;
import org.eclipse.swt.graphics.DeviceData;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.IntResult;
import org.junit.jupiter.params.ParameterizedTest;


/**
 * Tests allocation of SWT Resources.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public class SwtResourcesTest extends AbstractTestBase
{

    /**
     * Test for DIRSTUDIO-319.
     *
     * Creates multiple entries using the New Entry wizard. Checks that we don't
     * allocate too much SWT resources during the run.
     */
    @ParameterizedTest
    @LdapServersSource
    public void testSwtResourcesDelta( TestLdapServer server ) throws Exception
    {
        connectionsViewBot.createTestConnection( server );

        // run the new entry wizard once to ensure all SWT resources are created
        createAndDeleteEntry( "testSwtResourcesDelta" + 0 );

        // remember the SWT objects before the run
        int beforeObjectCount = getSwtObjectCount();

        // now lets run the new entry wizard it several times
        for ( int i = 1; i <= 8; i++ )
        {
            createAndDeleteEntry( "testSwtResourcesDelta" + i );
        }

        // get the SWT objects after the run
        int afterObjectCount = getSwtObjectCount();

        // we expect none or only very few additional SWT objects
        assertTrue( afterObjectCount - beforeObjectCount < 5,
            "Too many SWT resources were allocated in testSwtResourcesDelta: before=" + beforeObjectCount
                + ", after=" + afterObjectCount );
    }


    /**
     * Ensure that we have not allocated too many SWT resources during the
     * complete test suite.
     */
    @ParameterizedTest
    @LdapServersSource
    public void testSwtResourcesCount( TestLdapServer server ) throws Exception
    {
        connectionsViewBot.createTestConnection( server );
        int swtObjectCount = getSwtObjectCount();
        System.out.println( "### SWT resouces count: " + swtObjectCount );
        assertTrue( swtObjectCount < 1500, "Too many SWT resources were allocated: " + swtObjectCount );
    }


    private int getSwtObjectCount()
    {
        final SWTBot bot = new SWTBot();
        return UIThreadRunnable.syncExec( bot.getDisplay(), new IntResult()
        {
            public Integer run()
            {
                DeviceData info = bot.getDisplay().getDeviceData();
                if ( !info.tracking )
                {
                    fail(
                        "To run this test options 'org.eclipse.ui/debug' and 'org.eclipse.ui/trace/graphics' must be true." );
                }
                return info.objects.length;
            }
        } );
    }


    private void createAndDeleteEntry( final String name ) throws Exception
    {
        browserViewBot.selectAndExpandEntry( path( MISC_DN ) );
        NewEntryWizardBot wizardBot = browserViewBot.openNewEntryWizard();

        wizardBot.selectCreateEntryFromScratch();
        wizardBot.clickNextButton();

        wizardBot.addObjectClasses( "organization" );
        wizardBot.clickNextButton();

        wizardBot.setRdnType( 1, "o" );
        wizardBot.setRdnValue( 1, name );
        wizardBot.clickNextButton();

        wizardBot.clickFinishButton();

        assertTrue( browserViewBot.existsEntry( path( MISC_DN, "o=" + name ) ) );
        browserViewBot.selectEntry( path( MISC_DN, "o=" + name ) );
        DeleteDialogBot dialog = browserViewBot.openDeleteDialog();
        dialog.clickOkButton();
    }

}