aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/widgetset/client/EncoderResultDisplayConnector.java
blob: f2e97104042853b41623236509b38fccdb047f51 (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
/*
 * Copyright 2000-2014 Vaadin Ltd.
 * 
 * Licensed 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 com.vaadin.tests.widgetset.client;

import java.util.Arrays;
import java.util.List;

import com.vaadin.client.ServerConnector;
import com.vaadin.client.communication.JsonEncoder;
import com.vaadin.client.extensions.AbstractExtensionConnector;
import com.vaadin.client.metadata.Type;
import com.vaadin.client.metadata.TypeData;
import com.vaadin.shared.communication.ServerRpc;
import com.vaadin.shared.ui.Connect;
import com.vaadin.tests.serialization.EncodeResultDisplay.EncoderResultDisplayExtension;

import elemental.json.JsonValue;

@Connect(EncoderResultDisplayExtension.class)
public class EncoderResultDisplayConnector extends AbstractExtensionConnector {

    private ReportRpc reporter;

    public interface ReportRpc extends ServerRpc {
        public void report(String name, String encodedValue);
    }

    @Override
    protected void extend(ServerConnector target) {
        reporter = getRpcProxy(ReportRpc.class);

        reportEncode("My string");
        reportEncode(Character.valueOf('v'));
        reportEncode(Byte.valueOf((byte) 1));
        reportEncode(Integer.valueOf(3));
        reportEncode(Long.valueOf(Integer.MAX_VALUE + 1l));
        reportEncode(Float.valueOf((float) 1.1));
        reportEncode(Double.valueOf("2.2"));

        reportEncode(new String[] { "One", "Two" });
        reportEncode(
                "List",
                Arrays.asList("Three", "Four"),
                new Type(List.class.getName(), new Type[] { TypeData
                        .getType(String.class) }));
        reportEncode(new SimpleTestBean(5));

        reportEncode(Void.class.getSimpleName(), null,
                TypeData.getType(Void.class));
    }

    private void reportEncode(Object value) {
        Type type = TypeData.getType(value.getClass());
        reportEncode(value.getClass().getSimpleName(), value, type);
    }

    private void reportEncode(String name, Object value, Type type) {
        JsonValue encodedValue = JsonEncoder.encode(value, type,
                getConnection());
        reporter.report(name, encodedValue.toJson());
    }

}