chromium/third_party/blink/web_tests/http/tests/mojo/versioned-optional-numerics.html

<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script type="module">
import {
  InterfaceV0Remote,
  InterfaceV0Receiver,
  InterfaceV2Remote,
  InterfaceV2Receiver,
  RegularEnum,
} from "/gen/content/web_test/common/mojo_optional_numerics_unittest.mojom.m.js"

BigInt.prototype.toJSON = () => 'bigint';

class InterfaceV0Impl {
  constructor() {
    this.receiver = new InterfaceV0Receiver(this);
  }

  async methodWithVersionedParams(...args) {
    assert_equals(args.length, 0);
    return {};
  }

  async methodWithVersionedStruct(s) {
    assert_equals(Object.keys(s).length, 0);
    return {s: {}};
  }
}

class InterfaceV2Impl {
  constructor() {
    this.receiver = new InterfaceV2Receiver(this);
  }

  async methodWithVersionedParams(
    boolValue, uint8Value, int8Value, uint16Value, int16Value, uint32Value,
    int32Value, uint64Value, int64Value, floatValue, doubleValue, enumValue) {
    return this.transform({
      boolValue, uint8Value, int8Value, uint16Value, int16Value, uint32Value,
      int32Value, uint64Value, int64Value, floatValue, doubleValue, enumValue});
  }

  async methodWithVersionedStruct(s) {
    const transformedStruct = this.transform(s);
    return {s: transformedStruct};
  }

  transform({
    boolValue, uint8Value, int8Value, uint16Value, int16Value, uint32Value,
    int32Value, uint64Value, int64Value, floatValue, doubleValue, enumValue}) {
    return {
      boolValue: boolValue === null ? null : !boolValue,
      uint8Value: uint8Value === null ? null : uint8Value * 2,
      int8Value: int8Value === null ? null : int8Value * 2,
      uint16Value: uint16Value === null ? null : uint16Value * 2,
      int16Value: int16Value === null ? null : int16Value * 2,
      uint32Value: uint32Value === null ? null : uint32Value * 2,
      int32Value: int32Value === null ? null : int32Value * 2,
      uint64Value: uint64Value === null ? null : uint64Value * BigInt('2'),
      int64Value: int64Value === null ? null : int64Value * BigInt('2'),
      floatValue: floatValue === null ? null : floatValue * 3,
      doubleValue: doubleValue === null ? null : doubleValue * 3,
      enumValue: enumValue === null ? null : enumValue - 1,
    };
  }
}

function getV0RemoteAndV0Impl() {
  const v0Impl = new InterfaceV0Impl;
  const v0Remote = v0Impl.receiver.$.bindNewPipeAndPassRemote();

  return {v0Remote, v0Impl};
}

function getV2RemoteAndV0Impl() {
  const v0Impl = new InterfaceV0Impl;
  const v2Remote = new InterfaceV2Remote;
  const pendingReceiver = v2Remote.$.bindNewPipeAndPassReceiver();
  v0Impl.receiver.$.bindHandle(pendingReceiver.handle);

  return {v2Remote, v0Impl}
}

function getV0RemoteAndV2Impl() {
  const v0Remote = new InterfaceV0Remote;
  const v2Impl = new InterfaceV2Impl;
  const pendingReceiver = v0Remote.$.bindNewPipeAndPassReceiver();
  v2Impl.receiver.$.bindHandle(pendingReceiver.handle);

  return {v0Remote, v2Impl};
}

function getV2RemoteAndV2Impl() {
  const v2Impl = new InterfaceV2Impl;
  const v2Remote = v2Impl.receiver.$.bindNewPipeAndPassRemote();

  return {v2Remote, v2Impl};
}

// Versioned params/response params tests

promise_test(async () => {
  const {v0Remote, v0Impl} = getV0RemoteAndV0Impl();

  const response = await v0Remote.methodWithVersionedParams();
  assert_weak_equals(response, {});
}, 'V0 to V0. Basic test for sending/receiving numerics in versioned ' +
   'interfaces.');

