Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <memory> |
| 18 | |
Daniel Erat | c2a4b05 | 2015-09-30 17:04:48 -0600 | [diff] [blame] | 19 | #include <base/logging.h> |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 20 | #include <base/macros.h> |
| 21 | #include <binder/Binder.h> |
| 22 | #include <binder/IBinder.h> |
| 23 | #include <binderwrapper/binder_test_base.h> |
| 24 | #include <binderwrapper/stub_binder_wrapper.h> |
| 25 | #include <nativepower/constants.h> |
Daniel Erat | c2a4b05 | 2015-09-30 17:04:48 -0600 | [diff] [blame] | 26 | #include <nativepower/power_manager_client.h> |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 27 | #include <nativepower/power_manager_stub.h> |
| 28 | #include <nativepower/wake_lock.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | class WakeLockTest : public BinderTestBase { |
| 33 | public: |
| 34 | WakeLockTest() |
| 35 | : power_manager_(new PowerManagerStub()), |
| 36 | power_manager_binder_(power_manager_) { |
Daniel Erat | c2a4b05 | 2015-09-30 17:04:48 -0600 | [diff] [blame] | 37 | binder_wrapper()->SetBinderForService(kPowerManagerServiceName, |
| 38 | power_manager_binder_); |
| 39 | CHECK(client_.Init()); |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 40 | } |
| 41 | ~WakeLockTest() override = default; |
| 42 | |
| 43 | protected: |
| 44 | PowerManagerStub* power_manager_; // Owned by |power_manager_binder_|. |
| 45 | sp<IBinder> power_manager_binder_; |
Daniel Erat | c2a4b05 | 2015-09-30 17:04:48 -0600 | [diff] [blame] | 46 | PowerManagerClient client_; |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | DISALLOW_COPY_AND_ASSIGN(WakeLockTest); |
| 50 | }; |
| 51 | |
| 52 | TEST_F(WakeLockTest, CreateAndDestroy) { |
Daniel Erat | 4f13a60 | 2015-10-16 11:10:05 -0600 | [diff] [blame] | 53 | const uid_t kUid = 123; |
| 54 | binder_wrapper()->set_calling_uid(kUid); |
Daniel Erat | c2a4b05 | 2015-09-30 17:04:48 -0600 | [diff] [blame] | 55 | std::unique_ptr<WakeLock> lock(client_.CreateWakeLock("foo", "bar")); |
Daniel Erat | 4f13a60 | 2015-10-16 11:10:05 -0600 | [diff] [blame] | 56 | ASSERT_EQ(1, power_manager_->GetNumWakeLocks()); |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 57 | ASSERT_EQ(1u, binder_wrapper()->local_binders().size()); |
| 58 | EXPECT_EQ( |
Daniel Erat | 4f13a60 | 2015-10-16 11:10:05 -0600 | [diff] [blame] | 59 | PowerManagerStub::ConstructWakeLockString("foo", "bar", kUid), |
| 60 | power_manager_->GetWakeLockString(binder_wrapper()->local_binders()[0])); |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 61 | |
| 62 | lock.reset(); |
Daniel Erat | 4f13a60 | 2015-10-16 11:10:05 -0600 | [diff] [blame] | 63 | EXPECT_EQ(0, power_manager_->GetNumWakeLocks()); |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST_F(WakeLockTest, PowerManagerDeath) { |
Daniel Erat | c2a4b05 | 2015-09-30 17:04:48 -0600 | [diff] [blame] | 67 | std::unique_ptr<WakeLock> lock(client_.CreateWakeLock("foo", "bar")); |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 68 | binder_wrapper()->NotifyAboutBinderDeath(power_manager_binder_); |
| 69 | |
Daniel Erat | c2a4b05 | 2015-09-30 17:04:48 -0600 | [diff] [blame] | 70 | // Since PowerManagerClient was informed that the power manager died, WakeLock |
| 71 | // shouldn't try to release its lock on destruction. |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 72 | lock.reset(); |
Daniel Erat | 4f13a60 | 2015-10-16 11:10:05 -0600 | [diff] [blame] | 73 | EXPECT_EQ(1, power_manager_->GetNumWakeLocks()); |
Daniel Erat | cb57344 | 2015-09-18 09:58:03 -0600 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // namespace android |