#include "base/message_loop/message_pump.h"
#include <type_traits>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/message_loop/message_pump_for_io.h"
#include "base/message_loop/message_pump_for_ui.h"
#include "base/message_loop/message_pump_type.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_executor.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/input_hint_checker.h"
#include "base/android/yield_to_looper_checker.h"
#include "base/test/test_support_android.h"
#endif
#if !BUILDFLAG(IS_IOS)
#include "base/message_loop/message_pump_default.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AtMost;
using ::testing::Return;
namespace base {
namespace {
constexpr bool ChromeControlsNativeEventProcessing(MessagePumpType pump_type) {
#if BUILDFLAG(IS_MAC)
return pump_type != MessagePumpType::UI;
#elif BUILDFLAG(IS_IOS)
return false;
#else
return true;
#endif
}
class MockMessagePumpDelegate : public MessagePump::Delegate {
public:
explicit MockMessagePumpDelegate(MessagePumpType pump_type)
: check_work_items_(ChromeControlsNativeEventProcessing(pump_type)),
native_work_item_accounting_is_on_(
!ChromeControlsNativeEventProcessing(pump_type)) {}
~MockMessagePumpDelegate() override { ValidateNoOpenWorkItems(); }
MockMessagePumpDelegate(const MockMessagePumpDelegate&) = delete;
MockMessagePumpDelegate& operator=(const MockMessagePumpDelegate&) = delete;
void BeforeWait() override {}
void BeginNativeWorkBeforeDoWork() override {}
MOCK_METHOD(MessagePump::Delegate::NextWorkInfo, DoWork, ());
MOCK_METHOD(void, DoIdleWork, ());
void OnBeginWorkItem() override {
any_work_begun_ = true;
if (check_work_items_) {
MockOnBeginWorkItem();
}
++work_item_count_;
}
void OnEndWorkItem(int run_level_depth) override {
if (check_work_items_) {
MockOnEndWorkItem(run_level_depth);
}
EXPECT_EQ(run_level_depth, work_item_count_);
--work_item_count_;
EXPECT_GE(work_item_count_, 0);
}
int RunDepth() override { return work_item_count_; }
void ValidateNoOpenWorkItems() {
EXPECT_EQ(work_item_count_, 0);
if (native_work_item_accounting_is_on_) {
#if !BUILDFLAG(IS_IOS)
EXPECT_TRUE(any_work_begun_);
#endif
}
}
MOCK_METHOD(void, MockOnBeginWorkItem, ());
MOCK_METHOD(void, MockOnEndWorkItem, (int));
const bool check_work_items_;
const bool native_work_item_accounting_is_on_;
int work_item_count_ = 0;
bool any_work_begun_ = false;
};
class MessagePumpTest : public ::testing::TestWithParam<MessagePumpType> {
public:
MessagePumpTest() : message_pump_(MessagePump::Create(GetParam())) {}
protected:
#if defined(USE_GLIB)
std::map<MessagePump::Delegate*, int> do_work_counts;
#endif
void AddPreDoWorkExpectations(
testing::StrictMock<MockMessagePumpDelegate>& delegate) {
#if BUILDFLAG(IS_WIN)
if (GetParam() == MessagePumpType::UI) {
EXPECT_CALL(delegate, MockOnBeginWorkItem);
EXPECT_CALL(delegate, MockOnEndWorkItem);
EXPECT_CALL(delegate, MockOnBeginWorkItem).Times(AtMost(1));
EXPECT_CALL(delegate, MockOnEndWorkItem).Times(AtMost(1));
}
#endif
#if defined(USE_GLIB)
do_work_counts.try_emplace(&delegate, 0);
if (GetParam() == MessagePumpType::UI) {
if (++do_work_counts[&delegate] % 2) {
EXPECT_CALL(delegate, MockOnBeginWorkItem);
EXPECT_CALL(delegate, MockOnEndWorkItem);
}
}
#endif
}
void AddPostDoWorkExpectations(
testing::StrictMock<MockMessagePumpDelegate>& delegate) {
#if defined(USE_GLIB)
if (GetParam() == MessagePumpType::UI) {
EXPECT_CALL(delegate, MockOnBeginWorkItem).Times(AtMost(1));
EXPECT_CALL(delegate, MockOnEndWorkItem).Times(AtMost(1));
}
#endif
}
std::unique_ptr<MessagePump> message_pump_;
};
}
TEST_P(MessagePumpTest, QuitStopsWork) {
testing::InSequence sequence;
testing::StrictMock<MockMessagePumpDelegate> delegate(GetParam());
AddPreDoWorkExpectations(delegate);
EXPECT_CALL(delegate, DoWork).WillOnce([this] {
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{TimeTicks::Max()};
});
#if defined(USE_GLIB)
if (GetParam() == MessagePumpType::UI) {
AddPostDoWorkExpectations(delegate);
}
#endif
EXPECT_CALL(delegate, DoIdleWork()).Times(0);
message_pump_->ScheduleWork();
message_pump_->Run(&delegate);
}
#if BUILDFLAG(IS_ANDROID)
class MockInputHintChecker : public android::InputHintChecker {
public:
MOCK_METHOD(bool, HasInputImplWithThrottling, (), (override));
};
TEST_P(MessagePumpTest, DetectingHasInputYieldsOnUi) {
testing::InSequence sequence;
MessagePumpType pump_type = GetParam();
testing::StrictMock<MockMessagePumpDelegate> delegate(pump_type);
testing::StrictMock<MockInputHintChecker> hint_checker_mock;
android::InputHintChecker::ScopedOverrideInstance scoped_override_hint(
&hint_checker_mock);
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(android::kYieldWithInputHint);
android::InputHintChecker::InitializeFeatures();
uint32_t initial_work_enters = GetAndroidNonDelayedWorkEnterCount();
EXPECT_CALL(delegate, DoWork).WillOnce([] {
auto work_info =
MessagePump::Delegate::NextWorkInfo{.delayed_run_time = TimeTicks()};
CHECK(work_info.is_immediate());
return work_info;
});
if (pump_type == MessagePumpType::UI) {
EXPECT_CALL(hint_checker_mock, HasInputImplWithThrottling()).WillOnce([] {
return true;
});
}
EXPECT_CALL(delegate, DoWork).WillOnce([this] {
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{.delayed_run_time =
TimeTicks::Max()};
});
EXPECT_CALL(delegate, DoIdleWork()).Times(0);
message_pump_->Run(&delegate);
uint32_t work_loop_entered = (pump_type == MessagePumpType::UI) ? 2 : 0;
EXPECT_EQ(initial_work_enters + work_loop_entered,
GetAndroidNonDelayedWorkEnterCount());
}
TEST_P(MessagePumpTest, YieldDuringStartup) {
testing::InSequence sequence;
MessagePumpType pump_type = GetParam();
testing::StrictMock<MockMessagePumpDelegate> delegate(pump_type);
uint32_t initial_work_enters = GetAndroidNonDelayedWorkEnterCount();
EXPECT_CALL(delegate, DoWork).WillOnce([pump_type] {
if (pump_type == MessagePumpType::UI) {
android::YieldToLooperChecker::GetInstance().SetStartupRunning(true);
}
auto work_info =
MessagePump::Delegate::NextWorkInfo{.delayed_run_time = TimeTicks()};
CHECK(work_info.is_immediate());
return work_info;
});
EXPECT_CALL(delegate, DoWork).WillOnce([pump_type] {
if (pump_type == MessagePumpType::UI) {
android::YieldToLooperChecker::GetInstance().SetStartupRunning(false);
}
return MessagePump::Delegate::NextWorkInfo{.delayed_run_time = TimeTicks()};
});
EXPECT_CALL(delegate, DoWork).WillOnce([this] {
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{.delayed_run_time =
TimeTicks::Max()};
});
EXPECT_CALL(delegate, DoIdleWork()).Times(0);
message_pump_->Run(&delegate);
uint32_t work_loop_entered = (pump_type == MessagePumpType::UI) ? 2 : 0;
EXPECT_EQ(initial_work_enters + work_loop_entered,
GetAndroidNonDelayedWorkEnterCount());
}
#endif
TEST_P(MessagePumpTest, QuitStopsWorkWithNestedRunLoop) {
testing::InSequence sequence;
testing::StrictMock<MockMessagePumpDelegate> delegate(GetParam());
testing::StrictMock<MockMessagePumpDelegate> nested_delegate(GetParam());
AddPreDoWorkExpectations(delegate);
EXPECT_CALL(delegate, DoWork).WillOnce([&] {
message_pump_->Run(&nested_delegate);
return MessagePump::Delegate::NextWorkInfo();
});
AddPreDoWorkExpectations(nested_delegate);
EXPECT_CALL(nested_delegate, DoWork).WillOnce([&] {
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{TimeTicks::Max()};
});
AddPostDoWorkExpectations(nested_delegate);
AddPostDoWorkExpectations(delegate);
AddPreDoWorkExpectations(delegate);
EXPECT_CALL(delegate, DoWork).WillOnce([this] {
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{TimeTicks::Max()};
});
message_pump_->ScheduleWork();
message_pump_->Run(&delegate);
}
TEST_P(MessagePumpTest, LeewaySmokeTest) {
testing::StrictMock<MockMessagePumpDelegate> delegate(GetParam());
testing::InSequence sequence;
AddPreDoWorkExpectations(delegate);
EXPECT_CALL(delegate, DoWork).WillOnce([this] {
message_pump_->Quit();
auto now = TimeTicks::Now();
return MessagePump::Delegate::NextWorkInfo{now + Milliseconds(1),
Milliseconds(8), now};
});
EXPECT_CALL(delegate, DoIdleWork()).Times(AnyNumber());
message_pump_->ScheduleWork();
message_pump_->Run(&delegate);
}
TEST_P(MessagePumpTest, RunWithoutScheduleWorkInvokesDoWork) {
testing::InSequence sequence;
testing::StrictMock<MockMessagePumpDelegate> delegate(GetParam());
AddPreDoWorkExpectations(delegate);
EXPECT_CALL(delegate, DoWork).WillOnce([this] {
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{TimeTicks::Max()};
});
AddPostDoWorkExpectations(delegate);
#if BUILDFLAG(IS_IOS)
EXPECT_CALL(delegate, DoIdleWork).Times(AnyNumber());
#endif
message_pump_->Run(&delegate);
}
TEST_P(MessagePumpTest, NestedRunWithoutScheduleWorkInvokesDoWork) {
testing::InSequence sequence;
testing::StrictMock<MockMessagePumpDelegate> delegate(GetParam());
testing::StrictMock<MockMessagePumpDelegate> nested_delegate(GetParam());
AddPreDoWorkExpectations(delegate);
EXPECT_CALL(delegate, DoWork).WillOnce([this, &nested_delegate] {
message_pump_->Run(&nested_delegate);
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{TimeTicks::Max()};
});
AddPreDoWorkExpectations(nested_delegate);
EXPECT_CALL(nested_delegate, DoWork).WillOnce([this] {
message_pump_->Quit();
return MessagePump::Delegate::NextWorkInfo{TimeTicks::Max()};
});
AddPostDoWorkExpectations(nested_delegate);
AddPostDoWorkExpectations(delegate);
#if BUILDFLAG(IS_IOS)
EXPECT_CALL(nested_delegate, DoIdleWork).Times(AnyNumber());
EXPECT_CALL(delegate, DoIdleWork).Times(AnyNumber());
#endif
message_pump_->Run(&delegate);
}
INSTANTIATE_TEST_SUITE_P(All,
MessagePumpTest,
::testing::Values(MessagePumpType::DEFAULT,
MessagePumpType::UI,
MessagePumpType::IO));
#if !BUILDFLAG(IS_IOS)
TEST(MessagePumpDefaultTest, BusyLoop) {
MessagePumpDefault message_pump;
EXPECT_FALSE(message_pump.ShouldBusyLoop());
base::TimeDelta busy_loop_for = base::Milliseconds(1);
message_pump.SetBusyLoop(busy_loop_for);
EXPECT_TRUE(message_pump.ShouldBusyLoop());
for (int i = 0; i < 10; i++) {
message_pump.RecordWaitTime(busy_loop_for * 10);
}
EXPECT_FALSE(message_pump.ShouldBusyLoop());
message_pump.RecordWaitTime(busy_loop_for / 1.5);
EXPECT_TRUE(message_pump.ShouldBusyLoop());
message_pump.RecordWaitTime(busy_loop_for * 1.5);
EXPECT_FALSE(message_pump.ShouldBusyLoop());
for (int i = 0; i < 100; i++) {
message_pump.RecordWaitTime(busy_loop_for / 10);
}
EXPECT_TRUE(message_pump.ShouldBusyLoop());
message_pump.RecordWaitTime(busy_loop_for * 1.5);
EXPECT_TRUE(message_pump.ShouldBusyLoop());
}
#endif
}