This source file includes following definitions.
- GetMtpDeviceId
- GetStorageInfo
- MtpStorageAttached
- MtpStorageDetached
- SetUp
- TearDown
- observer
- mtp_device_observer
- TEST_F
- TEST_F
#include "components/storage_monitor/media_transfer_protocol_device_observer_linux.h"
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "components/storage_monitor/mock_removable_storage_observer.h"
#include "components/storage_monitor/storage_info.h"
#include "components/storage_monitor/storage_monitor.h"
#include "components/storage_monitor/test_storage_monitor.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace storage_monitor {
namespace {
const char kStorageLabel[] = "Camera V1.0";
const char kStorageLocation[] = "/usb:2,2,88888";
const char kStorageUniqueId[] = "VendorModelSerial:COM:MOD2012:283";
const char kStorageWithInvalidInfo[] = "usb:2,3,11111";
const char kStorageWithValidInfo[] = "usb:2,2,88888";
std::string GetMtpDeviceId(const std::string& unique_id) {
return StorageInfo::MakeDeviceId(StorageInfo::MTP_OR_PTP, unique_id);
}
void GetStorageInfo(const std::string& storage_name,
device::MediaTransferProtocolManager* mtp_manager,
std::string* id,
base::string16* label,
std::string* location) {
if (storage_name == kStorageWithInvalidInfo)
return;
ASSERT_EQ(kStorageWithValidInfo, storage_name);
*id = GetMtpDeviceId(kStorageUniqueId);
*label = base::ASCIIToUTF16(kStorageLabel);
*location = kStorageLocation;
}
class TestMediaTransferProtocolDeviceObserverLinux
: public MediaTransferProtocolDeviceObserverLinux {
public:
TestMediaTransferProtocolDeviceObserverLinux(
StorageMonitor::Receiver* receiver,
device::MediaTransferProtocolManager* mtp_manager)
: MediaTransferProtocolDeviceObserverLinux(receiver, mtp_manager,
&GetStorageInfo) {
}
void MtpStorageAttached(const std::string& storage_name) {
StorageChanged(true, storage_name);
base::RunLoop().RunUntilIdle();
}
void MtpStorageDetached(const std::string& storage_name) {
StorageChanged(false, storage_name);
base::RunLoop().RunUntilIdle();
}
private:
DISALLOW_COPY_AND_ASSIGN(TestMediaTransferProtocolDeviceObserverLinux);
};
}
class MediaTransferProtocolDeviceObserverLinuxTest : public testing::Test {
public:
MediaTransferProtocolDeviceObserverLinuxTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {}
protected:
virtual void SetUp() OVERRIDE {
mock_storage_observer_.reset(new MockRemovableStorageObserver);
TestStorageMonitor* monitor = TestStorageMonitor::CreateAndInstall();
mtp_device_observer_.reset(
new TestMediaTransferProtocolDeviceObserverLinux(
monitor->receiver(), monitor->media_transfer_protocol_manager()));
monitor->AddObserver(mock_storage_observer_.get());
}
virtual void TearDown() OVERRIDE {
StorageMonitor* monitor = StorageMonitor::GetInstance();
monitor->RemoveObserver(mock_storage_observer_.get());
mtp_device_observer_.reset();
TestStorageMonitor::Destroy();
}
MockRemovableStorageObserver& observer() {
return *mock_storage_observer_;
}
TestMediaTransferProtocolDeviceObserverLinux* mtp_device_observer() {
return mtp_device_observer_.get();
}
private:
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<TestMediaTransferProtocolDeviceObserverLinux> mtp_device_observer_;
scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest);
};
TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
std::string device_id = GetMtpDeviceId(kStorageUniqueId);
mtp_device_observer()->MtpStorageAttached(kStorageWithValidInfo);
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
EXPECT_EQ(device_id, observer().last_attached().device_id());
EXPECT_EQ(kStorageLocation, observer().last_attached().location());
mtp_device_observer()->MtpStorageDetached(kStorageWithValidInfo);
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
EXPECT_EQ(device_id, observer().last_detached().device_id());
}
TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, StorageWithInvalidInfo) {
mtp_device_observer()->MtpStorageAttached(kStorageWithInvalidInfo);
mtp_device_observer()->MtpStorageDetached(kStorageWithInvalidInfo);
EXPECT_EQ(0, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
}
}