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