• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.base.annotations.CalledByNative;
8 import org.chromium.base.annotations.JNINamespace;
9 
10 import java.nio.ByteBuffer;
11 import java.nio.ByteOrder;
12 
13 /**
14  * Utility class for testing message validation. The file format used to describe a message is
15  * described in The format is described in
16  * mojo/public/cpp/bindings/tests/validation_test_input_parser.h
17  */
18 @JNINamespace("mojo::android")
19 public class ValidationTestUtil {
20 
21     /**
22      * Content of a '.data' file.
23      */
24     public static class Data {
25         private final ByteBuffer mData;
26         private final int mHandlesCount;
27         private final String mErrorMessage;
28 
getData()29         public ByteBuffer getData() {
30             return mData;
31         }
32 
getHandlesCount()33         public int getHandlesCount() {
34             return mHandlesCount;
35         }
36 
getErrorMessage()37         public String getErrorMessage() {
38             return mErrorMessage;
39         }
40 
Data(ByteBuffer data, int handlesCount, String errorMessage)41         private Data(ByteBuffer data, int handlesCount, String errorMessage) {
42             this.mData = data;
43             this.mHandlesCount = handlesCount;
44             this.mErrorMessage = errorMessage;
45         }
46     }
47 
48     /**
49      * Parse a '.data' file.
50      */
parseData(String dataAsString)51     public static Data parseData(String dataAsString) {
52         return nativeParseData(dataAsString);
53     }
54 
nativeParseData(String dataAsString)55     private static native Data nativeParseData(String dataAsString);
56 
57     @CalledByNative
buildData(ByteBuffer data, int handlesCount, String errorMessage)58     private static Data buildData(ByteBuffer data, int handlesCount, String errorMessage) {
59         ByteBuffer copiedData = null;
60         if (data != null) {
61             copiedData = ByteBuffer.allocateDirect(data.limit());
62             copiedData.order(ByteOrder.LITTLE_ENDIAN);
63             copiedData.put(data);
64             copiedData.flip();
65         }
66         return new Data(copiedData, handlesCount, errorMessage);
67     }
68 }
69