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 <base/logging.h>
     18 #include <base/macros.h>
     19 #include <base/time/time.h>
     20 #include <binderwrapper/binder_test_base.h>
     21 #include <binderwrapper/stub_binder_wrapper.h>
     22 #include <nativepower/constants.h>
     23 #include <nativepower/power_manager_client.h>
     24 #include <nativepower/power_manager_stub.h>
     25 
     26 namespace android {
     27 
     28 class PowerManagerClientTest : public BinderTestBase {
     29  public:
     30   PowerManagerClientTest()
     31       : power_manager_(new PowerManagerStub()),
     32         power_manager_binder_(power_manager_) {
     33     binder_wrapper()->SetBinderForService(kPowerManagerServiceName,
     34                                           power_manager_binder_);
     35     CHECK(client_.Init());
     36   }
     37   ~PowerManagerClientTest() override = default;
     38 
     39  protected:
     40   PowerManagerStub* power_manager_;  // Owned by |power_manager_binder_|.
     41   sp<IBinder> power_manager_binder_;
     42   PowerManagerClient client_;
     43 
     44  private:
     45   DISALLOW_COPY_AND_ASSIGN(PowerManagerClientTest);
     46 };
     47 
     48 TEST_F(PowerManagerClientTest, Suspend) {
     49   EXPECT_EQ(0, power_manager_->num_suspend_requests());
     50 
     51   const auto kEventTime = base::TimeDelta::FromMilliseconds(123);
     52   const int kFlags = 0x456;
     53   EXPECT_TRUE(client_.Suspend(kEventTime, SuspendReason::POWER_BUTTON, kFlags));
     54   EXPECT_EQ(1, power_manager_->num_suspend_requests());
     55   EXPECT_EQ(PowerManagerStub::ConstructSuspendRequestString(
     56                 kEventTime.InMilliseconds(),
     57                 static_cast<int>(SuspendReason::POWER_BUTTON), kFlags),
     58             power_manager_->GetSuspendRequestString(0));
     59 }
     60 
     61 TEST_F(PowerManagerClientTest, ShutDown) {
     62   EXPECT_TRUE(client_.ShutDown(ShutdownReason::DEFAULT));
     63   ASSERT_EQ(1u, power_manager_->shutdown_reasons().size());
     64   EXPECT_EQ("", power_manager_->shutdown_reasons()[0]);
     65 
     66   EXPECT_TRUE(client_.ShutDown(ShutdownReason::USER_REQUESTED));
     67   ASSERT_EQ(2u, power_manager_->shutdown_reasons().size());
     68   EXPECT_EQ(kShutdownReasonUserRequested,
     69             power_manager_->shutdown_reasons()[1]);
     70 }
     71 
     72 TEST_F(PowerManagerClientTest, Reboot) {
     73   EXPECT_TRUE(client_.Reboot(RebootReason::DEFAULT));
     74   ASSERT_EQ(1u, power_manager_->reboot_reasons().size());
     75   EXPECT_EQ("", power_manager_->reboot_reasons()[0]);
     76 
     77   EXPECT_TRUE(client_.Reboot(RebootReason::RECOVERY));
     78   ASSERT_EQ(2u, power_manager_->reboot_reasons().size());
     79   EXPECT_EQ(kRebootReasonRecovery, power_manager_->reboot_reasons()[1]);
     80 }
     81 
     82 }  // namespace android
     83