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") {
"wake_lock.h",
"wake_lock_context.cc",
"wake_lock_context.h",
"wake_lock_for_testing.cc",
"wake_lock_for_testing.h",
"wake_lock_provider.cc",
"wake_lock_provider.h",
]
......
......@@ -6,7 +6,6 @@
#include <utility>
namespace device {
WakeLock::WakeLock(mojom::WakeLockRequest request,
......@@ -46,8 +45,9 @@ void WakeLock::RequestWakeLock() {
// Uses the Context to get the outstanding status of current binding.
// Two consecutive requests from the same client should be coalesced
// as one request.
if (*binding_set_.dispatch_context())
if (*binding_set_.dispatch_context()) {
return;
}
*binding_set_.dispatch_context() = true;
num_lock_requests_++;
......@@ -61,7 +61,7 @@ void WakeLock::CancelWakeLock() {
if (!(*binding_set_.dispatch_context()))
return;
DCHECK(num_lock_requests_ > 0);
DCHECK_GT(num_lock_requests_, 0);
*binding_set_.dispatch_context() = false;
num_lock_requests_--;
UpdateWakeLock();
......@@ -97,7 +97,7 @@ void WakeLock::HasWakeLockForTests(HasWakeLockForTestsCallback callback) {
}
void WakeLock::UpdateWakeLock() {
DCHECK(num_lock_requests_ >= 0);
DCHECK_GE(num_lock_requests_, 0);
if (num_lock_requests_) {
if (!wake_lock_)
......@@ -137,13 +137,11 @@ void WakeLock::RemoveWakeLock() {
void WakeLock::SwapWakeLock() {
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
// PowerSaveBlocker is unblocked while the new PowerSaveBlocker is not
// created.
auto new_wake_lock = std::make_unique<PowerSaveBlocker>(
type_, reason_, *description_, main_task_runner_, file_task_runner_);
wake_lock_.swap(new_wake_lock);
}
......
......@@ -6,6 +6,7 @@
#define SERVICES_DEVICE_WAKE_LOCK_WAKE_LOCK_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
......@@ -15,6 +16,7 @@
#include "services/device/public/mojom/wake_lock.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/wake_lock.h"
#include "services/device/wake_lock/wake_lock_context.h"
#include "ui/gfx/native_widget_types.h"
......
......@@ -4,6 +4,7 @@
#include "services/device/wake_lock/wake_lock_context.h"
#include <string>
#include <utility>
#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 @@
#include "services/device/wake_lock/wake_lock_provider.h"
#include <memory>
#include <string>
#include <utility>
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "services/device/wake_lock/wake_lock.h"
#include "services/device/wake_lock/wake_lock_for_testing.h"
namespace device {
bool WakeLockProvider::is_in_unittest_ = false;
// static
void WakeLockProvider::Create(
mojom::WakeLockProviderRequest request,
......@@ -35,7 +34,7 @@ WakeLockProvider::~WakeLockProvider() {}
void WakeLockProvider::GetWakeLockContextForID(
int context_id,
mojo::InterfaceRequest<mojom::WakeLockContext> request) {
DCHECK(context_id >= 0);
DCHECK_GE(context_id, 0);
mojo::MakeStrongBinding(
std::make_unique<WakeLockContext>(context_id, file_task_runner_,
native_view_getter_),
......@@ -47,17 +46,10 @@ void WakeLockProvider::GetWakeLockWithoutContext(
mojom::WakeLockReason reason,
const std::string& description,
mojom::WakeLockRequest request) {
if (is_in_unittest_) {
// Create WakeLockForTesting.
new WakeLockForTesting(std::move(request), type, reason, description,
WakeLockContext::WakeLockInvalidContextId,
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_);
}
// WakeLock owns itself.
new WakeLock(std::move(request), type, reason, description,
WakeLockContext::WakeLockInvalidContextId, native_view_getter_,
file_task_runner_);
}
} // namespace device
......@@ -37,8 +37,6 @@ class WakeLockProvider : public mojom::WakeLockProvider {
const std::string& description,
mojom::WakeLockRequest request) override;
static bool is_in_unittest_;
private:
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
WakeLockContextCallback native_view_getter_;
......
......@@ -2,6 +2,8 @@
// 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.h"
#include "base/run_loop.h"
#include "mojo/public/cpp/bindings/interface_ptr.h"
#include "services/device/device_service_test_base.h"
......@@ -25,7 +27,6 @@ class WakeLockTest : public DeviceServiceTestBase {
DeviceServiceTestBase::SetUp();
connector()->BindInterface(mojom::kServiceName, &wake_lock_provider_);
WakeLockProvider::is_in_unittest_ = true;
wake_lock_provider_->GetWakeLockWithoutContext(
device::mojom::WakeLockType::kPreventAppSuspension,
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