Use UintToString() for unsigned values.
[chromium-blink-merge.git] / base / test / test_support_android.cc
bloba0f825c813d54ce3c96d0726b4c51116ff383836
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.
5 #include <stdarg.h>
6 #include <string.h>
8 #include "base/android/path_utils.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/memory/singleton.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/message_loop/message_pump_android.h"
14 #include "base/path_service.h"
15 #include "base/synchronization/waitable_event.h"
17 namespace {
19 struct RunState {
20 RunState(base::MessagePump::Delegate* delegate, int run_depth)
21 : delegate(delegate),
22 run_depth(run_depth),
23 should_quit(false) {
26 base::MessagePump::Delegate* delegate;
28 // Used to count how many Run() invocations are on the stack.
29 int run_depth;
31 // Used to flag that the current Run() invocation should return ASAP.
32 bool should_quit;
35 RunState* g_state = NULL;
37 // A singleton WaitableEvent wrapper so we avoid a busy loop in
38 // MessagePumpForUIStub. Other platforms use the native event loop which blocks
39 // when there are no pending messages.
40 class Waitable {
41 public:
42 static Waitable* GetInstance() { return base::Singleton<Waitable>::get(); }
44 // Signals that there are more work to do.
45 void Signal() {
46 waitable_event_.Signal();
49 // Blocks until more work is scheduled.
50 void Block() {
51 waitable_event_.Wait();
54 void Quit() {
55 g_state->should_quit = true;
56 Signal();
59 private:
60 friend struct base::DefaultSingletonTraits<Waitable>;
62 Waitable()
63 : waitable_event_(false, false) {
66 base::WaitableEvent waitable_event_;
68 DISALLOW_COPY_AND_ASSIGN(Waitable);
71 // The MessagePumpForUI implementation for test purpose.
72 class MessagePumpForUIStub : public base::MessagePumpForUI {
73 ~MessagePumpForUIStub() override {}
75 void Start(base::MessagePump::Delegate* delegate) override {
76 NOTREACHED() << "The Start() method shouldn't be called in test, using"
77 " Run() method should be used.";
80 void Run(base::MessagePump::Delegate* delegate) override {
81 // The following was based on message_pump_glib.cc, except we're using a
82 // WaitableEvent since there are no native message loop to use.
83 RunState state(delegate, g_state ? g_state->run_depth + 1 : 1);
85 RunState* previous_state = g_state;
86 g_state = &state;
88 bool more_work_is_plausible = true;
90 for (;;) {
91 if (!more_work_is_plausible) {
92 Waitable::GetInstance()->Block();
93 if (g_state->should_quit)
94 break;
97 more_work_is_plausible = g_state->delegate->DoWork();
98 if (g_state->should_quit)
99 break;
101 base::TimeTicks delayed_work_time;
102 more_work_is_plausible |=
103 g_state->delegate->DoDelayedWork(&delayed_work_time);
104 if (g_state->should_quit)
105 break;
107 if (more_work_is_plausible)
108 continue;
110 more_work_is_plausible = g_state->delegate->DoIdleWork();
111 if (g_state->should_quit)
112 break;
114 more_work_is_plausible |= !delayed_work_time.is_null();
117 g_state = previous_state;
120 void Quit() override { Waitable::GetInstance()->Quit(); }
122 void ScheduleWork() override { Waitable::GetInstance()->Signal(); }
124 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override {
125 Waitable::GetInstance()->Signal();
129 scoped_ptr<base::MessagePump> CreateMessagePumpForUIStub() {
130 return scoped_ptr<base::MessagePump>(new MessagePumpForUIStub());
133 // Provides the test path for DIR_MODULE and DIR_ANDROID_APP_DATA.
134 bool GetTestProviderPath(int key, base::FilePath* result) {
135 switch (key) {
136 case base::DIR_ANDROID_APP_DATA: {
137 // For tests, app data is put in external storage.
138 return base::android::GetExternalStorageDirectory(result);
140 default:
141 return false;
145 void InitPathProvider(int key) {
146 base::FilePath path;
147 // If failed to override the key, that means the way has not been registered.
148 if (GetTestProviderPath(key, &path) && !PathService::Override(key, path))
149 PathService::RegisterProvider(&GetTestProviderPath, key, key + 1);
152 } // namespace
154 namespace base {
156 void InitAndroidTestLogging() {
157 logging::LoggingSettings settings;
158 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
159 logging::InitLogging(settings);
160 // To view log output with IDs and timestamps use "adb logcat -v threadtime".
161 logging::SetLogItems(false, // Process ID
162 false, // Thread ID
163 false, // Timestamp
164 false); // Tick count
167 void InitAndroidTestPaths() {
168 InitPathProvider(DIR_MODULE);
169 InitPathProvider(DIR_ANDROID_APP_DATA);
172 void InitAndroidTestMessageLoop() {
173 if (!MessageLoop::InitMessagePumpForUIFactory(&CreateMessagePumpForUIStub))
174 LOG(INFO) << "MessagePumpForUIFactory already set, unable to override.";
177 void InitAndroidTest() {
178 InitAndroidTestLogging();
179 InitAndroidTestPaths();
180 InitAndroidTestMessageLoop();
182 } // namespace base