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.
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "chrome/browser/storage_monitor/media_storage_util.h"
13 #include "chrome/browser/storage_monitor/removable_device_constants.h"
14 #include "chrome/browser/storage_monitor/storage_monitor.h"
15 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 const char kImageCaptureDeviceId[] = "ic:xyz";
27 using content::BrowserThread;
29 class MediaStorageUtilTest : public testing::Test {
31 MediaStorageUtilTest()
32 : thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD) {}
33 virtual ~MediaStorageUtilTest() {}
35 // Verify mounted device type.
36 void CheckDCIMDeviceType(const base::FilePath& mount_point) {
37 EXPECT_TRUE(MediaStorageUtil::HasDcim(mount_point));
40 void CheckNonDCIMDeviceType(const base::FilePath& mount_point) {
41 EXPECT_FALSE(MediaStorageUtil::HasDcim(mount_point));
44 void ProcessAttach(const std::string& id,
46 const base::FilePath::StringType& location) {
47 StorageInfo info(id, name, location, string16(), string16(), string16(), 0);
48 monitor_->receiver()->ProcessAttach(info);
52 // Create mount point for the test device.
53 base::FilePath CreateMountPoint(bool create_dcim_dir) {
54 base::FilePath path(scoped_temp_dir_.path());
56 path = path.Append(kDCIMDirectoryName);
57 if (!file_util::CreateDirectory(path))
58 return base::FilePath();
59 return scoped_temp_dir_.path();
62 virtual void SetUp() OVERRIDE {
63 monitor_ = TestStorageMonitor::CreateAndInstall();
64 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
67 virtual void TearDown() OVERRIDE {
69 TestStorageMonitor::RemoveSingleton();
72 static void PostQuitToUIThread() {
73 BrowserThread::PostTask(BrowserThread::UI,
75 base::MessageLoop::QuitClosure());
78 static void WaitForFileThread() {
79 BrowserThread::PostTask(BrowserThread::FILE,
81 base::Bind(&PostQuitToUIThread));
82 base::MessageLoop::current()->Run();
86 content::TestBrowserThreadBundle thread_bundle_;
87 TestStorageMonitor* monitor_;
88 base::ScopedTempDir scoped_temp_dir_;
91 // Test to verify that HasDcim() function returns true for the given media
92 // device mount point.
93 TEST_F(MediaStorageUtilTest, MediaDeviceAttached) {
94 // Create a dummy mount point with DCIM Directory.
95 base::FilePath mount_point(CreateMountPoint(true));
96 ASSERT_FALSE(mount_point.empty());
97 BrowserThread::PostTask(
98 BrowserThread::FILE, FROM_HERE,
99 base::Bind(&MediaStorageUtilTest::CheckDCIMDeviceType,
100 base::Unretained(this), mount_point));
101 base::RunLoop().RunUntilIdle();
104 // Test to verify that HasDcim() function returns false for a given non-media
105 // device mount point.
106 TEST_F(MediaStorageUtilTest, NonMediaDeviceAttached) {
107 // Create a dummy mount point without DCIM Directory.
108 base::FilePath mount_point(CreateMountPoint(false));
109 ASSERT_FALSE(mount_point.empty());
110 BrowserThread::PostTask(
111 BrowserThread::FILE, FROM_HERE,
112 base::Bind(&MediaStorageUtilTest::CheckNonDCIMDeviceType,
113 base::Unretained(this), mount_point));
114 base::RunLoop().RunUntilIdle();
117 TEST_F(MediaStorageUtilTest, CanCreateFileSystemForImageCapture) {
118 EXPECT_TRUE(MediaStorageUtil::CanCreateFileSystem(kImageCaptureDeviceId,
120 EXPECT_FALSE(MediaStorageUtil::CanCreateFileSystem(
121 "dcim:xyz", base::FilePath(FILE_PATH_LITERAL("relative"))));
122 EXPECT_FALSE(MediaStorageUtil::CanCreateFileSystem(
123 "dcim:xyz", base::FilePath(FILE_PATH_LITERAL("../refparent"))));
126 TEST_F(MediaStorageUtilTest, DetectDeviceFiltered) {
127 MediaStorageUtil::DeviceIdSet devices;
128 devices.insert(kImageCaptureDeviceId);
130 base::WaitableEvent event(true, false);
131 base::Closure signal_event =
132 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event));
134 // We need signal_event to be executed on the FILE thread, as the test thread
135 // is blocked. Therefore, we invoke FilterAttachedDevices on the FILE thread.
136 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
137 base::Bind(&MediaStorageUtil::FilterAttachedDevices,
138 base::Unretained(&devices), signal_event));
140 EXPECT_FALSE(devices.find(kImageCaptureDeviceId) != devices.end());
142 ProcessAttach(kImageCaptureDeviceId, ASCIIToUTF16("name"),
143 FILE_PATH_LITERAL("/location"));
144 devices.insert(kImageCaptureDeviceId);
146 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
147 base::Bind(&MediaStorageUtil::FilterAttachedDevices,
148 base::Unretained(&devices), signal_event));
151 EXPECT_TRUE(devices.find(kImageCaptureDeviceId) != devices.end());