aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/AbstractComponentTestCase.java
blob: 8666956bdb0b48d9886ab1728162b45a8ece762b (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
package com.vaadin.tests.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.UserError;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Field;
import com.vaadin.ui.Layout.SpacingHandler;

public abstract class AbstractComponentTestCase<T extends AbstractComponent>
        extends TestBase {

    protected static final ThemeResource ICON_16_HELP_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/16/help.png");
    protected static final ThemeResource ICON_16_FOLDER_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/16/folder.png");
    protected static final ThemeResource ICON_16_ERROR_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/16/error.png");
    protected static final ThemeResource ICON_16_USER_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/16/user.png");
    protected static final ThemeResource ICON_16_USER_PNG_UNCACHEABLE = uncacheableThemeResource("../runo/icons/16/user.png");
    protected static final ThemeResource ICON_32_ATTENTION_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/32/attention.png");
    protected static final ThemeResource ICON_32_ATTENTION_PNG_UNCACHEABLE = uncacheableThemeResource("../runo/icons/32/attention.png");
    protected static final ThemeResource ICON_64_EMAIL_REPLY_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/64/email-reply.png");
    protected static final ThemeResource ICON_64_EMAIL_REPLY_PNG_UNCACHEABLE = uncacheableThemeResource("../runo/icons/64/email-reply.png");

    private List<T> testComponents = new ArrayList<T>();

    abstract protected Class<T> getTestClass();

    protected static ThemeResource uncacheableThemeResource(
            String resourceLocation) {
        return new ThemeResource(resourceLocation + "?" + new Date().getTime());
    }

    protected static ThemeResource cacheableThemeResource(
            String resourceLocation) {
        return new ThemeResource(resourceLocation);
    }

    abstract protected void initializeComponents();

    @Override
    protected void setup() {
        ((SpacingHandler) getLayout()).setSpacing(true);

        // Create Components
        initializeComponents();
    }

    @Override
    protected Integer getTicketNumber() {
        return null;
    }

    protected void addTestComponent(T c) {
        testComponents.add(c);
        addComponent(c);
    }

    protected List<T> getTestComponents() {
        return testComponents;
    }

    public interface Command<T, VALUETYPE extends Object> {
        public void execute(T c, VALUETYPE value, Object data);
    }

    /* COMMANDS */

    protected Command<T, String> widthCommand = new Command<T, String>() {

        @Override
        public void execute(T t, String value, Object data) {
            t.setWidth(value);
        }
    };
    protected Command<T, String> heightCommand = new Command<T, String>() {

        @Override
        public void execute(T t, String value, Object data) {
            t.setHeight(value);
        }
    };

    protected Command<T, Boolean> enabledCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean enabled, Object data) {
            c.setEnabled(enabled);
        }
    };

    protected Command<T, Boolean> immediateCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean immediate, Object data) {
            c.setImmediate(immediate);
        }
    };

    protected Command<T, Boolean> errorIndicatorCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean enabled, Object data) {
            if (enabled) {
                c.setComponentError(new UserError(errorMessage));
            } else {
                c.setComponentError(null);

            }
        }
    };
    private String errorMessage = null;

    protected Command<T, String> errorMessageCommand = new Command<T, String>() {

        @Override
        public void execute(T c, String value, Object data) {
            errorMessage = value;
            if (c.getComponentError() != null) {
                errorIndicatorCommand.execute(c, true, null);
            }

        }

    };

    // TODO Move to AbstractFieldTestCase
    protected Command<T, Boolean> requiredCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean enabled, Object data) {
            if (c instanceof Field) {
                ((Field<?>) c).setRequired(enabled);
            } else {
                throw new IllegalArgumentException(c.getClass().getName()
                        + " is not a field and cannot be set to required");
            }
        }
    };
    protected Command<T, String> requiredErrorMessageCommand = new Command<T, String>() {

        @Override
        public void execute(T c, String value, Object data) {
            ((Field<?>) c).setRequiredError(value);
        }

    };

    protected Command<T, String> descriptionCommand = new Command<T, String>() {
        @Override
        public void execute(T c, String value, Object data) {
            c.setDescription(value);
        }
    };

    protected Command<T, Boolean> readonlyCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean enabled, Object data) {
            c.setReadOnly(enabled);
        }
    };

    protected Command<T, Boolean> visibleCommand = new Command<T, Boolean>() {

        @Override
        public void execute(T c, Boolean enabled, Object data) {
            c.setVisible(enabled);
        }
    };

    protected Command<T, Resource> iconCommand = new Command<T, Resource>() {

        @Override
        public void execute(T c, Resource value, Object data) {
            c.setIcon(value);
        }

    };
    protected Command<T, String> captionCommand = new Command<T, String>() {

        @Override
        public void execute(T c, String value, Object data) {
            c.setCaption(value);
        }

    };

    protected Command<T, Locale> localeCommand = new Command<T, Locale>() {

        @Override
        public void execute(T c, Locale value, Object data) {
            c.setLocale(value);
        }

    };

    protected <VALUET> void doCommand(Command<T, VALUET> command, VALUET value) {
        doCommand(command, value, null);
    }

    protected <VALUET> void doCommand(Command<T, VALUET> command, VALUET value,
            Object data) {
        for (T c : getTestComponents()) {
            command.execute(c, value, data);
        }
    }

    protected <VALUET> void doCommand(String commandName,
            Command<T, VALUET> command, VALUET value) {
        doCommand(commandName, command, value, null);
    }

    protected <VALUET> void doCommand(String commandName,
            Command<T, VALUET> command, VALUET value, Object data) {
        doCommand(command, value, data);
    }

    protected Command<T, String> styleNameCommand = new Command<T, String>() {
        @Override
        public void execute(T c, String value, Object data) {
            c.setStyleName(value);
        }
    };

    protected Command<T, String> primaryStyleNameCommand = new Command<T, String>() {
        @Override
        public void execute(T c, String value, Object data) {
            c.setPrimaryStyleName(value);
        }
    };

    @Override
    protected String getDescription() {
        return "Generic test case for " + getTestClass().getSimpleName();
    }

}