- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / storage_monitor / media_storage_util_unittest.cc
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 <string>
6
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"
20
21 namespace {
22
23 const char kImageCaptureDeviceId[] = "ic:xyz";
24
25 }  // namespace
26
27 using content::BrowserThread;
28
29 class MediaStorageUtilTest : public testing::Test {
30  public:
31   MediaStorageUtilTest()
32       : thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD) {}
33   virtual ~MediaStorageUtilTest() {}
34
35   // Verify mounted device type.
36   void CheckDCIMDeviceType(const base::FilePath& mount_point) {
37     EXPECT_TRUE(MediaStorageUtil::HasDcim(mount_point));
38   }
39
40   void CheckNonDCIMDeviceType(const base::FilePath& mount_point) {
41     EXPECT_FALSE(MediaStorageUtil::HasDcim(mount_point));
42   }
43
44   void ProcessAttach(const std::string& id,
45                      const string16& name,
46                      const base::FilePath::StringType& location) {
47     StorageInfo info(id, name, location, string16(), string16(), string16(), 0);
48     monitor_->receiver()->ProcessAttach(info);
49   }
50
51  protected:
52   // Create mount point for the test device.
53   base::FilePath CreateMountPoint(bool create_dcim_dir) {
54     base::FilePath path(scoped_temp_dir_.path());
55     if (create_dcim_dir)
56       path = path.Append(kDCIMDirectoryName);
57     if (!file_util::CreateDirectory(path))
58       return base::FilePath();
59     return scoped_temp_dir_.path();
60   }
61
62   virtual void SetUp() OVERRIDE {
63     monitor_ = TestStorageMonitor::CreateAndInstall();
64     ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
65   }
66
67   virtual void TearDown() OVERRIDE {
68     WaitForFileThread();
69     TestStorageMonitor::RemoveSingleton();
70   }
71
72   static void PostQuitToUIThread() {
73     BrowserThread::PostTask(BrowserThread::UI,
74                             FROM_HERE,
75                             base::MessageLoop::QuitClosure());
76   }
77
78   static void WaitForFileThread() {
79     BrowserThread::PostTask(BrowserThread::FILE,
80                             FROM_HERE,
81                             base::Bind(&PostQuitToUIThread));
82     base::MessageLoop::current()->Run();
83   }
84
85  private:
86   content::TestBrowserThreadBundle thread_bundle_;
87   TestStorageMonitor* monitor_;
88   base::ScopedTempDir scoped_temp_dir_;
89 };
90
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();
102 }
103
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();
115 }
116
117 TEST_F(MediaStorageUtilTest, CanCreateFileSystemForImageCapture) {
118   EXPECT_TRUE(MediaStorageUtil::CanCreateFileSystem(kImageCaptureDeviceId,
119                                                     base::FilePath()));
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"))));
124 }
125
126 TEST_F(MediaStorageUtilTest, DetectDeviceFiltered) {
127   MediaStorageUtil::DeviceIdSet devices;
128   devices.insert(kImageCaptureDeviceId);
129
130   base::WaitableEvent event(true, false);
131   base::Closure signal_event =
132       base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event));
133
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));
139   event.Wait();
140   EXPECT_FALSE(devices.find(kImageCaptureDeviceId) != devices.end());
141
142   ProcessAttach(kImageCaptureDeviceId, ASCIIToUTF16("name"),
143                 FILE_PATH_LITERAL("/location"));
144   devices.insert(kImageCaptureDeviceId);
145   event.Reset();
146   BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
147                           base::Bind(&MediaStorageUtil::FilterAttachedDevices,
148                                      base::Unretained(&devices), signal_event));
149   event.Wait();
150
151   EXPECT_TRUE(devices.find(kImageCaptureDeviceId) != devices.end());
152 }