promise_test(async () => {
  const {v0Remote, v2Impl} = getV0RemoteAndV2Impl();

  const response = await v0Remote.methodWithVersionedParams();
  assert_weak_equals(response, {});
}, 'V0 to V2. Basic test for sending/receiving numerics in versioned ' +
   'interfaces.');

promise_test(async () => {
  const {v2Remote, v0Impl} = getV2RemoteAndV0Impl();

  const nullArgs = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const expectedResponse = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const nullArgsResponse = await v2Remote.methodWithVersionedParams(
    ...Object.values(nullArgs));
  assert_weak_equals(nullArgsResponse, expectedResponse);
}, 'V2 to V0. Basic test for sending/receiving null numerics in versioned ' +
   'interfaces.');

promise_test(async () => {
  const args = {
    boolValue: true,
    uint8Value: 8,
    int8Value: -8,
    uint16Value: 16,
    int16Value: -16,
    uint32Value: 32,
    int32Value: -32,
    uint64Value: BigInt('64'),
    int64Value: BigInt('-64'),
    floatValue: -0.5,
    doubleValue: 0.25,
    enumValue: RegularEnum.kBar,
  };

  const expectedResponse = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const {v2Remote, v0Impl} = getV2RemoteAndV0Impl();

  const response = await v2Remote.methodWithVersionedParams(
    ...Object.values(args));
  assert_weak_equals(response, expectedResponse);

}, 'V2 to V0. Basic test for sending/receiving numerics in versioned ' +
   'interfaces.');

promise_test(async () => {
  const nullArgs = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const expectedResponse = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const {v2Remote, v2Impl} = getV2RemoteAndV2Impl();

  const nullArgsResponse =
    await v2Remote.methodWithVersionedParams(...Object.values(nullArgs));
  assert_weak_equals(nullArgsResponse, expectedResponse);

  const undefinedArgs = {
    boolValue: undefined,
    uint8Value: undefined,
    int8Value: undefined,
    uint16Value: undefined,
    int16Value: undefined,
    uint32Value: undefined,
    int32Value: undefined,
    uint64Value: undefined,
    int64Value: undefined,
    floatValue: undefined,
    doubleValue: undefined,
    enumValue: undefined,
  };

  const undefinedArgsResponse =
    await v2Remote.methodWithVersionedParams(...Object.values(undefinedArgs));
  assert_weak_equals(undefinedArgsResponse, expectedResponse);

  const emptyArgsResponse = await v2Remote.methodWithVersionedParams();
  assert_weak_equals(emptyArgsResponse, expectedResponse);

}, 'V2 to V2. Basic test for sending/receiving null numerics in versioned ' +
   'interfaces.');

promise_test(async () => {
  const args = {
    boolValue: true,
    uint8Value: 8,
    int8Value: -8,
    uint16Value: 16,
    int16Value: -16,
    uint32Value: 32,
    int32Value: -32,
    uint64Value: BigInt('64'),
    int64Value: BigInt('-64'),
    floatValue: -0.5,
    doubleValue: 0.25,
    enumValue: RegularEnum.kBar,
  };
  const expectedResponse = {
    boolValue: !args.boolValue,
    uint8Value: 16,
    int8Value: -16,
    uint16Value: 32,
    int16Value: -32,
    uint32Value: 64,
    int32Value: -64,
    uint64Value: BigInt('128'),
    int64Value: BigInt('-128'),
    floatValue: -1.5,
    doubleValue: 0.75,
    enumValue: RegularEnum.kFoo,
  };

  const {v2Remote, v2Impl} = getV2RemoteAndV2Impl();

  const response =
    await v2Remote.methodWithVersionedParams(...Object.values(args));
  assert_weak_equals(response, expectedResponse);
}, 'V2 to V2. Basic test for sending/receiving optional numerics in ' +
   'versioned interfaces.');

// Versioned struct tests.

promise_test(async () => {
  const {v0Remote, v0Impl} = getV0RemoteAndV0Impl();

  const {s} = await v0Remote.methodWithVersionedStruct({});
  assert_weak_equals(s, {});
}, 'V0 to V0. Basic test for sending/receiving versioned structs with ' +
   'optional numerics.');

