web_thread_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ios/web/public/thread/web_thread.h"
  5. #include "base/bind.h"
  6. #include "ios/web/public/test/web_task_environment.h"
  7. #include "ios/web/public/thread/web_task_traits.h"
  8. #include "ios/web/public/thread/web_thread.h"
  9. #include "ios/web/web_thread_impl.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #include "testing/platform_test.h"
  12. namespace web {
  13. class WebThreadTest : public PlatformTest {
  14. protected:
  15. static void BasicFunction(base::OnceClosure continuation,
  16. WebThread::ID target) {
  17. auto other_thread = target == WebThread::UI ? WebThread::IO : WebThread::UI;
  18. EXPECT_TRUE(WebThread::CurrentlyOn(target));
  19. EXPECT_FALSE(WebThread::CurrentlyOn(other_thread));
  20. std::move(continuation).Run();
  21. }
  22. web::WebTaskEnvironment task_environment_{WebTaskEnvironment::REAL_IO_THREAD};
  23. };
  24. TEST_F(WebThreadTest, BasePostTask) {
  25. base::RunLoop run_loop;
  26. EXPECT_TRUE(GetIOThreadTaskRunner({})->PostTask(
  27. FROM_HERE, base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
  28. WebThread::IO)));
  29. run_loop.Run();
  30. }
  31. TEST_F(WebThreadTest, GetUITaskRunnerPostTask) {
  32. base::RunLoop run_loop;
  33. EXPECT_TRUE(GetUIThreadTaskRunner({})->PostTask(
  34. FROM_HERE, base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
  35. WebThread::UI)));
  36. run_loop.Run();
  37. }
  38. TEST_F(WebThreadTest, GetIOTaskRunnerPostTask) {
  39. base::RunLoop run_loop;
  40. EXPECT_TRUE(GetIOThreadTaskRunner({})->PostTask(
  41. FROM_HERE, base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
  42. WebThread::IO)));
  43. run_loop.Run();
  44. }
  45. TEST_F(WebThreadTest, PostTaskViaTaskRunner) {
  46. scoped_refptr<base::TaskRunner> task_runner = GetIOThreadTaskRunner({});
  47. base::RunLoop run_loop;
  48. EXPECT_TRUE(task_runner->PostTask(
  49. FROM_HERE, base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
  50. WebThread::IO)));
  51. run_loop.Run();
  52. }
  53. TEST_F(WebThreadTest, PostTaskViaSequencedTaskRunner) {
  54. scoped_refptr<base::SequencedTaskRunner> task_runner =
  55. GetIOThreadTaskRunner({});
  56. base::RunLoop run_loop;
  57. EXPECT_TRUE(task_runner->PostTask(
  58. FROM_HERE, base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
  59. WebThread::IO)));
  60. run_loop.Run();
  61. }
  62. TEST_F(WebThreadTest, PostTaskViaSingleThreadTaskRunner) {
  63. scoped_refptr<base::SingleThreadTaskRunner> task_runner =
  64. GetIOThreadTaskRunner({});
  65. base::RunLoop run_loop;
  66. EXPECT_TRUE(task_runner->PostTask(
  67. FROM_HERE, base::BindOnce(&BasicFunction, run_loop.QuitWhenIdleClosure(),
  68. WebThread::IO)));
  69. run_loop.Run();
  70. }
  71. } // namespace web