OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "base/android/activity_status.h" | 5 #include "base/android/activity_status.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/callback_forward.h" | 7 #include "base/callback_forward.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 namespace android { | 17 namespace android { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
22 | 22 |
23 // An invalid ActivityState value. | 23 // An invalid ApplicationState value. |
24 const ActivityState kInvalidActivityState = static_cast<ActivityState>(100); | 24 const ApplicationState kInvalidApplicationState = |
| 25 static_cast<ApplicationState>(100); |
25 | 26 |
26 // Used to generate a callback that stores the new state at a given location. | 27 // Used to generate a callback that stores the new state at a given location. |
27 void StoreStateTo(ActivityState* target, ActivityState state) { | 28 void StoreStateTo(ApplicationState* target, ApplicationState state) { |
28 *target = state; | 29 *target = state; |
29 } | 30 } |
30 | 31 |
31 void RunTasksUntilIdle() { | 32 void RunTasksUntilIdle() { |
32 RunLoop run_loop; | 33 RunLoop run_loop; |
33 run_loop.RunUntilIdle(); | 34 run_loop.RunUntilIdle(); |
34 } | 35 } |
35 | 36 |
36 // Shared state for the multi-threaded test. | 37 // Shared state for the multi-threaded test. |
37 // This uses a thread to register for events and listen to them, while state | 38 // This uses a thread to register for events and listen to them, while state |
38 // changes are forced on the main thread. | 39 // changes are forced on the main thread. |
39 class MultiThreadedTest { | 40 class MultiThreadedTest { |
40 public: | 41 public: |
41 MultiThreadedTest() | 42 MultiThreadedTest() |
42 : activity_status_(ActivityStatus::GetInstance()), | 43 : activity_status_(ActivityStatus::GetInstance()), |
43 state_(kInvalidActivityState), | 44 state_(kInvalidApplicationState), |
44 event_(false, false), | 45 event_(false, false), |
45 thread_("ActivityStatusTest thread"), | 46 thread_("ActivityStatusTest thread"), |
46 main_() { | 47 main_() { |
47 } | 48 } |
48 | 49 |
49 void Run() { | 50 void Run() { |
50 // Start the thread and tell it to register for events. | 51 // Start the thread and tell it to register for events. |
51 thread_.Start(); | 52 thread_.Start(); |
52 thread_.message_loop() | 53 thread_.message_loop() |
53 ->PostTask(FROM_HERE, | 54 ->PostTask(FROM_HERE, |
54 base::Bind(&MultiThreadedTest::RegisterThreadForEvents, | 55 base::Bind(&MultiThreadedTest::RegisterThreadForEvents, |
55 base::Unretained(this))); | 56 base::Unretained(this))); |
56 | 57 |
57 // Wait for its completion. | 58 // Wait for its completion. |
58 event_.Wait(); | 59 event_.Wait(); |
59 | 60 |
60 // Change state, then wait for the thread to modify state. | 61 // Change state, then wait for the thread to modify state. |
61 activity_status_->OnActivityStateChange(ACTIVITY_STATE_CREATED); | 62 activity_status_->OnApplicationStateChange(APPLICATION_STATE_RUNNING); |
62 event_.Wait(); | 63 event_.Wait(); |
63 EXPECT_EQ(ACTIVITY_STATE_CREATED, state_); | 64 EXPECT_EQ(APPLICATION_STATE_RUNNING, state_); |
64 | 65 |
65 // Again | 66 // Again |
66 activity_status_->OnActivityStateChange(ACTIVITY_STATE_DESTROYED); | 67 activity_status_->OnApplicationStateChange(APPLICATION_STATE_DESTROYED); |
67 event_.Wait(); | 68 event_.Wait(); |
68 EXPECT_EQ(ACTIVITY_STATE_DESTROYED, state_); | 69 EXPECT_EQ(APPLICATION_STATE_DESTROYED, state_); |
69 } | 70 } |
70 | 71 |
71 private: | 72 private: |
72 void ExpectOnThread() { | 73 void ExpectOnThread() { |
73 EXPECT_EQ(thread_.message_loop(), base::MessageLoop::current()); | 74 EXPECT_EQ(thread_.message_loop(), base::MessageLoop::current()); |
74 } | 75 } |
75 | 76 |
76 void RegisterThreadForEvents() { | 77 void RegisterThreadForEvents() { |
77 ExpectOnThread(); | 78 ExpectOnThread(); |
78 listener_.reset(new ActivityStatus::Listener(base::Bind( | 79 listener_.reset(new ActivityStatus::Listener(base::Bind( |
79 &MultiThreadedTest::StoreStateAndSignal, base::Unretained(this)))); | 80 &MultiThreadedTest::StoreStateAndSignal, base::Unretained(this)))); |
80 EXPECT_TRUE(listener_.get()); | 81 EXPECT_TRUE(listener_.get()); |
81 event_.Signal(); | 82 event_.Signal(); |
82 } | 83 } |
83 | 84 |
84 void StoreStateAndSignal(ActivityState state) { | 85 void StoreStateAndSignal(ApplicationState state) { |
85 ExpectOnThread(); | 86 ExpectOnThread(); |
86 state_ = state; | 87 state_ = state; |
87 event_.Signal(); | 88 event_.Signal(); |
88 } | 89 } |
89 | 90 |
90 ActivityStatus* const activity_status_; | 91 ActivityStatus* const activity_status_; |
91 ActivityState state_; | 92 ApplicationState state_; |
92 base::WaitableEvent event_; | 93 base::WaitableEvent event_; |
93 base::Thread thread_; | 94 base::Thread thread_; |
94 base::MessageLoop main_; | 95 base::MessageLoop main_; |
95 scoped_ptr<ActivityStatus::Listener> listener_; | 96 scoped_ptr<ActivityStatus::Listener> listener_; |
96 }; | 97 }; |
97 | 98 |
98 } // namespace | 99 } // namespace |
99 | 100 |
100 TEST(ActivityStatusTest, SingleThread) { | 101 TEST(ActivityStatusTest, SingleThread) { |
101 MessageLoop message_loop; | 102 MessageLoop message_loop; |
102 | 103 |
103 ActivityState result = kInvalidActivityState; | 104 ApplicationState result = kInvalidApplicationState; |
104 | 105 |
105 // Create a new listener that stores the new state into |result| on every | 106 // Create a new listener that stores the new state into |result| on every |
106 // state change. | 107 // state change. |
107 ActivityStatus::Listener listener( | 108 ActivityStatus::Listener listener( |
108 base::Bind(&StoreStateTo, base::Unretained(&result))); | 109 base::Bind(&StoreStateTo, base::Unretained(&result))); |
109 | 110 |
110 EXPECT_EQ(kInvalidActivityState, result); | 111 EXPECT_EQ(kInvalidApplicationState, result); |
111 | 112 |
112 ActivityStatus* const activity_status = ActivityStatus::GetInstance(); | 113 ActivityStatus* const activity_status = ActivityStatus::GetInstance(); |
113 activity_status->OnActivityStateChange(ACTIVITY_STATE_CREATED); | 114 activity_status->OnApplicationStateChange(APPLICATION_STATE_RUNNING); |
114 RunTasksUntilIdle(); | 115 RunTasksUntilIdle(); |
115 EXPECT_EQ(ACTIVITY_STATE_CREATED, result); | 116 EXPECT_EQ(APPLICATION_STATE_RUNNING, result); |
116 | 117 |
117 activity_status->OnActivityStateChange(ACTIVITY_STATE_DESTROYED); | 118 activity_status->OnApplicationStateChange(APPLICATION_STATE_DESTROYED); |
118 RunTasksUntilIdle(); | 119 RunTasksUntilIdle(); |
119 EXPECT_EQ(ACTIVITY_STATE_DESTROYED, result); | 120 EXPECT_EQ(APPLICATION_STATE_DESTROYED, result); |
120 } | 121 } |
121 | 122 |
122 TEST(ActivityStatusTest, TwoThreads) { | 123 TEST(ActivityStatusTest, TwoThreads) { |
123 MultiThreadedTest test; | 124 MultiThreadedTest test; |
124 test.Run(); | 125 test.Run(); |
125 } | 126 } |
126 | 127 |
127 } // namespace android | 128 } // namespace android |
128 } // namespace base | 129 } // namespace base |
OLD | NEW |