promise_test(async () => {
  const {v0Remote, v2Impl} = getV0RemoteAndV2Impl();

  const {s} = await v0Remote.methodWithVersionedStruct({});
  assert_weak_equals(s, {});
}, 'V0 to V2. Basic test for sending/receiving versioned structs with ' +
   'optional numerics.');

promise_test(async () => {
  const {v2Remote, v0Impl} = getV2RemoteAndV0Impl();

  const structWithNull = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const {s} = await v2Remote.methodWithVersionedStruct(structWithNull);
  assert_weak_equals(s, structWithNull);
}, 'V2 to V0. Basic test for sending/receiving a struct with null numerics ' +
   'in versioned interfaces.');

promise_test(async () => {
  const structWithValues = {
    boolValue: true,
    uint8Value: 8,
    int8Value: -8,
    uint16Value: 16,
    int16Value: -16,
    uint32Value: 32,
    int32Value: -32,
    uint64Value: BigInt('64'),
    int64Value: BigInt('-64'),
    floatValue: -0.5,
    doubleValue: 0.25,
    enumValue: RegularEnum.kBar,
  };

  const expectedStruct = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const {v2Remote, v0Impl} = getV2RemoteAndV0Impl();

  const {s} = await v2Remote.methodWithVersionedStruct(structWithValues);
  assert_weak_equals(s, expectedStruct);
}, 'V2 to V0. Basic test for sending/receiving a versioned struct with ' +
   'optional numerics in versioned interfaces.');

promise_test(async () => {
  const structWithNull = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const expectedStruct = {
    boolValue: null,
    uint8Value: null,
    int8Value: null,
    uint16Value: null,
    int16Value: null,
    uint32Value: null,
    int32Value: null,
    uint64Value: null,
    int64Value: null,
    floatValue: null,
    doubleValue: null,
    enumValue: null,
  };

  const {v2Remote, v2Impl} = getV2RemoteAndV2Impl();

  {
    const {s} = await v2Remote.methodWithVersionedStruct(structWithNull);
    assert_weak_equals(s, expectedStruct);
  }

  const structWithUndefined = {
    boolValue: undefined,
    uint8Value: undefined,
    int8Value: undefined,
    uint16Value: undefined,
    int16Value: undefined,
    uint32Value: undefined,
    int32Value: undefined,
    uint64Value: undefined,
    int64Value: undefined,
    floatValue: undefined,
    doubleValue: undefined,
    enumValue: undefined,
  };

  {
    const {s} = await v2Remote.methodWithVersionedStruct(structWithUndefined);
    assert_weak_equals(s, expectedStruct);
  }

  {
    const {s} = await v2Remote.methodWithVersionedStruct({});
    assert_weak_equals(s, expectedStruct);
  }
}, 'V2 to V2. Basic test for sending/receiving versioned struct with null ' +
   'numerics in versioned interfaces.');

promise_test(async () => {
  const structWithValues = {
    boolValue: true,
    uint8Value: 8,
    int8Value: -8,
    uint16Value: 16,
    int16Value: -16,
    uint32Value: 32,
    int32Value: -32,
    uint64Value: BigInt('64'),
    int64Value: BigInt('-64'),
    floatValue: -0.5,
    doubleValue: 0.25,
    enumValue: RegularEnum.kBar,
  };
  const expectedStruct = {
    boolValue: false,
    uint8Value: 16,
    int8Value: -16,
    uint16Value: 32,
    int16Value: -32,
    uint32Value: 64,
    int32Value: -64,
    uint64Value: BigInt('128'),
    int64Value: BigInt('-128'),
    floatValue: -1.5,
    doubleValue: 0.75,
    enumValue: RegularEnum.kFoo,
  };

  const {v2Remote, v2Impl} = getV2RemoteAndV2Impl();

  const {s} = await v2Remote.methodWithVersionedStruct(structWithValues);
  assert_weak_equals(s, expectedStruct);
}, 'V2 to V2. Basic test for sending/receiving versioned struct with ' +
   'optional numerics in versioned interfaces.');
</script>