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