Commit 2f2ee2ca authored by Abhishek Bhardwaj's avatar Abhishek Bhardwaj Committed by Commit Bot

WakeLock: Remove separate wake lock class used in tests

This change removes a separate test implementation of wake locks. This
results in more production code being unit tested.

BUG=chromium:913353
TEST=Run WakeLockTest unit tests.

Change-Id: Ie4b3d3384c148c90c6ee937b91710bfe9b87d9c8
Reviewed-on: https://chromium-review.googlesource.com/c/1379704
Commit-Queue: Abhishek Bhardwaj <abhishekbh@chromium.org>
Reviewed-by: default avatarDan Erat <derat@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#618381}
parent a0cd397a
...@@ -10,8 +10,6 @@ source_set("wake_lock") { ...@@ -10,8 +10,6 @@ source_set("wake_lock") {
"wake_lock.h", "wake_lock.h",
"wake_lock_context.cc", "wake_lock_context.cc",
"wake_lock_context.h", "wake_lock_context.h",
"wake_lock_for_testing.cc",
"wake_lock_for_testing.h",
"wake_lock_provider.cc", "wake_lock_provider.cc",
"wake_lock_provider.h", "wake_lock_provider.h",
] ]
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <utility> #include <utility>
namespace device { namespace device {
WakeLock::WakeLock(mojom::WakeLockRequest request, WakeLock::WakeLock(mojom::WakeLockRequest request,
...@@ -46,8 +45,9 @@ void WakeLock::RequestWakeLock() { ...@@ -46,8 +45,9 @@ void WakeLock::RequestWakeLock() {
// Uses the Context to get the outstanding status of current binding. // Uses the Context to get the outstanding status of current binding.
// Two consecutive requests from the same client should be coalesced // Two consecutive requests from the same client should be coalesced
// as one request. // as one request.
if (*binding_set_.dispatch_context()) if (*binding_set_.dispatch_context()) {
return; return;
}
*binding_set_.dispatch_context() = true; *binding_set_.dispatch_context() = true;
num_lock_requests_++; num_lock_requests_++;
...@@ -61,7 +61,7 @@ void WakeLock::CancelWakeLock() { ...@@ -61,7 +61,7 @@ void WakeLock::CancelWakeLock() {
if (!(*binding_set_.dispatch_context())) if (!(*binding_set_.dispatch_context()))
return; return;
DCHECK(num_lock_requests_ > 0); DCHECK_GT(num_lock_requests_, 0);
*binding_set_.dispatch_context() = false; *binding_set_.dispatch_context() = false;
num_lock_requests_--; num_lock_requests_--;
UpdateWakeLock(); UpdateWakeLock();
...@@ -97,7 +97,7 @@ void WakeLock::HasWakeLockForTests(HasWakeLockForTestsCallback callback) { ...@@ -97,7 +97,7 @@ void WakeLock::HasWakeLockForTests(HasWakeLockForTestsCallback callback) {
} }
void WakeLock::UpdateWakeLock() { void WakeLock::UpdateWakeLock() {
DCHECK(num_lock_requests_ >= 0); DCHECK_GE(num_lock_requests_, 0);
if (num_lock_requests_) { if (num_lock_requests_) {
if (!wake_lock_) if (!wake_lock_)
...@@ -137,13 +137,11 @@ void WakeLock::RemoveWakeLock() { ...@@ -137,13 +137,11 @@ void WakeLock::RemoveWakeLock() {
void WakeLock::SwapWakeLock() { void WakeLock::SwapWakeLock() {
DCHECK(wake_lock_); DCHECK(wake_lock_);
auto new_wake_lock = std::make_unique<PowerSaveBlocker>(
type_, reason_, *description_, main_task_runner_, file_task_runner_);
// Do a swap to ensure that there isn't a brief period where the old // Do a swap to ensure that there isn't a brief period where the old
// PowerSaveBlocker is unblocked while the new PowerSaveBlocker is not // PowerSaveBlocker is unblocked while the new PowerSaveBlocker is not
// created. // created.
auto new_wake_lock = std::make_unique<PowerSaveBlocker>(
type_, reason_, *description_, main_task_runner_, file_task_runner_);
wake_lock_.swap(new_wake_lock); wake_lock_.swap(new_wake_lock);
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define SERVICES_DEVICE_WAKE_LOCK_WAKE_LOCK_H_ #define SERVICES_DEVICE_WAKE_LOCK_WAKE_LOCK_H_
#include <memory> #include <memory>
#include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
#include "services/device/public/mojom/wake_lock.mojom.h" #include "services/device/public/mojom/wake_lock.mojom.h"
#include "services/device/public/mojom/wake_lock_context.mojom.h" #include "services/device/public/mojom/wake_lock_context.mojom.h"
#include "services/device/wake_lock/power_save_blocker/power_save_blocker.h" #include "services/device/wake_lock/power_save_blocker/power_save_blocker.h"
#include "services/device/wake_lock/wake_lock.h"
#include "services/device/wake_lock/wake_lock_context.h" #include "services/device/wake_lock/wake_lock_context.h"
#include "ui/gfx/native_widget_types.h" #include "ui/gfx/native_widget_types.h"
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "services/device/wake_lock/wake_lock_context.h" #include "services/device/wake_lock/wake_lock_context.h"
#include <string>
#include <utility> #include <utility>
#include "services/device/wake_lock/wake_lock.h" #include "services/device/wake_lock/wake_lock.h"
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/device/wake_lock/wake_lock_for_testing.h"
#include <utility>
namespace device {
WakeLockForTesting::WakeLockForTesting(
mojom::WakeLockRequest request,
mojom::WakeLockType type,
mojom::WakeLockReason reason,
const std::string& description,
int context_id,
WakeLockContextCallback native_view_getter,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner)
: WakeLock(std::move(request),
type,
reason,
description,
context_id,
native_view_getter,
file_task_runner),
has_wake_lock_(false) {}
WakeLockForTesting::~WakeLockForTesting() {}
void WakeLockForTesting::HasWakeLockForTests(
HasWakeLockForTestsCallback callback) {
std::move(callback).Run(has_wake_lock_);
}
void WakeLockForTesting::UpdateWakeLock() {
DCHECK(num_lock_requests_ >= 0);
if (num_lock_requests_) {
if (!has_wake_lock_)
CreateWakeLock();
} else {
if (has_wake_lock_)
RemoveWakeLock();
}
}
void WakeLockForTesting::CreateWakeLock() {
DCHECK(!has_wake_lock_);
has_wake_lock_ = true;
}
void WakeLockForTesting::RemoveWakeLock() {
DCHECK(has_wake_lock_);
has_wake_lock_ = false;
}
void WakeLockForTesting::SwapWakeLock() {
DCHECK(has_wake_lock_);
}
} // namespace device
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_DEVICE_WAKE_LOCK_WAKE_LOCK_FOR_TESTING_H_
#define SERVICES_DEVICE_WAKE_LOCK_WAKE_LOCK_FOR_TESTING_H_
#include <memory>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/device/public/mojom/wake_lock.mojom.h"
#include "services/device/public/mojom/wake_lock_context.mojom.h"
#include "services/device/wake_lock/wake_lock.h"
#include "ui/gfx/native_widget_types.h"
namespace device {
class WakeLockForTesting : public WakeLock {
public:
WakeLockForTesting(
mojom::WakeLockRequest request,
mojom::WakeLockType type,
mojom::WakeLockReason reason,
const std::string& description,
int context_id,
WakeLockContextCallback native_view_getter,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner);
~WakeLockForTesting() override;
void HasWakeLockForTests(HasWakeLockForTestsCallback callback) override;
private:
void UpdateWakeLock() override;
void CreateWakeLock() override;
void RemoveWakeLock() override;
void SwapWakeLock() override;
bool has_wake_lock_;
DISALLOW_COPY_AND_ASSIGN(WakeLockForTesting);
};
} // namespace device
#endif // SERVICES_DEVICE_WAKE_LOCK_WAKE_LOCK_FOR_TESTING_H_
...@@ -4,16 +4,15 @@ ...@@ -4,16 +4,15 @@
#include "services/device/wake_lock/wake_lock_provider.h" #include "services/device/wake_lock/wake_lock_provider.h"
#include <memory>
#include <string>
#include <utility> #include <utility>
#include "mojo/public/cpp/bindings/strong_binding.h" #include "mojo/public/cpp/bindings/strong_binding.h"
#include "services/device/wake_lock/wake_lock.h" #include "services/device/wake_lock/wake_lock.h"
#include "services/device/wake_lock/wake_lock_for_testing.h"
namespace device { namespace device {
bool WakeLockProvider::is_in_unittest_ = false;
// static // static
void WakeLockProvider::Create( void WakeLockProvider::Create(
mojom::WakeLockProviderRequest request, mojom::WakeLockProviderRequest request,
...@@ -35,7 +34,7 @@ WakeLockProvider::~WakeLockProvider() {} ...@@ -35,7 +34,7 @@ WakeLockProvider::~WakeLockProvider() {}
void WakeLockProvider::GetWakeLockContextForID( void WakeLockProvider::GetWakeLockContextForID(
int context_id, int context_id,
mojo::InterfaceRequest<mojom::WakeLockContext> request) { mojo::InterfaceRequest<mojom::WakeLockContext> request) {
DCHECK(context_id >= 0); DCHECK_GE(context_id, 0);
mojo::MakeStrongBinding( mojo::MakeStrongBinding(
std::make_unique<WakeLockContext>(context_id, file_task_runner_, std::make_unique<WakeLockContext>(context_id, file_task_runner_,
native_view_getter_), native_view_getter_),
...@@ -47,17 +46,10 @@ void WakeLockProvider::GetWakeLockWithoutContext( ...@@ -47,17 +46,10 @@ void WakeLockProvider::GetWakeLockWithoutContext(
mojom::WakeLockReason reason, mojom::WakeLockReason reason,
const std::string& description, const std::string& description,
mojom::WakeLockRequest request) { mojom::WakeLockRequest request) {
if (is_in_unittest_) { // WakeLock owns itself.
// Create WakeLockForTesting. new WakeLock(std::move(request), type, reason, description,
new WakeLockForTesting(std::move(request), type, reason, description, WakeLockContext::WakeLockInvalidContextId, native_view_getter_,
WakeLockContext::WakeLockInvalidContextId, file_task_runner_);
native_view_getter_, file_task_runner_);
} else {
// WakeLock owns itself.
new WakeLock(std::move(request), type, reason, description,
WakeLockContext::WakeLockInvalidContextId, native_view_getter_,
file_task_runner_);
}
} }
} // namespace device } // namespace device
...@@ -37,8 +37,6 @@ class WakeLockProvider : public mojom::WakeLockProvider { ...@@ -37,8 +37,6 @@ class WakeLockProvider : public mojom::WakeLockProvider {
const std::string& description, const std::string& description,
mojom::WakeLockRequest request) override; mojom::WakeLockRequest request) override;
static bool is_in_unittest_;
private: private:
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
WakeLockContextCallback native_view_getter_; WakeLockContextCallback native_view_getter_;
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "services/device/wake_lock/wake_lock.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "mojo/public/cpp/bindings/interface_ptr.h" #include "mojo/public/cpp/bindings/interface_ptr.h"
#include "services/device/device_service_test_base.h" #include "services/device/device_service_test_base.h"
...@@ -25,7 +27,6 @@ class WakeLockTest : public DeviceServiceTestBase { ...@@ -25,7 +27,6 @@ class WakeLockTest : public DeviceServiceTestBase {
DeviceServiceTestBase::SetUp(); DeviceServiceTestBase::SetUp();
connector()->BindInterface(mojom::kServiceName, &wake_lock_provider_); connector()->BindInterface(mojom::kServiceName, &wake_lock_provider_);
WakeLockProvider::is_in_unittest_ = true;
wake_lock_provider_->GetWakeLockWithoutContext( wake_lock_provider_->GetWakeLockWithoutContext(
device::mojom::WakeLockType::kPreventAppSuspension, device::mojom::WakeLockType::kPreventAppSuspension,
device::mojom::WakeLockReason::kOther, "WakeLockTest", device::mojom::WakeLockReason::kOther, "WakeLockTest",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment