Skip QD1A.190821.011 in stage-aosp-master

Bug: 141248619
Change-Id: Ife2ba92f4c3c2bad08db288db184ba25bf52e4ad
diff --git a/Android.bp b/Android.bp
index 133073d..bc453fe 100644
--- a/Android.bp
+++ b/Android.bp
@@ -35,8 +35,12 @@
     defaults: ["libpower_defaults"],
     srcs: ["power_test.cpp"],
     static_libs: ["libpower"],
-    shared_libs: ["android.system.suspend@1.0"],
+    shared_libs: [
+        "android.system.suspend@1.0",
+        "suspend_control_aidl_interface-cpp",
+    ],
     test_suites: ["device-tests"],
+    require_root: true,
 }
 
 cc_library_shared {
diff --git a/audio/Android.bp b/audio/Android.bp
index a7c51af..b4c0799 100644
--- a/audio/Android.bp
+++ b/audio/Android.bp
@@ -21,6 +21,7 @@
     ],
 
     header_libs: [
+        "libaudioclient_headers",
         "libbase_headers",
         "libhardware_legacy_headers",
     ],
diff --git a/include/wakelock/wakelock.h b/include/wakelock/wakelock.h
new file mode 100644
index 0000000..75a7c72
--- /dev/null
+++ b/include/wakelock/wakelock.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+namespace android {
+namespace wakelock {
+
+// RAII-style wake lock implementation
+class WakeLock {
+  public:
+    WakeLock(const std::string& name);
+    ~WakeLock();
+
+  private:
+    class WakeLockImpl;
+    std::unique_ptr<WakeLockImpl> mImpl;
+};
+
+}  // namespace wakelock
+}  // namespace android
diff --git a/power.cpp b/power.cpp
index e9b6ccd..16ed961 100644
--- a/power.cpp
+++ b/power.cpp
@@ -17,9 +17,11 @@
 #define LOG_TAG "power"
 #define ATRACE_TAG ATRACE_TAG_POWER
 
+#include <hardware_legacy/power.h>
+#include <wakelock/wakelock.h>
+
 #include <android-base/logging.h>
 #include <android/system/suspend/1.0/ISystemSuspend.h>
-#include <hardware_legacy/power.h>
 #include <utils/Trace.h>
 
 #include <mutex>
@@ -44,6 +46,7 @@
     ATRACE_CALL();
     const auto& suspendService = getSystemSuspendServiceOnce();
     if (!suspendService) {
+        LOG(ERROR) << "ISystemSuspend::getService() failed.";
         return -1;
     }
 
@@ -77,3 +80,34 @@
     }
     return -1;
 }
+
+namespace android {
+namespace wakelock {
+
+class WakeLock::WakeLockImpl {
+  public:
+    WakeLockImpl(const std::string& name);
+    ~WakeLockImpl();
+
+  private:
+    sp<IWakeLock> mWakeLock;
+};
+
+WakeLock::WakeLock(const std::string& name) : mImpl(std::make_unique<WakeLockImpl>(name)) {}
+
+WakeLock::~WakeLock() = default;
+
+WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
+    static sp<ISystemSuspend> suspendService = ISystemSuspend::getService();
+    mWakeLock = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name);
+}
+
+WakeLock::WakeLockImpl::~WakeLockImpl() {
+    auto ret = mWakeLock->release();
+    if (!ret.isOk()) {
+        LOG(ERROR) << "IWakeLock::release() call failed: " << ret.description();
+    }
+}
+
+}  // namespace wakelock
+}  // namespace android
diff --git a/power_test.cpp b/power_test.cpp
index 601df64..d6e414d 100644
--- a/power_test.cpp
+++ b/power_test.cpp
@@ -14,7 +14,11 @@
  * limitations under the License.
  */
 
+#include <android/system/suspend/ISuspendControlService.h>
+#include <binder/IServiceManager.h>
+#include <gtest/gtest.h>
 #include <hardware_legacy/power.h>
+#include <wakelock/wakelock.h>
 
 #include <csignal>
 #include <cstdlib>
@@ -22,8 +26,9 @@
 #include <thread>
 #include <vector>
 
-#include <gtest/gtest.h>
-
+using android::sp;
+using android::system::suspend::ISuspendControlService;
+using android::system::suspend::WakeLockInfo;
 using namespace std::chrono_literals;
 
 namespace android {
@@ -73,8 +78,10 @@
             for (int j = 0; j < numLocks; j++) {
                 // We want ids to be unique.
                 std::string id = std::to_string(i) + "/" + std::to_string(j);
-                ASSERT_EQ(acquire_wake_lock(PARTIAL_WAKE_LOCK, id.c_str()), 0);
-                ASSERT_EQ(release_wake_lock(id.c_str()), 0);
+                ASSERT_EQ(acquire_wake_lock(PARTIAL_WAKE_LOCK, id.c_str()), 0)
+                    << "id: " << id;
+                ASSERT_EQ(release_wake_lock(id.c_str()), 0)
+                    << "id: " << id;;
             }
         });
     }
@@ -83,4 +90,56 @@
     }
 }
 
+class WakeLockTest : public ::testing::Test {
+   public:
+    virtual void SetUp() override {
+        sp<IBinder> control =
+            android::defaultServiceManager()->getService(android::String16("suspend_control"));
+        ASSERT_NE(control, nullptr) << "failed to get the suspend control service";
+        controlService = interface_cast<ISuspendControlService>(control);
+    }
+
+    // Returns true iff found.
+    bool findWakeLockInfoByName(const sp<ISuspendControlService>& service, const std::string& name,
+                                WakeLockInfo* info) {
+        std::vector<WakeLockInfo> wlStats;
+        service->getWakeLockStats(&wlStats);
+        auto it = std::find_if(wlStats.begin(), wlStats.end(),
+                               [&name](const auto& x) { return x.name == name; });
+        if (it != wlStats.end()) {
+            *info = *it;
+            return true;
+        }
+        return false;
+    }
+
+    // All userspace wake locks are registered with system suspend.
+    sp<ISuspendControlService> controlService;
+};
+
+// Test RAII properties of WakeLock destructor.
+TEST_F(WakeLockTest, WakeLockDestructor) {
+    auto name = std::to_string(rand());
+    {
+        android::wakelock::WakeLock wl{name};
+
+        WakeLockInfo info;
+        auto success = findWakeLockInfoByName(controlService, name, &info);
+        ASSERT_TRUE(success);
+        ASSERT_EQ(info.name, name);
+        ASSERT_EQ(info.pid, getpid());
+        ASSERT_TRUE(info.isActive);
+    }
+
+    // SystemSuspend receives wake lock release requests on hwbinder thread, while stats requests
+    // come on binder thread. Sleep to make sure that stats are reported *after* wake lock release.
+    std::this_thread::sleep_for(1ms);
+    WakeLockInfo info;
+    auto success = findWakeLockInfoByName(controlService, name, &info);
+    ASSERT_TRUE(success);
+    ASSERT_EQ(info.name, name);
+    ASSERT_EQ(info.pid, getpid());
+    ASSERT_FALSE(info.isActive);
+}
+
 }  // namespace android