1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.mojo;
6
7import org.chromium.mojo.system.Core;
8import org.chromium.mojo.system.DataPipe;
9import org.chromium.mojo.system.DataPipe.ConsumerHandle;
10import org.chromium.mojo.system.DataPipe.ProducerHandle;
11import org.chromium.mojo.system.Handle;
12import org.chromium.mojo.system.MessagePipeHandle;
13import org.chromium.mojo.system.MojoResult;
14import org.chromium.mojo.system.SharedBufferHandle;
15import org.chromium.mojo.system.UntypedHandle;
16import org.chromium.mojo.system.impl.CoreImpl;
17
18import java.nio.ByteBuffer;
19import java.util.List;
20
21/**
22 * A mock handle, that does nothing.
23 */
24public class HandleMock implements UntypedHandle, MessagePipeHandle,
25        ProducerHandle, ConsumerHandle, SharedBufferHandle {
26
27    /**
28     * @see Handle#close()
29     */
30    @Override
31    public void close() {
32        // Do nothing.
33    }
34
35    /**
36     * @see Handle#wait(Core.HandleSignals, long)
37     */
38    @Override
39    public int wait(Core.HandleSignals signals, long deadline) {
40        // Do nothing.
41        return MojoResult.OK;
42    }
43
44    /**
45     * @see Handle#isValid()
46     */
47    @Override
48    public boolean isValid() {
49        return true;
50    }
51
52    /**
53     * @see Handle#toUntypedHandle()
54     */
55    @Override
56    public UntypedHandle toUntypedHandle() {
57        return this;
58    }
59
60    /**
61     * @see org.chromium.mojo.system.Handle#getCore()
62     */
63    @Override
64    public Core getCore() {
65        return CoreImpl.getInstance();
66    }
67
68    /**
69     * @see org.chromium.mojo.system.UntypedHandle#pass()
70     */
71    @Override
72    public HandleMock pass() {
73        return this;
74    }
75
76    /**
77     * @see Handle#releaseNativeHandle()
78     */
79    @Override
80    public int releaseNativeHandle() {
81        return 0;
82    }
83
84    /**
85     * @see ConsumerHandle#discardData(int, DataPipe.ReadFlags)
86     */
87    @Override
88    public int discardData(int numBytes, DataPipe.ReadFlags flags) {
89        // Do nothing.
90        return 0;
91    }
92
93    /**
94     * @see ConsumerHandle#readData(java.nio.ByteBuffer, DataPipe.ReadFlags)
95     */
96    @Override
97    public int readData(ByteBuffer elements,
98            DataPipe.ReadFlags flags) {
99        // Do nothing.
100        return 0;
101    }
102
103    /**
104     * @see ConsumerHandle#beginReadData(int, DataPipe.ReadFlags)
105     */
106    @Override
107    public ByteBuffer beginReadData(int numBytes,
108            DataPipe.ReadFlags flags) {
109        // Do nothing.
110        return null;
111    }
112
113    /**
114     * @see ConsumerHandle#endReadData(int)
115     */
116    @Override
117    public void endReadData(int numBytesRead) {
118        // Do nothing.
119    }
120
121    /**
122     * @see ProducerHandle#writeData(java.nio.ByteBuffer, DataPipe.WriteFlags)
123     */
124    @Override
125    public int writeData(ByteBuffer elements,
126            DataPipe.WriteFlags flags) {
127        // Do nothing.
128        return 0;
129    }
130
131    /**
132     * @see ProducerHandle#beginWriteData(int, DataPipe.WriteFlags)
133     */
134    @Override
135    public ByteBuffer beginWriteData(int numBytes,
136            DataPipe.WriteFlags flags) {
137        // Do nothing.
138        return null;
139    }
140
141    /**
142     * @see ProducerHandle#endWriteData(int)
143     */
144    @Override
145    public void endWriteData(int numBytesWritten) {
146        // Do nothing.
147    }
148
149    /**
150     * @see MessagePipeHandle#writeMessage(java.nio.ByteBuffer, java.util.List,
151     *      MessagePipeHandle.WriteFlags)
152     */
153    @Override
154    public void writeMessage(ByteBuffer bytes, List<? extends Handle> handles,
155            WriteFlags flags) {
156        // Do nothing.
157    }
158
159    /**
160     * @see MessagePipeHandle#readMessage(java.nio.ByteBuffer, int, MessagePipeHandle.ReadFlags)
161     */
162    @Override
163    public ReadMessageResult readMessage(ByteBuffer bytes, int maxNumberOfHandles,
164            ReadFlags flags) {
165        // Do nothing.
166        return new ReadMessageResult();
167    }
168
169    /**
170     * @see UntypedHandle#toMessagePipeHandle()
171     */
172    @Override
173    public MessagePipeHandle toMessagePipeHandle() {
174        return this;
175    }
176
177    /**
178     * @see UntypedHandle#toDataPipeConsumerHandle()
179     */
180    @Override
181    public ConsumerHandle toDataPipeConsumerHandle() {
182        return this;
183    }
184
185    /**
186     * @see UntypedHandle#toDataPipeProducerHandle()
187     */
188    @Override
189    public ProducerHandle toDataPipeProducerHandle() {
190        return this;
191    }
192
193    /**
194     * @see UntypedHandle#toSharedBufferHandle()
195     */
196    @Override
197    public SharedBufferHandle toSharedBufferHandle() {
198        return this;
199    }
200
201    /**
202     * @see SharedBufferHandle#duplicate(SharedBufferHandle.DuplicateOptions)
203     */
204    @Override
205    public SharedBufferHandle duplicate(DuplicateOptions options) {
206        // Do nothing.
207        return null;
208    }
209
210    /**
211     * @see SharedBufferHandle#map(long, long, SharedBufferHandle.MapFlags)
212     */
213    @Override
214    public ByteBuffer map(long offset, long numBytes, MapFlags flags) {
215        // Do nothing.
216        return null;
217    }
218
219    /**
220     * @see SharedBufferHandle#unmap(java.nio.ByteBuffer)
221     */
222    @Override
223    public void unmap(ByteBuffer buffer) {
224        // Do nothing.
225    }
226
227}
228