Home | History | Annotate | Download | only in bindings
      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 
      5 package org.chromium.mojo.bindings;
      6 
      7 import org.chromium.mojo.TestUtils;
      8 import org.chromium.mojo.system.Handle;
      9 import org.chromium.mojo.system.MessagePipeHandle;
     10 import org.chromium.mojo.system.MojoException;
     11 import org.chromium.mojo.system.Pair;
     12 import org.chromium.mojo.system.impl.CoreImpl;
     13 
     14 import java.io.Closeable;
     15 import java.nio.ByteBuffer;
     16 import java.util.ArrayList;
     17 import java.util.List;
     18 
     19 /**
     20  * Utility class for bindings tests.
     21  */
     22 public class BindingsTestUtils {
     23 
     24     /**
     25      * {@link MessageReceiver} that records any message it receives.
     26      */
     27     public static class RecordingMessageReceiver extends SideEffectFreeCloseable
     28             implements MessageReceiver {
     29 
     30         public final List<Message> messages = new ArrayList<Message>();
     31 
     32         /**
     33          * @see MessageReceiver#accept(Message)
     34          */
     35         @Override
     36         public boolean accept(Message message) {
     37             messages.add(message);
     38             return true;
     39         }
     40     }
     41 
     42     /**
     43      * {@link MessageReceiverWithResponder} that records any message it receives.
     44      */
     45     public static class RecordingMessageReceiverWithResponder extends RecordingMessageReceiver
     46             implements MessageReceiverWithResponder {
     47 
     48         public final List<Pair<Message, MessageReceiver>> messagesWithReceivers =
     49                 new ArrayList<Pair<Message, MessageReceiver>>();
     50 
     51         /**
     52          * @see MessageReceiverWithResponder#acceptWithResponder(Message, MessageReceiver)
     53          */
     54         @Override
     55         public boolean acceptWithResponder(Message message, MessageReceiver responder) {
     56             messagesWithReceivers.add(Pair.create(message, responder));
     57             return true;
     58         }
     59     }
     60 
     61     /**
     62      * {@link ConnectionErrorHandler} that records any error it received.
     63      */
     64     public static class CapturingErrorHandler implements ConnectionErrorHandler {
     65 
     66         private MojoException mLastMojoException = null;
     67 
     68         /**
     69          * @see ConnectionErrorHandler#onConnectionError(MojoException)
     70          */
     71         @Override
     72         public void onConnectionError(MojoException e) {
     73             mLastMojoException = e;
     74         }
     75 
     76         /**
     77          * Returns the last recorded exception.
     78          */
     79         public MojoException getLastMojoException() {
     80             return mLastMojoException;
     81         }
     82 
     83     }
     84 
     85     /**
     86      * Creates a new valid {@link Message}. The message will have a valid header.
     87      */
     88     public static Message newRandomMessage(int size) {
     89         assert size > 16;
     90         ByteBuffer message = TestUtils.newRandomBuffer(size);
     91         int[] headerAsInts = {16, 2, 0, 0};
     92         for (int i = 0; i < 4; ++i) {
     93             message.putInt(4 * i, headerAsInts[i]);
     94         }
     95         message.position(0);
     96         return new Message(message, new ArrayList<Handle>());
     97     }
     98 
     99     public static <I extends Interface, P extends Interface.Proxy> P newProxyOverPipe(
    100             Interface.Manager<I, P> manager, I impl, List<Closeable> toClose) {
    101         Pair<MessagePipeHandle, MessagePipeHandle> handles =
    102                 CoreImpl.getInstance().createMessagePipe(null);
    103         P proxy = manager.attachProxy(handles.first, 0);
    104         toClose.add(proxy);
    105         manager.bind(impl, handles.second);
    106         return proxy;
    107     }
    108 }
    109