diff options
author | merkulova <merkulova@chromium.org> | 2015-03-02 00:45:56 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-02 08:46:30 +0000 |
commit | 4209a162a2a4a9b49c9096909fbfadb7a2608b72 (patch) | |
tree | d1e160362b99dc8a80865d645573caa7354276ad | |
parent | c3a3f671f8e0c5777c6bc825f21d61c6bcafbec3 (diff) | |
download | chromium_src-4209a162a2a4a9b49c9096909fbfadb7a2608b72.zip chromium_src-4209a162a2a4a9b49c9096909fbfadb7a2608b72.tar.gz chromium_src-4209a162a2a4a9b49c9096909fbfadb7a2608b72.tar.bz2 |
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}
-rw-r--r-- | chrome/browser/chromeos/device/input_service_proxy.cc | 30 | ||||
-rw-r--r-- | chrome/browser/chromeos/device/input_service_proxy.h | 9 | ||||
-rw-r--r-- | chrome/browser/chromeos/login/hid_detection_browsertest.cc | 124 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 | ||||
-rw-r--r-- | device/hid/BUILD.gn | 2 | ||||
-rw-r--r-- | device/hid/fake_input_service_linux.cc | 30 | ||||
-rw-r--r-- | device/hid/fake_input_service_linux.h | 29 | ||||
-rw-r--r-- | device/hid/hid.gyp | 4 | ||||
-rw-r--r-- | device/hid/input_service_linux.cc | 7 | ||||
-rw-r--r-- | device/hid/input_service_linux.h | 13 |
10 files changed, 229 insertions, 20 deletions
diff --git a/chrome/browser/chromeos/device/input_service_proxy.cc b/chrome/browser/chromeos/device/input_service_proxy.cc index 861ef6c..3d27ab1 100644 --- a/chrome/browser/chromeos/device/input_service_proxy.cc +++ b/chrome/browser/chromeos/device/input_service_proxy.cc @@ -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()); diff --git a/chrome/browser/chromeos/device/input_service_proxy.h b/chrome/browser/chromeos/device/input_service_proxy.h index e41196f..44a0a63 100644 --- a/chrome/browser/chromeos/device/input_service_proxy.h +++ b/chrome/browser/chromeos/device/input_service_proxy.h @@ -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); diff --git a/chrome/browser/chromeos/login/hid_detection_browsertest.cc b/chrome/browser/chromeos/login/hid_detection_browsertest.cc new file mode 100644 index 0000000..ebb23d2 --- /dev/null +++ b/chrome/browser/chromeos/login/hid_detection_browsertest.cc @@ -0,0 +1,124 @@ +// 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 diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 015a219..593f2e6 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -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', diff --git a/device/hid/BUILD.gn b/device/hid/BUILD.gn index b037573..4a6a1b2 100644 --- a/device/hid/BUILD.gn +++ b/device/hid/BUILD.gn @@ -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", diff --git a/device/hid/fake_input_service_linux.cc b/device/hid/fake_input_service_linux.cc new file mode 100644 index 0000000..ae3f085 --- /dev/null +++ b/device/hid/fake_input_service_linux.cc @@ -0,0 +1,30 @@ +// 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 diff --git a/device/hid/fake_input_service_linux.h b/device/hid/fake_input_service_linux.h new file mode 100644 index 0000000..1b46d98 --- /dev/null +++ b/device/hid/fake_input_service_linux.h @@ -0,0 +1,29 @@ +// 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_ diff --git a/device/hid/hid.gyp b/device/hid/hid.gyp index 42215d7..00b9a90 100644 --- a/device/hid/hid.gyp +++ b/device/hid/hid.gyp @@ -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', ], diff --git a/device/hid/input_service_linux.cc b/device/hid/input_service_linux.cc index f6fd0dc..81cac1a 100644 --- a/device/hid/input_service_linux.cc +++ b/device/hid/input_service_linux.cc @@ -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 diff --git a/device/hid/input_service_linux.h b/device/hid/input_service_linux.h index 55bf70c..7eda3bb 100644 --- a/device/hid/input_service_linux.h +++ b/device/hid/input_service_linux.h @@ -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); |