| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/media_gallery/media_device_notifications_window_win.h" | |
| 6 | |
| 7 #include <dbt.h> | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/scoped_temp_dir.h" | |
| 16 #include "base/string_number_conversions.h" | |
| 17 #include "base/system_monitor/system_monitor.h" | |
| 18 #include "base/test/mock_devices_changed_observer.h" | |
| 19 #include "chrome/browser/media_gallery/media_storage_util.h" | |
| 20 #include "content/public/test/test_browser_thread.h" | |
| 21 #include "testing/gmock/include/gmock/gmock.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 using content::BrowserThread; | |
| 27 | |
| 28 LRESULT GetVolumeName(LPCWSTR drive, | |
| 29 LPWSTR volume_name, | |
| 30 unsigned int volume_name_length) { | |
| 31 DCHECK(volume_name_length > wcslen(drive) + 2); | |
| 32 *volume_name = 'V'; | |
| 33 wcscpy(volume_name + 1, drive); | |
| 34 return TRUE; | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 namespace chrome { | |
| 40 | |
| 41 using chrome::MediaDeviceNotificationsWindowWin; | |
| 42 using testing::_; | |
| 43 | |
| 44 class MediaDeviceNotificationsWindowWinTest : public testing::Test { | |
| 45 public: | |
| 46 MediaDeviceNotificationsWindowWinTest() | |
| 47 : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 48 file_thread_(BrowserThread::FILE) { } | |
| 49 virtual ~MediaDeviceNotificationsWindowWinTest() { } | |
| 50 | |
| 51 protected: | |
| 52 virtual void SetUp() OVERRIDE { | |
| 53 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 54 file_thread_.Start(); | |
| 55 window_ = new MediaDeviceNotificationsWindowWin(&GetVolumeName); | |
| 56 system_monitor_.AddDevicesChangedObserver(&observer_); | |
| 57 } | |
| 58 | |
| 59 virtual void TearDown() { | |
| 60 system_monitor_.RemoveDevicesChangedObserver(&observer_); | |
| 61 WaitForFileThread(); | |
| 62 } | |
| 63 | |
| 64 static void PostQuitToUIThread() { | |
| 65 BrowserThread::PostTask(BrowserThread::UI, | |
| 66 FROM_HERE, | |
| 67 MessageLoop::QuitClosure()); | |
| 68 } | |
| 69 | |
| 70 static void WaitForFileThread() { | |
| 71 BrowserThread::PostTask(BrowserThread::FILE, | |
| 72 FROM_HERE, | |
| 73 base::Bind(&PostQuitToUIThread)); | |
| 74 MessageLoop::current()->Run(); | |
| 75 } | |
| 76 | |
| 77 void DoDevicesAttachedTest(const std::vector<int>& device_indices); | |
| 78 void DoDevicesDetachedTest(const std::vector<int>& device_indices); | |
| 79 | |
| 80 MessageLoop message_loop_; | |
| 81 content::TestBrowserThread ui_thread_; | |
| 82 content::TestBrowserThread file_thread_; | |
| 83 | |
| 84 base::SystemMonitor system_monitor_; | |
| 85 base::MockDevicesChangedObserver observer_; | |
| 86 scoped_refptr<MediaDeviceNotificationsWindowWin> window_; | |
| 87 }; | |
| 88 | |
| 89 void MediaDeviceNotificationsWindowWinTest::DoDevicesAttachedTest( | |
| 90 const std::vector<int>& device_indices) { | |
| 91 DEV_BROADCAST_VOLUME volume_broadcast; | |
| 92 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | |
| 93 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | |
| 94 volume_broadcast.dbcv_unitmask = 0x0; | |
| 95 volume_broadcast.dbcv_flags = 0x0; | |
| 96 { | |
| 97 testing::InSequence sequence; | |
| 98 for (std::vector<int>::const_iterator it = device_indices.begin(); | |
| 99 it != device_indices.end(); | |
| 100 ++it) { | |
| 101 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | |
| 102 std::wstring drive(L"_:\\"); | |
| 103 drive[0] = 'A' + *it; | |
| 104 FilePath::StringType name = L"V" + drive; | |
| 105 std::string device_id = MediaStorageUtil::MakeDeviceId( | |
| 106 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, | |
| 107 base::IntToString(*it)); | |
| 108 EXPECT_CALL(observer_, OnRemovableStorageAttached(device_id, name, drive)) | |
| 109 .Times(0); | |
| 110 } | |
| 111 } | |
| 112 window_->OnDeviceChange(DBT_DEVICEARRIVAL, | |
| 113 reinterpret_cast<DWORD>(&volume_broadcast)); | |
| 114 message_loop_.RunAllPending(); | |
| 115 } | |
| 116 | |
| 117 void MediaDeviceNotificationsWindowWinTest::DoDevicesDetachedTest( | |
| 118 const std::vector<int>& device_indices) { | |
| 119 DEV_BROADCAST_VOLUME volume_broadcast; | |
| 120 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | |
| 121 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | |
| 122 volume_broadcast.dbcv_unitmask = 0x0; | |
| 123 volume_broadcast.dbcv_flags = 0x0; | |
| 124 { | |
| 125 testing::InSequence sequence; | |
| 126 for (std::vector<int>::const_iterator it = device_indices.begin(); | |
| 127 it != device_indices.end(); | |
| 128 ++it) { | |
| 129 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | |
| 130 std::string device_id = MediaStorageUtil::MakeDeviceId( | |
| 131 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, | |
| 132 base::IntToString(*it)); | |
| 133 EXPECT_CALL(observer_, OnRemovableStorageDetached(device_id)).Times(0); | |
| 134 } | |
| 135 } | |
| 136 window_->OnDeviceChange(DBT_DEVICEREMOVECOMPLETE, | |
| 137 reinterpret_cast<DWORD>(&volume_broadcast)); | |
| 138 message_loop_.RunAllPending(); | |
| 139 } | |
| 140 | |
| 141 TEST_F(MediaDeviceNotificationsWindowWinTest, RandomMessage) { | |
| 142 window_->OnDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); | |
| 143 message_loop_.RunAllPending(); | |
| 144 } | |
| 145 | |
| 146 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttached) { | |
| 147 std::vector<int> device_indices; | |
| 148 device_indices.push_back(1); | |
| 149 device_indices.push_back(5); | |
| 150 device_indices.push_back(7); | |
| 151 | |
| 152 DoDevicesAttachedTest(device_indices); | |
| 153 } | |
| 154 | |
| 155 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttachedHighBoundary) { | |
| 156 std::vector<int> device_indices; | |
| 157 device_indices.push_back(25); | |
| 158 | |
| 159 DoDevicesAttachedTest(device_indices); | |
| 160 } | |
| 161 | |
| 162 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttachedLowBoundary) { | |
| 163 std::vector<int> device_indices; | |
| 164 device_indices.push_back(0); | |
| 165 | |
| 166 DoDevicesAttachedTest(device_indices); | |
| 167 } | |
| 168 | |
| 169 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesAttachedAdjacentBits) { | |
| 170 std::vector<int> device_indices; | |
| 171 device_indices.push_back(0); | |
| 172 device_indices.push_back(1); | |
| 173 device_indices.push_back(2); | |
| 174 device_indices.push_back(3); | |
| 175 | |
| 176 DoDevicesAttachedTest(device_indices); | |
| 177 } | |
| 178 | |
| 179 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetached) { | |
| 180 std::vector<int> device_indices; | |
| 181 device_indices.push_back(1); | |
| 182 device_indices.push_back(5); | |
| 183 device_indices.push_back(7); | |
| 184 | |
| 185 DoDevicesDetachedTest(device_indices); | |
| 186 } | |
| 187 | |
| 188 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetachedHighBoundary) { | |
| 189 std::vector<int> device_indices; | |
| 190 device_indices.push_back(25); | |
| 191 | |
| 192 DoDevicesDetachedTest(device_indices); | |
| 193 } | |
| 194 | |
| 195 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetachedLowBoundary) { | |
| 196 std::vector<int> device_indices; | |
| 197 device_indices.push_back(0); | |
| 198 | |
| 199 DoDevicesDetachedTest(device_indices); | |
| 200 } | |
| 201 | |
| 202 TEST_F(MediaDeviceNotificationsWindowWinTest, DevicesDetachedAdjacentBits) { | |
| 203 std::vector<int> device_indices; | |
| 204 device_indices.push_back(0); | |
| 205 device_indices.push_back(1); | |
| 206 device_indices.push_back(2); | |
| 207 device_indices.push_back(3); | |
| 208 | |
| 209 DoDevicesDetachedTest(device_indices); | |
| 210 } | |
| 211 | |
| 212 } // namespace chrome | |
| OLD | NEW |