Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2246)

Unified Diff: base/run_loop_unittest.cc

Issue 2818533003: Make nesting/running states a RunLoop rather than a MessageLoop concept. (Closed)
Patch Set: still need to check MessageLoop::current() in Mojo's RunLoopNestingObserver::GetForThread() Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/run_loop.cc ('k') | chrome/browser/app_controller_mac.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/run_loop_unittest.cc
diff --git a/base/run_loop_unittest.cc b/base/run_loop_unittest.cc
index 5be97d4a7d0abb6b86b0579df4253599cd157f87..dcddcfbf78a9c2257ec11b72194fae44ba3b2960 100644
--- a/base/run_loop_unittest.cc
+++ b/base/run_loop_unittest.cc
@@ -11,6 +11,7 @@
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
+#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
@@ -62,12 +63,12 @@ class RunLoopTest : public testing::Test {
} // namespace
TEST_F(RunLoopTest, QuitWhenIdle) {
- message_loop_.task_runner()->PostTask(
+ ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_),
Unretained(&counter_)));
- message_loop_.task_runner()->PostTask(
+ ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_)));
- message_loop_.task_runner()->PostDelayedTask(
+ ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1));
run_loop_.Run();
@@ -75,14 +76,14 @@ TEST_F(RunLoopTest, QuitWhenIdle) {
}
TEST_F(RunLoopTest, QuitWhenIdleNestedLoop) {
- message_loop_.task_runner()->PostTask(
+ ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&RunNestedLoopTask, Unretained(&counter_)));
- message_loop_.task_runner()->PostTask(
+ ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_),
Unretained(&counter_)));
- message_loop_.task_runner()->PostTask(
+ ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_)));
- message_loop_.task_runner()->PostDelayedTask(
+ ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1));
run_loop_.Run();
@@ -90,11 +91,11 @@ TEST_F(RunLoopTest, QuitWhenIdleNestedLoop) {
}
TEST_F(RunLoopTest, QuitWhenIdleClosure) {
- message_loop_.task_runner()->PostTask(FROM_HERE,
- run_loop_.QuitWhenIdleClosure());
- message_loop_.task_runner()->PostTask(
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
+ run_loop_.QuitWhenIdleClosure());
+ ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_)));
- message_loop_.task_runner()->PostDelayedTask(
+ ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1));
run_loop_.Run();
@@ -113,4 +114,95 @@ TEST_F(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) {
quit_when_idle_closure.Run();
}
+TEST_F(RunLoopTest, IsRunningOnCurrentThread) {
+ EXPECT_FALSE(RunLoop::IsRunningOnCurrentThread());
+ ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE,
+ Bind([]() { EXPECT_TRUE(RunLoop::IsRunningOnCurrentThread()); }));
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure());
+ run_loop_.Run();
+}
+
+TEST_F(RunLoopTest, IsNestedOnCurrentThread) {
+ EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
+
+ ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, Bind([]() {
+ EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
+
+ RunLoop nested_run_loop;
+
+ ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE,
+ Bind([]() { EXPECT_TRUE(RunLoop::IsNestedOnCurrentThread()); }));
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
+ nested_run_loop.QuitClosure());
+
+ EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
+ MessageLoop::ScopedNestableTaskAllower allower(MessageLoop::current());
+ nested_run_loop.Run();
+ EXPECT_FALSE(RunLoop::IsNestedOnCurrentThread());
+ }));
+
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure());
+ run_loop_.Run();
+}
+
+class MockNestingObserver : public RunLoop::NestingObserver {
+ public:
+ MockNestingObserver() = default;
+
+ // RunLoop::NestingObserver:
+ MOCK_METHOD0(OnBeginNestedRunLoop, void());
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockNestingObserver);
+};
+
+TEST_F(RunLoopTest, NestingObservers) {
+ EXPECT_TRUE(RunLoop::IsNestingAllowedOnCurrentThread());
+
+ testing::StrictMock<MockNestingObserver> nesting_observer;
+
+ RunLoop::AddNestingObserverOnCurrentThread(&nesting_observer);
+
+ const RepeatingClosure run_nested_loop = Bind([]() {
+ RunLoop nested_run_loop;
+ ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, Bind([]() {
+ EXPECT_TRUE(RunLoop::IsNestingAllowedOnCurrentThread());
+ }));
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
+ nested_run_loop.QuitClosure());
+ MessageLoop::ScopedNestableTaskAllower allower(MessageLoop::current());
+ nested_run_loop.Run();
+ });
+
+ // Generate a stack of nested RunLoops, an OnBeginNestedRunLoop() is
+ // expected when beginning each nesting depth.
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_nested_loop);
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_nested_loop);
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop_.QuitClosure());
+
+ EXPECT_CALL(nesting_observer, OnBeginNestedRunLoop()).Times(2);
+ run_loop_.Run();
+
+ RunLoop::RemoveNestingObserverOnCurrentThread(&nesting_observer);
+}
+
+// Disabled on Android per http://crbug.com/643760.
+#if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
+TEST_F(RunLoopTest, DisallowWaitingDeathTest) {
+ EXPECT_TRUE(RunLoop::IsNestingAllowedOnCurrentThread());
+ RunLoop::DisallowNestingOnCurrentThread();
+ EXPECT_FALSE(RunLoop::IsNestingAllowedOnCurrentThread());
+
+ ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind([]() {
+ RunLoop nested_run_loop;
+ nested_run_loop.RunUntilIdle();
+ }));
+ EXPECT_DEATH({ run_loop_.RunUntilIdle(); }, "Check failed");
+}
+#endif // defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
+
} // namespace base
« no previous file with comments | « base/run_loop.cc ('k') | chrome/browser/app_controller_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698