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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/serial/serial.h"
#include "third_party/blink/public/mojom/serial/serial.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_string_unsignedlong.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_serial_port_filter.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
namespace blink {
namespace {
constexpr uint16_t kTestVendorId = 0x0001;
constexpr uint16_t kTestProductId = 0x0002;
constexpr char kTestServiceClassId[] = "05079c61-147f-473d-8127-fab1bbad7e1a";
} // namespace
TEST(SerialTest, CreateMojoFilter_EmptyFilter) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
SerialPortFilter* js_filter = SerialPortFilter::Create(scope.GetIsolate());
mojom::blink::SerialPortFilterPtr mojo_filter =
Serial::CreateMojoFilter(js_filter, scope.GetExceptionState());
EXPECT_FALSE(mojo_filter);
EXPECT_TRUE(scope.GetExceptionState().HadException());
EXPECT_EQ(ToExceptionCode(ESErrorType::kTypeError),
scope.GetExceptionState().Code());
}
TEST(SerialTest, CreateMojoFilter_VendorId) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
SerialPortFilter* js_filter = SerialPortFilter::Create(scope.GetIsolate());
js_filter->setUsbVendorId(kTestVendorId);
mojom::blink::SerialPortFilterPtr mojo_filter =
Serial::CreateMojoFilter(js_filter, scope.GetExceptionState());
ASSERT_TRUE(mojo_filter);
EXPECT_FALSE(scope.GetExceptionState().HadException());
EXPECT_TRUE(mojo_filter->has_vendor_id);
EXPECT_EQ(kTestVendorId, mojo_filter->vendor_id);
EXPECT_FALSE(mojo_filter->has_product_id);
EXPECT_FALSE(mojo_filter->bluetooth_service_class_id);
}
TEST(SerialTest, CreateMojoFilter_ProductNoVendorId) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
SerialPortFilter* js_filter = SerialPortFilter::Create(scope.GetIsolate());
// If the filter has a product ID then it must also have a vendor ID.
js_filter->setUsbProductId(kTestProductId);
mojom::blink::SerialPortFilterPtr mojo_filter =
Serial::CreateMojoFilter(js_filter, scope.GetExceptionState());
EXPECT_FALSE(mojo_filter);
EXPECT_TRUE(scope.GetExceptionState().HadException());
EXPECT_EQ(ToExceptionCode(ESErrorType::kTypeError),
scope.GetExceptionState().Code());
}
TEST(SerialTest, CreateMojoFilter_BluetoothServiceClassAndVendorId) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
SerialPortFilter* js_filter = SerialPortFilter::Create(scope.GetIsolate());
// Can't have both Bluetooth and USB filter parameters.
V8UnionStringOrUnsignedLong* uuid =
MakeGarbageCollected<V8UnionStringOrUnsignedLong>(kTestServiceClassId);
js_filter->setUsbVendorId(kTestVendorId);
js_filter->setBluetoothServiceClassId(uuid);
mojom::blink::SerialPortFilterPtr mojo_filter =
Serial::CreateMojoFilter(js_filter, scope.GetExceptionState());
EXPECT_FALSE(mojo_filter);
EXPECT_TRUE(scope.GetExceptionState().HadException());
EXPECT_EQ(ToExceptionCode(ESErrorType::kTypeError),
scope.GetExceptionState().Code());
}
TEST(SerialTest, CreateMojoFilter_BluetoothServiceClassAndProductId) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
SerialPortFilter* js_filter = SerialPortFilter::Create(scope.GetIsolate());
// Can't have both Bluetooth and USB filter parameters.
js_filter->setUsbProductId(kTestProductId);
js_filter->setBluetoothServiceClassId(
MakeGarbageCollected<V8UnionStringOrUnsignedLong>(kTestServiceClassId));
mojom::blink::SerialPortFilterPtr mojo_filter =
Serial::CreateMojoFilter(js_filter, scope.GetExceptionState());
EXPECT_FALSE(mojo_filter);
EXPECT_TRUE(scope.GetExceptionState().HadException());
EXPECT_EQ(ToExceptionCode(ESErrorType::kTypeError),
scope.GetExceptionState().Code());
}
TEST(SerialTest, CreateMojoFilter_BluetoothServiceClass) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
SerialPortFilter* js_filter = SerialPortFilter::Create(scope.GetIsolate());
js_filter->setBluetoothServiceClassId(
MakeGarbageCollected<V8UnionStringOrUnsignedLong>(kTestServiceClassId));
mojom::blink::SerialPortFilterPtr mojo_filter =
Serial::CreateMojoFilter(js_filter, scope.GetExceptionState());
ASSERT_TRUE(mojo_filter);
EXPECT_FALSE(scope.GetExceptionState().HadException());
EXPECT_FALSE(mojo_filter->has_vendor_id);
EXPECT_FALSE(mojo_filter->has_product_id);
ASSERT_TRUE(mojo_filter->bluetooth_service_class_id);
EXPECT_EQ(kTestServiceClassId, mojo_filter->bluetooth_service_class_id->uuid);
}
TEST(SerialTest, CreateMojoFilter_InvalidBluetoothServiceClass) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
SerialPortFilter* js_filter = SerialPortFilter::Create(scope.GetIsolate());
js_filter->setBluetoothServiceClassId(
MakeGarbageCollected<V8UnionStringOrUnsignedLong>("invalid-uuid"));
mojom::blink::SerialPortFilterPtr mojo_filter =
Serial::CreateMojoFilter(js_filter, scope.GetExceptionState());
EXPECT_FALSE(mojo_filter);
EXPECT_TRUE(scope.GetExceptionState().HadException());
EXPECT_EQ(ToExceptionCode(ESErrorType::kTypeError),
scope.GetExceptionState().Code());
}
} // namespace blink
|