1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/android/application_status_listener.h"
9 #include "base/functional/bind.h"
10 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/test/task_environment.h"
13 #include "base/threading/thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
21 using base::android::ScopedJavaLocalRef;
23 // An invalid ApplicationState value.
24 const ApplicationState kInvalidApplicationState =
25 static_cast<ApplicationState>(100);
27 // Used to generate a callback that stores the new state at a given location.
28 void StoreStateTo(ApplicationState* target, ApplicationState state) {
32 void RunTasksUntilIdle() {
34 run_loop.RunUntilIdle();
37 // Shared state for the multi-threaded test.
38 // This uses a thread to register for events and listen to them, while state
39 // changes are forced on the main thread.
40 class MultiThreadedTest {
43 : state_(kInvalidApplicationState),
44 event_(WaitableEvent::ResetPolicy::AUTOMATIC,
45 WaitableEvent::InitialState::NOT_SIGNALED),
46 thread_("ApplicationStatusTest thread") {}
49 // Start the thread and tell it to register for events.
51 thread_.task_runner()->PostTask(
52 FROM_HERE, base::BindOnce(&MultiThreadedTest::RegisterThreadForEvents,
53 base::Unretained(this)));
55 // Wait for its completion.
58 // Change state, then wait for the thread to modify state.
59 ApplicationStatusListener::NotifyApplicationStateChange(
60 APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
62 EXPECT_EQ(APPLICATION_STATE_HAS_RUNNING_ACTIVITIES, state_);
65 ApplicationStatusListener::NotifyApplicationStateChange(
66 APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES);
68 EXPECT_EQ(APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES, state_);
72 void ExpectOnThread() {
73 EXPECT_TRUE(thread_.task_runner()->BelongsToCurrentThread());
76 void RegisterThreadForEvents() {
78 listener_ = ApplicationStatusListener::New(base::BindRepeating(
79 &MultiThreadedTest::StoreStateAndSignal, base::Unretained(this)));
80 EXPECT_TRUE(listener_.get());
84 void StoreStateAndSignal(ApplicationState state) {
90 ApplicationState state_;
91 base::WaitableEvent event_;
93 test::TaskEnvironment task_environment_;
94 std::unique_ptr<ApplicationStatusListener> listener_;
99 TEST(ApplicationStatusListenerTest, SingleThread) {
100 test::TaskEnvironment task_environment;
102 ApplicationState result = kInvalidApplicationState;
104 // Create a new listener that stores the new state into |result| on every
106 auto listener = ApplicationStatusListener::New(
107 base::BindRepeating(&StoreStateTo, base::Unretained(&result)));
109 EXPECT_EQ(kInvalidApplicationState, result);
111 ApplicationStatusListener::NotifyApplicationStateChange(
112 APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
114 EXPECT_EQ(APPLICATION_STATE_HAS_RUNNING_ACTIVITIES, result);
116 ApplicationStatusListener::NotifyApplicationStateChange(
117 APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES);
119 EXPECT_EQ(APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES, result);
122 TEST(ApplicationStatusListenerTest, TwoThreads) {
123 MultiThreadedTest test;
127 } // namespace android