summaryrefslogtreecommitdiff
path: root/mojo/android/javatests/src/org/chromium/mojo/bindings/ValidationTestUtil.java
blob: 91b993c3b63dfe197aa3f11270eccd0d0a978e7a (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
// Copyright 2014 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 org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Utility class for testing message validation. The file format used to describe a message is
 * described in The format is described in
 * mojo/public/cpp/bindings/tests/validation_test_input_parser.h
 */
@JNINamespace("mojo::android")
public class ValidationTestUtil {

    /**
     * Content of a '.data' file.
     */
    public static class Data {
        private final ByteBuffer mData;
        private final int mHandlesCount;
        private final String mErrorMessage;

        public ByteBuffer getData() {
            return mData;
        }

        public int getHandlesCount() {
            return mHandlesCount;
        }

        public String getErrorMessage() {
            return mErrorMessage;
        }

        private Data(ByteBuffer data, int handlesCount, String errorMessage) {
            this.mData = data;
            this.mHandlesCount = handlesCount;
            this.mErrorMessage = errorMessage;
        }
    }

    /**
     * Parse a '.data' file.
     */
    public static Data parseData(String dataAsString) {
        return nativeParseData(dataAsString);
    }

    private static native Data nativeParseData(String dataAsString);

    @CalledByNative
    private static Data buildData(ByteBuffer data, int handlesCount, String errorMessage) {
        ByteBuffer copiedData = null;
        if (data != null) {
            copiedData = ByteBuffer.allocateDirect(data.limit());
            copiedData.order(ByteOrder.LITTLE_ENDIAN);
            copiedData.put(data);
            copiedData.flip();
        }
        return new Data(copiedData, handlesCount, errorMessage);
    }
}