aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/ui/RichTextAreaTest.java
blob: f9c97df754f09166a98b1a909e111e03845dd3bc (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
/*
 * Copyright 2000-2018 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.ui;

import static com.vaadin.ui.ComponentTest.isDirty;
import static com.vaadin.ui.ComponentTest.syncToClient;
import static com.vaadin.ui.ComponentTest.updateDiffState;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.vaadin.server.ClientConnector;
import com.vaadin.server.ServerRpcManager;
import com.vaadin.server.ServerRpcManager.RpcInvocationException;
import com.vaadin.shared.ui.richtextarea.RichTextAreaServerRpc;
import com.vaadin.tests.util.MockUI;

public class RichTextAreaTest {

    @Test
    public void initiallyEmpty() {
        RichTextArea tf = new RichTextArea();
        assertTrue(tf.isEmpty());
    }

    @Test
    public void setValueServerWhenReadOnly() {
        RichTextArea tf = new RichTextArea();
        tf.setReadOnly(true);
        tf.setValue("foo");
        assertEquals("foo", tf.getValue());
    }

    @Test
    public void diffStateAfterClientSetValueWhenReadOnly() {
        UI ui = new MockUI();

        // If the client has a non-readonly text field which is set to read-only
        // on the server, then any update from the client must cause both the
        // readonly state and the old value to be sent
        RichTextArea rta = new RichTextArea();
        ui.setContent(rta);
        rta.setValue("bar");
        rta.setReadOnly(true);
        syncToClient(rta);

        // Client thinks the field says "foo" but it won't be updated because
        // the field is readonly
        ServerRpcManager.getRpcProxy(rta, RichTextAreaServerRpc.class)
                .setText("foo");

        // The real value will be sent back as long as the field is marked as
        // dirty and diffstate contains what the client has
        assertEquals("foo", getDiffStateString(rta, "value"));
        assertTrue("Component should be marked dirty", isDirty(rta));
    }

    @Test
    public void setValueClientNotSentBack() throws RpcInvocationException {
        UI ui = new MockUI();
        RichTextArea rta = new RichTextArea();
        ui.setContent(rta);
        rta.setValue("bar");

        updateDiffState(rta);
        ServerRpcManager.getRpcProxy(rta, RichTextAreaServerRpc.class)
                .setText("foo");
        assertEquals("foo", getDiffStateString(rta, "value"));
    }

    private String getDiffStateString(ClientConnector connector, String key) {
        return connector.getUI().getConnectorTracker().getDiffState(connector)
                .get(key).asString();
    }

    @Test
    public void setValueClientRefusedWhenReadOnly() {
        RichTextArea tf = new RichTextArea();
        tf.setValue("bar");
        tf.setReadOnly(true);
        tf.setValue("foo", true);
        assertEquals("bar", tf.getValue());
    }

    @Test(expected = NullPointerException.class)
    public void setValue_nullValue_throwsNPE() {
        RichTextArea tf = new RichTextArea();
        tf.setValue(null);
    }

    @Test
    public void emptyAfterClear() {
        RichTextArea tf = new RichTextArea();
        tf.setValue("foobar");
        assertFalse(tf.isEmpty());
        tf.clear();
        assertTrue(tf.isEmpty());
    }

}