diff options
author | tpayne@chromium.org <tpayne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-25 18:35:08 +0000 |
---|---|---|
committer | tpayne@chromium.org <tpayne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-25 18:35:08 +0000 |
commit | b83575d22ecc2937e065c5b53ff6a74447c3f3af (patch) | |
tree | 64c9b763a261b961d6b9bcdef5f13589a836b183 | |
parent | ad1acb571488dad4b3adc67a8199fe4c6ede96f5 (diff) | |
download | chromium_src-b83575d22ecc2937e065c5b53ff6a74447c3f3af.zip chromium_src-b83575d22ecc2937e065c5b53ff6a74447c3f3af.tar.gz chromium_src-b83575d22ecc2937e065c5b53ff6a74447c3f3af.tar.bz2 |
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
-rw-r--r-- | chrome/browser/chrome_browser_main_win.cc | 6 | ||||
-rw-r--r-- | chrome/browser/chrome_browser_main_win.h | 10 | ||||
-rw-r--r-- | chrome/browser/media_gallery/media_device_notifications_window_win.cc | 136 | ||||
-rw-r--r-- | chrome/browser/media_gallery/media_device_notifications_window_win.h | 49 | ||||
-rw-r--r-- | chrome/browser/media_gallery/media_device_notifications_window_win_unittest.cc | 166 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 | ||||
-rw-r--r-- | content/browser/system_message_window_win.cc | 63 | ||||
-rw-r--r-- | content/browser/system_message_window_win.h | 8 | ||||
-rw-r--r-- | content/browser/system_message_window_win_unittest.cc | 131 |
10 files changed, 372 insertions, 200 deletions
diff --git a/chrome/browser/chrome_browser_main_win.cc b/chrome/browser/chrome_browser_main_win.cc index 84653ed..4ed9f39 100644 --- a/chrome/browser/chrome_browser_main_win.cc +++ b/chrome/browser/chrome_browser_main_win.cc @@ -22,6 +22,7 @@ #include "base/win/wrapped_window_proc.h" #include "chrome/browser/browser_util_win.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/profiles/profile_info_cache.h" #include "chrome/browser/profiles/profile_shortcut_manager_win.h" @@ -168,6 +169,9 @@ ChromeBrowserMainPartsWin::ChromeBrowserMainPartsWin( } } +ChromeBrowserMainPartsWin::~ChromeBrowserMainPartsWin() { +} + void ChromeBrowserMainPartsWin::ToolkitInitialized() { ChromeBrowserMainParts::ToolkitInitialized(); gfx::PlatformFontWin::adjust_font_callback = &AdjustUIFont; @@ -180,6 +184,8 @@ void ChromeBrowserMainPartsWin::PreMainMessageLoopStart() { // Make sure that we know how to handle exceptions from the message loop. InitializeWindowProcExceptions(); } + media_device_notifications_window_.reset( + new chrome::MediaDeviceNotificationsWindowWin()); } // static diff --git a/chrome/browser/chrome_browser_main_win.h b/chrome/browser/chrome_browser_main_win.h index 3ba4a90..2feb9fd 100644 --- a/chrome/browser/chrome_browser_main_win.h +++ b/chrome/browser/chrome_browser_main_win.h @@ -8,10 +8,16 @@ #define CHROME_BROWSER_CHROME_BROWSER_MAIN_WIN_H_ #pragma once +#include "base/memory/scoped_ptr.h" #include "chrome/browser/chrome_browser_main.h" class CommandLine; +namespace chrome { +class MediaDeviceNotificationsWindowWin; +} // namespace chrome + + // Handle uninstallation when given the appropriate the command-line switch. // If |chrome_still_running| is true a modal dialog will be shown asking the // user to close the other chrome instance. @@ -22,6 +28,8 @@ class ChromeBrowserMainPartsWin : public ChromeBrowserMainParts { explicit ChromeBrowserMainPartsWin( const content::MainFunctionParams& parameters); + virtual ChromeBrowserMainPartsWin::~ChromeBrowserMainPartsWin(); + // BrowserParts overrides. virtual void ToolkitInitialized() OVERRIDE; virtual void PreMainMessageLoopStart() OVERRIDE; @@ -54,6 +62,8 @@ class ChromeBrowserMainPartsWin : public ChromeBrowserMainParts { static bool CheckMachineLevelInstall(); private: + scoped_ptr<chrome::MediaDeviceNotificationsWindowWin> + media_device_notifications_window_; DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainPartsWin); }; diff --git a/chrome/browser/media_gallery/media_device_notifications_window_win.cc b/chrome/browser/media_gallery/media_device_notifications_window_win.cc new file mode 100644 index 0000000..498ade9 --- /dev/null +++ b/chrome/browser/media_gallery/media_device_notifications_window_win.cc @@ -0,0 +1,136 @@ +// 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 diff --git a/chrome/browser/media_gallery/media_device_notifications_window_win.h b/chrome/browser/media_gallery/media_device_notifications_window_win.h new file mode 100644 index 0000000..ebf7153 --- /dev/null +++ b/chrome/browser/media_gallery/media_device_notifications_window_win.h @@ -0,0 +1,49 @@ +// 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_ diff --git a/chrome/browser/media_gallery/media_device_notifications_window_win_unittest.cc b/chrome/browser/media_gallery/media_device_notifications_window_win_unittest.cc new file mode 100644 index 0000000..09392e3 --- /dev/null +++ b/chrome/browser/media_gallery/media_device_notifications_window_win_unittest.cc @@ -0,0 +1,166 @@ +// 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); +} diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 5be6aeee..e643b29 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1311,6 +1311,8 @@ 'browser/media/media_stream_devices_menu_model.h', 'browser/media_gallery/media_device_notifications_linux.cc', '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.h', 'browser/media_gallery/media_gallery_database_types.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 88e5b39..e971395 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1437,6 +1437,7 @@ 'browser/managed_mode_unittest.cc', 'browser/media/media_internals_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/metrics/metrics_log_unittest.cc', 'browser/metrics/metrics_log_serializer_unittest.cc', diff --git a/content/browser/system_message_window_win.cc b/content/browser/system_message_window_win.cc index 1aca1c1..347e717d 100644 --- a/content/browser/system_message_window_win.cc +++ b/content/browser/system_message_window_win.cc @@ -6,50 +6,15 @@ #include <windows.h> #include <dbt.h> -#include <string> -#include "base/file_path.h" -#include "base/sys_string_conversions.h" +#include "base/logging.h" #include "base/system_monitor/system_monitor.h" #include "base/win/wrapped_window_proc.h" static const wchar_t* const WindowClassName = L"Chrome_SystemMessageWindow"; -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 - - -SystemMessageWindowWin::SystemMessageWindowWin() - : volume_name_func_(&GetVolumeName) { - Init(); -} - -SystemMessageWindowWin::SystemMessageWindowWin(VolumeNameFunc volume_name_func) - : volume_name_func_(volume_name_func) { - Init(); -} - -void SystemMessageWindowWin::Init() { +SystemMessageWindowWin::SystemMessageWindowWin() { HINSTANCE hinst = GetModuleHandle(NULL); WNDCLASSEX wc = {0}; @@ -79,30 +44,6 @@ LRESULT SystemMessageWindowWin::OnDeviceChange(UINT event_type, DWORD data) { case DBT_DEVNODES_CHANGED: monitor->ProcessDevicesChanged(); 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; } diff --git a/content/browser/system_message_window_win.h b/content/browser/system_message_window_win.h index a44b1fa..36c800c 100644 --- a/content/browser/system_message_window_win.h +++ b/content/browser/system_message_window_win.h @@ -11,16 +11,9 @@ #include "base/basictypes.h" #include "content/common/content_export.h" -typedef LRESULT (*VolumeNameFunc)(LPCWSTR drive, - LPWSTR volume_name, - unsigned int volume_name_len); - class CONTENT_EXPORT SystemMessageWindowWin { public: SystemMessageWindowWin(); - // Only for use in unit tests. - explicit SystemMessageWindowWin::SystemMessageWindowWin( - VolumeNameFunc volumeNameFunc); virtual ~SystemMessageWindowWin(); @@ -44,7 +37,6 @@ class CONTENT_EXPORT SystemMessageWindowWin { } HWND window_; - VolumeNameFunc volume_name_func_; DISALLOW_COPY_AND_ASSIGN(SystemMessageWindowWin); }; diff --git a/content/browser/system_message_window_win_unittest.cc b/content/browser/system_message_window_win_unittest.cc index 1db4c19..d8eb7b7 100644 --- a/content/browser/system_message_window_win_unittest.cc +++ b/content/browser/system_message_window_win_unittest.cc @@ -9,29 +9,13 @@ #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 SystemMessageWindowWinTest : public testing::Test { public: - SystemMessageWindowWinTest() : window_(&GetVolumeName) { } virtual ~SystemMessageWindowWinTest() { } protected: @@ -39,61 +23,12 @@ class SystemMessageWindowWinTest : public testing::Test { 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_; 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) { EXPECT_CALL(observer_, OnDevicesChanged()).Times(1); window_.OnDeviceChange(DBT_DEVNODES_CHANGED, NULL); @@ -104,69 +39,3 @@ TEST_F(SystemMessageWindowWinTest, RandomMessage) { window_.OnDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); 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); -} |