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

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: Fix race condition in unit test. 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) {}
38 virtual ~MediaStorageUtilTest() { }
39
40 // Verify mounted device type.
41 void CheckDeviceType(const base::FilePath::StringType& mount_point,
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 HasDcim() function returns true for the given media
98 // 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 HasDcim() function returns false for a given non-media
110 // 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;
78 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop);
79
80 MediaStorageUtil::DeviceIdSet devices; 158 MediaStorageUtil::DeviceIdSet devices;
81 devices.insert(kImageCaptureDeviceId); 159 devices.insert(kImageCaptureDeviceId);
82 160
83 base::WaitableEvent event(true, false); 161 base::WaitableEvent event(true, false);
tommycli 2013/03/11 17:31:01 Run FilterAttachedDevices on the FILE thread.
Greg Billock 2013/03/11 18:07:43 This is fine. FilterAttachedDevices bounces to the
vandebo (ex-Chrome) 2013/03/11 22:27:03 FYI - I think another way that would have worked t
84 MediaStorageUtil::FilterAttachedDevices(&devices, 162 base::Closure signal_event =
85 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); 163 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event));
86 loop.RunUntilIdle(); 164
165 // We need signal_event to be executed on the FILE thread, as the test thread
166 // is blocked. Therefore, we invoke FilterAttachedDevices on the FILE thread.
167 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
168 base::Bind(&MediaStorageUtil::FilterAttachedDevices,
169 base::Unretained(&devices), signal_event));
87 event.Wait(); 170 event.Wait();
88 EXPECT_FALSE(devices.find(kImageCaptureDeviceId) != devices.end()); 171 EXPECT_FALSE(devices.find(kImageCaptureDeviceId) != devices.end());
89 172
90 ProcessAttach(kImageCaptureDeviceId, ASCIIToUTF16("name"), 173 ProcessAttach(kImageCaptureDeviceId, ASCIIToUTF16("name"),
91 FILE_PATH_LITERAL("/location")); 174 FILE_PATH_LITERAL("/location"));
92 devices.insert(kImageCaptureDeviceId); 175 devices.insert(kImageCaptureDeviceId);
93 event.Reset(); 176 event.Reset();
94 MediaStorageUtil::FilterAttachedDevices(&devices, 177 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
95 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); 178 base::Bind(&MediaStorageUtil::FilterAttachedDevices,
96 loop.RunUntilIdle(); 179 base::Unretained(&devices), signal_event));
97 event.Wait(); 180 event.Wait();
98 181
99 EXPECT_TRUE(devices.find(kImageCaptureDeviceId) != devices.end()); 182 EXPECT_TRUE(devices.find(kImageCaptureDeviceId) != devices.end());
100 } 183 }
101 184
102 } // namespace chrome 185 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/storage_monitor/media_storage_util.cc ('k') | chrome/browser/storage_monitor/storage_monitor_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698