Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Side by Side Diff: chrome/browser/storage_monitor/media_storage_util_unittest.cc

Issue 12544005: Consolidate storage_monitor/MediaDeviceNotificationsUtils into MediaStorageUtil. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: another chromeos fix Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <string> 5 #include <string>
6 6
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
7 #include "base/message_loop.h" 9 #include "base/message_loop.h"
8 #include "base/synchronization/waitable_event.h" 10 #include "base/synchronization/waitable_event.h"
9 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/storage_monitor/media_storage_util.h" 12 #include "chrome/browser/storage_monitor/media_storage_util.h"
13 #include "chrome/browser/storage_monitor/removable_device_constants.h"
11 #include "chrome/browser/storage_monitor/storage_monitor.h" 14 #include "chrome/browser/storage_monitor/storage_monitor.h"
12 #include "chrome/browser/storage_monitor/test_storage_monitor.h" 15 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
13 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread.h" 17 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
16 19
17 namespace chrome { 20 namespace chrome {
18 21
19 namespace { 22 namespace {
20 23
21 // Sample mtp device id and unique id. 24 // Sample mtp device id and unique id.
22 const char kMtpDeviceId[] = "mtp:VendorModelSerial:ABC:1233:1237912873"; 25 const char kMtpDeviceId[] = "mtp:VendorModelSerial:ABC:1233:1237912873";
23 const char kUniqueId[] = "VendorModelSerial:ABC:1233:1237912873"; 26 const char kUniqueId[] = "VendorModelSerial:ABC:1233:1237912873";
24 const char kImageCaptureDeviceId[] = "ic:xyz"; 27 const char kImageCaptureDeviceId[] = "ic:xyz";
25 28
26 } // namespace 29 } // namespace
27 30
31 using content::BrowserThread;
32
28 class MediaStorageUtilTest : public testing::Test { 33 class MediaStorageUtilTest : public testing::Test {
29 public: 34 public:
35 MediaStorageUtilTest()
36 : ui_thread_(BrowserThread::UI, &message_loop_),
37 file_thread_(BrowserThread::FILE) { }
Greg Billock 2013/03/06 21:48:27 No space between braces
38 virtual ~MediaStorageUtilTest() { }
39
40 // Verify mounted device type.
41 void checkDeviceType(const base::FilePath::StringType& mount_point,
Greg Billock 2013/03/06 21:48:27 Capitalize
42 bool expected_val) {
43 if (expected_val)
44 EXPECT_TRUE(MediaStorageUtil::HasDcim(mount_point));
45 else
46 EXPECT_FALSE(MediaStorageUtil::HasDcim(mount_point));
47 }
48
30 void ProcessAttach(const std::string& id, 49 void ProcessAttach(const std::string& id,
31 const string16& name, 50 const string16& name,
32 const base::FilePath::StringType& location) { 51 const base::FilePath::StringType& location) {
33 monitor_.receiver()->ProcessAttach(StorageInfo(id, name, location)); 52 monitor_.receiver()->ProcessAttach(StorageInfo(id, name, location));
34 } 53 }
35 54
55 protected:
56 // Create mount point for the test device.
57 base::FilePath CreateMountPoint(bool create_dcim_dir) {
58 base::FilePath path(scoped_temp_dir_.path());
59 if (create_dcim_dir)
60 path = path.Append(kDCIMDirectoryName);
61 if (!file_util::CreateDirectory(path))
62 return base::FilePath();
63 return scoped_temp_dir_.path();
64 }
65
66 virtual void SetUp() OVERRIDE {
67 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
68 file_thread_.Start();
69 }
70
71 virtual void TearDown() {
72 WaitForFileThread();
73 }
74
75 static void PostQuitToUIThread() {
76 BrowserThread::PostTask(BrowserThread::UI,
77 FROM_HERE,
78 MessageLoop::QuitClosure());
79 }
80
81 static void WaitForFileThread() {
82 BrowserThread::PostTask(BrowserThread::FILE,
83 FROM_HERE,
84 base::Bind(&PostQuitToUIThread));
85 MessageLoop::current()->Run();
86 }
87
88 MessageLoop message_loop_;
89
36 private: 90 private:
37 chrome::test::TestStorageMonitor monitor_; 91 chrome::test::TestStorageMonitor monitor_;
92 content::TestBrowserThread ui_thread_;
93 content::TestBrowserThread file_thread_;
94 base::ScopedTempDir scoped_temp_dir_;
38 }; 95 };
39 96
97 // Test to verify that IsMediaDevice() function returns true for the given
98 // media device mount point.
99 TEST_F(MediaStorageUtilTest, MediaDeviceAttached) {
100 // Create a dummy mount point with DCIM Directory.
101 base::FilePath mount_point(CreateMountPoint(true));
102 BrowserThread::PostTask(
103 BrowserThread::FILE, FROM_HERE,
104 base::Bind(&MediaStorageUtilTest::checkDeviceType,
105 base::Unretained(this), mount_point.value(), true));
106 message_loop_.RunUntilIdle();
107 }
108
109 // Test to verify that IsMediaDevice() function returns false for a given
110 // non-media device mount point.
111 TEST_F(MediaStorageUtilTest, NonMediaDeviceAttached) {
112 // Create a dummy mount point without DCIM Directory.
113 base::FilePath mount_point(CreateMountPoint(false));
114 BrowserThread::PostTask(
115 BrowserThread::FILE, FROM_HERE,
116 base::Bind(&MediaStorageUtilTest::checkDeviceType,
117 base::Unretained(this), mount_point.value(), false));
118 message_loop_.RunUntilIdle();
119 }
120
40 // Test to verify |MediaStorageUtil::MakeDeviceId| functionality using a sample 121 // Test to verify |MediaStorageUtil::MakeDeviceId| functionality using a sample
41 // mtp device unique id. 122 // mtp device unique id.
42 TEST_F(MediaStorageUtilTest, MakeMtpDeviceId) { 123 TEST_F(MediaStorageUtilTest, MakeMtpDeviceId) {
43 std::string device_id = 124 std::string device_id =
44 MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP, kUniqueId); 125 MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP, kUniqueId);
45 ASSERT_EQ(kMtpDeviceId, device_id); 126 ASSERT_EQ(kMtpDeviceId, device_id);
46 } 127 }
47 128
48 // Test to verify |MediaStorageUtil::CrackDeviceId| functionality using a sample 129 // Test to verify |MediaStorageUtil::CrackDeviceId| functionality using a sample
49 // mtp device id. 130 // mtp device id.
(...skipping 17 matching lines...) Expand all
67 TEST_F(MediaStorageUtilTest, CanCreateFileSystemForImageCapture) { 148 TEST_F(MediaStorageUtilTest, CanCreateFileSystemForImageCapture) {
68 EXPECT_TRUE(MediaStorageUtil::CanCreateFileSystem(kImageCaptureDeviceId, 149 EXPECT_TRUE(MediaStorageUtil::CanCreateFileSystem(kImageCaptureDeviceId,
69 base::FilePath())); 150 base::FilePath()));
70 EXPECT_FALSE(MediaStorageUtil::CanCreateFileSystem( 151 EXPECT_FALSE(MediaStorageUtil::CanCreateFileSystem(
71 "dcim:xyz", base::FilePath(FILE_PATH_LITERAL("relative")))); 152 "dcim:xyz", base::FilePath(FILE_PATH_LITERAL("relative"))));
72 EXPECT_FALSE(MediaStorageUtil::CanCreateFileSystem( 153 EXPECT_FALSE(MediaStorageUtil::CanCreateFileSystem(
73 "dcim:xyz", base::FilePath(FILE_PATH_LITERAL("../refparent")))); 154 "dcim:xyz", base::FilePath(FILE_PATH_LITERAL("../refparent"))));
74 } 155 }
75 156
76 TEST_F(MediaStorageUtilTest, DetectDeviceFiltered) { 157 TEST_F(MediaStorageUtilTest, DetectDeviceFiltered) {
77 MessageLoop loop; 158 MessageLoop loop;
Greg Billock 2013/03/06 21:50:53 Probably need to use the test loop here.
78 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); 159 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop);
79 160
80 MediaStorageUtil::DeviceIdSet devices; 161 MediaStorageUtil::DeviceIdSet devices;
81 devices.insert(kImageCaptureDeviceId); 162 devices.insert(kImageCaptureDeviceId);
82 163
83 base::WaitableEvent event(true, false); 164 base::WaitableEvent event(true, false);
84 MediaStorageUtil::FilterAttachedDevices(&devices, 165 MediaStorageUtil::FilterAttachedDevices(&devices,
85 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); 166 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
86 loop.RunUntilIdle(); 167 loop.RunUntilIdle();
87 event.Wait(); 168 event.Wait();
88 EXPECT_FALSE(devices.find(kImageCaptureDeviceId) != devices.end()); 169 EXPECT_FALSE(devices.find(kImageCaptureDeviceId) != devices.end());
89 170
90 ProcessAttach(kImageCaptureDeviceId, ASCIIToUTF16("name"), 171 ProcessAttach(kImageCaptureDeviceId, ASCIIToUTF16("name"),
91 FILE_PATH_LITERAL("/location")); 172 FILE_PATH_LITERAL("/location"));
92 devices.insert(kImageCaptureDeviceId); 173 devices.insert(kImageCaptureDeviceId);
93 event.Reset(); 174 event.Reset();
94 MediaStorageUtil::FilterAttachedDevices(&devices, 175 MediaStorageUtil::FilterAttachedDevices(&devices,
95 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); 176 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
96 loop.RunUntilIdle(); 177 loop.RunUntilIdle();
97 event.Wait(); 178 event.Wait();
98 179
99 EXPECT_TRUE(devices.find(kImageCaptureDeviceId) != devices.end()); 180 EXPECT_TRUE(devices.find(kImageCaptureDeviceId) != devices.end());
100 } 181 }
101 182
102 } // namespace chrome 183 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698