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.bindings;
6
7import android.test.suitebuilder.annotation.SmallTest;
8
9import junit.framework.TestCase;
10
11import org.chromium.mojo.HandleMock;
12import org.chromium.mojo.bindings.test.mojom.mojo.Struct1;
13import org.chromium.mojo.bindings.test.mojom.mojo.Struct2;
14import org.chromium.mojo.bindings.test.mojom.mojo.Struct3;
15import org.chromium.mojo.bindings.test.mojom.mojo.Struct4;
16import org.chromium.mojo.bindings.test.mojom.mojo.Struct5;
17import org.chromium.mojo.bindings.test.mojom.mojo.Struct6;
18import org.chromium.mojo.bindings.test.mojom.mojo.StructOfNullables;
19
20/**
21 * Tests for the serialization logic of the generated structs, using structs defined in
22 * mojo/public/interfaces/bindings/tests/serialization_test_structs.mojom .
23 */
24public class SerializationTest extends TestCase {
25
26    private static void assertThrowsSerializationException(Struct struct) {
27        try {
28            struct.serialize(null);
29            fail("Serialization of invalid struct should have thrown an exception.");
30        } catch (SerializationException ex) {
31            // Expected.
32        }
33    }
34
35    /**
36     * Verifies that serializing a struct with an invalid handle of a non-nullable type throws an
37     * exception.
38     */
39    @SmallTest
40    public void testHandle() {
41        Struct2 struct = new Struct2();
42        assertFalse(struct.hdl.isValid());
43        assertThrowsSerializationException(struct);
44
45        // Make the struct valid and verify that it serializes without an exception.
46        struct.hdl = new HandleMock();
47        struct.serialize(null);
48    }
49
50    /**
51     * Verifies that serializing a struct with a null struct pointer throws an exception.
52     */
53    @SmallTest
54    public void testStructPointer() {
55        Struct3 struct = new Struct3();
56        assertNull(struct.struct1);
57        assertThrowsSerializationException(struct);
58
59        // Make the struct valid and verify that it serializes without an exception.
60        struct.struct1 = new Struct1();
61        struct.serialize(null);
62    }
63
64    /**
65     * Verifies that serializing a struct with an array of structs throws an exception when the
66     * struct is invalid.
67     */
68    @SmallTest
69    public void testStructArray() {
70        Struct4 struct = new Struct4();
71        assertNull(struct.array);
72        assertThrowsSerializationException(struct);
73
74        // Create the (1-element) array but have the element null.
75        struct.array = new Struct1[1];
76        assertThrowsSerializationException(struct);
77
78        // Create the array element, struct should serialize now.
79        struct.array[0] = new Struct1();
80        struct.serialize(null);
81    }
82
83    /**
84     * Verifies that serializing a struct with a fixed-size array of incorrect length throws an
85     * exception.
86     */
87    @SmallTest
88    public void testFixedSizeArray() {
89        Struct5 struct = new Struct5();
90        assertNull(struct.pair);
91        assertThrowsSerializationException(struct);
92
93        // Create the (1-element) array, 2-element array is required.
94        struct.pair = new Struct1[1];
95        struct.pair[0] = new Struct1();
96        assertThrowsSerializationException(struct);
97
98        // Create the array of a correct size, struct should serialize now.
99        struct.pair = new Struct1[2];
100        struct.pair[0] = new Struct1();
101        struct.pair[1] = new Struct1();
102        struct.serialize(null);
103    }
104
105    /**
106     * Verifies that serializing a struct with a null string throws an exception.
107     */
108    @SmallTest
109    public void testString() {
110        Struct6 struct = new Struct6();
111        assertNull(struct.str);
112        assertThrowsSerializationException(struct);
113
114        // Make the struct valid and verify that it serializes without an exception.
115        struct.str = "";
116        struct.serialize(null);
117    }
118
119    /**
120     * Verifies that a struct with an invalid nullable handle, null nullable struct pointer and null
121     * nullable string serializes without an exception.
122     */
123    @SmallTest
124    public void testNullableFields() {
125        StructOfNullables struct = new StructOfNullables();
126        assertFalse(struct.hdl.isValid());
127        assertNull(struct.struct1);
128        assertNull(struct.str);
129        struct.serialize(null);
130    }
131}
132