File: remote_open_url_client_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (259 lines) | stat: -rw-r--r-- 8,847 bytes parent folder | download | duplicates (3)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/remote_open_url/remote_open_url_client.h"

#include <memory>

#include "base/command_line.h"
#include "base/functional/callback_forward.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "remoting/host/host_mock_objects.h"
#include "remoting/host/mojom/remote_url_opener.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace remoting {

namespace {

using testing::_;
using testing::AnyNumber;
using testing::Return;

// The IPC channel doesn't work if we use a mock clock, so we just reduce the
// timeout to shorten the test time.
constexpr base::TimeDelta kTestRequestTimeout = base::Milliseconds(500);

class MockRemoteOpenUrlClientDelegate : public RemoteOpenUrlClient::Delegate {
 public:
  MOCK_METHOD(void, OpenUrlOnFallbackBrowser, (const GURL& url), (override));
  MOCK_METHOD(void, ShowOpenUrlError, (const GURL& url), (override));
};

class MockRemoteUrlOpener : public mojom::RemoteUrlOpener {
 public:
  MOCK_METHOD(void,
              OpenUrl,
              (const GURL& url, OpenUrlCallback callback),
              (override));
};

base::CommandLine::StringType ToCommandLineString(const char* str) {
#if BUILDFLAG(IS_WIN)
  return base::UTF8ToWide(str);
#else
  return str;
#endif
}

}  // namespace

class RemoteOpenUrlClientTest : public testing::Test {
 public:
  RemoteOpenUrlClientTest();
  ~RemoteOpenUrlClientTest() override;

 protected:
  void BindMockRemoteUrlOpener();

  // These raw pointer objects are owned by |client_| so |client_| must outlive
  // the last use of them.
  raw_ptr<MockRemoteOpenUrlClientDelegate, DanglingUntriaged> delegate_;
  raw_ptr<MockChromotingHostServicesProvider, DanglingUntriaged> api_provider_;

  std::unique_ptr<RemoteOpenUrlClient> client_;
  MockChromotingSessionServices mock_api_;
  MockRemoteUrlOpener remote_url_opener_;
  mojo::Receiver<mojom::RemoteUrlOpener> remote_url_opener_receiver_{
      &remote_url_opener_};

 private:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::IO};
};

RemoteOpenUrlClientTest::RemoteOpenUrlClientTest() {
  auto delegate = std::make_unique<MockRemoteOpenUrlClientDelegate>();
  delegate_ = delegate.get();
  auto api_provider = std::make_unique<MockChromotingHostServicesProvider>();
  api_provider_ = api_provider.get();
  client_ = base::WrapUnique(new RemoteOpenUrlClient(
      std::move(delegate), std::move(api_provider), kTestRequestTimeout));
}

RemoteOpenUrlClientTest::~RemoteOpenUrlClientTest() = default;

void RemoteOpenUrlClientTest::BindMockRemoteUrlOpener() {
  EXPECT_CALL(*api_provider_, set_disconnect_handler(_)).Times(AnyNumber());
  EXPECT_CALL(*api_provider_, GetSessionServices())
      .WillRepeatedly(Return(&mock_api_));
  EXPECT_CALL(mock_api_, BindRemoteUrlOpener(_))
      .WillRepeatedly(
          [this](mojo::PendingReceiver<mojom::RemoteUrlOpener> receiver) {
            remote_url_opener_receiver_.Bind(std::move(receiver));
          });
}

TEST_F(RemoteOpenUrlClientTest, OpenFallbackBrowserWithNoUrl) {
  EXPECT_CALL(*delegate_, OpenUrlOnFallbackBrowser(GURL(""))).Times(1);

  client_->OpenFallbackBrowser();
}

TEST_F(RemoteOpenUrlClientTest, OpenInvalidUrl_ShowsError) {
  EXPECT_CALL(*delegate_, ShowOpenUrlError(GURL("invalid-url"))).Times(1);
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run()).Times(1);

  client_->Open(ToCommandLineString("invalid-url"), done.Get());
}

TEST_F(RemoteOpenUrlClientTest, OpenUrlWithUnsupportedScheme_FallsBack) {
  EXPECT_CALL(*delegate_,
              OpenUrlOnFallbackBrowser(GURL("ftp://unsupported.com/")))
      .Times(1);
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run()).Times(1);

  client_->Open(ToCommandLineString("ftp://unsupported.com/"), done.Get());
}

TEST_F(RemoteOpenUrlClientTest,
       OpenWhenHostServicesApiIsNotProvided_FallsBack) {
  EXPECT_CALL(*api_provider_, set_disconnect_handler(_));
  EXPECT_CALL(*api_provider_, GetSessionServices()).WillOnce(Return(nullptr));
  EXPECT_CALL(*delegate_, OpenUrlOnFallbackBrowser(GURL("http://google.com/")))
      .Times(1);
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run()).Times(1);

  client_->Open(ToCommandLineString("http://google.com/"), done.Get());
}

