Index: content/browser/media_device_notifications_linux_unittest.cc |
=================================================================== |
--- content/browser/media_device_notifications_linux_unittest.cc (revision 0) |
+++ content/browser/media_device_notifications_linux_unittest.cc (revision 0) |
@@ -0,0 +1,351 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <mntent.h> |
+#include <stdio.h> |
+ |
+#include <string> |
+ |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop.h" |
+#include "base/scoped_temp_dir.h" |
+#include "base/system_monitor/system_monitor.h" |
+#include "content/browser/browser_thread_impl.h" |
+#include "content/browser/media_device_notifications_linux.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const char* kValidFS = "vfat"; |
+const char* kInvalidFS = "invalidfs"; |
+ |
+const char* kInvalidPath = "invalid path does not exist"; |
+ |
+const char* kDevice1 = "d1"; |
+const char* kDevice2 = "d2"; |
+const char* kDevice3 = "d3"; |
+ |
+const char* kMountPointA = "mnt_a"; |
+const char* kMountPointB = "mnt_b"; |
+ |
+} // namespace |
+ |
+namespace content { |
+ |
+class MediaDeviceNotificationsLinuxTest : public testing::Test { |
+ public: |
+ struct MtabTestData { |
+ MtabTestData(const char* mount_device, |
+ const char* mount_point, |
+ const char* mount_type) |
+ : mount_device(mount_device), |
+ mount_point(mount_point), |
+ mount_type(mount_type) { |
+ } |
+ |
+ const char* mount_device; |
+ const char* mount_point; |
+ const char* mount_type; |
+ }; |
+ |
+ class DummyDeviceChangeObserver |
vandebo (ex-Chrome)
2012/03/06 00:41:40
You should use MockDevicesChangedObserver
Lei Zhang
2012/03/06 19:00:25
Done.
|
+ : public base::SystemMonitor::DevicesChangedObserver { |
+ public: |
+ DummyDeviceChangeObserver() |
+ : attach_count_(0), |
+ detach_count_(0) { |
+ } |
+ virtual ~DummyDeviceChangeObserver() {} |
+ |
+ void Reset() { |
+ attach_count_ = 0; |
+ detach_count_ = 0; |
+ } |
+ |
+ int attach_count() { return attach_count_; } |
+ int detach_count() { return detach_count_; } |
+ |
+ // base::SystemMonitor::DevicesChangedObserver implementation. |
+ virtual void OnMediaDeviceAttached( |
+ const base::SystemMonitor::DeviceIdType& id, |
+ const std::string& name, |
+ const FilePath& path) OVERRIDE { |
+ ++attach_count_; |
+ } |
+ virtual void OnMediaDeviceDetached( |
+ const base::SystemMonitor::DeviceIdType& id) OVERRIDE { |
+ ++detach_count_; |
+ } |
+ |
+ private: |
+ int attach_count_; |
+ int detach_count_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DummyDeviceChangeObserver); |
+ }; |
+ |
+ MediaDeviceNotificationsLinuxTest() |
+ : message_loop_(MessageLoop::TYPE_IO), |
+ file_thread_(BrowserThread::FILE, &message_loop_) { |
+ system_monitor_.reset(new base::SystemMonitor()); |
+ system_monitor_->AddDevicesChangedObserver(&dummy_device_change_observer_); |
+ } |
+ virtual ~MediaDeviceNotificationsLinuxTest() {} |
+ |
+ protected: |
+ virtual void SetUp() { |
+ dummy_device_change_observer_.Reset(); |
+ |
+ // Create and set up a temp dir with files for the test. |
+ ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
+ FilePath test_dir = scoped_temp_dir_.path().AppendASCII("etc"); |
+ ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
+ mtab_file_ = test_dir.AppendASCII("foo"); |
vandebo (ex-Chrome)
2012/03/06 00:41:40
why not mtab ?
Lei Zhang
2012/03/06 19:00:25
The name is arbitrary. I changed it to "test_mtab"
|
+ struct MtabTestData initial_test_data[] = { |
+ MtabTestData("dummydevice", "dummydir", kInvalidFS), |
+ }; |
+ WriteToMtab(initial_test_data, arraysize(initial_test_data), true); |
+ |
+ // Initialize the test subject. |
+ notifications_ = new MediaDeviceNotificationsLinux(mtab_file_); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, |
+ notifications_.get())); |
+ message_loop_.RunAllPending(); |
+ } |
+ |
+ virtual void TearDown() { |
+ message_loop_.RunAllPending(); |
+ notifications_ = NULL; |
+ ASSERT_TRUE(scoped_temp_dir_.Delete()); |
+ } |
+ |
+ // Used to run tests. When the mtab file gets modified, the message loop |
+ // needs to run in order to react to the file modification. |
+ // See WriteToMtab for parameters. |
+ void WriteToMtabAndRunLoop(struct MtabTestData* data, |
+ size_t data_size, |
+ bool overwrite) { |
+ WriteToMtab(data, data_size, overwrite); |
+ message_loop_.RunAllPending(); |
+ } |
+ |
+ // Create a directory named |dir| relative to the test directory. |
+ // Set |with_dcim_dir| to true if the created directory will have a "DCIM" |
+ // subdirectory. |
+ // Returns the full path to the created directory on success, or an empty |
+ // path on failure. |
+ FilePath CreateMountPoint(const char* dir, bool with_dcim_dir) { |
+ FilePath return_path(scoped_temp_dir_.path()); |
+ return_path = return_path.AppendASCII(dir); |
+ FilePath path(return_path); |
+ if (with_dcim_dir) |
+ path = path.AppendASCII("DCIM"); |
+ if (!file_util::CreateDirectory(path)) |
+ return FilePath(); |
+ return return_path; |
+ } |
+ |
+ int attach_count() { return dummy_device_change_observer_.attach_count(); } |
+ int detach_count() { return dummy_device_change_observer_.detach_count(); } |
+ |
+ private: |
+ // Write the test mtab data to |mtab_file_|. |
+ // |data| is an array of mtab entries. |
+ // |data_size| is the array size of |data|. |
+ // |overwrite| specifies whether to overwrite |mtab_file_|. |
+ void WriteToMtab(struct MtabTestData* data, |
+ size_t data_size, |
+ bool overwrite) { |
+ FILE* fd = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); |
+ ASSERT_TRUE(fd); |
+ |
+ scoped_ptr<char> mnt_opts(strdup("rw")); |
+ struct mntent entry; |
+ entry.mnt_opts = mnt_opts.get(); |
+ entry.mnt_freq = 0; |
+ entry.mnt_passno = 0; |
+ for (size_t i = 0; i < data_size; ++i) { |
+ scoped_ptr<char> mnt_fsname(strdup(data[i].mount_device)); |
+ scoped_ptr<char> mnt_dir(strdup(data[i].mount_point)); |
+ scoped_ptr<char> mnt_type(strdup(data[i].mount_type)); |
+ entry.mnt_fsname = mnt_fsname.get(); |
+ entry.mnt_dir = mnt_dir.get(); |
+ entry.mnt_type = mnt_type.get(); |
+ int add_result = addmntent(fd, &entry); |
+ ASSERT_EQ(0, add_result); |
+ } |
+ int end_result = endmntent(fd); |
+ ASSERT_EQ(1, end_result); |
+ } |
+ |
+ // The message loop and file thread to run tests on. |
+ MessageLoop message_loop_; |
+ BrowserThreadImpl file_thread_; |
+ |
+ // SystemMonitor and DevicesChangedObserver to hook together to test. |
+ scoped_ptr<base::SystemMonitor> system_monitor_; |
+ DummyDeviceChangeObserver dummy_device_change_observer_; |
+ |
+ // Temporary directory for created test data. |
+ ScopedTempDir scoped_temp_dir_; |
+ // Path to the test mtab file. |
+ FilePath mtab_file_; |
+ |
+ scoped_refptr<MediaDeviceNotificationsLinux> notifications_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); |
+}; |
+ |
+TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { |
+ FilePath test_path = CreateMountPoint(kMountPointA, true); |
+ ASSERT_FALSE(test_path.empty()); |
+ struct MtabTestData test_data[] = { |
+ MtabTestData(kDevice1, kInvalidPath, kValidFS), |
+ MtabTestData(kDevice2, test_path.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data, arraysize(test_data), false); |
+ EXPECT_EQ(1, attach_count()); |
+ EXPECT_EQ(0, detach_count()); |
+ |
+ WriteToMtabAndRunLoop(NULL, 0, true); |
+ EXPECT_EQ(1, attach_count()); |
+ EXPECT_EQ(1, detach_count()); |
+} |
+ |
+// Only mount points with DCIM directories are recognized. |
+TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { |
+ FilePath test_path1 = CreateMountPoint(kMountPointA, true); |
+ ASSERT_FALSE(test_path1.empty()); |
+ struct MtabTestData test_data1[] = { |
+ MtabTestData(kDevice1, test_path1.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); |
+ EXPECT_EQ(1, attach_count()); |
+ EXPECT_EQ(0, detach_count()); |
+ |
+ FilePath test_path2 = CreateMountPoint(kMountPointB, false); |
+ ASSERT_FALSE(test_path2.empty()); |
+ struct MtabTestData test_data2[] = { |
+ MtabTestData(kDevice2, test_path2.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); |
+ EXPECT_EQ(1, attach_count()); |
+ EXPECT_EQ(0, detach_count()); |
+ |
+ WriteToMtabAndRunLoop(NULL, 0, true); |
+ EXPECT_EQ(1, attach_count()); |
+ EXPECT_EQ(1, detach_count()); |
+} |
+ |
+TEST_F(MediaDeviceNotificationsLinuxTest, MultiDeviceMultiMount) { |
+ FilePath test_path1 = CreateMountPoint(kMountPointA, true); |
+ FilePath test_path2 = CreateMountPoint(kMountPointB, true); |
+ ASSERT_FALSE(test_path1.empty()); |
+ ASSERT_FALSE(test_path2.empty()); |
+ |
+ // Attach two devices. |
+ // kDevice1 -> kMountPointA |
+ // kDevice2 -> kMountPointB |
+ struct MtabTestData test_data1[] = { |
+ MtabTestData(kDevice1, test_path1.value().c_str(), kValidFS), |
+ MtabTestData(kDevice2, test_path2.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); |
+ EXPECT_EQ(2, attach_count()); |
+ EXPECT_EQ(0, detach_count()); |
+ |
+ // Attach |kDevice1| to |kMountPointB|. |
+ // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been |
+ // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. |
+ // kDevice1 -> kMountPointA |
+ // kDevice2 -> kMountPointB |
+ // kDevice1 -> kMountPointB |
+ struct MtabTestData test_data2[] = { |
+ MtabTestData(kDevice1, test_path2.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); |
+ EXPECT_EQ(3, attach_count()); |
+ EXPECT_EQ(2, detach_count()); |
+ |
+ // Attach |kDevice2| to |kMountPointA|. |
+ // kDevice1 -> kMountPointA |
+ // kDevice2 -> kMountPointB |
+ // kDevice1 -> kMountPointB |
+ // kDevice2 -> kMountPointA |
+ struct MtabTestData test_data3[] = { |
+ MtabTestData(kDevice2, test_path1.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data3, arraysize(test_data3), false); |
+ EXPECT_EQ(4, attach_count()); |
+ EXPECT_EQ(2, detach_count()); |
+ |
+ // Detach |kDevice2| from |kMountPointA|. |
+ // kDevice1 -> kMountPointA |
+ // kDevice2 -> kMountPointB |
+ // kDevice1 -> kMountPointB |
+ struct MtabTestData test_data4[] = { |
+ MtabTestData(kDevice1, test_path1.value().c_str(), kValidFS), |
+ MtabTestData(kDevice2, test_path2.value().c_str(), kValidFS), |
+ MtabTestData(kDevice1, test_path2.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data4, arraysize(test_data4), true); |
+ EXPECT_EQ(4, attach_count()); |
+ EXPECT_EQ(3, detach_count()); |
+ |
+ // Detach |kDevice1| from |kMountPointB|. |
+ // kDevice1 -> kMountPointA |
+ // kDevice2 -> kMountPointB |
+ WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true); |
+ EXPECT_EQ(6, attach_count()); |
+ EXPECT_EQ(4, detach_count()); |
+ |
+ // Detach all devices. |
+ WriteToMtabAndRunLoop(NULL, 0, true); |
+ EXPECT_EQ(6, attach_count()); |
+ EXPECT_EQ(6, detach_count()); |
+} |
+ |
+TEST_F(MediaDeviceNotificationsLinuxTest, MultiDeviceMount2) { |
+ FilePath test_path1 = CreateMountPoint(kMountPointA, true); |
+ FilePath test_path2 = CreateMountPoint(kMountPointB, true); |
+ ASSERT_FALSE(test_path1.empty()); |
+ ASSERT_FALSE(test_path2.empty()); |
+ |
+ // |kDevice1| is most recently mounted at |kMountPointB|. |
+ // kDevice1 -> kMountPointA |
+ // kDevice2 -> kMountPointB |
+ // kDevice1 -> kMountPointB |
+ struct MtabTestData test_data2[] = { |
+ MtabTestData(kDevice1, test_path2.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); |
+ EXPECT_EQ(1, attach_count()); |
+ EXPECT_EQ(0, detach_count()); |
+ |
+ // Attach |kDevice3| to |kMountPointB|. |
+ // |kDevice1| is inaccessible at its most recent mount point, so it is |
+ // detached and unavailable, even though it is still accessible via |
+ // |kMountPointA|. |
+ // kDevice1 -> kMountPointA |
+ // kDevice2 -> kMountPointB |
+ // kDevice1 -> kMountPointB |
+ // kDevice3 -> kMountPointB |
+ struct MtabTestData test_data3[] = { |
+ MtabTestData(kDevice3, test_path2.value().c_str(), kValidFS), |
+ }; |
+ WriteToMtabAndRunLoop(test_data3, arraysize(test_data3), false); |
+ EXPECT_EQ(2, attach_count()); |
+ EXPECT_EQ(1, detach_count()); |
+ |
+ // Detach all devices. |
+ WriteToMtabAndRunLoop(NULL, 0, true); |
+ EXPECT_EQ(2, attach_count()); |
+ EXPECT_EQ(2, detach_count()); |
+} |
+ |
+} // namespace content |
Property changes on: content/browser/media_device_notifications_linux_unittest.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |