test_support_android.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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
  3. // found in the LICENSE file.
  4. #include <android/looper.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include "base/android/path_utils.h"
  8. #include "base/files/file_path.h"
  9. #include "base/logging.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/singleton.h"
  12. #include "base/message_loop/message_pump.h"
  13. #include "base/message_loop/message_pump_android.h"
  14. #include "base/path_service.h"
  15. #include "base/synchronization/waitable_event.h"
  16. #include "base/test/multiprocess_test.h"
  17. namespace {
  18. base::FilePath* g_test_data_dir = nullptr;
  19. struct RunState {
  20. RunState(base::MessagePump::Delegate* delegate, int run_depth)
  21. : delegate(delegate),
  22. run_depth(run_depth),
  23. should_quit(false) {
  24. }
  25. raw_ptr<base::MessagePump::Delegate> delegate;
  26. // Used to count how many Run() invocations are on the stack.
  27. int run_depth;
  28. // Used to flag that the current Run() invocation should return ASAP.
  29. bool should_quit;
  30. };
  31. RunState* g_state = nullptr;
  32. // A singleton WaitableEvent wrapper so we avoid a busy loop in
  33. // MessagePumpForUIStub. Other platforms use the native event loop which blocks
  34. // when there are no pending messages.
  35. class Waitable {
  36. public:
  37. static Waitable* GetInstance() {
  38. return base::Singleton<Waitable,
  39. base::LeakySingletonTraits<Waitable>>::get();
  40. }
  41. Waitable(const Waitable&) = delete;
  42. Waitable& operator=(const Waitable&) = delete;
  43. // Signals that there are more work to do.
  44. void Signal() { waitable_event_.Signal(); }
  45. // Blocks until more work is scheduled.
  46. void Block() { waitable_event_.Wait(); }
  47. void Quit() {
  48. g_state->should_quit = true;
  49. Signal();
  50. }
  51. private:
  52. friend struct base::DefaultSingletonTraits<Waitable>;
  53. Waitable()
  54. : waitable_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
  55. base::WaitableEvent::InitialState::NOT_SIGNALED) {}
  56. base::WaitableEvent waitable_event_;
  57. };
  58. // The MessagePumpForUI implementation for test purpose.
  59. class MessagePumpForUIStub : public base::MessagePumpForUI {
  60. public:
  61. MessagePumpForUIStub() : base::MessagePumpForUI() { Waitable::GetInstance(); }
  62. ~MessagePumpForUIStub() override {}
  63. // In tests, there isn't a native thread, as such RunLoop::Run() should be
  64. // used to run the loop instead of attaching and delegating to the native
  65. // loop. As such, this override ignores the Attach() request.
  66. void Attach(base::MessagePump::Delegate* delegate) override {}
  67. void Run(base::MessagePump::Delegate* delegate) override {
  68. // The following was based on message_pump_glib.cc, except we're using a
  69. // WaitableEvent since there are no native message loop to use.
  70. RunState state(delegate, g_state ? g_state->run_depth + 1 : 1);
  71. RunState* previous_state = g_state;
  72. g_state = &state;
  73. // When not nested we can use the looper, otherwise fall back
  74. // to the stub implementation.
  75. if (g_state->run_depth > 1) {
  76. RunNested(delegate);
  77. } else {
  78. SetQuit(false);
  79. SetDelegate(delegate);
  80. // Pump the loop once in case we're starting off idle as ALooper_pollOnce
  81. // will never return in that case.
  82. ScheduleWork();
  83. while (true) {
  84. // Waits for either the delayed, or non-delayed fds to be signalled,
  85. // calling either OnDelayedLooperCallback, or
  86. // OnNonDelayedLooperCallback, respectively. This uses Android's Looper
  87. // implementation, which is based off of epoll.
  88. ALooper_pollOnce(-1, nullptr, nullptr, nullptr);
  89. if (ShouldQuit())
  90. break;
  91. }
  92. }
  93. g_state = previous_state;
  94. }
  95. void RunNested(base::MessagePump::Delegate* delegate) {
  96. bool more_work_is_plausible = true;
  97. for (;;) {
  98. if (!more_work_is_plausible) {
  99. Waitable::GetInstance()->Block();
  100. if (g_state->should_quit)
  101. break;
  102. }
  103. Delegate::NextWorkInfo next_work_info = g_state->delegate->DoWork();
  104. more_work_is_plausible = next_work_info.is_immediate();
  105. if (g_state->should_quit)
  106. break;
  107. if (more_work_is_plausible)
  108. continue;
  109. more_work_is_plausible = g_state->delegate->DoIdleWork();
  110. if (g_state->should_quit)
  111. break;
  112. more_work_is_plausible |= !next_work_info.delayed_run_time.is_max();
  113. }
  114. }
  115. void Quit() override {
  116. CHECK(g_state);
  117. if (g_state->run_depth > 1) {
  118. Waitable::GetInstance()->Quit();
  119. } else {
  120. MessagePumpForUI::Quit();
  121. }
  122. }
  123. void ScheduleWork() override {
  124. if (g_state && g_state->run_depth > 1) {
  125. Waitable::GetInstance()->Signal();
  126. } else {
  127. MessagePumpForUI::ScheduleWork();
  128. }
  129. }
  130. void ScheduleDelayedWork(
  131. const Delegate::NextWorkInfo& next_work_info) override {
  132. if (g_state && g_state->run_depth > 1) {
  133. Waitable::GetInstance()->Signal();
  134. } else {
  135. MessagePumpForUI::ScheduleDelayedWork(next_work_info);
  136. }
  137. }
  138. };
  139. std::unique_ptr<base::MessagePump> CreateMessagePumpForUIStub() {
  140. return std::unique_ptr<base::MessagePump>(new MessagePumpForUIStub());
  141. }
  142. // Provides the test path for paths overridden during tests.
  143. bool GetTestProviderPath(int key, base::FilePath* result) {
  144. switch (key) {
  145. // On Android, our tests don't have permission to write to DIR_MODULE.
  146. // gtest/test_runner.py pushes data to external storage.
  147. // TODO(agrieve): Stop overriding DIR_ANDROID_APP_DATA.
  148. // https://crbug.com/617734
  149. // Instead DIR_ASSETS should be used to discover assets file location in
  150. // tests.
  151. case base::DIR_ANDROID_APP_DATA:
  152. case base::DIR_ASSETS:
  153. case base::DIR_SRC_TEST_DATA_ROOT:
  154. case base::DIR_GEN_TEST_DATA_ROOT:
  155. CHECK(g_test_data_dir != nullptr);
  156. *result = *g_test_data_dir;
  157. return true;
  158. default:
  159. return false;
  160. }
  161. }
  162. void InitPathProvider(int key) {
  163. base::FilePath path;
  164. // If failed to override the key, that means the way has not been registered.
  165. if (GetTestProviderPath(key, &path) &&
  166. !base::PathService::Override(key, path)) {
  167. base::PathService::RegisterProvider(&GetTestProviderPath, key, key + 1);
  168. }
  169. }
  170. } // namespace
  171. namespace base {
  172. void InitAndroidTestPaths(const FilePath& test_data_dir) {
  173. if (g_test_data_dir) {
  174. CHECK(test_data_dir == *g_test_data_dir);
  175. return;
  176. }
  177. g_test_data_dir = new FilePath(test_data_dir);
  178. InitPathProvider(DIR_ANDROID_APP_DATA);
  179. InitPathProvider(DIR_ASSETS);
  180. InitPathProvider(DIR_SRC_TEST_DATA_ROOT);
  181. InitPathProvider(DIR_GEN_TEST_DATA_ROOT);
  182. }
  183. void InitAndroidTestMessageLoop() {
  184. // NOTE something else such as a JNI call may have already overridden the UI
  185. // factory.
  186. if (!MessagePump::IsMessagePumpForUIFactoryOveridden())
  187. MessagePump::OverrideMessagePumpForUIFactory(&CreateMessagePumpForUIStub);
  188. }
  189. } // namespace base