IN_PROC_BROWSER_TEST_F(WebUsbTest, ...) {...}, where the case name contains Open or Close.EvalJs() or ExecJs() syntactically correct JavaScript against the current web_contents().EXPECT_TRUE(EvalJs(...)) (or equivalent) for the first opened checkEXPECT_FALSE(EvalJs(...)) for the second opened checkEXPECT_EQ(ListValueOf("123456"), EvalJs(...)) to verify the device is still recognizedEvalJs() or ExecJs() calls:navigator.usb.requestDevice({ filters: [{ vendorId: 0 }] }) (for granting permission to the fake device).open().opened.close().openednavigator.usb.getDevices()content/browser/usb/usb_browsertest.ccSample test:
diff --git a/content/browser/usb/usb_browsertest.cc b/content/browser/usb/usb_browsertest.cc
index db91960bc0c1c..e9474bc9aeb8f 100644
--- a/content/browser/usb/usb_browsertest.cc
+++ b/content/browser/usb/usb_browsertest.cc
@@ -235,6 +235,39 @@ IN_PROC_BROWSER_TEST_F(WebUsbTest, ForgetDevice) {
})())"));
}
+IN_PROC_BROWSER_TEST_F(WebUsbTest, OpenClose) {
+ // Request permission to access the fake device.
+ EXPECT_EQ("123456", EvalJs(web_contents(),
+ R"((async () => {
+ let device =
+ await navigator.usb.requestDevice({ filters: [{ vendorId: 0 }] });
+ return device.serialNumber;
+ })())"));
+
+ // Get the device and open it.
+ EXPECT_EQ(true, EvalJs(web_contents(),
+ R"((async () => {
+ let devices = await navigator.usb.getDevices();
+ await devices[0].open();
+ return devices[0].opened;
+ })())"));
+
+ // Close the device.
+ EXPECT_EQ(false, EvalJs(web_contents(),
+ R"((async () => {
+ let devices = await navigator.usb.getDevices();
+ await devices[0].close();
+ return devices[0].opened;
+ })())"));
+
+ // Check that the device is still in the getDevices() array.
+ EXPECT_EQ(ListValueOf("123456"), EvalJs(web_contents(),
+ R"((async () => {
+ let devices = await navigator.usb.getDevices();
+ return devices.map(d => d.serialNumber);
+ })())"));
+}
+
} // namespace
} // namespace content