1// Copyright 2013 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[JavaPackage="org.chromium.mojo.bindings.test.mojom.test_structs"]
6module mojo.test;
7
8import "mojo/public/interfaces/bindings/tests/rect.mojom";
9
10struct NamedRegion {
11  string? name;
12  array<Rect>? rects;
13};
14
15struct RectPair {
16  Rect? first;
17  Rect? second;
18};
19
20struct EmptyStruct {
21};
22
23[Native]
24struct UnmappedNativeStruct;
25
26// Used to verify that struct fields which don't specify a default are
27// initialized to: false for bool, 0 for numbers, and null for strings,
28// handles, and structs. The "?" nullable suffix shouldn't have any
29// impact on initial field values.
30
31struct NoDefaultFieldValues {
32  bool f0;
33  int8 f1;
34  uint8 f2;
35  int16 f3;
36  uint16 f4;
37  int32 f5;
38  uint32 f6;
39  int64 f7;
40  uint64 f8;
41  float f9;
42  double f10;
43  string f11;
44  string? f12;
45  handle<message_pipe> f13;
46  handle<data_pipe_consumer> f14;
47  handle<data_pipe_producer> f15;
48  handle<message_pipe>? f16;
49  handle<data_pipe_consumer>? f17;
50  handle<data_pipe_producer>? f18;
51  handle f19;
52  handle? f20;
53  handle<shared_buffer> f21;
54  handle<shared_buffer>? f22;
55  array<string> f23;
56  array<string?> f24;
57  array<string>? f25;
58  array<string?>? f26;
59  EmptyStruct f27;
60  EmptyStruct? f28;
61};
62
63// Used to verify that struct fields with an explicit default value
64// are initialized correctly. The "?" nullable suffix shouldn't have any
65// impact on initial field values.
66
67struct DefaultFieldValues {
68  const string kFoo = "foo";
69  bool f0 = true;
70  int8 f1 = 100;
71  uint8 f2 = 100;
72  int16 f3 = 100;
73  uint16 f4 = 100;
74  int32 f5 = 100;
75  uint32 f6 = 100;
76  int64 f7 = 100;
77  uint64 f8 = 100;
78  float f9 = 100;
79  float f10 = 100.0;
80  double f11 = 100;
81  double f12 = 100.0;
82  string f13 = kFoo;
83  string? f14 = kFoo;
84  Rect f15 = default;
85  Rect? f16 = default;
86};
87
88// Used to verify that the code generated for enum and const values defined
89// within a struct is correct. Assuming that a constant's value can be a literal
90// or another constant and that enum values can either be an integer constant or
91// another value from the same enum type.
92
93struct ScopedConstants {
94  const int32 TEN = 10;
95  const int32 ALSO_TEN = TEN;
96  enum EType {
97    E0,
98    E1,
99    E2 = 10,
100    E3 = E2,
101    E4,
102  };
103  EType f0 = E0; // 0
104  EType f1 = E1; // 1
105  EType f2 = E2; // 10
106  EType f3 = E3; // 10
107  EType f4 = E4; // 11
108  int32 f5 = TEN;
109  int32 f6 = ALSO_TEN;
110};
111
112// Used to verify that all possible Map key field types can be encoded and
113// decoded successfully.
114
115struct MapKeyTypes {
116  // TODO(yzshen): WTF::HashMap doesn't support bool as key.
117  // map<bool, bool> f0;
118  map<int8, int8> f1;
119  map<uint8, uint8> f2;
120  map<int16, int16> f3;
121  map<uint16, uint16> f4;
122  map<int32, int32> f5;
123  map<uint32, uint32> f6;
124  map<int64, int64> f7;
125  map<uint64, uint64> f8;
126  map<float, float> f9;
127  map<double, double> f10;
128  map<string, string> f11;
129  // TODO(crbug.com/628104): JS doesn't support struct as key.  map<Rect, Rect>
130  // f12;
131};
132
133// Used to verify that various map value types can be encoded and decoded
134// successfully.
135
136struct MapValueTypes {
137  map<string, array<string>> f0;
138  map<string, array<string>?> f1;
139  map<string, array<string?>> f2;
140  map<string, array<string, 2>> f3;
141  map<string, array<array<string, 2>?>> f4;
142  map<string, array<array<string, 2>, 1>> f5;
143  map<string, Rect?> f6;
144  map<string, map<string, string>> f7;
145  map<string, array<map<string, string>>> f8;
146  map<string, handle> f9;
147  map<string, array<handle>> f10;
148  map<string, map<string, handle>> f11;
149};
150
151// Used to verify that various array types can be encoded and decoded
152// successfully.
153
154struct ArrayValueTypes {
155  array<int8> f0;
156  array<int16> f1;
157  array<int32> f2;
158  array<int64> f3;
159  array<float> f4;
160  array<double> f5;
161  array<SomeInterface> f6;
162  array<SomeInterface&> f7;
163};
164
165// Used to verify that various float and double values can be encoded and
166// decoded correctly.
167
168struct FloatNumberValues {
169  const double V0 = double.INFINITY;
170  const double V1 = double.NEGATIVE_INFINITY;
171  const double V2 = double.NAN;
172  const float V3 = float.INFINITY;
173  const float V4 = float.NEGATIVE_INFINITY;
174  const float V5 = float.NAN;
175  const float V6 = 0;
176  const double V7 = 1234567890.123;
177  const double V8 = 1.2E+20;
178  const double V9 = -1.2E+20;
179
180  double f0 = V0;
181  double f1 = V1;
182  double f2 = V2;
183  float f3 = V3;
184  float f4 = V4;
185  float f5 = V5;
186  float f6 = V6;
187  double f7 = V7;
188  double f8 = V8;
189  double f9 = V9;
190};
191
192// Used to verify that various signed integer values can be encoded and
193// decoded correctly.
194
195struct IntegerNumberValues {
196  const int8 V0 = -128; // Minimum
197  const int8 V1 = -1;   // -1
198  const int8 V2 = 0;    // 0
199  const int8 V3 = 42;   // An arbitrary valid value.
200  const int8 V4 = 127;  // Maximum
201
202  const int16 V5 = -32768; // ...
203  const int16 V6 = -1;
204  const int16 V7 = 0;
205  const int16 V8 = 12345;
206  const int16 V9 = 32767;
207
208  const int32 V10 = -2147483648;
209  const int32 V11 = -1;
210  const int32 V12 = 0;
211  const int32 V13 = 1234567890;
212  const int32 V14 = 2147483647;
213
214  // The limits for JavaScript integers are +/- (2^53 - 1).
215  const int64 V15 = -9007199254740991; // Number.MIN_SAFE_INTEGER
216  const int64 V16 = -1;
217  const int64 V17 = 0;
218  const int64 V18 = 1234567890123456;
219  const int64 V19 = 9007199254740991; // Number.MAX_SAFE_INTEGER
220
221  int8 f0 = V0;
222  int8 f1 = V1;
223  int8 f2 = V2;
224  int8 f3 = V3;
225  int8 f4 = V4;
226
227  int16 f5 = V5;
228  int16 f6 = V6;
229  int16 f7 = V7;
230  int16 f8 = V8;
231  int16 f9 = V9;
232
233  int32 f10 = V10;
234  int32 f11 = V11;
235  int32 f12 = V12;
236  int32 f13 = V13;
237  int32 f14 = V14;
238
239  int64 f15 = V15;
240  int64 f16 = V16;
241  int64 f17 = V17;
242  int64 f18 = V18;
243  int64 f19 = V19;
244};
245
246// Used to verify that various unsigned integer values can be encoded and
247// decoded correctly.
248
249struct UnsignedNumberValues {
250  const uint8 V0 = 0;    // Minimum = 0.
251  const uint8 V1 = 42;   // An arbitrary valid value.
252  const uint8 V2 = 0xFF; // Maximum
253
254  const uint16 V3 = 0; // ...
255  const uint16 V4 = 12345;
256  const uint16 V5 = 0xFFFF;
257
258  const uint32 V6 = 0;
259  const uint32 V7 = 1234567890;
260  const uint32 V8 = 0xFFFFFFFF;
261
262  // The limits for JavaScript integers are +/- (2^53 - 1).
263  const uint64 V9 = 0;
264  const uint64 V10 = 1234567890123456;
265  const uint64 V11 = 9007199254740991; // Number.MAX_SAFE_INTEGER
266
267  uint8 f0 = V0;
268  uint8 f1 = V1;
269  uint8 f2 = V2;
270
271  uint16 f3 = V3;
272  uint16 f4 = V4;
273  uint16 f5 = V5;
274
275  uint32 f6 = V6;
276  uint32 f7  = V7;
277  uint32 f8 = V8;
278
279  uint64 f9 = V9;
280  uint64 f10 = V10;
281  uint64 f11 = V11;
282};
283
284// Used to verify that various (packed) boolean array values can be encoded
285// and decoded correctly.
286
287struct BitArrayValues {
288  array<bool, 1> f0;
289  array<bool, 7> f1;
290  array<bool, 9> f2;
291  array<bool> f3;
292  array<array<bool>> f4;
293  array<array<bool>?> f5;
294  array<array<bool, 2>?> f6;
295};
296
297// Used to verify that different versions can be decoded correctly.
298
299struct MultiVersionStruct {
300  [MinVersion=0]
301  int32 f_int32;
302  [MinVersion=1]
303  Rect? f_rect;
304  [MinVersion=3]
305  string? f_string;
306  [MinVersion=5]
307  array<int8>? f_array;
308  [MinVersion=7]
309  handle<message_pipe>? f_message_pipe;
310  [MinVersion=7]
311  bool f_bool;
312  [MinVersion=9]
313  int16 f_int16;
314};
315
316struct MultiVersionStructV0 {
317  [MinVersion=0]
318  int32 f_int32;
319};
320
321struct MultiVersionStructV1 {
322  [MinVersion=0]
323  int32 f_int32;
324  [MinVersion=1]
325  Rect? f_rect;
326};
327
328struct MultiVersionStructV3 {
329  [MinVersion=0]
330  int32 f_int32;
331  [MinVersion=1]
332  Rect? f_rect;
333  [MinVersion=3]
334  string? f_string;
335};
336
337struct MultiVersionStructV5 {
338  [MinVersion=0]
339  int32 f_int32;
340  [MinVersion=1]
341  Rect? f_rect;
342  [MinVersion=3]
343  string? f_string;
344  [MinVersion=5]
345  array<int8>? f_array;
346};
347
348struct MultiVersionStructV7 {
349  [MinVersion=0]
350  int32 f_int32;
351  [MinVersion=1]
352  Rect? f_rect;
353  [MinVersion=3]
354  string? f_string;
355  [MinVersion=5]
356  array<int8>? f_array;
357  [MinVersion=7]
358  handle<message_pipe>? f_message_pipe;
359  [MinVersion=7]
360  bool f_bool;
361};
362
363// A struct where the fields are not sorted by their ordinals.
364struct ReorderedStruct {
365  [MinVersion=2]
366  int32 a@3 = 3;
367  [MinVersion=4]
368  int32 b@6 = 6;
369  [MinVersion=1]
370  int32 c@1 = 1;
371};
372
373// Used to verify that interfaces that are struct members can be defined in the
374// same file.
375
376interface SomeInterface {
377  SomeMethod(RectPair pair) => (RectPair other_pair);
378};
379
380struct ContainsInterface {
381  SomeInterface some_interface;
382};
383
384// Verify that a field can be called |other|.
385
386struct ContainsOther {
387  int32 other;
388};
389
390// Used to verify that structs can contain interface requests.
391
392struct ContainsInterfaceRequest {
393  SomeInterface& request;
394};
395
396// Used to verify that boolean fields are correctly serialized/deserialized.
397
398struct SingleBoolStruct {
399  bool value;
400};
401
402// Used to verify that structs containing typemapped types can be hashed (if the
403// typemapped type itself is hashable).
404
405struct ContainsHashable {
406  TypemappedRect rect;
407};
408
409// Used to test that nested structs can be hashed. The nested struct mustn't be
410// nullable.
411
412struct SimpleNestedStruct {
413  ContainsOther nested;
414};
415