blob: af30f1a0ccc0ad365f7865d1683a1d72b41c22f8 (
plain)
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
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "content/browser/serial/serial_test_utils.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/cpp/test/fake_serial_port_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
const char kTestUrl[] = "https://www.google.com";
const char kCrossOriginTestUrl[] = "https://www.chromium.org";
class SerialTest : public RenderViewHostImplTestHarness {
public:
SerialTest() {
ON_CALL(test_client_.delegate(), GetPortManager)
.WillByDefault(testing::Return(&port_manager_));
}
~SerialTest() override = default;
void SetUp() override {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
original_client_ = SetBrowserClientForTesting(&test_client_);
RenderViewHostTestHarness::SetUp();
}
void TearDown() override {
RenderViewHostTestHarness::TearDown();
if (original_client_)
SetBrowserClientForTesting(original_client_);
}
device::FakeSerialPortManager* port_manager() { return &port_manager_; }
private:
SerialTestContentBrowserClient test_client_;
ContentBrowserClient* original_client_ = nullptr;
device::FakeSerialPortManager port_manager_;
DISALLOW_COPY_AND_ASSIGN(SerialTest);
};
} // namespace
TEST_F(SerialTest, OpenAndClosePort) {
NavigateAndCommit(GURL(kTestUrl));
mojo::Remote<blink::mojom::SerialService> service;
contents()->GetMainFrame()->BindSerialService(
service.BindNewPipeAndPassReceiver());
auto token = base::UnguessableToken::Create();
auto port_info = device::mojom::SerialPortInfo::New();
port_info->token = token;
port_manager()->AddPort(std::move(port_info));
EXPECT_FALSE(contents()->IsConnectedToSerialPort());
mojo::Remote<device::mojom::SerialPort> port;
service->GetPort(token, port.BindNewPipeAndPassReceiver());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(contents()->IsConnectedToSerialPort());
port.reset();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(contents()->IsConnectedToSerialPort());
}
TEST_F(SerialTest, OpenAndNavigateCrossOrigin) {
NavigateAndCommit(GURL(kTestUrl));
mojo::Remote<blink::mojom::SerialService> service;
contents()->GetMainFrame()->BindSerialService(
service.BindNewPipeAndPassReceiver());
auto token = base::UnguessableToken::Create();
auto port_info = device::mojom::SerialPortInfo::New();
port_info->token = token;
port_manager()->AddPort(std::move(port_info));
EXPECT_FALSE(contents()->IsConnectedToSerialPort());
mojo::Remote<device::mojom::SerialPort> port;
service->GetPort(token, port.BindNewPipeAndPassReceiver());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(contents()->IsConnectedToSerialPort());
NavigateAndCommit(GURL(kCrossOriginTestUrl));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(contents()->IsConnectedToSerialPort());
port.FlushForTesting();
EXPECT_FALSE(port.is_connected());
}
} // namespace content
|