| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 #include "chrome/browser/metrics/thread_watcher_android.h" |
| 6 #include "base/android/application_status_listener.h" | 6 #include "base/android/application_status_listener.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "chrome/browser/metrics/thread_watcher.h" | 9 #include "chrome/browser/metrics/thread_watcher.h" |
| 10 #include "chrome/browser/metrics/thread_watcher_android.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "content/public/test/test_browser_thread.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 12 |
| 14 using content::BrowserThread; | |
| 15 | |
| 16 namespace { | 13 namespace { |
| 17 void OnThreadWatcherTask(base::WaitableEvent* event) { | 14 void OnThreadWatcherTask(base::WaitableEvent* event) { |
| 18 event->Signal(); | 15 event->Signal(); |
| 19 } | 16 } |
| 20 | 17 |
| 21 void PostAndWaitForWatchdogThread(base::WaitableEvent* event) { | 18 void PostAndWaitForWatchdogThread(base::WaitableEvent* event) { |
| 22 WatchDogThread::PostDelayedTask( | 19 WatchDogThread::PostDelayedTask( |
| 23 FROM_HERE, | 20 FROM_HERE, |
| 24 base::Bind(&OnThreadWatcherTask, event), | 21 base::Bind(&OnThreadWatcherTask, event), |
| 25 base::TimeDelta::FromSeconds(0)); | 22 base::TimeDelta::FromSeconds(0)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 37 | 34 |
| 38 PostAndWaitForWatchdogThread(&watchdog_thread_event); | 35 PostAndWaitForWatchdogThread(&watchdog_thread_event); |
| 39 } | 36 } |
| 40 | 37 |
| 41 } // namespace | 38 } // namespace |
| 42 | 39 |
| 43 TEST(ThreadWatcherAndroidTest, ApplicationStatusNotification) { | 40 TEST(ThreadWatcherAndroidTest, ApplicationStatusNotification) { |
| 44 // Do not delay the ThreadWatcherList initialization for this test. | 41 // Do not delay the ThreadWatcherList initialization for this test. |
| 45 ThreadWatcherList::g_initialize_delay_seconds = 0; | 42 ThreadWatcherList::g_initialize_delay_seconds = 0; |
| 46 | 43 |
| 47 base::MessageLoopForUI message_loop_for_ui; | 44 content::TestBrowserThreadBundle test_browser_thread_bundle; |
| 48 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop_for_ui); | |
| 49 | 45 |
| 50 std::unique_ptr<WatchDogThread> watchdog_thread_(new WatchDogThread()); | 46 std::unique_ptr<WatchDogThread> watchdog_thread_(new WatchDogThread()); |
| 51 watchdog_thread_->StartAndWaitForTesting(); | 47 watchdog_thread_->StartAndWaitForTesting(); |
| 52 | 48 |
| 53 EXPECT_FALSE(ThreadWatcherList::g_thread_watcher_list_); | 49 EXPECT_FALSE(ThreadWatcherList::g_thread_watcher_list_); |
| 54 | 50 |
| 55 | 51 |
| 56 // Register, and notify the application has just started, | 52 // Register, and notify the application has just started, |
| 57 // and ensure the thread watcher list is created. | 53 // and ensure the thread watcher list is created. |
| 58 ThreadWatcherAndroid::RegisterApplicationStatusListener(); | 54 ThreadWatcherAndroid::RegisterApplicationStatusListener(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 70 // And again the last transition, STOPPED -> STARTED. | 66 // And again the last transition, STOPPED -> STARTED. |
| 71 NotifyApplicationStateChange( | 67 NotifyApplicationStateChange( |
| 72 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES); | 68 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES); |
| 73 EXPECT_TRUE(ThreadWatcherList::g_thread_watcher_list_); | 69 EXPECT_TRUE(ThreadWatcherList::g_thread_watcher_list_); |
| 74 | 70 |
| 75 // ThreadWatcherList::StartWatchingAll() creates g_thread_watcher_observer_. | 71 // ThreadWatcherList::StartWatchingAll() creates g_thread_watcher_observer_. |
| 76 // This should be released by ThreadWatcherList::StopWatchingAll() in the end | 72 // This should be released by ThreadWatcherList::StopWatchingAll() in the end |
| 77 // of test to not affect other test cases. | 73 // of test to not affect other test cases. |
| 78 ThreadWatcherList::StopWatchingAll(); | 74 ThreadWatcherList::StopWatchingAll(); |
| 79 } | 75 } |
| OLD | NEW |