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.
5 #include "chrome/browser/system_monitor/media_device_notifications_utils.h"
7 #include "base/file_path.h"
8 #include "base/file_util.h"
9 #include "base/message_loop.h"
10 #include "base/scoped_temp_dir.h"
11 #include "chrome/browser/system_monitor/removable_device_constants.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread
;
19 class MediaDeviceNotificationUtilsTest
: public testing::Test
{
21 MediaDeviceNotificationUtilsTest()
22 : ui_thread_(BrowserThread::UI
, &message_loop_
),
23 file_thread_(BrowserThread::FILE) { }
24 virtual ~MediaDeviceNotificationUtilsTest() { }
26 // Verify mounted device type.
27 void checkDeviceType(const FilePath::StringType
& mount_point
,
30 EXPECT_TRUE(IsMediaDevice(mount_point
));
32 EXPECT_FALSE(IsMediaDevice(mount_point
));
36 // Create mount point for the test device.
37 FilePath
CreateMountPoint(bool create_dcim_dir
) {
38 FilePath
path(scoped_temp_dir_
.path());
40 path
= path
.Append(kDCIMDirectoryName
);
41 if (!file_util::CreateDirectory(path
))
43 return scoped_temp_dir_
.path();
46 virtual void SetUp() OVERRIDE
{
47 ASSERT_TRUE(scoped_temp_dir_
.CreateUniqueTempDir());
51 virtual void TearDown() {
55 static void PostQuitToUIThread() {
56 BrowserThread::PostTask(BrowserThread::UI
,
58 MessageLoop::QuitClosure());
61 static void WaitForFileThread() {
62 BrowserThread::PostTask(BrowserThread::FILE,
64 base::Bind(&PostQuitToUIThread
));
65 MessageLoop::current()->Run();
68 MessageLoop message_loop_
;
71 content::TestBrowserThread ui_thread_
;
72 content::TestBrowserThread file_thread_
;
73 ScopedTempDir scoped_temp_dir_
;
76 // Test to verify that IsMediaDevice() function returns true for the given
77 // media device mount point.
78 TEST_F(MediaDeviceNotificationUtilsTest
, MediaDeviceAttached
) {
79 // Create a dummy mount point with DCIM Directory.
80 FilePath
mount_point(CreateMountPoint(true));
81 BrowserThread::PostTask(
82 BrowserThread::FILE, FROM_HERE
,
83 base::Bind(&MediaDeviceNotificationUtilsTest::checkDeviceType
,
84 base::Unretained(this), mount_point
.value(), true));
85 message_loop_
.RunAllPending();
88 // Test to verify that IsMediaDevice() function returns false for a given
89 // non-media device mount point.
90 TEST_F(MediaDeviceNotificationUtilsTest
, NonMediaDeviceAttached
) {
91 // Create a dummy mount point without DCIM Directory.
92 FilePath
mount_point(CreateMountPoint(false));
93 BrowserThread::PostTask(
94 BrowserThread::FILE, FROM_HERE
,
95 base::Bind(&MediaDeviceNotificationUtilsTest::checkDeviceType
,
96 base::Unretained(this), mount_point
.value(), false));
97 message_loop_
.RunAllPending();
100 } // namespace chrome