1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdarg.h>
#include <string.h>
#include "base/file_path.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/message_pump_android.h"
#include "base/path_service.h"
#include "base/synchronization/waitable_event.h"
namespace {
// The test implementation of AndroidOS stores everything in the following
// directory.
const char* kAndroidTestTempDirectory = "/data/local/tmp";
struct RunState {
RunState(base::MessagePump::Delegate* delegate, int run_depth)
: delegate(delegate),
run_depth(run_depth),
should_quit(false) {
}
base::MessagePump::Delegate* delegate;
// Used to count how many Run() invocations are on the stack.
int run_depth;
// Used to flag that the current Run() invocation should return ASAP.
bool should_quit;
};
RunState* g_state = NULL;
// A singleton WaitableEvent wrapper so we avoid a busy loop in
// MessagePumpForUIStub. Other platforms use the native event loop which blocks
// when there are no pending messages.
class Waitable {
public:
static Waitable* GetInstance() {
return Singleton<Waitable>::get();
}
// Signals that there are more work to do.
void Signal() {
waitable_event_.Signal();
}
// Blocks until more work is scheduled.
void Block() {
waitable_event_.Wait();
}
void Quit() {
g_state->should_quit = true;
Signal();
}
private:
friend struct DefaultSingletonTraits<Waitable>;
Waitable()
: waitable_event_(false, false) {
}
base::WaitableEvent waitable_event_;
};
// The MessagePumpForUI implementation for test purpose.
class MessagePumpForUIStub : public base::MessagePumpForUI {
void Start(base::MessagePump::Delegate* delegate) {
NOTREACHED() << "The Start() method shouldn't be called in test, using"
" Run() method should be used.";
}
void Run(base::MessagePump::Delegate* delegate) {
// The following was based on message_pump_glib.cc, except we're using a
// WaitableEvent since there are no native message loop to use.
RunState state(delegate, g_state ? g_state->run_depth + 1 : 1);
RunState* previous_state = g_state;
g_state = &state;
bool more_work_is_plausible = true;
for (;;) {
if (!more_work_is_plausible) {
Waitable::GetInstance()->Block();
if (g_state->should_quit)
break;
}
more_work_is_plausible = g_state->delegate->DoWork();
if (g_state->should_quit)
break;
base::TimeTicks delayed_work_time;
more_work_is_plausible |=
g_state->delegate->DoDelayedWork(&delayed_work_time);
if (g_state->should_quit)
break;
if (more_work_is_plausible)
continue;
more_work_is_plausible = g_state->delegate->DoIdleWork();
if (g_state->should_quit)
break;
more_work_is_plausible |= !delayed_work_time.is_null();
}
g_state = previous_state;
}
void Quit() {
Waitable::GetInstance()->Quit();
}
void ScheduleWork() {
Waitable::GetInstance()->Signal();
}
void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) {
Waitable::GetInstance()->Signal();
}
};
// Provides the test path for DIR_MODULE, DIR_CACHE and DIR_ANDROID_APP_DATA.
bool PathTestProviderAndroid(int key, FilePath* result) {
switch (key) {
case base::DIR_MODULE: {
*result = FilePath(kAndroidTestTempDirectory);
return true;
}
case base::DIR_CACHE: {
*result = FilePath(kAndroidTestTempDirectory);
return true;
}
case base::DIR_ANDROID_APP_DATA: {
*result = FilePath(kAndroidTestTempDirectory);
return true;
}
default:
return false;
}
}
// The factory method to create a MessagePumpForUI.
base::MessagePump* CreateMessagePumpForUIStub() {
return new MessagePumpForUIStub();
}
} // namespace
void InitAndroidTestStub() {
logging::InitLogging(NULL,
logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
logging::DONT_LOCK_LOG_FILE,
logging::DELETE_OLD_LOG_FILE,
logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
// To view log output with IDs and timestamps use "adb logcat -v threadtime".
logging::SetLogItems(false, // Process ID
false, // Thread ID
false, // Timestamp
false); // Tick count
PathService::RegisterProvider(&PathTestProviderAndroid, base::DIR_MODULE,
base::DIR_MODULE + 1);
PathService::RegisterProvider(&PathTestProviderAndroid, base::DIR_CACHE,
base::DIR_CACHE + 1);
PathService::RegisterProvider(&PathTestProviderAndroid,
base::DIR_ANDROID_APP_DATA, base::DIR_ANDROID_APP_DATA + 1);
MessageLoop::InitMessagePumpForUIFactory(&CreateMessagePumpForUIStub);
}
|