summaryrefslogtreecommitdiff
path: root/mojo/public/java/system/javatests/src/org/chromium/mojo/bindings/InterfaceControlMessageTest.java
blob: fcd612137aa9ac9205efdf1a299bd7ef161905e6 (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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.mojo.bindings;

import android.support.test.filters.SmallTest;

import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.mojo.MojoTestRule;
import org.chromium.mojo.bindings.Callbacks.Callback1;
import org.chromium.mojo.bindings.test.mojom.sample.IntegerAccessor;
import org.chromium.mojo.system.MojoException;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Tests for interface control messages.
 */
@RunWith(BaseJUnit4ClassRunner.class)
public class InterfaceControlMessageTest {
    @Rule
    public MojoTestRule mTestRule = new MojoTestRule();

    private final List<Closeable> mCloseablesToClose = new ArrayList<Closeable>();

    /**
     * See mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.
     */
    class IntegerAccessorImpl extends SideEffectFreeCloseable implements IntegerAccessor {
        private long mValue = 0;
        private int mEnum = 0;
        private boolean mEncounteredError = false;

        /**
         * @see ConnectionErrorHandler#onConnectionError(MojoException)
         */
        @Override
        public void onConnectionError(MojoException e) {
            mEncounteredError = true;
        }

        /**
         * @see IntegerAccessor#getInteger(IntegerAccessor.GetIntegerResponse)
         */
        @Override
        public void getInteger(GetIntegerResponse response) {
            response.call(mValue, mEnum);
        }

        /**
         * @see IntegerAccessor#setInteger(long, int)
         */
        @Override
        public void setInteger(long value, int enumValue) {
            mValue = value;
            mEnum = enumValue;
        }

        public long getValue() {
            return mValue;
        }

        public boolean encounteredError() {
            return mEncounteredError;
        }
    }

    /**
     * @see MojoTestCase#tearDown()
     */
    @After
    public void tearDown() throws Exception {
        // Close the elements in the reverse order they were added. This is needed because it is an
        // error to close the handle of a proxy without closing the proxy first.
        Collections.reverse(mCloseablesToClose);
        for (Closeable c : mCloseablesToClose) {
            c.close();
        }
    }

    @Test
    @SmallTest
    public void testQueryVersion() {
        IntegerAccessor.Proxy p = BindingsTestUtils.newProxyOverPipe(
                IntegerAccessor.MANAGER, new IntegerAccessorImpl(), mCloseablesToClose);
        Assert.assertEquals(0, p.getProxyHandler().getVersion());
        p.getProxyHandler().queryVersion(new Callback1<Integer>() {
            @Override
            public void call(Integer version) {
                Assert.assertEquals(3, version.intValue());
            }
        });
        mTestRule.runLoopUntilIdle();
        Assert.assertEquals(3, p.getProxyHandler().getVersion());
    }

    @Test
    @SmallTest
    public void testRequireVersion() {
        IntegerAccessorImpl impl = new IntegerAccessorImpl();
        IntegerAccessor.Proxy p = BindingsTestUtils.newProxyOverPipe(
                IntegerAccessor.MANAGER, impl, mCloseablesToClose);

        Assert.assertEquals(0, p.getProxyHandler().getVersion());

        p.getProxyHandler().requireVersion(1);
        Assert.assertEquals(1, p.getProxyHandler().getVersion());
        p.setInteger(123, Enum.VALUE);
        mTestRule.runLoopUntilIdle();
        Assert.assertFalse(impl.encounteredError());
        Assert.assertEquals(123, impl.getValue());

        p.getProxyHandler().requireVersion(3);
        Assert.assertEquals(3, p.getProxyHandler().getVersion());
        p.setInteger(456, Enum.VALUE);
        mTestRule.runLoopUntilIdle();
        Assert.assertFalse(impl.encounteredError());
        Assert.assertEquals(456, impl.getValue());

        // Require a version that is not supported by the implementation side.
        p.getProxyHandler().requireVersion(4);
        // getVersion() is updated synchronously.
        Assert.assertEquals(4, p.getProxyHandler().getVersion());
        p.setInteger(789, Enum.VALUE);
        mTestRule.runLoopUntilIdle();
        Assert.assertTrue(impl.encounteredError());
        // The call to setInteger() after requireVersion() is ignored.
        Assert.assertEquals(456, impl.getValue());
    }
}