Commit 4209a162 authored by merkulova's avatar merkulova Committed by Commit bot

Created fakes for HID-detection screen testing. Initial browsertest added.

BUG=456779
TBR=keybuk@chromium.org

Review URL: https://codereview.chromium.org/913773002

Cr-Commit-Position: refs/heads/master@{#318667}
parent c3a3f671
......@@ -15,6 +15,9 @@ typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo;
namespace chromeos {
// static
BrowserThread::ID InputServiceProxy::thread_identifier_ = BrowserThread::FILE;
class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer {
public:
ServiceObserver() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); }
......@@ -73,7 +76,7 @@ class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer {
private:
bool CalledOnValidThread() const {
return BrowserThread::CurrentlyOn(BrowserThread::FILE);
return BrowserThread::CurrentlyOn(InputServiceProxy::thread_identifier_);
}
base::WeakPtr<InputServiceProxy> proxy_;
......@@ -82,10 +85,12 @@ class InputServiceProxy::ServiceObserver : public InputServiceLinux::Observer {
};
InputServiceProxy::InputServiceProxy()
: service_observer_(new ServiceObserver()), weak_factory_(this) {
: service_observer_(new ServiceObserver()),
task_runner_(BrowserThread::GetMessageLoopProxyForThread(
thread_identifier_)),
weak_factory_(this) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE,
task_runner_->PostTask(
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::Initialize,
base::Unretained(service_observer_.get()),
......@@ -94,8 +99,7 @@ InputServiceProxy::InputServiceProxy()
InputServiceProxy::~InputServiceProxy() {
DCHECK(thread_checker_.CalledOnValidThread());
BrowserThread::PostTask(
BrowserThread::FILE,
task_runner_->PostTask(
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::Shutdown,
base::Unretained(service_observer_.release())));
......@@ -104,7 +108,7 @@ InputServiceProxy::~InputServiceProxy() {
// static
void InputServiceProxy::WarmUp() {
content::BrowserThread::PostTask(
content::BrowserThread::FILE,
thread_identifier_,
FROM_HERE,
base::Bind(base::IgnoreResult(&InputServiceLinux::GetInstance)));
}
......@@ -123,8 +127,8 @@ void InputServiceProxy::RemoveObserver(Observer* observer) {
void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE,
base::PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::GetDevices,
base::Unretained(service_observer_.get())),
......@@ -134,8 +138,7 @@ void InputServiceProxy::GetDevices(const GetDevicesCallback& callback) {
void InputServiceProxy::GetDeviceInfo(const std::string& id,
const GetDeviceInfoCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
BrowserThread::PostTask(
BrowserThread::FILE,
task_runner_->PostTask(
FROM_HERE,
base::Bind(&InputServiceProxy::ServiceObserver::GetDeviceInfo,
base::Unretained(service_observer_.release()),
......@@ -143,6 +146,11 @@ void InputServiceProxy::GetDeviceInfo(const std::string& id,
callback));
}
// static
void InputServiceProxy::SetThreadIdForTesting(BrowserThread::ID thread_id) {
InputServiceProxy::thread_identifier_ = thread_id;
}
void InputServiceProxy::OnDeviceAdded(
const InputServiceLinux::InputDeviceInfo& info) {
DCHECK(thread_checker_.CalledOnValidThread());
......
......@@ -11,7 +11,9 @@
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/task_runner.h"
#include "base/threading/thread_checker.h"
#include "content/public/browser/browser_thread.h"
#include "device/hid/input_service_linux.h"
namespace chromeos {
......@@ -47,7 +49,12 @@ class InputServiceProxy {
void GetDeviceInfo(const std::string& id,
const GetDeviceInfoCallback& callback);
// Should be called once before any InputServiceProxy instance is created.
static void SetThreadIdForTesting(content::BrowserThread::ID thread_id);
private:
static content::BrowserThread::ID thread_identifier_;
class ServiceObserver;
void OnDeviceAdded(const device::InputServiceLinux::InputDeviceInfo& info);
......@@ -58,6 +65,8 @@ class InputServiceProxy {
base::ThreadChecker thread_checker_;
scoped_refptr<base::TaskRunner> task_runner_;
base::WeakPtrFactory<InputServiceProxy> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(InputServiceProxy);
......
// Copyright 2014 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 "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/login/test/oobe_base_test.h"
#include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
#include "chrome/browser/chromeos/login/ui/oobe_display.h"
#include "content/public/browser/browser_thread.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/hid/fake_input_service_linux.h"
#include "device/hid/input_service_linux.h"
#include "testing/gmock/include/gmock/gmock.h"
using content::BrowserThread;
using device::InputServiceLinux;
using testing::_;
using InputDeviceInfo = InputServiceLinux::InputDeviceInfo;
namespace {
void SetUpBluetoothMock(
scoped_refptr<
testing::NiceMock<device::MockBluetoothAdapter> > mock_adapter,
bool is_present) {
device::BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter);
EXPECT_CALL(*mock_adapter, IsPresent())
.WillRepeatedly(testing::Return(is_present));
EXPECT_CALL(*mock_adapter, IsPowered())
.WillRepeatedly(testing::Return(true));
EXPECT_CALL(*mock_adapter, GetDevices()).WillRepeatedly(
testing::Return(device::BluetoothAdapter::ConstDeviceList()));
}
} // namespace
namespace chromeos {
class HidDetectionTest : public OobeBaseTest {
public:
typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo;
HidDetectionTest() : weak_ptr_factory_(this) {
InputServiceProxy::SetThreadIdForTesting(content::BrowserThread::UI);
HidDetectionTest::InitInputService();
}
~HidDetectionTest() override {}
void InitInputService() {
input_service_linux_.reset(new device::FakeInputServiceLinux);
InputServiceLinux::SetForTesting(input_service_linux_.get());
}
void SetUpOnMainThread() override {
OobeBaseTest::SetUpOnMainThread();
}
void SetUpInProcessBrowserTestFixture() override {
OobeBaseTest::SetUpInProcessBrowserTestFixture();
mock_adapter_ = new testing::NiceMock<device::MockBluetoothAdapter>();
SetUpBluetoothMock(mock_adapter_, true);
}
void AddUsbMouse(const std::string& mouse_id) {
InputDeviceInfo mouse;
mouse.id = mouse_id;
mouse.subsystem = InputDeviceInfo::SUBSYSTEM_INPUT;
mouse.type = InputDeviceInfo::TYPE_USB;
mouse.is_mouse = true;
LOG(ERROR) << input_service_linux_.get();
input_service_linux_->AddDeviceForTesting(mouse);
}
void AddUsbKeyboard(const std::string& keyboard_id) {
InputDeviceInfo keyboard;
keyboard.id = keyboard_id;
keyboard.subsystem = InputDeviceInfo::SUBSYSTEM_INPUT;
keyboard.type = InputDeviceInfo::TYPE_USB;
keyboard.is_keyboard = true;
input_service_linux_->AddDeviceForTesting(keyboard);
}
private:
scoped_refptr<
testing::NiceMock<device::MockBluetoothAdapter> > mock_adapter_;
scoped_ptr<device::FakeInputServiceLinux> input_service_linux_;
base::WeakPtrFactory<HidDetectionTest> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(HidDetectionTest);
};
class HidDetectionSkipTest : public HidDetectionTest {
public:
HidDetectionSkipTest() {
AddUsbMouse("mouse");
AddUsbKeyboard("keyboard");
}
void SetUpOnMainThread() override {
HidDetectionTest::SetUpOnMainThread();
}
};
IN_PROC_BROWSER_TEST_F(HidDetectionTest, NoDevicesConnected) {
OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_HID_DETECTION).Wait();
}
IN_PROC_BROWSER_TEST_F(HidDetectionSkipTest, BothDevicesPreConnected) {
OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_NETWORK).Wait();
}
} // namespace chromeos
......@@ -657,6 +657,7 @@
'browser/chromeos/login/enrollment/mock_enrollment_screen.cc',
'browser/chromeos/login/enrollment/mock_enrollment_screen.h',
'browser/chromeos/login/existing_user_controller_browsertest.cc',
'browser/chromeos/login/hid_detection_browsertest.cc',
'browser/chromeos/login/kiosk_browsertest.cc',
'browser/chromeos/login/lock/screen_locker_tester.cc',
'browser/chromeos/login/lock/screen_locker_tester.h',
......
......@@ -6,6 +6,8 @@ source_set("hid") {
sources = [
"device_monitor_linux.cc",
"device_monitor_linux.h",
"fake_input_service_linux.cc",
"fake_input_service_linux.h",
"hid_collection_info.cc",
"hid_collection_info.h",
"hid_connection.cc",
......
// Copyright 2015 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 "device/hid/fake_input_service_linux.h"
#include <string>
#include <vector>
namespace device {
FakeInputServiceLinux::FakeInputServiceLinux() {
}
FakeInputServiceLinux::~FakeInputServiceLinux() {
}
void FakeInputServiceLinux::AddDeviceForTesting(const InputDeviceInfo& info) {
AddDevice(info);
}
void FakeInputServiceLinux::RemoveDeviceForTesting(const std::string& id) {
RemoveDevice(id);
}
void FakeInputServiceLinux::ClearDeviceList() {
devices_.clear();
}
} // namespace device
\ No newline at end of file
// Copyright 2015 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 DEVICE_HID_FAKE_INPUT_SERVICE_LINUX_H_
#define DEVICE_HID_FAKE_INPUT_SERVICE_LINUX_H_
#include <string>
#include "device/hid/input_service_linux.h"
namespace device {
class FakeInputServiceLinux : public InputServiceLinux {
public:
FakeInputServiceLinux();
~FakeInputServiceLinux() override;
void AddDeviceForTesting(const InputDeviceInfo& info);
void RemoveDeviceForTesting(const std::string& id);
void ClearDeviceList();
DISALLOW_COPY_AND_ASSIGN(FakeInputServiceLinux);
};
} // namespace device
#endif // DEVICE_HID_FAKE_INPUT_SERVICE_LINUX_H_
......@@ -21,6 +21,8 @@
'sources': [
'device_monitor_linux.cc',
'device_monitor_linux.h',
'fake_input_service_linux.cc',
'fake_input_service_linux.h',
'hid_collection_info.cc',
'hid_collection_info.h',
'hid_connection.cc',
......@@ -66,6 +68,8 @@
'device_monitor_linux.h',
'hid_service_linux.cc',
'hid_service_linux.h',
'fake_input_service_linux.cc',
'fake_input_service_linux.h',
'input_service_linux.cc',
'input_service_linux.h',
],
......
......@@ -90,6 +90,9 @@ class InputServiceLinuxImpl : public InputServiceLinux,
};
InputServiceLinuxImpl::InputServiceLinuxImpl() {
base::ThreadRestrictions::AssertIOAllowed();
base::MessageLoop::current()->AddDestructionObserver(this);
DeviceMonitorLinux::GetInstance()->AddObserver(this);
DeviceMonitorLinux::GetInstance()->Enumerate(base::Bind(
&InputServiceLinuxImpl::OnDeviceAdded, base::Unretained(this)));
......@@ -98,6 +101,7 @@ InputServiceLinuxImpl::InputServiceLinuxImpl() {
InputServiceLinuxImpl::~InputServiceLinuxImpl() {
if (DeviceMonitorLinux::HasInstance())
DeviceMonitorLinux::GetInstance()->RemoveObserver(this);
base::MessageLoop::current()->RemoveDestructionObserver(this);
}
void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) {
......@@ -162,13 +166,10 @@ InputServiceLinux::InputDeviceInfo::InputDeviceInfo()
is_touchscreen(false) {}
InputServiceLinux::InputServiceLinux() {
base::ThreadRestrictions::AssertIOAllowed();
base::MessageLoop::current()->AddDestructionObserver(this);
}
InputServiceLinux::~InputServiceLinux() {
DCHECK(CalledOnValidThread());
base::MessageLoop::current()->RemoveDestructionObserver(this);
}
// static
......
......@@ -45,6 +45,9 @@ class InputServiceLinux : public base::MessageLoop::DestructionObserver {
bool is_touchscreen : 1;
};
using DeviceMap = base::hash_map<std::string, InputDeviceInfo>;
class Observer {
public:
virtual ~Observer() {}
......@@ -53,6 +56,7 @@ class InputServiceLinux : public base::MessageLoop::DestructionObserver {
};
InputServiceLinux();
~InputServiceLinux() override;
static InputServiceLinux* GetInstance();
static bool HasInstance();
......@@ -73,21 +77,18 @@ class InputServiceLinux : public base::MessageLoop::DestructionObserver {
void WillDestroyCurrentMessageLoop() override;
protected:
~InputServiceLinux() override;
void AddDevice(const InputDeviceInfo& info);
void RemoveDevice(const std::string& id);
bool CalledOnValidThread() const;
private:
friend struct base::DefaultDeleter<InputServiceLinux>;
typedef base::hash_map<std::string, InputDeviceInfo> DeviceMap;
DeviceMap devices_;
ObserverList<Observer> observers_;
private:
friend struct base::DefaultDeleter<InputServiceLinux>;
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(InputServiceLinux);
......
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