Commit b83575d2 authored by tpayne@chromium.org's avatar tpayne@chromium.org

Moves media device notification code to chrome/browser

BUG=NONE
TEST=NONE


Review URL: http://codereview.chromium.org/10211008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133946 0039d316-1c4b-4281-b951-d872f2087c98
parent ad1acb57
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "base/win/wrapped_window_proc.h" #include "base/win/wrapped_window_proc.h"
#include "chrome/browser/browser_util_win.h" #include "chrome/browser/browser_util_win.h"
#include "chrome/browser/first_run/first_run.h" #include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/media_gallery/media_device_notifications_window_win.h"
#include "chrome/browser/metrics/metrics_service.h" #include "chrome/browser/metrics/metrics_service.h"
#include "chrome/browser/profiles/profile_info_cache.h" #include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profile_shortcut_manager_win.h" #include "chrome/browser/profiles/profile_shortcut_manager_win.h"
...@@ -168,6 +169,9 @@ ChromeBrowserMainPartsWin::ChromeBrowserMainPartsWin( ...@@ -168,6 +169,9 @@ ChromeBrowserMainPartsWin::ChromeBrowserMainPartsWin(
} }
} }
ChromeBrowserMainPartsWin::~ChromeBrowserMainPartsWin() {
}
void ChromeBrowserMainPartsWin::ToolkitInitialized() { void ChromeBrowserMainPartsWin::ToolkitInitialized() {
ChromeBrowserMainParts::ToolkitInitialized(); ChromeBrowserMainParts::ToolkitInitialized();
gfx::PlatformFontWin::adjust_font_callback = &AdjustUIFont; gfx::PlatformFontWin::adjust_font_callback = &AdjustUIFont;
...@@ -180,6 +184,8 @@ void ChromeBrowserMainPartsWin::PreMainMessageLoopStart() { ...@@ -180,6 +184,8 @@ void ChromeBrowserMainPartsWin::PreMainMessageLoopStart() {
// Make sure that we know how to handle exceptions from the message loop. // Make sure that we know how to handle exceptions from the message loop.
InitializeWindowProcExceptions(); InitializeWindowProcExceptions();
} }
media_device_notifications_window_.reset(
new chrome::MediaDeviceNotificationsWindowWin());
} }
// static // static
......
...@@ -8,10 +8,16 @@ ...@@ -8,10 +8,16 @@
#define CHROME_BROWSER_CHROME_BROWSER_MAIN_WIN_H_ #define CHROME_BROWSER_CHROME_BROWSER_MAIN_WIN_H_
#pragma once #pragma once
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chrome_browser_main.h" #include "chrome/browser/chrome_browser_main.h"
class CommandLine; class CommandLine;
namespace chrome {
class MediaDeviceNotificationsWindowWin;
} // namespace chrome
// Handle uninstallation when given the appropriate the command-line switch. // Handle uninstallation when given the appropriate the command-line switch.
// If |chrome_still_running| is true a modal dialog will be shown asking the // If |chrome_still_running| is true a modal dialog will be shown asking the
// user to close the other chrome instance. // user to close the other chrome instance.
...@@ -22,6 +28,8 @@ class ChromeBrowserMainPartsWin : public ChromeBrowserMainParts { ...@@ -22,6 +28,8 @@ class ChromeBrowserMainPartsWin : public ChromeBrowserMainParts {
explicit ChromeBrowserMainPartsWin( explicit ChromeBrowserMainPartsWin(
const content::MainFunctionParams& parameters); const content::MainFunctionParams& parameters);
virtual ChromeBrowserMainPartsWin::~ChromeBrowserMainPartsWin();
// BrowserParts overrides. // BrowserParts overrides.
virtual void ToolkitInitialized() OVERRIDE; virtual void ToolkitInitialized() OVERRIDE;
virtual void PreMainMessageLoopStart() OVERRIDE; virtual void PreMainMessageLoopStart() OVERRIDE;
...@@ -54,6 +62,8 @@ class ChromeBrowserMainPartsWin : public ChromeBrowserMainParts { ...@@ -54,6 +62,8 @@ class ChromeBrowserMainPartsWin : public ChromeBrowserMainParts {
static bool CheckMachineLevelInstall(); static bool CheckMachineLevelInstall();
private: private:
scoped_ptr<chrome::MediaDeviceNotificationsWindowWin>
media_device_notifications_window_;
DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainPartsWin); DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainPartsWin);
}; };
......
// Copyright (c) 2012 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 "chrome/browser/media_gallery/media_device_notifications_window_win.h"
#include <windows.h>
#include <dbt.h>
#include <string>
#include "base/file_path.h"
#include "base/sys_string_conversions.h"
#include "base/system_monitor/system_monitor.h"
#include "base/win/wrapped_window_proc.h"
static const wchar_t* const WindowClassName =
L"Chrome_MediaDeviceNotificationWindow";
namespace {
LRESULT GetVolumeName(LPCWSTR drive,
LPWSTR volume_name,
unsigned int volume_name_len) {
return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL,
NULL, NULL, 0);
}
// Returns 0 if the devicetype is not volume.
DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) {
PDEV_BROADCAST_HDR dev_broadcast_hdr =
reinterpret_cast<PDEV_BROADCAST_HDR>(data);
if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) {
PDEV_BROADCAST_VOLUME dev_broadcast_volume =
reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr);
return dev_broadcast_volume->dbcv_unitmask;
}
return 0;
}
} // namespace
namespace chrome {
MediaDeviceNotificationsWindowWin::MediaDeviceNotificationsWindowWin()
: volume_name_func_(&GetVolumeName) {
Init();
}
MediaDeviceNotificationsWindowWin::MediaDeviceNotificationsWindowWin(
VolumeNameFunc volume_name_func) : volume_name_func_(volume_name_func) {
Init();
}
void MediaDeviceNotificationsWindowWin::Init() {
HINSTANCE hinst = GetModuleHandle(NULL);
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = base::win::WrappedWindowProc<
&MediaDeviceNotificationsWindowWin::WndProcThunk>;
wc.hInstance = hinst;
wc.lpszClassName = WindowClassName;
ATOM clazz = RegisterClassEx(&wc);
DCHECK(clazz);
window_ = CreateWindow(WindowClassName, 0, 0, 0, 0, 0, 0, 0, 0, hinst, 0);
SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
}
MediaDeviceNotificationsWindowWin::~MediaDeviceNotificationsWindowWin() {
if (window_) {
DestroyWindow(window_);
UnregisterClass(WindowClassName, GetModuleHandle(NULL));
}
}
LRESULT MediaDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type,
DWORD data) {
base::SystemMonitor* monitor = base::SystemMonitor::Get();
switch (event_type) {
case DBT_DEVICEARRIVAL: {
DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
for (int i = 0; unitmask; ++i, unitmask >>= 1) {
if (unitmask & 0x01) {
FilePath::StringType drive(L"_:\\");
drive[0] = L'A' + i;
WCHAR volume_name[MAX_PATH + 1];
if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) {
monitor->ProcessMediaDeviceAttached(
i, base::SysWideToUTF8(volume_name), FilePath(drive));
}
}
}
break;
}
case DBT_DEVICEREMOVECOMPLETE: {
DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
for (int i = 0; unitmask; ++i, unitmask >>= 1) {
if (unitmask & 0x01) {
monitor->ProcessMediaDeviceDetached(i);
}
}
break;
}
}
return TRUE;
}
LRESULT CALLBACK MediaDeviceNotificationsWindowWin::WndProc(
HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
switch (message) {
case WM_DEVICECHANGE:
return OnDeviceChange(static_cast<UINT>(wparam),
static_cast<DWORD>(lparam));
default:
break;
}
return ::DefWindowProc(hwnd, message, wparam, lparam);
}
// static
LRESULT CALLBACK MediaDeviceNotificationsWindowWin::WndProcThunk(
HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam) {
MediaDeviceNotificationsWindowWin* msg_wnd =
reinterpret_cast<MediaDeviceNotificationsWindowWin*>(
GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (msg_wnd)
return msg_wnd->WndProc(hwnd, message, wparam, lparam);
return ::DefWindowProc(hwnd, message, wparam, lparam);
}
} // namespace chrome
// Copyright (c) 2012 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 CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_WINDOW_WIN_H_
#define CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_WINDOW_WIN_H_
#pragma once
#include <windows.h>
#include "base/basictypes.h"
typedef LRESULT (*VolumeNameFunc)(LPCWSTR drive,
LPWSTR volume_name,
unsigned int volume_name_len);
namespace chrome {
class MediaDeviceNotificationsWindowWin {
public:
MediaDeviceNotificationsWindowWin();
// Only for use in unit tests.
explicit MediaDeviceNotificationsWindowWin(VolumeNameFunc volumeNameFunc);
virtual ~MediaDeviceNotificationsWindowWin();
virtual LRESULT OnDeviceChange(UINT event_type, DWORD data);
private:
void Init();
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam);
static LRESULT CALLBACK WndProcThunk(HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam);
HWND window_;
VolumeNameFunc volume_name_func_;
DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsWindowWin);
};
} // namespace chrome
#endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_WINDOW_WIN_H_
// Copyright (c) 2012 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 "chrome/browser/media_gallery/media_device_notifications_window_win.h"
#include <dbt.h>
#include <string>
#include <vector>
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/sys_string_conversions.h"
#include "base/system_monitor/system_monitor.h"
#include "base/test/mock_devices_changed_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
LRESULT GetVolumeName(LPCWSTR drive,
LPWSTR volume_name,
unsigned int volume_name_length) {
DCHECK(volume_name_length > wcslen(drive) + 2);
*volume_name = 'V';
wcscpy(volume_name + 1, drive);
return TRUE;
}
} // namespace
class MediaDeviceNotificationsWindowWinTest : public testing::Test {
public:
MediaDeviceNotificationsWindowWinTest() : window_(&GetVolumeName) { }
virtual ~MediaDeviceNotificationsWindowWinTest() { }
protected:
virtual void SetUp() OVERRIDE {
system_monitor_.AddDevicesChangedObserver(&observer_);
}
void DoDevicesAttachedTest(const std::vector<int>& deviceIndices);
void DoDevicesDetachedTest(const std::vector<int>& deviceIndices);
MessageLoop message_loop_;
base::SystemMonitor system_monitor_;
base::MockDevicesChangedObserver observer_;
chrome::MediaDeviceNotificationsWindowWin window_;
};
void MediaDeviceNotificationsWindowWinTest::DoDevicesAttachedTest(
const std::vector<int>& device_indices) {
DEV_BROADCAST_VOLUME volume_broadcast;
volume_broadcast.dbcv_size = sizeof(volume_broadcast);
volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME;
volume_broadcast.dbcv_unitmask = 0x0;
volume_broadcast.dbcv_flags = 0x0;
{
testing::InSequence sequnce;
for (std::vector<int>::const_iterator it = device_indices.begin();
it != device_indices.end();
++it) {
volume_broadcast.dbcv_unitmask |= 0x1 << *it;
std::wstring drive(L"_:\\");
drive[0] = 'A' + *it;
std::string name("V");
name.append(base::SysWideToUTF8(drive));
EXPECT_CALL(observer_, OnMediaDeviceAttached(*it, name, FilePath(drive)));
}
}
window_.OnDeviceChange(DBT_DEVICEARRIVAL,
reinterpret_cast<DWORD>(&volume_broadcast));
message_loop_.RunAllPending();
};
void MediaDeviceNotificationsWindowWinTest::DoDevicesDetachedTest(
const std::vector<int>& device_indices) {
DEV_BROADCAST_VOLUME volume_broadcast;
volume_broadcast.dbcv_size = sizeof(volume_broadcast);
volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME;
volume_broadcast.dbcv_unitmask = 0x0;
volume_broadcast.dbcv_flags = 0x0;
{
testing::InSequence sequence;
for (std::vector<int>::const_iterator it = device_indices.begin();
it != device_indices.end();
++it) {
volume_broadcast.dbcv_unitmask |= 0x1 << *it;
EXPECT_CALL(observer_, OnMediaDeviceDetached(*it));
}
}
window_.OnDeviceChange(DBT_DEVICEREMOVECOMPLETE,
reinterpret_cast<DWORD>(&volume_broadcast));
message_loop_.RunAllPending();
};
TEST_F(MediaDeviceNotificationsWindowWinTest, RandomMessage) {
window_.OnDeviceChange(DBT_DEVICEQUERYREMOVE, NULL);
message_loop_.RunAllPending();
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttached) {
std::vector<int> device_indices;
device_indices.push_back(1);
device_indices.push_back(5);
device_indices.push_back(7);
DoDevicesAttachedTest(device_indices);
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttachedHighBoundary) {
std::vector<int> device_indices;
device_indices.push_back(25);
DoDevicesAttachedTest(device_indices);
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttachedLowBoundary) {
std::vector<int> device_indices;
device_indices.push_back(0);
DoDevicesAttachedTest(device_indices);
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttachedAdjacentBits) {
std::vector<int> device_indices;
device_indices.push_back(0);
device_indices.push_back(1);
device_indices.push_back(2);
device_indices.push_back(3);
DoDevicesAttachedTest(device_indices);
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetached) {
std::vector<int> device_indices;
device_indices.push_back(1);
device_indices.push_back(5);
device_indices.push_back(7);
DoDevicesDetachedTest(device_indices);
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetachedHighBoundary) {
std::vector<int> device_indices;
device_indices.push_back(25);
DoDevicesDetachedTest(device_indices);
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetachedLowBoundary) {
std::vector<int> device_indices;
device_indices.push_back(0);
DoDevicesDetachedTest(device_indices);
}
TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetachedAdjacentBits) {
std::vector<int> device_indices;
device_indices.push_back(0);
device_indices.push_back(1);
device_indices.push_back(2);
device_indices.push_back(3);
DoDevicesDetachedTest(device_indices);
}
...@@ -1311,6 +1311,8 @@ ...@@ -1311,6 +1311,8 @@
'browser/media/media_stream_devices_menu_model.h', 'browser/media/media_stream_devices_menu_model.h',
'browser/media_gallery/media_device_notifications_linux.cc', 'browser/media_gallery/media_device_notifications_linux.cc',
'browser/media_gallery/media_device_notifications_linux.h', 'browser/media_gallery/media_device_notifications_linux.h',
'browser/media_gallery/media_device_notifications_window_win.cc',
'browser/media_gallery/media_device_notifications_window_win.h',
'browser/media_gallery/media_gallery_database.cc', 'browser/media_gallery/media_gallery_database.cc',
'browser/media_gallery/media_gallery_database.h', 'browser/media_gallery/media_gallery_database.h',
'browser/media_gallery/media_gallery_database_types.cc', 'browser/media_gallery/media_gallery_database_types.cc',
......
...@@ -1437,6 +1437,7 @@ ...@@ -1437,6 +1437,7 @@
'browser/managed_mode_unittest.cc', 'browser/managed_mode_unittest.cc',
'browser/media/media_internals_unittest.cc', 'browser/media/media_internals_unittest.cc',
'browser/media_gallery/media_device_notifications_linux_unittest.cc', 'browser/media_gallery/media_device_notifications_linux_unittest.cc',
'browser/media_gallery/media_device_notifications_window_win_unittest.cc',
'browser/media_gallery/media_gallery_database_unittest.cc', 'browser/media_gallery/media_gallery_database_unittest.cc',
'browser/metrics/metrics_log_unittest.cc', 'browser/metrics/metrics_log_unittest.cc',
'browser/metrics/metrics_log_serializer_unittest.cc', 'browser/metrics/metrics_log_serializer_unittest.cc',
......
...@@ -6,50 +6,15 @@ ...@@ -6,50 +6,15 @@
#include <windows.h> #include <windows.h>
#include <dbt.h> #include <dbt.h>
#include <string>
#include "base/file_path.h" #include "base/logging.h"
#include "base/sys_string_conversions.h"
#include "base/system_monitor/system_monitor.h" #include "base/system_monitor/system_monitor.h"
#include "base/win/wrapped_window_proc.h" #include "base/win/wrapped_window_proc.h"
static const wchar_t* const WindowClassName = L"Chrome_SystemMessageWindow"; static const wchar_t* const WindowClassName = L"Chrome_SystemMessageWindow";
namespace {
LRESULT GetVolumeName(LPCWSTR drive, SystemMessageWindowWin::SystemMessageWindowWin() {
LPWSTR volume_name,
unsigned int volume_name_len) {
return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL,
NULL, NULL, 0);
}
// Returns 0 if the devicetype is not volume.
DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) {
PDEV_BROADCAST_HDR dev_broadcast_hdr =
reinterpret_cast<PDEV_BROADCAST_HDR>(data);
if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) {
PDEV_BROADCAST_VOLUME dev_broadcast_volume =
reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr);
return dev_broadcast_volume->dbcv_unitmask;
}
return 0;
}
} // namespace
SystemMessageWindowWin::SystemMessageWindowWin()
: volume_name_func_(&GetVolumeName) {
Init();
}
SystemMessageWindowWin::SystemMessageWindowWin(VolumeNameFunc volume_name_func)
: volume_name_func_(volume_name_func) {
Init();
}
void SystemMessageWindowWin::Init() {
HINSTANCE hinst = GetModuleHandle(NULL); HINSTANCE hinst = GetModuleHandle(NULL);
WNDCLASSEX wc = {0}; WNDCLASSEX wc = {0};
...@@ -79,30 +44,6 @@ LRESULT SystemMessageWindowWin::OnDeviceChange(UINT event_type, DWORD data) { ...@@ -79,30 +44,6 @@ LRESULT SystemMessageWindowWin::OnDeviceChange(UINT event_type, DWORD data) {
case DBT_DEVNODES_CHANGED: case DBT_DEVNODES_CHANGED:
monitor->ProcessDevicesChanged(); monitor->ProcessDevicesChanged();
break; break;
case DBT_DEVICEARRIVAL: {
DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
for (int i = 0; unitmask; ++i, unitmask >>= 1) {
if (unitmask & 0x01) {
FilePath::StringType drive(L"_:\\");
drive[0] = L'A' + i;
WCHAR volume_name[MAX_PATH + 1];
if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) {
monitor->ProcessMediaDeviceAttached(
i, base::SysWideToUTF8(volume_name), FilePath(drive));
}
}
}
break;
}
case DBT_DEVICEREMOVECOMPLETE: {
DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
for (int i = 0; unitmask; ++i, unitmask >>= 1) {
if (unitmask & 0x01) {
monitor->ProcessMediaDeviceDetached(i);
}
}
break;
}
} }
return TRUE; return TRUE;
} }
......
...@@ -11,16 +11,9 @@ ...@@ -11,16 +11,9 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
typedef LRESULT (*VolumeNameFunc)(LPCWSTR drive,
LPWSTR volume_name,
unsigned int volume_name_len);
class CONTENT_EXPORT SystemMessageWindowWin { class CONTENT_EXPORT SystemMessageWindowWin {
public: public:
SystemMessageWindowWin(); SystemMessageWindowWin();
// Only for use in unit tests.
explicit SystemMessageWindowWin::SystemMessageWindowWin(
VolumeNameFunc volumeNameFunc);
virtual ~SystemMessageWindowWin(); virtual ~SystemMessageWindowWin();
...@@ -44,7 +37,6 @@ class CONTENT_EXPORT SystemMessageWindowWin { ...@@ -44,7 +37,6 @@ class CONTENT_EXPORT SystemMessageWindowWin {
} }
HWND window_; HWND window_;
VolumeNameFunc volume_name_func_;
DISALLOW_COPY_AND_ASSIGN(SystemMessageWindowWin); DISALLOW_COPY_AND_ASSIGN(SystemMessageWindowWin);
}; };
......
...@@ -9,29 +9,13 @@ ...@@ -9,29 +9,13 @@
#include <vector> #include <vector>
#include "base/file_path.h" #include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/sys_string_conversions.h"
#include "base/system_monitor/system_monitor.h" #include "base/system_monitor/system_monitor.h"
#include "base/test/mock_devices_changed_observer.h" #include "base/test/mock_devices_changed_observer.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace {
LRESULT GetVolumeName(LPCWSTR drive,
LPWSTR volume_name,
unsigned int volume_name_length) {
DCHECK(volume_name_length > wcslen(drive) + 2);
*volume_name = 'V';
wcscpy(volume_name + 1, drive);
return TRUE;
}
} // namespace
class SystemMessageWindowWinTest : public testing::Test { class SystemMessageWindowWinTest : public testing::Test {
public: public:
SystemMessageWindowWinTest() : window_(&GetVolumeName) { }
virtual ~SystemMessageWindowWinTest() { } virtual ~SystemMessageWindowWinTest() { }
protected: protected:
...@@ -39,61 +23,12 @@ class SystemMessageWindowWinTest : public testing::Test { ...@@ -39,61 +23,12 @@ class SystemMessageWindowWinTest : public testing::Test {
system_monitor_.AddDevicesChangedObserver(&observer_); system_monitor_.AddDevicesChangedObserver(&observer_);
} }
void DoDevicesAttachedTest(const std::vector<int>& deviceIndices);
void DoDevicesDetachedTest(const std::vector<int>& deviceIndices);
MessageLoop message_loop_; MessageLoop message_loop_;
base::SystemMonitor system_monitor_; base::SystemMonitor system_monitor_;
base::MockDevicesChangedObserver observer_; base::MockDevicesChangedObserver observer_;
SystemMessageWindowWin window_; SystemMessageWindowWin window_;
}; };
void SystemMessageWindowWinTest::DoDevicesAttachedTest(
const std::vector<int>& device_indices) {
DEV_BROADCAST_VOLUME volume_broadcast;
volume_broadcast.dbcv_size = sizeof(volume_broadcast);
volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME;
volume_broadcast.dbcv_unitmask = 0x0;
volume_broadcast.dbcv_flags = 0x0;
{
testing::InSequence sequnce;
for (std::vector<int>::const_iterator it = device_indices.begin();
it != device_indices.end();
++it) {
volume_broadcast.dbcv_unitmask |= 0x1 << *it;
std::wstring drive(L"_:\\");
drive[0] = 'A' + *it;
std::string name("V");
name.append(base::SysWideToUTF8(drive));
EXPECT_CALL(observer_, OnMediaDeviceAttached(*it, name, FilePath(drive)));
}
}
window_.OnDeviceChange(DBT_DEVICEARRIVAL,
reinterpret_cast<DWORD>(&volume_broadcast));
message_loop_.RunAllPending();
};
void SystemMessageWindowWinTest::DoDevicesDetachedTest(
const std::vector<int>& device_indices) {
DEV_BROADCAST_VOLUME volume_broadcast;
volume_broadcast.dbcv_size = sizeof(volume_broadcast);
volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME;
volume_broadcast.dbcv_unitmask = 0x0;
volume_broadcast.dbcv_flags = 0x0;
{
testing::InSequence sequence;
for (std::vector<int>::const_iterator it = device_indices.begin();
it != device_indices.end();
++it) {
volume_broadcast.dbcv_unitmask |= 0x1 << *it;
EXPECT_CALL(observer_, OnMediaDeviceDetached(*it));
}
}
window_.OnDeviceChange(DBT_DEVICEREMOVECOMPLETE,
reinterpret_cast<DWORD>(&volume_broadcast));
message_loop_.RunAllPending();
};
TEST_F(SystemMessageWindowWinTest, DevicesChanged) { TEST_F(SystemMessageWindowWinTest, DevicesChanged) {
EXPECT_CALL(observer_, OnDevicesChanged()).Times(1); EXPECT_CALL(observer_, OnDevicesChanged()).Times(1);
window_.OnDeviceChange(DBT_DEVNODES_CHANGED, NULL); window_.OnDeviceChange(DBT_DEVNODES_CHANGED, NULL);
...@@ -104,69 +39,3 @@ TEST_F(SystemMessageWindowWinTest, RandomMessage) { ...@@ -104,69 +39,3 @@ TEST_F(SystemMessageWindowWinTest, RandomMessage) {
window_.OnDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); window_.OnDeviceChange(DBT_DEVICEQUERYREMOVE, NULL);
message_loop_.RunAllPending(); message_loop_.RunAllPending();
} }
TEST_F(SystemMessageWindowWinTest, DevicesAttached) {
std::vector<int> device_indices;
device_indices.push_back(1);
device_indices.push_back(5);
device_indices.push_back(7);
DoDevicesAttachedTest(device_indices);
}
TEST_F(SystemMessageWindowWinTest, DevicesAttachedHighBoundary) {
std::vector<int> device_indices;
device_indices.push_back(25);
DoDevicesAttachedTest(device_indices);
}
TEST_F(SystemMessageWindowWinTest, DevicesAttachedLowBoundary) {
std::vector<int> device_indices;
device_indices.push_back(0);
DoDevicesAttachedTest(device_indices);
}
TEST_F(SystemMessageWindowWinTest, DevicesAttachedAdjacentBits) {
std::vector<int> device_indices;
device_indices.push_back(0);
device_indices.push_back(1);
device_indices.push_back(2);
device_indices.push_back(3);
DoDevicesAttachedTest(device_indices);
}
TEST_F(SystemMessageWindowWinTest, DevicesDetached) {
std::vector<int> device_indices;
device_indices.push_back(1);
device_indices.push_back(5);
device_indices.push_back(7);
DoDevicesDetachedTest(device_indices);
}
TEST_F(SystemMessageWindowWinTest, DevicesDetachedHighBoundary) {
std::vector<int> device_indices;
device_indices.push_back(25);
DoDevicesDetachedTest(device_indices);
}
TEST_F(SystemMessageWindowWinTest, DevicesDetachedLowBoundary) {
std::vector<int> device_indices;
device_indices.push_back(0);
DoDevicesDetachedTest(device_indices);
}
TEST_F(SystemMessageWindowWinTest, DevicesDetachedAdjacentBits) {
std::vector<int> device_indices;
device_indices.push_back(0);
device_indices.push_back(1);
device_indices.push_back(2);
device_indices.push_back(3);
DoDevicesDetachedTest(device_indices);
}
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