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
3 // found in the LICENSE file.
5 #include "base/android/activity_status.h"
7 #include "base/callback_forward.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/run_loop.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
21 using base::android::ScopedJavaLocalRef
;
23 // An invalid ActivityState value.
24 const ActivityState kInvalidActivityState
= static_cast<ActivityState
>(100);
26 // Used to generate a callback that stores the new state at a given location.
27 void StoreStateTo(ActivityState
* target
, ActivityState state
) {
31 void RunTasksUntilIdle() {
33 run_loop
.RunUntilIdle();
36 // Shared state for the multi-threaded test.
37 // This uses a thread to register for events and listen to them, while state
38 // changes are forced on the main thread.
39 class MultiThreadedTest
{
42 : activity_status_(ActivityStatus::GetInstance()),
43 state_(kInvalidActivityState
),
45 thread_("ActivityStatusTest thread"),
50 // Start the thread and tell it to register for events.
52 thread_
.message_loop()
54 base::Bind(&MultiThreadedTest::RegisterThreadForEvents
,
55 base::Unretained(this)));
57 // Wait for its completion.
60 // Change state, then wait for the thread to modify state.
61 activity_status_
->OnActivityStateChange(ACTIVITY_STATE_CREATED
);
63 EXPECT_EQ(ACTIVITY_STATE_CREATED
, state_
);
66 activity_status_
->OnActivityStateChange(ACTIVITY_STATE_DESTROYED
);
68 EXPECT_EQ(ACTIVITY_STATE_DESTROYED
, state_
);
72 void ExpectOnThread() {
73 EXPECT_EQ(thread_
.message_loop(), base::MessageLoop::current());
76 void RegisterThreadForEvents() {
78 listener_
.reset(new ActivityStatus::Listener(base::Bind(
79 &MultiThreadedTest::StoreStateAndSignal
, base::Unretained(this))));
80 EXPECT_TRUE(listener_
.get());
84 void StoreStateAndSignal(ActivityState state
) {
90 ActivityStatus
* const activity_status_
;
92 base::WaitableEvent event_
;
94 base::MessageLoop main_
;
95 scoped_ptr
<ActivityStatus::Listener
> listener_
;
100 TEST(ActivityStatusTest
, SingleThread
) {
101 MessageLoop message_loop
;
103 ActivityState result
= kInvalidActivityState
;
105 // Create a new listener that stores the new state into |result| on every
107 ActivityStatus::Listener
listener(
108 base::Bind(&StoreStateTo
, base::Unretained(&result
)));
110 EXPECT_EQ(kInvalidActivityState
, result
);
112 ActivityStatus
* const activity_status
= ActivityStatus::GetInstance();
113 activity_status
->OnActivityStateChange(ACTIVITY_STATE_CREATED
);
115 EXPECT_EQ(ACTIVITY_STATE_CREATED
, result
);
117 activity_status
->OnActivityStateChange(ACTIVITY_STATE_DESTROYED
);
119 EXPECT_EQ(ACTIVITY_STATE_DESTROYED
, result
);
122 TEST(ActivityStatusTest
, TwoThreads
) {
123 MultiThreadedTest test
;
127 } // namespace android