application_status_listener_unittest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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
  3. // found in the LICENSE file.
  4. #include "base/android/application_status_listener.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/run_loop.h"
  8. #include "base/synchronization/waitable_event.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/threading/thread.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace base {
  13. namespace android {
  14. namespace {
  15. using base::android::ScopedJavaLocalRef;
  16. // An invalid ApplicationState value.
  17. const ApplicationState kInvalidApplicationState =
  18. static_cast<ApplicationState>(100);
  19. // Used to generate a callback that stores the new state at a given location.
  20. void StoreStateTo(ApplicationState* target, ApplicationState state) {
  21. *target = state;
  22. }
  23. void RunTasksUntilIdle() {
  24. RunLoop run_loop;
  25. run_loop.RunUntilIdle();
  26. }
  27. // Shared state for the multi-threaded test.
  28. // This uses a thread to register for events and listen to them, while state
  29. // changes are forced on the main thread.
  30. class MultiThreadedTest {
  31. public:
  32. MultiThreadedTest()
  33. : state_(kInvalidApplicationState),
  34. event_(WaitableEvent::ResetPolicy::AUTOMATIC,
  35. WaitableEvent::InitialState::NOT_SIGNALED),
  36. thread_("ApplicationStatusTest thread") {}
  37. void Run() {
  38. // Start the thread and tell it to register for events.
  39. thread_.Start();
  40. thread_.task_runner()->PostTask(
  41. FROM_HERE, base::BindOnce(&MultiThreadedTest::RegisterThreadForEvents,
  42. base::Unretained(this)));
  43. // Wait for its completion.
  44. event_.Wait();
  45. // Change state, then wait for the thread to modify state.
  46. ApplicationStatusListener::NotifyApplicationStateChange(
  47. APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
  48. event_.Wait();
  49. EXPECT_EQ(APPLICATION_STATE_HAS_RUNNING_ACTIVITIES, state_);
  50. // Again
  51. ApplicationStatusListener::NotifyApplicationStateChange(
  52. APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES);
  53. event_.Wait();
  54. EXPECT_EQ(APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES, state_);
  55. }
  56. private:
  57. void ExpectOnThread() {
  58. EXPECT_TRUE(thread_.task_runner()->BelongsToCurrentThread());
  59. }
  60. void RegisterThreadForEvents() {
  61. ExpectOnThread();
  62. listener_ = ApplicationStatusListener::New(base::BindRepeating(
  63. &MultiThreadedTest::StoreStateAndSignal, base::Unretained(this)));
  64. EXPECT_TRUE(listener_.get());
  65. event_.Signal();
  66. }
  67. void StoreStateAndSignal(ApplicationState state) {
  68. ExpectOnThread();
  69. state_ = state;
  70. event_.Signal();
  71. }
  72. ApplicationState state_;
  73. base::WaitableEvent event_;
  74. base::Thread thread_;
  75. test::TaskEnvironment task_environment_;
  76. std::unique_ptr<ApplicationStatusListener> listener_;
  77. };
  78. } // namespace
  79. TEST(ApplicationStatusListenerTest, SingleThread) {
  80. test::TaskEnvironment task_environment;
  81. ApplicationState result = kInvalidApplicationState;
  82. // Create a new listener that stores the new state into |result| on every
  83. // state change.
  84. auto listener = ApplicationStatusListener::New(
  85. base::BindRepeating(&StoreStateTo, base::Unretained(&result)));
  86. EXPECT_EQ(kInvalidApplicationState, result);
  87. ApplicationStatusListener::NotifyApplicationStateChange(
  88. APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
  89. RunTasksUntilIdle();
  90. EXPECT_EQ(APPLICATION_STATE_HAS_RUNNING_ACTIVITIES, result);
  91. ApplicationStatusListener::NotifyApplicationStateChange(
  92. APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES);
  93. RunTasksUntilIdle();
  94. EXPECT_EQ(APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES, result);
  95. }
  96. TEST(ApplicationStatusListenerTest, TwoThreads) {
  97. MultiThreadedTest test;
  98. test.Run();
  99. }
  100. } // namespace android
  101. } // namespace base