2012-04-03 01:34:16 +00:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
2021-06-17 16:08:01 -04:00
|
|
|
#include "include/base/cef_callback.h"
|
2012-04-03 01:34:16 +00:00
|
|
|
#include "include/cef_process_message.h"
|
|
|
|
#include "include/cef_task.h"
|
2017-12-19 15:14:00 -05:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-18 12:31:21 -05:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
|
|
|
#include "tests/ceftests/test_util.h"
|
2016-11-17 18:52:42 -05:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
|
|
|
#include "tests/shared/renderer/client_app_renderer.h"
|
2012-04-03 01:34:16 +00:00
|
|
|
|
2015-01-31 04:41:36 +00:00
|
|
|
using client::ClientAppRenderer;
|
2015-01-23 19:09:34 +00:00
|
|
|
|
2012-04-03 01:34:16 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Unique values for the SendRecv test.
|
2023-04-28 18:17:03 -04:00
|
|
|
const char kSendRecvUrl[] = "https://tests/ProcessMessageTest.SendRecv";
|
2014-01-27 23:31:03 +00:00
|
|
|
const char kSendRecvMsg[] = "ProcessMessageTest.SendRecv";
|
2012-04-03 01:34:16 +00:00
|
|
|
|
|
|
|
// Creates a test message.
|
|
|
|
CefRefPtr<CefProcessMessage> CreateTestMessage() {
|
|
|
|
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create(kSendRecvMsg);
|
|
|
|
EXPECT_TRUE(msg.get());
|
2021-05-20 14:54:07 -04:00
|
|
|
EXPECT_TRUE(msg->IsValid());
|
|
|
|
EXPECT_FALSE(msg->IsReadOnly());
|
2012-04-03 01:34:16 +00:00
|
|
|
|
|
|
|
CefRefPtr<CefListValue> args = msg->GetArgumentList();
|
|
|
|
EXPECT_TRUE(args.get());
|
2021-05-20 14:54:07 -04:00
|
|
|
EXPECT_TRUE(args->IsValid());
|
|
|
|
EXPECT_FALSE(args->IsReadOnly());
|
2012-04-03 01:34:16 +00:00
|
|
|
|
2016-11-04 14:38:59 -04:00
|
|
|
size_t index = 0;
|
2012-04-03 01:34:16 +00:00
|
|
|
args->SetNull(index++);
|
|
|
|
args->SetInt(index++, 5);
|
|
|
|
args->SetDouble(index++, 10.543);
|
|
|
|
args->SetBool(index++, true);
|
|
|
|
args->SetString(index++, "test string");
|
|
|
|
args->SetList(index++, args->Copy());
|
|
|
|
|
2016-11-04 14:38:59 -04:00
|
|
|
EXPECT_EQ(index, args->GetSize());
|
2012-04-03 01:34:16 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renderer side.
|
2015-01-31 04:41:36 +00:00
|
|
|
class SendRecvRendererTest : public ClientAppRenderer::Delegate {
|
2012-04-03 01:34:16 +00:00
|
|
|
public:
|
2024-01-20 12:00:09 -05:00
|
|
|
SendRecvRendererTest() = default;
|
2012-04-03 01:34:16 +00:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool OnProcessMessageReceived(CefRefPtr<ClientAppRenderer> app,
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
2019-05-24 23:23:43 +03:00
|
|
|
CefRefPtr<CefFrame> frame,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefProcessId source_process,
|
|
|
|
CefRefPtr<CefProcessMessage> message) override {
|
2012-04-03 01:34:16 +00:00
|
|
|
if (message->GetName() == kSendRecvMsg) {
|
|
|
|
EXPECT_TRUE(browser.get());
|
2019-05-24 23:23:43 +03:00
|
|
|
EXPECT_TRUE(frame.get());
|
2012-04-03 01:34:16 +00:00
|
|
|
EXPECT_EQ(PID_BROWSER, source_process);
|
|
|
|
EXPECT_TRUE(message.get());
|
2021-05-20 14:54:07 -04:00
|
|
|
EXPECT_TRUE(message->IsValid());
|
|
|
|
EXPECT_TRUE(message->IsReadOnly());
|
2012-04-03 01:34:16 +00:00
|
|
|
|
2019-05-24 23:23:43 +03:00
|
|
|
const std::string& url = frame->GetURL();
|
2014-01-27 23:31:03 +00:00
|
|
|
if (url == kSendRecvUrl) {
|
2012-04-03 01:34:16 +00:00
|
|
|
// Echo the message back to the sender natively.
|
2019-05-24 23:23:43 +03:00
|
|
|
frame->SendProcessMessage(PID_BROWSER, message);
|
2021-05-20 14:54:07 -04:00
|
|
|
EXPECT_FALSE(message->IsValid());
|
2012-04-03 01:34:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message not handled.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SendRecvRendererTest);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Browser side.
|
|
|
|
class SendRecvTestHandler : public TestHandler {
|
|
|
|
public:
|
2017-12-19 15:14:00 -05:00
|
|
|
explicit SendRecvTestHandler(cef_thread_id_t send_thread)
|
|
|
|
: send_thread_(send_thread) {}
|
2012-04-03 01:34:16 +00:00
|
|
|
|
2014-11-12 19:25:15 +00:00
|
|
|
void RunTest() override {
|
2014-01-27 23:31:03 +00:00
|
|
|
AddResource(kSendRecvUrl, "<html><body>TEST</body></html>", "text/html");
|
|
|
|
CreateBrowser(kSendRecvUrl);
|
2015-01-09 23:40:26 +00:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
2012-04-03 01:34:16 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 19:25:15 +00:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2017-12-19 15:14:00 -05:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
|
|
|
|
2012-04-03 01:34:16 +00:00
|
|
|
// Send the message to the renderer process.
|
2017-12-19 15:14:00 -05:00
|
|
|
if (!CefCurrentlyOn(send_thread_)) {
|
2021-06-19 15:54:45 -04:00
|
|
|
CefPostTask(send_thread_,
|
|
|
|
base::BindOnce(&SendRecvTestHandler::SendMessage, this,
|
|
|
|
browser, frame));
|
2017-12-19 15:14:00 -05:00
|
|
|
} else {
|
2019-05-24 23:23:43 +03:00
|
|
|
SendMessage(browser, frame);
|
2017-12-19 15:14:00 -05:00
|
|
|
}
|
2012-04-03 01:34:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
|
2019-05-24 23:23:43 +03:00
|
|
|
CefRefPtr<CefFrame> frame,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefProcessId source_process,
|
|
|
|
CefRefPtr<CefProcessMessage> message) override {
|
2017-12-19 15:14:00 -05:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
2012-04-03 01:34:16 +00:00
|
|
|
EXPECT_TRUE(browser.get());
|
2019-05-24 23:23:43 +03:00
|
|
|
EXPECT_TRUE(frame.get());
|
2012-04-03 01:34:16 +00:00
|
|
|
EXPECT_EQ(PID_RENDERER, source_process);
|
|
|
|
EXPECT_TRUE(message.get());
|
2021-05-20 14:54:07 -04:00
|
|
|
EXPECT_TRUE(message->IsValid());
|
2012-04-03 01:34:16 +00:00
|
|
|
EXPECT_TRUE(message->IsReadOnly());
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2023-12-25 15:31:01 -07:00
|
|
|
// Verify that the received message is the same as the sent message.
|
2021-05-14 12:58:55 -04:00
|
|
|
TestProcessMessageEqual(CreateTestMessage(), message);
|
2012-04-03 01:34:16 +00:00
|
|
|
|
|
|
|
got_message_.yes();
|
|
|
|
|
|
|
|
// Test is complete.
|
|
|
|
DestroyTest();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:14:00 -05:00
|
|
|
protected:
|
|
|
|
void DestroyTest() override {
|
|
|
|
EXPECT_TRUE(got_message_);
|
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-24 23:23:43 +03:00
|
|
|
void SendMessage(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame) {
|
2017-12-19 15:14:00 -05:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(send_thread_));
|
2021-05-20 14:54:07 -04:00
|
|
|
auto message = CreateTestMessage();
|
|
|
|
frame->SendProcessMessage(PID_RENDERER, message);
|
|
|
|
|
|
|
|
// The message will be invalidated immediately, no matter what thread we
|
|
|
|
// send from.
|
|
|
|
EXPECT_FALSE(message->IsValid());
|
2017-12-19 15:14:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cef_thread_id_t send_thread_;
|
2012-04-03 01:34:16 +00:00
|
|
|
TrackCallback got_message_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SendRecvTestHandler);
|
2012-04-03 01:34:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-12-25 15:31:01 -07:00
|
|
|
// Verify send from the UI thread and receive.
|
2017-12-19 15:14:00 -05:00
|
|
|
TEST(ProcessMessageTest, SendRecvUI) {
|
|
|
|
CefRefPtr<SendRecvTestHandler> handler = new SendRecvTestHandler(TID_UI);
|
2012-04-03 01:34:16 +00:00
|
|
|
handler->ExecuteTest();
|
2017-12-19 15:14:00 -05:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
|
|
|
}
|
2012-04-03 01:34:16 +00:00
|
|
|
|
2023-12-25 15:31:01 -07:00
|
|
|
// Verify send from the IO thread and receive.
|
2017-12-19 15:14:00 -05:00
|
|
|
TEST(ProcessMessageTest, SendRecvIO) {
|
|
|
|
CefRefPtr<SendRecvTestHandler> handler = new SendRecvTestHandler(TID_IO);
|
|
|
|
handler->ExecuteTest();
|
2015-01-09 23:40:26 +00:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-04-03 01:34:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 23:31:03 +00:00
|
|
|
// Verify create.
|
2012-04-03 01:34:16 +00:00
|
|
|
TEST(ProcessMessageTest, Create) {
|
|
|
|
CefRefPtr<CefProcessMessage> message =
|
|
|
|
CefProcessMessage::Create(kSendRecvMsg);
|
|
|
|
EXPECT_TRUE(message.get());
|
|
|
|
EXPECT_TRUE(message->IsValid());
|
|
|
|
EXPECT_FALSE(message->IsReadOnly());
|
|
|
|
EXPECT_STREQ(kSendRecvMsg, message->GetName().ToString().c_str());
|
|
|
|
|
|
|
|
CefRefPtr<CefListValue> args = message->GetArgumentList();
|
|
|
|
EXPECT_TRUE(args.get());
|
|
|
|
EXPECT_TRUE(args->IsValid());
|
2021-05-14 12:58:55 -04:00
|
|
|
EXPECT_FALSE(args->IsOwned());
|
2012-04-03 01:34:16 +00:00
|
|
|
EXPECT_FALSE(args->IsReadOnly());
|
|
|
|
}
|
|
|
|
|
2014-01-27 23:31:03 +00:00
|
|
|
// Verify copy.
|
2012-04-03 01:34:16 +00:00
|
|
|
TEST(ProcessMessageTest, Copy) {
|
|
|
|
CefRefPtr<CefProcessMessage> message = CreateTestMessage();
|
|
|
|
CefRefPtr<CefProcessMessage> message2 = message->Copy();
|
|
|
|
TestProcessMessageEqual(message, message2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry point for creating process message renderer test objects.
|
2012-04-12 20:21:50 +00:00
|
|
|
// Called from client_app_delegates.cc.
|
|
|
|
void CreateProcessMessageRendererTests(
|
2015-01-31 04:41:36 +00:00
|
|
|
ClientAppRenderer::DelegateSet& delegates) {
|
2012-04-03 01:34:16 +00:00
|
|
|
// For ProcessMessageTest.SendRecv
|
2012-04-12 20:21:50 +00:00
|
|
|
delegates.insert(new SendRecvRendererTest);
|
2012-04-03 01:34:16 +00:00
|
|
|
}
|