TEST_F(RemoteOpenUrlClientTest, OpenUrlThenReceiverClosed_FallsBack) {
  BindMockRemoteUrlOpener();

  base::RunLoop run_loop;
  EXPECT_CALL(*delegate_, OpenUrlOnFallbackBrowser(GURL("http://google.com/")))
      .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure()));
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run()).Times(1);

  client_->Open(ToCommandLineString("http://google.com/"), done.Get());
  remote_url_opener_receiver_.reset();
  run_loop.Run();
}

TEST_F(RemoteOpenUrlClientTest, OpenUrl_Success) {
  BindMockRemoteUrlOpener();

  EXPECT_CALL(remote_url_opener_, OpenUrl(GURL("http://google.com/"), _))
      .WillOnce(base::test::RunOnceCallback<1>(mojom::OpenUrlResult::SUCCESS));

  base::RunLoop test_run_loop;
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run())
      .WillOnce(base::test::RunOnceClosure(test_run_loop.QuitClosure()));

  client_->Open(ToCommandLineString("http://google.com/"), done.Get());
  test_run_loop.Run();
}

TEST_F(RemoteOpenUrlClientTest, OpenUrl_Failure) {
  BindMockRemoteUrlOpener();

  EXPECT_CALL(remote_url_opener_, OpenUrl(GURL("http://google.com/"), _))
      .WillOnce(base::test::RunOnceCallback<1>(mojom::OpenUrlResult::FAILURE));
  EXPECT_CALL(*delegate_, ShowOpenUrlError(GURL("http://google.com/")))
      .Times(1);

  base::RunLoop test_run_loop;
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run())
      .WillOnce(base::test::RunOnceClosure(test_run_loop.QuitClosure()));

  client_->Open(ToCommandLineString("http://google.com/"), done.Get());
  test_run_loop.Run();
}

TEST_F(RemoteOpenUrlClientTest, OpenUrl_LocalFallback) {
  BindMockRemoteUrlOpener();

  EXPECT_CALL(remote_url_opener_, OpenUrl(GURL("http://google.com/"), _))
      .WillOnce(
          base::test::RunOnceCallback<1>(mojom::OpenUrlResult::LOCAL_FALLBACK));
  EXPECT_CALL(*delegate_, OpenUrlOnFallbackBrowser(GURL("http://google.com/")))
      .Times(1);

  base::RunLoop test_run_loop;
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run())
      .WillOnce(base::test::RunOnceClosure(test_run_loop.QuitClosure()));

  client_->Open(ToCommandLineString("http://google.com/"), done.Get());
  test_run_loop.Run();
}

TEST_F(RemoteOpenUrlClientTest, OpenUrlTimeout_LocalFallback) {
  BindMockRemoteUrlOpener();

  mojom::RemoteUrlOpener::OpenUrlCallback captured_callback;
  EXPECT_CALL(remote_url_opener_, OpenUrl(GURL("http://google.com/"), _))
      .WillOnce([&](const GURL& url,
                    mojom::RemoteUrlOpener::OpenUrlCallback callback) {
        captured_callback = std::move(callback);
      });
  EXPECT_CALL(*delegate_, OpenUrlOnFallbackBrowser(GURL("http://google.com/")))
      .Times(1);

  base::RunLoop test_run_loop;
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run())
      .WillOnce(base::test::RunOnceClosure(test_run_loop.QuitClosure()));

  client_->Open(ToCommandLineString("http://google.com/"), done.Get());
  test_run_loop.Run();

  // OpenUrlCallback fails a DCHECK if the callback is destroyed before it gets
  // called, so we have to capture it and call it here before it goes out of the
  // scope.
  std::move(captured_callback).Run(mojom::OpenUrlResult::FAILURE);
}

TEST_F(RemoteOpenUrlClientTest, OpenFilePath_LocalFallback) {
#if BUILDFLAG(IS_WIN)
  const wchar_t* file_path = L"C:\\test\\file\\path";
  GURL file_url("file:///C:/test/file/path");
#else
  const char* file_path = "/test/file/path";
  GURL file_url("file:///test/file/path");
#endif

  BindMockRemoteUrlOpener();

  EXPECT_CALL(*delegate_, OpenUrlOnFallbackBrowser(file_url)).Times(1);

  base::RunLoop test_run_loop;
  base::MockCallback<base::OnceClosure> done;
  EXPECT_CALL(done, Run())
      .WillOnce(base::test::RunOnceClosure(test_run_loop.QuitClosure()));

  client_->Open(file_path, done.Get());
  test_run_loop.Run();
}

}  // namespace remoting