1ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
2ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
3ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
4ffe3c632Sopenharmony_ci//
5ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
6ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
7ffe3c632Sopenharmony_ci// met:
8ffe3c632Sopenharmony_ci//
9ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
10ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
11ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
12ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
13ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
14ffe3c632Sopenharmony_ci// distribution.
15ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
16ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
17ffe3c632Sopenharmony_ci// this software without specific prior written permission.
18ffe3c632Sopenharmony_ci//
19ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ffe3c632Sopenharmony_ci
31ffe3c632Sopenharmony_cigoog.require('goog.crypt.base64');
32ffe3c632Sopenharmony_cigoog.require('goog.testing.asserts');
33ffe3c632Sopenharmony_ci// CommonJS-LoadFromFile: testbinary_pb proto.jspb.test
34ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.ForeignMessage');
35ffe3c632Sopenharmony_ci// CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test
36ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.Proto3Enum');
37ffe3c632Sopenharmony_cigoog.require('proto.jspb.test.TestProto3');
38ffe3c632Sopenharmony_ci// CommonJS-LoadFromFile: google/protobuf/any_pb proto.google.protobuf
39ffe3c632Sopenharmony_cigoog.require('proto.google.protobuf.Any');
40ffe3c632Sopenharmony_ci// CommonJS-LoadFromFile: google/protobuf/timestamp_pb proto.google.protobuf
41ffe3c632Sopenharmony_cigoog.require('proto.google.protobuf.Timestamp');
42ffe3c632Sopenharmony_ci// CommonJS-LoadFromFile: google/protobuf/struct_pb proto.google.protobuf
43ffe3c632Sopenharmony_cigoog.require('proto.google.protobuf.Struct');
44ffe3c632Sopenharmony_ci
45ffe3c632Sopenharmony_ci
46ffe3c632Sopenharmony_civar BYTES = new Uint8Array([1, 2, 8, 9]);
47ffe3c632Sopenharmony_civar BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
48ffe3c632Sopenharmony_ci
49ffe3c632Sopenharmony_ci
50ffe3c632Sopenharmony_ci/**
51ffe3c632Sopenharmony_ci * Helper: compare a bytes field to an expected value
52ffe3c632Sopenharmony_ci * @param {Uint8Array|string} arr
53ffe3c632Sopenharmony_ci * @param {Uint8Array} expected
54ffe3c632Sopenharmony_ci * @return {boolean}
55ffe3c632Sopenharmony_ci */
56ffe3c632Sopenharmony_cifunction bytesCompare(arr, expected) {
57ffe3c632Sopenharmony_ci  if (typeof arr === 'string') {
58ffe3c632Sopenharmony_ci    arr = goog.crypt.base64.decodeStringToUint8Array(arr);
59ffe3c632Sopenharmony_ci  }
60ffe3c632Sopenharmony_ci  if (arr.length != expected.length) {
61ffe3c632Sopenharmony_ci    return false;
62ffe3c632Sopenharmony_ci  }
63ffe3c632Sopenharmony_ci  for (var i = 0; i < arr.length; i++) {
64ffe3c632Sopenharmony_ci    if (arr[i] != expected[i]) {
65ffe3c632Sopenharmony_ci      return false;
66ffe3c632Sopenharmony_ci    }
67ffe3c632Sopenharmony_ci  }
68ffe3c632Sopenharmony_ci  return true;
69ffe3c632Sopenharmony_ci}
70ffe3c632Sopenharmony_ci
71ffe3c632Sopenharmony_ci
72ffe3c632Sopenharmony_cidescribe('proto3Test', function() {
73ffe3c632Sopenharmony_ci
74ffe3c632Sopenharmony_ci  /**
75ffe3c632Sopenharmony_ci   * Test default values don't affect equality test.
76ffe3c632Sopenharmony_ci   */
77ffe3c632Sopenharmony_ci  it('testEqualsProto3', function() {
78ffe3c632Sopenharmony_ci    var msg1 = new proto.jspb.test.TestProto3();
79ffe3c632Sopenharmony_ci    var msg2 = new proto.jspb.test.TestProto3();
80ffe3c632Sopenharmony_ci    msg2.setSingularString('');
81ffe3c632Sopenharmony_ci
82ffe3c632Sopenharmony_ci    assertTrue(jspb.Message.equals(msg1, msg2));
83ffe3c632Sopenharmony_ci  });
84ffe3c632Sopenharmony_ci
85ffe3c632Sopenharmony_ci
86ffe3c632Sopenharmony_ci  /**
87ffe3c632Sopenharmony_ci   * Test setting when a field has default semantics.
88ffe3c632Sopenharmony_ci   */
89ffe3c632Sopenharmony_ci  it('testSetProto3ToValueAndBackToDefault', function() {
90ffe3c632Sopenharmony_ci    var msg = new proto.jspb.test.TestProto3();
91ffe3c632Sopenharmony_ci
92ffe3c632Sopenharmony_ci    // Setting should work normally.
93ffe3c632Sopenharmony_ci    msg.setSingularString('optionalString');
94ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularString(), 'optionalString');
95ffe3c632Sopenharmony_ci
96ffe3c632Sopenharmony_ci    // Clearing should work too ...
97ffe3c632Sopenharmony_ci    msg.setSingularString('');
98ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularString(), '');
99ffe3c632Sopenharmony_ci
100ffe3c632Sopenharmony_ci    // ... and shouldn't affect the equality with a brand new message.
101ffe3c632Sopenharmony_ci    assertTrue(jspb.Message.equals(msg, new proto.jspb.test.TestProto3()));
102ffe3c632Sopenharmony_ci  });
103ffe3c632Sopenharmony_ci
104ffe3c632Sopenharmony_ci  /**
105ffe3c632Sopenharmony_ci   * Test defaults for proto3 message fields.
106ffe3c632Sopenharmony_ci   */
107ffe3c632Sopenharmony_ci  it('testProto3FieldDefaults', function() {
108ffe3c632Sopenharmony_ci    var msg = new proto.jspb.test.TestProto3();
109ffe3c632Sopenharmony_ci
110ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularInt32(), 0);
111ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularInt64(), 0);
112ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularUint32(), 0);
113ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularUint64(), 0);
114ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSint32(), 0);
115ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSint64(), 0);
116ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularFixed32(), 0);
117ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularFixed64(), 0);
118ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSfixed32(), 0);
119ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSfixed64(), 0);
120ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularFloat(), 0);
121ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularDouble(), 0);
122ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularString(), '');
123ffe3c632Sopenharmony_ci
124ffe3c632Sopenharmony_ci    // TODO(b/26173701): when we change bytes fields default getter to return
125ffe3c632Sopenharmony_ci    // Uint8Array, we'll want to switch this assertion to match the u8 case.
126ffe3c632Sopenharmony_ci    assertEquals(typeof msg.getSingularBytes(), 'string');
127ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularBytes_asU8() instanceof Uint8Array, true);
128ffe3c632Sopenharmony_ci    assertEquals(typeof msg.getSingularBytes_asB64(), 'string');
129ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularBytes().length, 0);
130ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularBytes_asU8().length, 0);
131ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularBytes_asB64(), '');
132ffe3c632Sopenharmony_ci
133ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularForeignEnum(),
134ffe3c632Sopenharmony_ci                 proto.jspb.test.Proto3Enum.PROTO3_FOO);
135ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularForeignMessage(), undefined);
136ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularForeignMessage(), undefined);
137ffe3c632Sopenharmony_ci
138ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedInt32List().length, 0);
139ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedInt64List().length, 0);
140ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedUint32List().length, 0);
141ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedUint64List().length, 0);
142ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedSint32List().length, 0);
143ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedSint64List().length, 0);
144ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedFixed32List().length, 0);
145ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedFixed64List().length, 0);
146ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedSfixed32List().length, 0);
147ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedSfixed64List().length, 0);
148ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedFloatList().length, 0);
149ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedDoubleList().length, 0);
150ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedStringList().length, 0);
151ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedBytesList().length, 0);
152ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedForeignEnumList().length, 0);
153ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedForeignMessageList().length, 0);
154ffe3c632Sopenharmony_ci  });
155ffe3c632Sopenharmony_ci
156ffe3c632Sopenharmony_ci  /**
157ffe3c632Sopenharmony_ci   * Test presence for proto3 optional fields.
158ffe3c632Sopenharmony_ci   */
159ffe3c632Sopenharmony_ci  it('testProto3Optional', function() {
160ffe3c632Sopenharmony_ci    var msg = new proto.jspb.test.TestProto3();
161ffe3c632Sopenharmony_ci
162ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalInt32(), 0);
163ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalInt64(), 0);
164ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalUint32(), 0);
165ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalUint64(), 0);
166ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalSint32(), 0);
167ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalSint64(), 0);
168ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalFixed32(), 0);
169ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalFixed64(), 0);
170ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalSfixed32(), 0);
171ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalSfixed64(), 0);
172ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalFloat(), 0);
173ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalDouble(), 0);
174ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalString(), '');
175ffe3c632Sopenharmony_ci
176ffe3c632Sopenharmony_ci    // TODO(b/26173701): when we change bytes fields default getter to return
177ffe3c632Sopenharmony_ci    // Uint8Array, we'll want to switch this assertion to match the u8 case.
178ffe3c632Sopenharmony_ci    assertEquals(typeof msg.getOptionalBytes(), 'string');
179ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalBytes_asU8() instanceof Uint8Array, true);
180ffe3c632Sopenharmony_ci    assertEquals(typeof msg.getOptionalBytes_asB64(), 'string');
181ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalBytes().length, 0);
182ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalBytes_asU8().length, 0);
183ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalBytes_asB64(), '');
184ffe3c632Sopenharmony_ci
185ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalForeignEnum(),
186ffe3c632Sopenharmony_ci                 proto.jspb.test.Proto3Enum.PROTO3_FOO);
187ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalForeignMessage(), undefined);
188ffe3c632Sopenharmony_ci    assertEquals(msg.getOptionalForeignMessage(), undefined);
189ffe3c632Sopenharmony_ci
190ffe3c632Sopenharmony_ci    // Serializing an empty proto yields the empty string.
191ffe3c632Sopenharmony_ci    assertEquals(msg.serializeBinary().length, 0);
192ffe3c632Sopenharmony_ci
193ffe3c632Sopenharmony_ci    // Values start as unset, but can be explicitly set even to default values
194ffe3c632Sopenharmony_ci    // like 0.
195ffe3c632Sopenharmony_ci    assertFalse(msg.hasOptionalInt32());
196ffe3c632Sopenharmony_ci    msg.setOptionalInt32(0);
197ffe3c632Sopenharmony_ci    assertTrue(msg.hasOptionalInt32());
198ffe3c632Sopenharmony_ci
199ffe3c632Sopenharmony_ci    assertFalse(msg.hasOptionalInt64());
200ffe3c632Sopenharmony_ci    msg.setOptionalInt64(0);
201ffe3c632Sopenharmony_ci    assertTrue(msg.hasOptionalInt64());
202ffe3c632Sopenharmony_ci
203ffe3c632Sopenharmony_ci    assertFalse(msg.hasOptionalString());
204ffe3c632Sopenharmony_ci    msg.setOptionalString("");
205ffe3c632Sopenharmony_ci    assertTrue(msg.hasOptionalString());
206ffe3c632Sopenharmony_ci
207ffe3c632Sopenharmony_ci    // Now the proto will have a non-zero size, even though its values are 0.
208ffe3c632Sopenharmony_ci    var serialized = msg.serializeBinary();
209ffe3c632Sopenharmony_ci    assertNotEquals(serialized.length, 0);
210ffe3c632Sopenharmony_ci
211ffe3c632Sopenharmony_ci    var msg2 = proto.jspb.test.TestProto3.deserializeBinary(serialized);
212ffe3c632Sopenharmony_ci    assertTrue(msg2.hasOptionalInt32());
213ffe3c632Sopenharmony_ci    assertTrue(msg2.hasOptionalInt64());
214ffe3c632Sopenharmony_ci    assertTrue(msg2.hasOptionalString());
215ffe3c632Sopenharmony_ci
216ffe3c632Sopenharmony_ci    // We can clear fields to go back to empty.
217ffe3c632Sopenharmony_ci    msg2.clearOptionalInt32();
218ffe3c632Sopenharmony_ci    assertFalse(msg2.hasOptionalInt32());
219ffe3c632Sopenharmony_ci
220ffe3c632Sopenharmony_ci    msg2.clearOptionalString();
221ffe3c632Sopenharmony_ci    assertFalse(msg2.hasOptionalString());
222ffe3c632Sopenharmony_ci  });
223ffe3c632Sopenharmony_ci
224ffe3c632Sopenharmony_ci  /**
225ffe3c632Sopenharmony_ci   * Test that all fields can be set ,and read via a serialization roundtrip.
226ffe3c632Sopenharmony_ci   */
227ffe3c632Sopenharmony_ci  it('testProto3FieldSetGet', function () {
228ffe3c632Sopenharmony_ci    var msg = new proto.jspb.test.TestProto3();
229ffe3c632Sopenharmony_ci
230ffe3c632Sopenharmony_ci    msg.setSingularInt32(-42);
231ffe3c632Sopenharmony_ci    msg.setSingularInt64(-0x7fffffff00000000);
232ffe3c632Sopenharmony_ci    msg.setSingularUint32(0x80000000);
233ffe3c632Sopenharmony_ci    msg.setSingularUint64(0xf000000000000000);
234ffe3c632Sopenharmony_ci    msg.setSingularSint32(-100);
235ffe3c632Sopenharmony_ci    msg.setSingularSint64(-0x8000000000000000);
236ffe3c632Sopenharmony_ci    msg.setSingularFixed32(1234);
237ffe3c632Sopenharmony_ci    msg.setSingularFixed64(0x1234567800000000);
238ffe3c632Sopenharmony_ci    msg.setSingularSfixed32(-1234);
239ffe3c632Sopenharmony_ci    msg.setSingularSfixed64(-0x1234567800000000);
240ffe3c632Sopenharmony_ci    msg.setSingularFloat(1.5);
241ffe3c632Sopenharmony_ci    msg.setSingularDouble(-1.5);
242ffe3c632Sopenharmony_ci    msg.setSingularBool(true);
243ffe3c632Sopenharmony_ci    msg.setSingularString('hello world');
244ffe3c632Sopenharmony_ci    msg.setSingularBytes(BYTES);
245ffe3c632Sopenharmony_ci    var submsg = new proto.jspb.test.ForeignMessage();
246ffe3c632Sopenharmony_ci    submsg.setC(16);
247ffe3c632Sopenharmony_ci    msg.setSingularForeignMessage(submsg);
248ffe3c632Sopenharmony_ci    msg.setSingularForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
249ffe3c632Sopenharmony_ci
250ffe3c632Sopenharmony_ci    msg.setRepeatedInt32List([-42]);
251ffe3c632Sopenharmony_ci    msg.setRepeatedInt64List([-0x7fffffff00000000]);
252ffe3c632Sopenharmony_ci    msg.setRepeatedUint32List([0x80000000]);
253ffe3c632Sopenharmony_ci    msg.setRepeatedUint64List([0xf000000000000000]);
254ffe3c632Sopenharmony_ci    msg.setRepeatedSint32List([-100]);
255ffe3c632Sopenharmony_ci    msg.setRepeatedSint64List([-0x8000000000000000]);
256ffe3c632Sopenharmony_ci    msg.setRepeatedFixed32List([1234]);
257ffe3c632Sopenharmony_ci    msg.setRepeatedFixed64List([0x1234567800000000]);
258ffe3c632Sopenharmony_ci    msg.setRepeatedSfixed32List([-1234]);
259ffe3c632Sopenharmony_ci    msg.setRepeatedSfixed64List([-0x1234567800000000]);
260ffe3c632Sopenharmony_ci    msg.setRepeatedFloatList([1.5]);
261ffe3c632Sopenharmony_ci    msg.setRepeatedDoubleList([-1.5]);
262ffe3c632Sopenharmony_ci    msg.setRepeatedBoolList([true]);
263ffe3c632Sopenharmony_ci    msg.setRepeatedStringList(['hello world']);
264ffe3c632Sopenharmony_ci    msg.setRepeatedBytesList([BYTES]);
265ffe3c632Sopenharmony_ci    submsg = new proto.jspb.test.ForeignMessage();
266ffe3c632Sopenharmony_ci    submsg.setC(1000);
267ffe3c632Sopenharmony_ci    msg.setRepeatedForeignMessageList([submsg]);
268ffe3c632Sopenharmony_ci    msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]);
269ffe3c632Sopenharmony_ci
270ffe3c632Sopenharmony_ci    msg.setOneofString('asdf');
271ffe3c632Sopenharmony_ci
272ffe3c632Sopenharmony_ci    var serialized = msg.serializeBinary();
273ffe3c632Sopenharmony_ci    msg = proto.jspb.test.TestProto3.deserializeBinary(serialized);
274ffe3c632Sopenharmony_ci
275ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularInt32(), -42);
276ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularInt64(), -0x7fffffff00000000);
277ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularUint32(), 0x80000000);
278ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularUint64(), 0xf000000000000000);
279ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSint32(), -100);
280ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSint64(), -0x8000000000000000);
281ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularFixed32(), 1234);
282ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularFixed64(), 0x1234567800000000);
283ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSfixed32(), -1234);
284ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularSfixed64(), -0x1234567800000000);
285ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularFloat(), 1.5);
286ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularDouble(), -1.5);
287ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularBool(), true);
288ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularString(), 'hello world');
289ffe3c632Sopenharmony_ci    assertEquals(true, bytesCompare(msg.getSingularBytes(), BYTES));
290ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularForeignMessage().getC(), 16);
291ffe3c632Sopenharmony_ci    assertEquals(msg.getSingularForeignEnum(),
292ffe3c632Sopenharmony_ci        proto.jspb.test.Proto3Enum.PROTO3_BAR);
293ffe3c632Sopenharmony_ci
294ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedInt32List(), [-42]);
295ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]);
296ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]);
297ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]);
298ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedSint32List(), [-100]);
299ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]);
300ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedFixed32List(), [1234]);
301ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]);
302ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]);
303ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]);
304ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedFloatList(), [1.5]);
305ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]);
306ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedBoolList(), [true]);
307ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedStringList(), ['hello world']);
308ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedBytesList().length, 1);
309ffe3c632Sopenharmony_ci    assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
310ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedForeignMessageList().length, 1);
311ffe3c632Sopenharmony_ci    assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000);
312ffe3c632Sopenharmony_ci    assertElementsEquals(msg.getRepeatedForeignEnumList(),
313ffe3c632Sopenharmony_ci        [proto.jspb.test.Proto3Enum.PROTO3_BAR]);
314ffe3c632Sopenharmony_ci
315ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofString(), 'asdf');
316ffe3c632Sopenharmony_ci  });
317ffe3c632Sopenharmony_ci
318ffe3c632Sopenharmony_ci
319ffe3c632Sopenharmony_ci  /**
320ffe3c632Sopenharmony_ci   * Test that oneofs continue to have a notion of field presence.
321ffe3c632Sopenharmony_ci   */
322ffe3c632Sopenharmony_ci  it('testOneofs', function() {
323ffe3c632Sopenharmony_ci    // Default instance.
324ffe3c632Sopenharmony_ci    var msg = new proto.jspb.test.TestProto3();
325ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofUint32(), 0);
326ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofForeignMessage(), undefined);
327ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofString(), '');
328ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofBytes(), '');
329ffe3c632Sopenharmony_ci
330ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofUint32());
331ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofForeignMessage());
332ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofString());
333ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofBytes());
334ffe3c632Sopenharmony_ci
335ffe3c632Sopenharmony_ci    // Integer field.
336ffe3c632Sopenharmony_ci    msg.setOneofUint32(42);
337ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofUint32(), 42);
338ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofForeignMessage(), undefined);
339ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofString(), '');
340ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofBytes(), '');
341ffe3c632Sopenharmony_ci
342ffe3c632Sopenharmony_ci    assertTrue(msg.hasOneofUint32());
343ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofForeignMessage());
344ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofString());
345ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofBytes());
346ffe3c632Sopenharmony_ci
347ffe3c632Sopenharmony_ci    // Sub-message field.
348ffe3c632Sopenharmony_ci    var submsg = new proto.jspb.test.ForeignMessage();
349ffe3c632Sopenharmony_ci    msg.setOneofForeignMessage(submsg);
350ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofUint32(), 0);
351ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofForeignMessage(), submsg);
352ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofString(), '');
353ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofBytes(), '');
354ffe3c632Sopenharmony_ci
355ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofUint32());
356ffe3c632Sopenharmony_ci    assertTrue(msg.hasOneofForeignMessage());
357ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofString());
358ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofBytes());
359ffe3c632Sopenharmony_ci
360ffe3c632Sopenharmony_ci    // String field.
361ffe3c632Sopenharmony_ci    msg.setOneofString('hello');
362ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofUint32(), 0);
363ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofForeignMessage(), undefined);
364ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofString(), 'hello');
365ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofBytes(), '');
366ffe3c632Sopenharmony_ci
367ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofUint32());
368ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofForeignMessage());
369ffe3c632Sopenharmony_ci    assertTrue(msg.hasOneofString());
370ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofBytes());
371ffe3c632Sopenharmony_ci
372ffe3c632Sopenharmony_ci    // Bytes field.
373ffe3c632Sopenharmony_ci    msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
374ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofUint32(), 0);
375ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofForeignMessage(), undefined);
376ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofString(), '');
377ffe3c632Sopenharmony_ci    assertEquals(msg.getOneofBytes_asB64(),
378ffe3c632Sopenharmony_ci        goog.crypt.base64.encodeString('\u00FF\u00FF'));
379ffe3c632Sopenharmony_ci
380ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofUint32());
381ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofForeignMessage());
382ffe3c632Sopenharmony_ci    assertFalse(msg.hasOneofString());
383ffe3c632Sopenharmony_ci    assertTrue(msg.hasOneofBytes());
384ffe3c632Sopenharmony_ci  });
385ffe3c632Sopenharmony_ci
386ffe3c632Sopenharmony_ci
387ffe3c632Sopenharmony_ci  /**
388ffe3c632Sopenharmony_ci   * Test that "default"-valued primitive fields are not emitted on the wire.
389ffe3c632Sopenharmony_ci   */
390ffe3c632Sopenharmony_ci  it('testNoSerializeDefaults', function() {
391ffe3c632Sopenharmony_ci    var msg = new proto.jspb.test.TestProto3();
392ffe3c632Sopenharmony_ci
393ffe3c632Sopenharmony_ci    // Set each primitive to a non-default value, then back to its default, to
394ffe3c632Sopenharmony_ci    // ensure that the serialization is actually checking the value and not just
395ffe3c632Sopenharmony_ci    // whether it has ever been set.
396ffe3c632Sopenharmony_ci    msg.setSingularInt32(42);
397ffe3c632Sopenharmony_ci    msg.setSingularInt32(0);
398ffe3c632Sopenharmony_ci    msg.setSingularDouble(3.14);
399ffe3c632Sopenharmony_ci    msg.setSingularDouble(0.0);
400ffe3c632Sopenharmony_ci    msg.setSingularBool(true);
401ffe3c632Sopenharmony_ci    msg.setSingularBool(false);
402ffe3c632Sopenharmony_ci    msg.setSingularString('hello world');
403ffe3c632Sopenharmony_ci    msg.setSingularString('');
404ffe3c632Sopenharmony_ci    msg.setSingularBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
405ffe3c632Sopenharmony_ci    msg.setSingularBytes('');
406ffe3c632Sopenharmony_ci    msg.setSingularForeignMessage(new proto.jspb.test.ForeignMessage());
407ffe3c632Sopenharmony_ci    msg.setSingularForeignMessage(null);
408ffe3c632Sopenharmony_ci    msg.setSingularForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
409ffe3c632Sopenharmony_ci    msg.setSingularForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO);
410ffe3c632Sopenharmony_ci    msg.setOneofUint32(32);
411ffe3c632Sopenharmony_ci    msg.clearOneofUint32();
412ffe3c632Sopenharmony_ci
413ffe3c632Sopenharmony_ci
414ffe3c632Sopenharmony_ci    var serialized = msg.serializeBinary();
415ffe3c632Sopenharmony_ci    assertEquals(0, serialized.length);
416ffe3c632Sopenharmony_ci  });
417ffe3c632Sopenharmony_ci
418ffe3c632Sopenharmony_ci  /**
419ffe3c632Sopenharmony_ci   * Test that base64 string and Uint8Array are interchangeable in bytes fields.
420ffe3c632Sopenharmony_ci   */
421ffe3c632Sopenharmony_ci  it('testBytesFieldsInterop', function() {
422ffe3c632Sopenharmony_ci    var msg = new proto.jspb.test.TestProto3();
423ffe3c632Sopenharmony_ci    // Set as a base64 string and check all the getters work.
424ffe3c632Sopenharmony_ci    msg.setSingularBytes(BYTES_B64);
425ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes_asU8(), BYTES));
426ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes_asB64(), BYTES));
427ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes(), BYTES));
428ffe3c632Sopenharmony_ci
429ffe3c632Sopenharmony_ci    // Test binary serialize round trip doesn't break it.
430ffe3c632Sopenharmony_ci    msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary());
431ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes_asU8(), BYTES));
432ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes_asB64(), BYTES));
433ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes(), BYTES));
434ffe3c632Sopenharmony_ci
435ffe3c632Sopenharmony_ci    msg = new proto.jspb.test.TestProto3();
436ffe3c632Sopenharmony_ci    // Set as a Uint8Array and check all the getters work.
437ffe3c632Sopenharmony_ci    msg.setSingularBytes(BYTES);
438ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes_asU8(), BYTES));
439ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes_asB64(), BYTES));
440ffe3c632Sopenharmony_ci    assertTrue(bytesCompare(msg.getSingularBytes(), BYTES));
441ffe3c632Sopenharmony_ci  });
442ffe3c632Sopenharmony_ci
443ffe3c632Sopenharmony_ci  it('testTimestampWellKnownType', function() {
444ffe3c632Sopenharmony_ci    var msg = new proto.google.protobuf.Timestamp();
445ffe3c632Sopenharmony_ci    msg.fromDate(new Date(123456789));
446ffe3c632Sopenharmony_ci    assertEquals(123456, msg.getSeconds());
447ffe3c632Sopenharmony_ci    assertEquals(789000000, msg.getNanos());
448ffe3c632Sopenharmony_ci    var date = msg.toDate();
449ffe3c632Sopenharmony_ci    assertEquals(123456789, date.getTime());
450ffe3c632Sopenharmony_ci    var anotherMsg = proto.google.protobuf.Timestamp.fromDate(date);
451ffe3c632Sopenharmony_ci    assertEquals(msg.getSeconds(), anotherMsg.getSeconds());
452ffe3c632Sopenharmony_ci    assertEquals(msg.getNanos(), anotherMsg.getNanos());
453ffe3c632Sopenharmony_ci  });
454ffe3c632Sopenharmony_ci
455ffe3c632Sopenharmony_ci  it('testStructWellKnownType', function() {
456ffe3c632Sopenharmony_ci    var jsObj = {
457ffe3c632Sopenharmony_ci      abc: "def",
458ffe3c632Sopenharmony_ci      number: 12345.678,
459ffe3c632Sopenharmony_ci      nullKey: null,
460ffe3c632Sopenharmony_ci      boolKey: true,
461ffe3c632Sopenharmony_ci      listKey: [1, null, true, false, "abc"],
462ffe3c632Sopenharmony_ci      structKey: {foo: "bar", somenum: 123},
463ffe3c632Sopenharmony_ci      complicatedKey: [{xyz: {abc: [3, 4, null, false]}}, "zzz"]
464ffe3c632Sopenharmony_ci    };
465ffe3c632Sopenharmony_ci
466ffe3c632Sopenharmony_ci    var struct = proto.google.protobuf.Struct.fromJavaScript(jsObj);
467ffe3c632Sopenharmony_ci    var jsObj2 = struct.toJavaScript();
468ffe3c632Sopenharmony_ci
469ffe3c632Sopenharmony_ci    assertEquals("def", jsObj2.abc);
470ffe3c632Sopenharmony_ci    assertEquals(12345.678, jsObj2.number);
471ffe3c632Sopenharmony_ci    assertEquals(null, jsObj2.nullKey);
472ffe3c632Sopenharmony_ci    assertEquals(true, jsObj2.boolKey);
473ffe3c632Sopenharmony_ci    assertEquals("abc", jsObj2.listKey[4]);
474ffe3c632Sopenharmony_ci    assertEquals("bar", jsObj2.structKey.foo);
475ffe3c632Sopenharmony_ci    assertEquals(4, jsObj2.complicatedKey[0].xyz.abc[1]);
476ffe3c632Sopenharmony_ci  });
477ffe3c632Sopenharmony_ci});
478