This source file includes following definitions.
- GetInstance
- Signal
- Block
- Quit
- Start
- Run
- Quit
- ScheduleWork
- ScheduleDelayedWork
- CreateMessagePumpForUIStub
- GetTestProviderPath
- InitPathProvider
- InitAndroidTestLogging
- InitAndroidTestPaths
- InitAndroidTestMessageLoop
- InitAndroidTest
#include <stdarg.h>
#include <string.h>
#include "base/android/path_utils.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_pump_android.h"
#include "base/path_service.h"
#include "base/synchronization/waitable_event.h"
namespace {
struct RunState {
RunState(base::MessagePump::Delegate* delegate, int run_depth)
: delegate(delegate),
run_depth(run_depth),
should_quit(false) {
}
base::MessagePump::Delegate* delegate;
int run_depth;
bool should_quit;
};
RunState* g_state = NULL;
class Waitable {
public:
static Waitable* GetInstance() {
return Singleton<Waitable>::get();
}
void Signal() {
waitable_event_.Signal();
}
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_;
DISALLOW_COPY_AND_ASSIGN(Waitable);
};
class MessagePumpForUIStub : public base::MessagePumpForUI {
virtual ~MessagePumpForUIStub() {}
virtual void Start(base::MessagePump::Delegate* delegate) OVERRIDE {
NOTREACHED() << "The Start() method shouldn't be called in test, using"
" Run() method should be used.";
}
virtual void Run(base::MessagePump::Delegate* delegate) OVERRIDE {
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;
}
virtual void Quit() OVERRIDE {
Waitable::GetInstance()->Quit();
}
virtual void ScheduleWork() OVERRIDE {
Waitable::GetInstance()->Signal();
}
virtual void ScheduleDelayedWork(
const base::TimeTicks& delayed_work_time) OVERRIDE {
Waitable::GetInstance()->Signal();
}
};
scoped_ptr<base::MessagePump> CreateMessagePumpForUIStub() {
return scoped_ptr<base::MessagePump>(new MessagePumpForUIStub());
};
bool GetTestProviderPath(int key, base::FilePath* result) {
switch (key) {
case base::DIR_MODULE: {
return base::android::GetExternalStorageDirectory(result);
}
case base::DIR_ANDROID_APP_DATA: {
return base::android::GetExternalStorageDirectory(result);
}
default:
return false;
}
}
void InitPathProvider(int key) {
base::FilePath path;
if (GetTestProviderPath(key, &path) && !PathService::Override(key, path))
PathService::RegisterProvider(&GetTestProviderPath, key, key + 1);
}
}
namespace base {
void InitAndroidTestLogging() {
logging::LoggingSettings settings;
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
logging::InitLogging(settings);
logging::SetLogItems(false,
false,
false,
false);
}
void InitAndroidTestPaths() {
InitPathProvider(DIR_MODULE);
InitPathProvider(DIR_ANDROID_APP_DATA);
}
void InitAndroidTestMessageLoop() {
if (!MessageLoop::InitMessagePumpForUIFactory(&CreateMessagePumpForUIStub))
LOG(INFO) << "MessagePumpForUIFactory already set, unable to override.";
}
void InitAndroidTest() {
InitAndroidTestLogging();
InitAndroidTestPaths();
InitAndroidTestMessageLoop();
}
}