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

Side by Side Diff: base/message_loop/message_loop_unittest.cc

Issue 998063002: base: Make it possible to replace the MessageLoop's task runner (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: No more MessageLoopProxy. Created 5 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <vector> 5 #include <vector>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/bindable_single_thread_task_runner.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
13 #include "base/message_loop/message_loop_test.h" 14 #include "base/message_loop/message_loop_test.h"
14 #include "base/pending_task.h" 15 #include "base/pending_task.h"
15 #include "base/posix/eintr_wrapper.h" 16 #include "base/posix/eintr_wrapper.h"
16 #include "base/run_loop.h" 17 #include "base/run_loop.h"
17 #include "base/synchronization/waitable_event.h" 18 #include "base/synchronization/waitable_event.h"
19 #include "base/test/test_simple_task_runner.h"
18 #include "base/thread_task_runner_handle.h" 20 #include "base/thread_task_runner_handle.h"
19 #include "base/threading/platform_thread.h" 21 #include "base/threading/platform_thread.h"
20 #include "base/threading/thread.h" 22 #include "base/threading/thread.h"
21 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
22 24
23 #if defined(OS_WIN) 25 #if defined(OS_WIN)
24 #include "base/message_loop/message_pump_dispatcher.h" 26 #include "base/message_loop/message_pump_dispatcher.h"
25 #include "base/message_loop/message_pump_win.h" 27 #include "base/message_loop/message_pump_win.h"
26 #include "base/process/memory.h" 28 #include "base/process/memory.h"
27 #include "base/strings/string16.h" 29 #include "base/strings/string16.h"
(...skipping 976 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 ASSERT_TRUE(message_hwnd) << GetLastError(); 1006 ASSERT_TRUE(message_hwnd) << GetLastError();
1005 1007
1006 ASSERT_TRUE(PostMessage(message_hwnd, kSignalMsg, 0, 1)); 1008 ASSERT_TRUE(PostMessage(message_hwnd, kSignalMsg, 0, 1));
1007 1009
1008 loop.Run(); 1010 loop.Run();
1009 1011
1010 ASSERT_TRUE(UnregisterClass(MAKEINTATOM(atom), instance)); 1012 ASSERT_TRUE(UnregisterClass(MAKEINTATOM(atom), instance));
1011 } 1013 }
1012 #endif // defined(OS_WIN) 1014 #endif // defined(OS_WIN)
1013 1015
1016 class TestTaskRunner : public BindableSingleThreadTaskRunner {
1017 public:
1018 TestTaskRunner() : was_bound_(false) {}
1019
1020 // BindableSingleThreadTaskRunner implementation:
1021 void BindToCurrentThread() override { was_bound_ = true; }
1022 bool PostDelayedTask(const tracked_objects::Location& from_here,
1023 const base::Closure& task,
1024 base::TimeDelta delay) override {
1025 return false;
1026 }
1027 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
1028 const base::Closure& task,
1029 base::TimeDelta delay) override {
1030 return false;
1031 }
1032 bool RunsTasksOnCurrentThread() const override { return was_bound_; }
1033
1034 bool was_bound() const { return was_bound_; }
1035
1036 private:
1037 ~TestTaskRunner() override {}
1038
1039 bool was_bound_;
1040 };
1041
1042 TEST(MessageLoopTest, SwapTaskRunnerBeforeBinding) {
1043 scoped_ptr<MessageLoop> loop(MessageLoop::CreateUnbound(
1044 MessageLoop::TYPE_DEFAULT, MessageLoop::MessagePumpFactoryCallback()));
1045 scoped_refptr<TestTaskRunner> new_runner(new TestTaskRunner());
1046
1047 loop->SwapTaskRunner(new_runner);
1048 EXPECT_EQ(new_runner, loop->task_runner());
1049
1050 loop->BindToCurrentThread();
1051 EXPECT_TRUE(new_runner->was_bound());
1052 EXPECT_EQ(new_runner, ThreadTaskRunnerHandle::Get());
1053 }
1054
1055 TEST(MessageLoopTest, SwapTaskRunnerAfterBinding) {
1056 MessageLoop loop;
1057 scoped_refptr<TestTaskRunner> new_runner(new TestTaskRunner());
1058
1059 new_runner->BindToCurrentThread();
1060 loop.SwapTaskRunner(new_runner);
1061 EXPECT_EQ(new_runner, loop.task_runner());
1062 EXPECT_EQ(new_runner, ThreadTaskRunnerHandle::Get());
1063 }
1064
1065 TEST(MessageLoopTest, OriginalRunnerWorks) {
1066 MessageLoop loop;
1067 scoped_refptr<TestTaskRunner> new_runner(new TestTaskRunner());
1068
1069 new_runner->BindToCurrentThread();
1070 scoped_refptr<BindableSingleThreadTaskRunner> original_runner(
1071 loop.SwapTaskRunner(new_runner));
1072
1073 scoped_refptr<Foo> foo(new Foo());
1074 original_runner->PostTask(FROM_HERE,
1075 Bind(&Foo::Test1ConstRef, foo.get(), "a"));
1076 loop.RunUntilIdle();
1077 EXPECT_EQ(1, foo->test_count());
1078 }
1079
1014 } // namespace base 1080 } // namespace base
OLDNEW
« base/message_loop/message_loop.cc ('K') | « base/message_loop/message_loop_task_runner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698