Home | History | Annotate | Download | only in client
      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 
     19 #include <base/logging.h>
     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>
     26 #include <nativepower/power_manager_client.h>
     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_) {
     37     binder_wrapper()->SetBinderForService(kPowerManagerServiceName,
     38                                           power_manager_binder_);
     39     CHECK(client_.Init());
     40   }
     41   ~WakeLockTest() override = default;
     42 
     43  protected:
     44   PowerManagerStub* power_manager_;  // Owned by |power_manager_binder_|.
     45   sp<IBinder> power_manager_binder_;
     46   PowerManagerClient client_;
     47 
     48  private:
     49   DISALLOW_COPY_AND_ASSIGN(WakeLockTest);
     50 };
     51 
     52 TEST_F(WakeLockTest, CreateAndDestroy) {
     53   const uid_t kUid = 123;
     54   binder_wrapper()->set_calling_uid(kUid);
     55   std::unique_ptr<WakeLock> lock(client_.CreateWakeLock("foo", "bar"));
     56   ASSERT_EQ(1, power_manager_->GetNumWakeLocks());
     57   ASSERT_EQ(1u, binder_wrapper()->local_binders().size());
     58   EXPECT_EQ(
     59       PowerManagerStub::ConstructWakeLockString("foo", "bar", kUid),
     60       power_manager_->GetWakeLockString(binder_wrapper()->local_binders()[0]));
     61 
     62   lock.reset();
     63   EXPECT_EQ(0, power_manager_->GetNumWakeLocks());
     64 }
     65 
     66 TEST_F(WakeLockTest, PowerManagerDeath) {
     67   std::unique_ptr<WakeLock> lock(client_.CreateWakeLock("foo", "bar"));
     68   binder_wrapper()->NotifyAboutBinderDeath(power_manager_binder_);
     69 
     70   // Since PowerManagerClient was informed that the power manager died, WakeLock
     71   // shouldn't try to release its lock on destruction.
     72   lock.reset();
     73   EXPECT_EQ(1, power_manager_->GetNumWakeLocks());
     74 }
     75 
     76 }  // namespace android
     77