aboutsummaryrefslogtreecommitdiff
path: root/daemon/wake_lock_manager_unittest.cc
diff options
context:
space:
mode:
authorDaniel Erat <derat@google.com>2015-09-18 09:58:03 -0600
committerDaniel Erat <derat@google.com>2015-09-18 12:04:37 -0600
commitcb57344ce792f41e58bc476840a82eba14b0fde9 (patch)
treed1cbf876d54cd2d953afba4dec454211917a0c4a /daemon/wake_lock_manager_unittest.cc
parent11ad82ee79a70d27788a725fb235029864569363 (diff)
downloadnativepower-cb57344ce792f41e58bc476840a82eba14b0fde9.tar.gz
Add libnativepower and nativepowerman.
Add the skeleton of a C++ library and daemon that can be used for power management by Brillo. Bug: 22122485 Change-Id: I3769ecc3e7b43efc3e03af4afada48d570f56ef9
Diffstat (limited to 'daemon/wake_lock_manager_unittest.cc')
-rw-r--r--daemon/wake_lock_manager_unittest.cc142
1 files changed, 142 insertions, 0 deletions
diff --git a/daemon/wake_lock_manager_unittest.cc b/daemon/wake_lock_manager_unittest.cc
new file mode 100644
index 0000000..887b564
--- /dev/null
+++ b/daemon/wake_lock_manager_unittest.cc
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#include <base/files/file_path.h>
+#include <base/files/file_util.h>
+#include <base/files/scoped_temp_dir.h>
+#include <base/logging.h>
+#include <base/macros.h>
+#include <binder/IBinder.h>
+#include <binderwrapper/binder_test_base.h>
+#include <binderwrapper/stub_binder_wrapper.h>
+
+#include "wake_lock_manager.h"
+
+namespace android {
+
+class WakeLockManagerTest : public BinderTestBase {
+ public:
+ WakeLockManagerTest() {
+ CHECK(temp_dir_.CreateUniqueTempDir());
+ lock_path_ = temp_dir_.path().Append("lock");
+ unlock_path_ = temp_dir_.path().Append("unlock");
+ ClearFiles();
+
+ manager_.set_paths_for_testing(lock_path_, unlock_path_);
+ CHECK(manager_.Init());
+ }
+ ~WakeLockManagerTest() override = default;
+
+ protected:
+ // Returns the contents of |path|.
+ std::string ReadFile(const base::FilePath& path) const {
+ std::string value;
+ CHECK(base::ReadFileToString(path, &value));
+ return value;
+ }
+
+ // Clears |lock_path_| and |unlock_path_|.
+ void ClearFiles() {
+ CHECK(base::WriteFile(lock_path_, "", 0) == 0);
+ CHECK(base::WriteFile(unlock_path_, "", 0) == 0);
+ }
+
+ base::ScopedTempDir temp_dir_;
+
+ // Files within |temp_dir_| simulating /sys/power/wake_lock and wake_unlock.
+ base::FilePath lock_path_;
+ base::FilePath unlock_path_;
+
+ WakeLockManager manager_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(WakeLockManagerTest);
+};
+
+TEST_F(WakeLockManagerTest, AddAndRemoveRequests) {
+ // A kernel wake lock should be created for the first request.
+ sp<BBinder> binder1 = binder_wrapper()->CreateLocalBinder();
+ EXPECT_TRUE(manager_.AddRequest(binder1, "1", "1", -1));
+ EXPECT_EQ(WakeLockManager::kLockName, ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+
+ // Nothing should happen when a second request is made.
+ ClearFiles();
+ sp<BBinder> binder2 = binder_wrapper()->CreateLocalBinder();
+ EXPECT_TRUE(manager_.AddRequest(binder2, "2", "2", -1));
+ EXPECT_EQ("", ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+
+ // The wake lock should still be held after the first request is withdrawn.
+ ClearFiles();
+ EXPECT_TRUE(manager_.RemoveRequest(binder1));
+ EXPECT_EQ("", ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+
+ // When there are no more requests, the wake lock should be released.
+ ClearFiles();
+ EXPECT_TRUE(manager_.RemoveRequest(binder2));
+ EXPECT_EQ("", ReadFile(lock_path_));
+ EXPECT_EQ(WakeLockManager::kLockName, ReadFile(unlock_path_));
+}
+
+TEST_F(WakeLockManagerTest, DuplicateRequest) {
+ sp<BBinder> binder = binder_wrapper()->CreateLocalBinder();
+ EXPECT_TRUE(manager_.AddRequest(binder, "foo", "bar", -1));
+ EXPECT_EQ(WakeLockManager::kLockName, ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+
+ // Send a second request using the same binder and check a new wake lock isn't
+ // created.
+ ClearFiles();
+ EXPECT_TRUE(manager_.AddRequest(binder, "a", "b", -1));
+ EXPECT_EQ("", ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+
+ ClearFiles();
+ EXPECT_TRUE(manager_.RemoveRequest(binder));
+ EXPECT_EQ("", ReadFile(lock_path_));
+ EXPECT_EQ(WakeLockManager::kLockName, ReadFile(unlock_path_));
+}
+
+TEST_F(WakeLockManagerTest, InvalidRemoval) {
+ // Trying to remove an unknown binder should fail and not do anything.
+ sp<BBinder> binder = binder_wrapper()->CreateLocalBinder();
+ EXPECT_FALSE(manager_.RemoveRequest(binder));
+ EXPECT_EQ("", ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+}
+
+TEST_F(WakeLockManagerTest, BinderDeath) {
+ sp<BBinder> binder = binder_wrapper()->CreateLocalBinder();
+ EXPECT_TRUE(manager_.AddRequest(binder, "foo", "bar", -1));
+ EXPECT_EQ(WakeLockManager::kLockName, ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+
+ // If the binder dies, the wake lock should be released.
+ ClearFiles();
+ binder_wrapper()->NotifyAboutBinderDeath(binder);
+ EXPECT_EQ("", ReadFile(lock_path_));
+ EXPECT_EQ(WakeLockManager::kLockName, ReadFile(unlock_path_));
+
+ // Check that a new request can be created using the same binder.
+ ClearFiles();
+ EXPECT_TRUE(manager_.AddRequest(binder, "foo", "bar", -1));
+ EXPECT_EQ(WakeLockManager::kLockName, ReadFile(lock_path_));
+ EXPECT_EQ("", ReadFile(unlock_path_));
+}
+
+} // namespace android