| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/system_message_window_win.h" | 5 #include "chrome/browser/media_gallery/media_device_notification_window_win.h" |
| 6 | 6 |
| 7 #include <dbt.h> | 7 #include <dbt.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
| 14 #include "base/system_monitor/system_monitor.h" | 14 #include "base/system_monitor/system_monitor.h" |
| 15 #include "base/test/mock_devices_changed_observer.h" | 15 #include "base/test/mock_devices_changed_observer.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 LRESULT GetVolumeName(LPCWSTR drive, | 21 LRESULT GetVolumeName(LPCWSTR drive, |
| 22 LPWSTR volume_name, | 22 LPWSTR volume_name, |
| 23 unsigned int volume_name_length) { | 23 unsigned int volume_name_length) { |
| 24 DCHECK(volume_name_length > wcslen(drive) + 2); | 24 DCHECK(volume_name_length > wcslen(drive) + 2); |
| 25 *volume_name = 'V'; | 25 *volume_name = 'V'; |
| 26 wcscpy(volume_name + 1, drive); | 26 wcscpy(volume_name + 1, drive); |
| 27 return TRUE; | 27 return TRUE; |
| 28 } | 28 } |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 class SystemMessageWindowWinTest : public testing::Test { | 32 class MediaDeviceNotificationWindowWinTest : public testing::Test { |
| 33 public: | 33 public: |
| 34 SystemMessageWindowWinTest() : window_(&GetVolumeName) { } | 34 MediaDeviceNotificationWindowWinTest() : window_(&GetVolumeName) { } |
| 35 virtual ~SystemMessageWindowWinTest() { } | 35 virtual ~MediaDeviceNotificationWindowWinTest() { } |
| 36 | 36 |
| 37 protected: | 37 protected: |
| 38 virtual void SetUp() OVERRIDE { | 38 virtual void SetUp() OVERRIDE { |
| 39 system_monitor_.AddDevicesChangedObserver(&observer_); | 39 system_monitor_.AddDevicesChangedObserver(&observer_); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void DoDevicesAttachedTest(const std::vector<int>& deviceIndices); | 42 void DoDevicesAttachedTest(const std::vector<int>& deviceIndices); |
| 43 void DoDevicesDetachedTest(const std::vector<int>& deviceIndices); | 43 void DoDevicesDetachedTest(const std::vector<int>& deviceIndices); |
| 44 | 44 |
| 45 MessageLoop message_loop_; | 45 MessageLoop message_loop_; |
| 46 base::SystemMonitor system_monitor_; | 46 base::SystemMonitor system_monitor_; |
| 47 base::MockDevicesChangedObserver observer_; | 47 base::MockDevicesChangedObserver observer_; |
| 48 SystemMessageWindowWin window_; | 48 chrome::MediaDeviceNotificationWindowWin window_; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 void SystemMessageWindowWinTest::DoDevicesAttachedTest( | 51 void MediaDeviceNotificationWindowWinTest::DoDevicesAttachedTest( |
| 52 const std::vector<int>& device_indices) { | 52 const std::vector<int>& device_indices) { |
| 53 DEV_BROADCAST_VOLUME volume_broadcast; | 53 DEV_BROADCAST_VOLUME volume_broadcast; |
| 54 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | 54 volume_broadcast.dbcv_size = sizeof(volume_broadcast); |
| 55 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | 55 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; |
| 56 volume_broadcast.dbcv_unitmask = 0x0; | 56 volume_broadcast.dbcv_unitmask = 0x0; |
| 57 volume_broadcast.dbcv_flags = 0x0; | 57 volume_broadcast.dbcv_flags = 0x0; |
| 58 { | 58 { |
| 59 testing::InSequence sequnce; | 59 testing::InSequence sequnce; |
| 60 for (std::vector<int>::const_iterator it = device_indices.begin(); | 60 for (std::vector<int>::const_iterator it = device_indices.begin(); |
| 61 it != device_indices.end(); | 61 it != device_indices.end(); |
| 62 ++it) { | 62 ++it) { |
| 63 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | 63 volume_broadcast.dbcv_unitmask |= 0x1 << *it; |
| 64 std::wstring drive(L"_:\\"); | 64 std::wstring drive(L"_:\\"); |
| 65 drive[0] = 'A' + *it; | 65 drive[0] = 'A' + *it; |
| 66 std::string name("V"); | 66 std::string name("V"); |
| 67 name.append(base::SysWideToUTF8(drive)); | 67 name.append(base::SysWideToUTF8(drive)); |
| 68 EXPECT_CALL(observer_, OnMediaDeviceAttached(*it, name, FilePath(drive))); | 68 EXPECT_CALL(observer_, OnMediaDeviceAttached(*it, name, FilePath(drive))); |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 window_.OnDeviceChange(DBT_DEVICEARRIVAL, | 71 window_.OnDeviceChange(DBT_DEVICEARRIVAL, |
| 72 reinterpret_cast<DWORD>(&volume_broadcast)); | 72 reinterpret_cast<DWORD>(&volume_broadcast)); |
| 73 message_loop_.RunAllPending(); | 73 message_loop_.RunAllPending(); |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 void SystemMessageWindowWinTest::DoDevicesDetachedTest( | 76 void MediaDeviceNotificationWindowWinTest::DoDevicesDetachedTest( |
| 77 const std::vector<int>& device_indices) { | 77 const std::vector<int>& device_indices) { |
| 78 DEV_BROADCAST_VOLUME volume_broadcast; | 78 DEV_BROADCAST_VOLUME volume_broadcast; |
| 79 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | 79 volume_broadcast.dbcv_size = sizeof(volume_broadcast); |
| 80 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | 80 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; |
| 81 volume_broadcast.dbcv_unitmask = 0x0; | 81 volume_broadcast.dbcv_unitmask = 0x0; |
| 82 volume_broadcast.dbcv_flags = 0x0; | 82 volume_broadcast.dbcv_flags = 0x0; |
| 83 { | 83 { |
| 84 testing::InSequence sequence; | 84 testing::InSequence sequence; |
| 85 for (std::vector<int>::const_iterator it = device_indices.begin(); | 85 for (std::vector<int>::const_iterator it = device_indices.begin(); |
| 86 it != device_indices.end(); | 86 it != device_indices.end(); |
| 87 ++it) { | 87 ++it) { |
| 88 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | 88 volume_broadcast.dbcv_unitmask |= 0x1 << *it; |
| 89 EXPECT_CALL(observer_, OnMediaDeviceDetached(*it)); | 89 EXPECT_CALL(observer_, OnMediaDeviceDetached(*it)); |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 window_.OnDeviceChange(DBT_DEVICEREMOVECOMPLETE, | 92 window_.OnDeviceChange(DBT_DEVICEREMOVECOMPLETE, |
| 93 reinterpret_cast<DWORD>(&volume_broadcast)); | 93 reinterpret_cast<DWORD>(&volume_broadcast)); |
| 94 message_loop_.RunAllPending(); | 94 message_loop_.RunAllPending(); |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 TEST_F(SystemMessageWindowWinTest, DevicesChanged) { | 97 TEST_F(MediaDeviceNotificationWindowWinTest, RandomMessage) { |
| 98 EXPECT_CALL(observer_, OnDevicesChanged()).Times(1); | |
| 99 window_.OnDeviceChange(DBT_DEVNODES_CHANGED, NULL); | |
| 100 message_loop_.RunAllPending(); | |
| 101 } | |
| 102 | |
| 103 TEST_F(SystemMessageWindowWinTest, RandomMessage) { | |
| 104 window_.OnDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); | 98 window_.OnDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); |
| 105 message_loop_.RunAllPending(); | 99 message_loop_.RunAllPending(); |
| 106 } | 100 } |
| 107 | 101 |
| 108 TEST_F(SystemMessageWindowWinTest, DevicesAttached) { | 102 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesAttached) { |
| 109 std::vector<int> device_indices; | 103 std::vector<int> device_indices; |
| 110 device_indices.push_back(1); | 104 device_indices.push_back(1); |
| 111 device_indices.push_back(5); | 105 device_indices.push_back(5); |
| 112 device_indices.push_back(7); | 106 device_indices.push_back(7); |
| 113 | 107 |
| 114 DoDevicesAttachedTest(device_indices); | 108 DoDevicesAttachedTest(device_indices); |
| 115 } | 109 } |
| 116 | 110 |
| 117 TEST_F(SystemMessageWindowWinTest, DevicesAttachedHighBoundary) { | 111 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesAttachedHighBoundary) { |
| 118 std::vector<int> device_indices; | 112 std::vector<int> device_indices; |
| 119 device_indices.push_back(25); | 113 device_indices.push_back(25); |
| 120 | 114 |
| 121 DoDevicesAttachedTest(device_indices); | 115 DoDevicesAttachedTest(device_indices); |
| 122 } | 116 } |
| 123 | 117 |
| 124 TEST_F(SystemMessageWindowWinTest, DevicesAttachedLowBoundary) { | 118 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesAttachedLowBoundary) { |
| 125 std::vector<int> device_indices; | 119 std::vector<int> device_indices; |
| 126 device_indices.push_back(0); | 120 device_indices.push_back(0); |
| 127 | 121 |
| 128 DoDevicesAttachedTest(device_indices); | 122 DoDevicesAttachedTest(device_indices); |
| 129 } | 123 } |
| 130 | 124 |
| 131 TEST_F(SystemMessageWindowWinTest, DevicesAttachedAdjacentBits) { | 125 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesAttachedAdjacentBits) { |
| 132 std::vector<int> device_indices; | 126 std::vector<int> device_indices; |
| 133 device_indices.push_back(0); | 127 device_indices.push_back(0); |
| 134 device_indices.push_back(1); | 128 device_indices.push_back(1); |
| 135 device_indices.push_back(2); | 129 device_indices.push_back(2); |
| 136 device_indices.push_back(3); | 130 device_indices.push_back(3); |
| 137 | 131 |
| 138 DoDevicesAttachedTest(device_indices); | 132 DoDevicesAttachedTest(device_indices); |
| 139 } | 133 } |
| 140 | 134 |
| 141 TEST_F(SystemMessageWindowWinTest, DevicesDetached) { | 135 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesDetached) { |
| 142 std::vector<int> device_indices; | 136 std::vector<int> device_indices; |
| 143 device_indices.push_back(1); | 137 device_indices.push_back(1); |
| 144 device_indices.push_back(5); | 138 device_indices.push_back(5); |
| 145 device_indices.push_back(7); | 139 device_indices.push_back(7); |
| 146 | 140 |
| 147 DoDevicesDetachedTest(device_indices); | 141 DoDevicesDetachedTest(device_indices); |
| 148 } | 142 } |
| 149 | 143 |
| 150 TEST_F(SystemMessageWindowWinTest, DevicesDetachedHighBoundary) { | 144 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesDetachedHighBoundary) { |
| 151 std::vector<int> device_indices; | 145 std::vector<int> device_indices; |
| 152 device_indices.push_back(25); | 146 device_indices.push_back(25); |
| 153 | 147 |
| 154 DoDevicesDetachedTest(device_indices); | 148 DoDevicesDetachedTest(device_indices); |
| 155 } | 149 } |
| 156 | 150 |
| 157 TEST_F(SystemMessageWindowWinTest, DevicesDetachedLowBoundary) { | 151 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesDetachedLowBoundary) { |
| 158 std::vector<int> device_indices; | 152 std::vector<int> device_indices; |
| 159 device_indices.push_back(0); | 153 device_indices.push_back(0); |
| 160 | 154 |
| 161 DoDevicesDetachedTest(device_indices); | 155 DoDevicesDetachedTest(device_indices); |
| 162 } | 156 } |
| 163 | 157 |
| 164 TEST_F(SystemMessageWindowWinTest, DevicesDetachedAdjacentBits) { | 158 TEST_F(MediaDeviceNotificationWindowWinTest, DevicesDetachedAdjacentBits) { |
| 165 std::vector<int> device_indices; | 159 std::vector<int> device_indices; |
| 166 device_indices.push_back(0); | 160 device_indices.push_back(0); |
| 167 device_indices.push_back(1); | 161 device_indices.push_back(1); |
| 168 device_indices.push_back(2); | 162 device_indices.push_back(2); |
| 169 device_indices.push_back(3); | 163 device_indices.push_back(3); |
| 170 | 164 |
| 171 DoDevicesDetachedTest(device_indices); | 165 DoDevicesDetachedTest(device_indices); |
| 172 } | 166 } |
| OLD | NEW |