This source file includes following definitions.
- OnThreadWatcherTask
- PostAndWaitForWatchdogThread
- NotifyApplicationStateChange
- TEST
#include "base/android/application_status_listener.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/metrics/thread_watcher.h"
#include "chrome/browser/metrics/thread_watcher_android.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace {
void OnThreadWatcherTask(base::WaitableEvent* event) {
event->Signal();
}
void PostAndWaitForWatchdogThread(base::WaitableEvent* event) {
WatchDogThread::PostDelayedTask(
FROM_HERE,
base::Bind(&OnThreadWatcherTask, event),
base::TimeDelta::FromSeconds(0));
EXPECT_TRUE(event->TimedWait(base::TimeDelta::FromSeconds(1)));
}
void NotifyApplicationStateChange(base::android::ApplicationState state) {
base::WaitableEvent watchdog_thread_event(false, false);
base::android::ApplicationStatusListener::NotifyApplicationStateChange(state);
base::RunLoop().RunUntilIdle();
PostAndWaitForWatchdogThread(&watchdog_thread_event);
}
}
TEST(ThreadWatcherAndroidTest, ApplicationStatusNotification) {
ThreadWatcherList::g_initialize_delay_seconds = 0;
base::MessageLoopForUI message_loop_for_ui;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop_for_ui);
scoped_ptr<WatchDogThread> watchdog_thread_(new WatchDogThread());
watchdog_thread_->Start();
EXPECT_FALSE(ThreadWatcherList::g_thread_watcher_list_);
ThreadWatcherAndroid::RegisterApplicationStatusListener();
ThreadWatcherList::StartWatchingAll(*CommandLine::ForCurrentProcess());
NotifyApplicationStateChange(
base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
EXPECT_TRUE(ThreadWatcherList::g_thread_watcher_list_);
NotifyApplicationStateChange(
base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
EXPECT_FALSE(ThreadWatcherList::g_thread_watcher_list_);
NotifyApplicationStateChange(
base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
EXPECT_TRUE(ThreadWatcherList::g_thread_watcher_list_);
}