2020-08-20 00:27:27 +02:00
|
|
|
// Copyright (c) 2020 The Chromium Embedded Framework 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 <sstream>
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_cxx17_backports.h"
|
2022-08-01 22:19:36 +02:00
|
|
|
#include "include/cef_command_line.h"
|
2020-08-20 00:27:27 +02:00
|
|
|
#include "include/cef_task.h"
|
|
|
|
#include "include/cef_waitable_event.h"
|
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
|
|
|
#include "include/wrapper/cef_helpers.h"
|
|
|
|
#include "tests/ceftests/test_request.h"
|
2022-07-31 01:53:48 +02:00
|
|
|
#include "tests/ceftests/test_server_observer.h"
|
2020-08-20 00:27:27 +02:00
|
|
|
#include "tests/ceftests/track_callback.h"
|
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct TestState {
|
2022-07-31 03:17:40 +02:00
|
|
|
bool https_server = false;
|
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
TrackCallback got_initialized_;
|
|
|
|
TrackCallback got_request_;
|
|
|
|
TrackCallback got_response_;
|
|
|
|
TrackCallback got_shutdown_;
|
|
|
|
|
|
|
|
bool ExpectAll() {
|
|
|
|
EXPECT_TRUE(got_initialized_);
|
|
|
|
EXPECT_TRUE(got_request_);
|
|
|
|
EXPECT_TRUE(got_response_);
|
|
|
|
EXPECT_TRUE(got_shutdown_);
|
2022-07-31 01:53:48 +02:00
|
|
|
return got_initialized_ && got_request_ && got_response_ && got_shutdown_;
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char kResponseData[] = "Test data";
|
|
|
|
|
|
|
|
class TestServerObserver : public test_server::ObserverHelper {
|
|
|
|
public:
|
|
|
|
TestServerObserver(TestState* state,
|
|
|
|
const std::string& path,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure done_callback)
|
2020-08-20 00:27:27 +02:00
|
|
|
: state_(state),
|
|
|
|
path_(path),
|
2021-06-19 21:54:45 +02:00
|
|
|
done_callback_(std::move(done_callback)),
|
2020-08-20 00:27:27 +02:00
|
|
|
weak_ptr_factory_(this) {
|
|
|
|
DCHECK(state);
|
|
|
|
DCHECK(!path.empty());
|
|
|
|
DCHECK(!done_callback_.is_null());
|
2022-07-31 03:17:40 +02:00
|
|
|
Initialize(state_->https_server);
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
~TestServerObserver() override { std::move(done_callback_).Run(); }
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
void OnInitialized(const std::string& server_origin) override {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
EXPECT_FALSE(state_->got_initialized_);
|
|
|
|
EXPECT_FALSE(state_->got_request_);
|
|
|
|
EXPECT_FALSE(state_->got_response_);
|
|
|
|
EXPECT_FALSE(state_->got_shutdown_);
|
|
|
|
|
|
|
|
state_->got_initialized_.yes();
|
|
|
|
|
|
|
|
url_ = server_origin + path_;
|
|
|
|
|
|
|
|
// Send a request to the server.
|
|
|
|
test_request::SendConfig config;
|
|
|
|
config.request_ = CefRequest::Create();
|
|
|
|
config.request_->SetURL(url_);
|
|
|
|
test_request::Send(config,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&TestServerObserver::OnRequestResponse,
|
|
|
|
weak_ptr_factory_.GetWeakPtr()));
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-02 03:14:12 +02:00
|
|
|
bool OnTestServerRequest(CefRefPtr<CefRequest> request,
|
|
|
|
const ResponseCallback& response_callback) override {
|
2020-08-20 00:27:27 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
const std::string& url = request->GetURL();
|
|
|
|
if (url != url_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
EXPECT_TRUE(state_->got_initialized_);
|
|
|
|
EXPECT_FALSE(state_->got_request_);
|
|
|
|
EXPECT_FALSE(state_->got_response_);
|
|
|
|
EXPECT_FALSE(state_->got_shutdown_);
|
|
|
|
|
|
|
|
state_->got_request_.yes();
|
|
|
|
|
2022-07-31 01:53:48 +02:00
|
|
|
auto response = CefResponse::Create();
|
|
|
|
response->SetStatus(200);
|
|
|
|
response->SetMimeType("text/plain");
|
|
|
|
|
|
|
|
response_callback.Run(response, kResponseData);
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
// Stop propagating the callback.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnRequestResponse(const test_request::State& state) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
// Don't test for disconnected, which may race response.
|
|
|
|
EXPECT_TRUE(state_->got_initialized_);
|
|
|
|
EXPECT_TRUE(state_->got_request_);
|
|
|
|
EXPECT_FALSE(state_->got_response_);
|
|
|
|
EXPECT_FALSE(state_->got_shutdown_);
|
|
|
|
|
|
|
|
state_->got_response_.yes();
|
|
|
|
|
|
|
|
EXPECT_STREQ(url_.c_str(), state.request_->GetURL().ToString().c_str());
|
|
|
|
EXPECT_EQ(UR_SUCCESS, state.status_);
|
|
|
|
EXPECT_EQ(ERR_NONE, state.error_code_);
|
|
|
|
EXPECT_EQ(200, state.response_->GetStatus());
|
|
|
|
EXPECT_STREQ(kResponseData, state.download_data_.c_str());
|
|
|
|
|
|
|
|
// Trigger shutdown asynchronously.
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(&TestServerObserver::Shutdown,
|
|
|
|
weak_ptr_factory_.GetWeakPtr()));
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnShutdown() override {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
EXPECT_TRUE(state_->got_initialized_);
|
|
|
|
EXPECT_TRUE(state_->got_request_);
|
|
|
|
EXPECT_TRUE(state_->got_response_);
|
|
|
|
EXPECT_FALSE(state_->got_shutdown_);
|
|
|
|
|
|
|
|
state_->got_shutdown_.yes();
|
|
|
|
|
|
|
|
// End the test.
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TestState* const state_;
|
|
|
|
const std::string path_;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure done_callback_;
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
std::string url_;
|
|
|
|
|
|
|
|
base::WeakPtrFactory<TestServerObserver> weak_ptr_factory_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestServerObserver);
|
|
|
|
};
|
|
|
|
|
|
|
|
void CreateObserverOnUIThread(TestState* state,
|
|
|
|
const std::string& path,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure done_callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(CreateObserverOnUIThread,
|
|
|
|
base::Unretained(state), path,
|
|
|
|
std::move(done_callback)));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
new TestServerObserver(state, path, std::move(done_callback));
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SignalIfDone(CefRefPtr<CefWaitableEvent> event,
|
|
|
|
size_t* count,
|
|
|
|
size_t total) {
|
|
|
|
if (++(*count) == total) {
|
|
|
|
event->Signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 22:19:36 +02:00
|
|
|
void Wait(CefRefPtr<CefWaitableEvent> event) {
|
|
|
|
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch(
|
|
|
|
"disable-test-timeout")) {
|
|
|
|
event->Wait();
|
|
|
|
} else {
|
|
|
|
event->TimedWait(2000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-31 03:17:40 +02:00
|
|
|
void RunHelperSingle(bool https_server) {
|
2020-08-20 00:27:27 +02:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
|
|
|
|
|
|
|
TestState state;
|
2022-07-31 03:17:40 +02:00
|
|
|
state.https_server = https_server;
|
2020-08-20 00:27:27 +02:00
|
|
|
CreateObserverOnUIThread(&state, "/TestServerTest.ObserverHelperSingle",
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&CefWaitableEvent::Signal, event));
|
2022-08-01 22:19:36 +02:00
|
|
|
|
|
|
|
Wait(event);
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(state.ExpectAll());
|
|
|
|
}
|
|
|
|
|
2022-07-31 03:17:40 +02:00
|
|
|
void RunHelperMultiple(bool https_server) {
|
2020-08-20 00:27:27 +02:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
|
|
|
|
|
|
|
TestState states[3];
|
|
|
|
size_t count = 0;
|
2021-06-17 22:08:01 +02:00
|
|
|
const size_t size = base::size(states);
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "/TestServerTest.ObserverHelperMultiple" << i;
|
2021-06-19 21:54:45 +02:00
|
|
|
auto done_callback =
|
|
|
|
base::BindOnce(SignalIfDone, event, base::Unretained(&count), size);
|
2022-07-31 03:17:40 +02:00
|
|
|
states[i].https_server = https_server;
|
2021-06-19 21:54:45 +02:00
|
|
|
CreateObserverOnUIThread(&states[i], ss.str(), std::move(done_callback));
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-01 22:19:36 +02:00
|
|
|
Wait(event);
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(size, count);
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
EXPECT_TRUE(states[i].ExpectAll()) << i;
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 03:17:40 +02:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(TestServerObserverTest, HelperSingleHttp) {
|
|
|
|
RunHelperSingle(/*https_server=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestServerObserverTest, HelperMultipleHttp) {
|
|
|
|
RunHelperMultiple(/*https_server=*/false);
|
|
|
|
}
|
2022-08-01 22:19:36 +02:00
|
|
|
|
|
|
|
TEST(TestServerObserverTest, HelperSingleHttps) {
|
|
|
|
RunHelperSingle(/*https_server=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestServerObserverTest, HelperMultipleHttps) {
|
|
|
|
RunHelperMultiple(/*https_server=*/true);
|
|
|
|
}
|