OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/threading/thread.h" | 5 #include "base/threading/thread.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 // underlying thread (instead its messages can be processed by a RunLoop on the | 479 // underlying thread (instead its messages can be processed by a RunLoop on the |
480 // stack). | 480 // stack). |
481 class ExternalMessageLoopThread : public Thread { | 481 class ExternalMessageLoopThread : public Thread { |
482 public: | 482 public: |
483 ExternalMessageLoopThread() : Thread("ExternalMessageLoopThread") {} | 483 ExternalMessageLoopThread() : Thread("ExternalMessageLoopThread") {} |
484 | 484 |
485 ~ExternalMessageLoopThread() override { Stop(); } | 485 ~ExternalMessageLoopThread() override { Stop(); } |
486 | 486 |
487 void InstallMessageLoop() { SetMessageLoop(&external_message_loop_); } | 487 void InstallMessageLoop() { SetMessageLoop(&external_message_loop_); } |
488 | 488 |
| 489 void VerifyUsingExternalMessageLoop( |
| 490 bool expected_using_external_message_loop) { |
| 491 EXPECT_EQ(expected_using_external_message_loop, |
| 492 using_external_message_loop()); |
| 493 } |
| 494 |
489 private: | 495 private: |
490 base::MessageLoop external_message_loop_; | 496 base::MessageLoop external_message_loop_; |
491 | 497 |
492 DISALLOW_COPY_AND_ASSIGN(ExternalMessageLoopThread); | 498 DISALLOW_COPY_AND_ASSIGN(ExternalMessageLoopThread); |
493 }; | 499 }; |
494 | 500 |
495 } // namespace | 501 } // namespace |
496 | 502 |
497 TEST_F(ThreadTest, ExternalMessageLoop) { | 503 TEST_F(ThreadTest, ExternalMessageLoop) { |
498 ExternalMessageLoopThread a; | 504 ExternalMessageLoopThread a; |
499 EXPECT_FALSE(a.message_loop()); | 505 EXPECT_FALSE(a.message_loop()); |
500 EXPECT_FALSE(a.IsRunning()); | 506 EXPECT_FALSE(a.IsRunning()); |
| 507 a.VerifyUsingExternalMessageLoop(false); |
501 | 508 |
502 a.InstallMessageLoop(); | 509 a.InstallMessageLoop(); |
503 EXPECT_TRUE(a.message_loop()); | 510 EXPECT_TRUE(a.message_loop()); |
504 EXPECT_TRUE(a.IsRunning()); | 511 EXPECT_TRUE(a.IsRunning()); |
| 512 a.VerifyUsingExternalMessageLoop(true); |
505 | 513 |
506 bool ran = false; | 514 bool ran = false; |
507 a.task_runner()->PostTask( | 515 a.task_runner()->PostTask( |
508 FROM_HERE, base::Bind([](bool* toggled) { *toggled = true; }, &ran)); | 516 FROM_HERE, base::Bind([](bool* toggled) { *toggled = true; }, &ran)); |
509 base::RunLoop().RunUntilIdle(); | 517 base::RunLoop().RunUntilIdle(); |
510 EXPECT_TRUE(ran); | 518 EXPECT_TRUE(ran); |
511 | 519 |
512 a.Stop(); | 520 a.Stop(); |
513 EXPECT_FALSE(a.message_loop()); | 521 EXPECT_FALSE(a.message_loop()); |
514 EXPECT_FALSE(a.IsRunning()); | 522 EXPECT_FALSE(a.IsRunning()); |
| 523 a.VerifyUsingExternalMessageLoop(true); |
515 | 524 |
516 // Confirm that running any remaining tasks posted from Stop() goes smoothly | 525 // Confirm that running any remaining tasks posted from Stop() goes smoothly |
517 // (e.g. https://codereview.chromium.org/2135413003/#ps300001 crashed if | 526 // (e.g. https://codereview.chromium.org/2135413003/#ps300001 crashed if |
518 // StopSoon() posted Thread::ThreadQuitHelper() while |run_loop_| was null). | 527 // StopSoon() posted Thread::ThreadQuitHelper() while |run_loop_| was null). |
519 base::RunLoop().RunUntilIdle(); | 528 base::RunLoop().RunUntilIdle(); |
520 } | 529 } |
OLD | NEW |