blob: dbd15059da9307453dfa1ff577400768ea0ffebc [file] [log] [blame]
Daniel Eratcb573442015-09-18 09:58:03 -06001/*
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 Eratc2a4b052015-09-30 17:04:48 -060019#include <base/logging.h>
Daniel Eratcb573442015-09-18 09:58:03 -060020#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 Eratc2a4b052015-09-30 17:04:48 -060026#include <nativepower/power_manager_client.h>
Daniel Eratcb573442015-09-18 09:58:03 -060027#include <nativepower/power_manager_stub.h>
28#include <nativepower/wake_lock.h>
29
30namespace android {
31
32class WakeLockTest : public BinderTestBase {
33 public:
34 WakeLockTest()
35 : power_manager_(new PowerManagerStub()),
36 power_manager_binder_(power_manager_) {
Daniel Eratc2a4b052015-09-30 17:04:48 -060037 binder_wrapper()->SetBinderForService(kPowerManagerServiceName,
38 power_manager_binder_);
39 CHECK(client_.Init());
Daniel Eratcb573442015-09-18 09:58:03 -060040 }
41 ~WakeLockTest() override = default;
42
43 protected:
44 PowerManagerStub* power_manager_; // Owned by |power_manager_binder_|.
45 sp<IBinder> power_manager_binder_;
Daniel Eratc2a4b052015-09-30 17:04:48 -060046 PowerManagerClient client_;
Daniel Eratcb573442015-09-18 09:58:03 -060047
48 private:
49 DISALLOW_COPY_AND_ASSIGN(WakeLockTest);
50};
51
52TEST_F(WakeLockTest, CreateAndDestroy) {
Daniel Erat4f13a602015-10-16 11:10:05 -060053 const uid_t kUid = 123;
54 binder_wrapper()->set_calling_uid(kUid);
Daniel Eratc2a4b052015-09-30 17:04:48 -060055 std::unique_ptr<WakeLock> lock(client_.CreateWakeLock("foo", "bar"));
Daniel Erat4f13a602015-10-16 11:10:05 -060056 ASSERT_EQ(1, power_manager_->GetNumWakeLocks());
Daniel Eratcb573442015-09-18 09:58:03 -060057 ASSERT_EQ(1u, binder_wrapper()->local_binders().size());
58 EXPECT_EQ(
Daniel Erat4f13a602015-10-16 11:10:05 -060059 PowerManagerStub::ConstructWakeLockString("foo", "bar", kUid),
60 power_manager_->GetWakeLockString(binder_wrapper()->local_binders()[0]));
Daniel Eratcb573442015-09-18 09:58:03 -060061
62 lock.reset();
Daniel Erat4f13a602015-10-16 11:10:05 -060063 EXPECT_EQ(0, power_manager_->GetNumWakeLocks());
Daniel Eratcb573442015-09-18 09:58:03 -060064}
65
66TEST_F(WakeLockTest, PowerManagerDeath) {
Daniel Eratc2a4b052015-09-30 17:04:48 -060067 std::unique_ptr<WakeLock> lock(client_.CreateWakeLock("foo", "bar"));
Daniel Eratcb573442015-09-18 09:58:03 -060068 binder_wrapper()->NotifyAboutBinderDeath(power_manager_binder_);
69
Daniel Eratc2a4b052015-09-30 17:04:48 -060070 // Since PowerManagerClient was informed that the power manager died, WakeLock
71 // shouldn't try to release its lock on destruction.
Daniel Eratcb573442015-09-18 09:58:03 -060072 lock.reset();
Daniel Erat4f13a602015-10-16 11:10:05 -060073 EXPECT_EQ(1, power_manager_->GetNumWakeLocks());
Daniel Eratcb573442015-09-18 09:58:03 -060074}
75
76} // namespace android