Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / android / application_status_listener_unittest.cc
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.
4
5 #include "base/android/application_status_listener.h"
6
7 #include <memory>
8
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"
15
16 namespace base {
17 namespace android {
18
19 namespace {
20
21 using base::android::ScopedJavaLocalRef;
22
23 // An invalid ApplicationState value.
24 const ApplicationState kInvalidApplicationState =
25     static_cast<ApplicationState>(100);
26
27 // Used to generate a callback that stores the new state at a given location.
28 void StoreStateTo(ApplicationState* target, ApplicationState state) {
29   *target = state;
30 }
31
32 void RunTasksUntilIdle() {
33   RunLoop run_loop;
34   run_loop.RunUntilIdle();
35 }
36
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 {
41  public:
42   MultiThreadedTest()
43       : state_(kInvalidApplicationState),
44         event_(WaitableEvent::ResetPolicy::AUTOMATIC,
45                WaitableEvent::InitialState::NOT_SIGNALED),
46         thread_("ApplicationStatusTest thread") {}
47
48   void Run() {
49     // Start the thread and tell it to register for events.
50     thread_.Start();
51     thread_.task_runner()->PostTask(
52         FROM_HERE, base::BindOnce(&MultiThreadedTest::RegisterThreadForEvents,
53                                   base::Unretained(this)));
54
55     // Wait for its completion.
56     event_.Wait();
57
58     // Change state, then wait for the thread to modify state.
59     ApplicationStatusListener::NotifyApplicationStateChange(
60         APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
61     event_.Wait();
62     EXPECT_EQ(APPLICATION_STATE_HAS_RUNNING_ACTIVITIES, state_);
63
64     // Again
65     ApplicationStatusListener::NotifyApplicationStateChange(
66         APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES);
67     event_.Wait();
68     EXPECT_EQ(APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES, state_);
69   }
70
71  private:
72   void ExpectOnThread() {
73     EXPECT_TRUE(thread_.task_runner()->BelongsToCurrentThread());
74   }
75
76   void RegisterThreadForEvents() {
77     ExpectOnThread();
78     listener_ = ApplicationStatusListener::New(base::BindRepeating(
79         &MultiThreadedTest::StoreStateAndSignal, base::Unretained(this)));
80     EXPECT_TRUE(listener_.get());
81     event_.Signal();
82   }
83
84   void StoreStateAndSignal(ApplicationState state) {
85     ExpectOnThread();
86     state_ = state;
87     event_.Signal();
88   }
89
90   ApplicationState state_;
91   base::WaitableEvent event_;
92   base::Thread thread_;
93   test::TaskEnvironment task_environment_;
94   std::unique_ptr<ApplicationStatusListener> listener_;
95 };
96
97 }  // namespace
98
99 TEST(ApplicationStatusListenerTest, SingleThread) {
100   test::TaskEnvironment task_environment;
101
102   ApplicationState result = kInvalidApplicationState;
103
104   // Create a new listener that stores the new state into |result| on every
105   // state change.
106   auto listener = ApplicationStatusListener::New(
107       base::BindRepeating(&StoreStateTo, base::Unretained(&result)));
108
109   EXPECT_EQ(kInvalidApplicationState, result);
110
111   ApplicationStatusListener::NotifyApplicationStateChange(
112       APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
113   RunTasksUntilIdle();
114   EXPECT_EQ(APPLICATION_STATE_HAS_RUNNING_ACTIVITIES, result);
115
116   ApplicationStatusListener::NotifyApplicationStateChange(
117       APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES);
118   RunTasksUntilIdle();
119   EXPECT_EQ(APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES, result);
120 }
121
122 TEST(ApplicationStatusListenerTest, TwoThreads) {
123   MultiThreadedTest test;
124   test.Run();
125 }
126
127 }  // namespace android
128 }  // namespace base