Commit 81935f5c authored by haven@chromium.org's avatar haven@chromium.org

Adds API tests for listRemovableStorageDevices

BUG=370658

Review URL: https://codereview.chromium.org/266363006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270030 0039d316-1c4b-4281-b951-d872f2087c98
parent f9c5d39d
// Copyright 2014 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 "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/common/extensions/api/image_writer_private.h"
namespace extensions {
using api::image_writer_private::RemovableStorageDevice;
class ImageWriterPrivateApiTest : public ExtensionApiTest {
public:
virtual void SetUpOnMainThread() OVERRIDE {
scoped_refptr<StorageDeviceList> device_list(new StorageDeviceList);
RemovableStorageDevice* expected1 = new RemovableStorageDevice();
expected1->vendor = "Vendor 1";
expected1->model = "Model 1";
expected1->capacity = 1 << 20;
expected1->storage_unit_id = "/test/id/1";
RemovableStorageDevice* expected2 = new RemovableStorageDevice();
expected2->vendor = "Vendor 2";
expected2->model = "Model 2";
expected2->capacity = 1 << 22;
expected2->storage_unit_id = "/test/id/2";
linked_ptr<RemovableStorageDevice> device1(expected1);
device_list->data.push_back(device1);
linked_ptr<RemovableStorageDevice> device2(expected2);
device_list->data.push_back(device2);
RemovableStorageProvider::SetDeviceListForTesting(device_list);
}
virtual void CleanUpOnMainThread() OVERRIDE {
RemovableStorageProvider::ClearDeviceListForTesting();
}
};
IN_PROC_BROWSER_TEST_F(ImageWriterPrivateApiTest, TestListDevices) {
ASSERT_TRUE(RunExtensionTest("image_writer_private/list_devices"))
<< message_;
}
} // namespace extensions
// Copyright 2014 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 "base/lazy_instance.h"
#include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
#include "content/public/browser/browser_thread.h"
namespace extensions {
// A device list to be returned when testing.
static base::LazyInstance<scoped_refptr<StorageDeviceList> > g_test_device_list;
// TODO(haven): Udev code may be duplicated in the Chrome codebase.
// https://code.google.com/p/chromium/issues/detail?id=284898
void RemovableStorageProvider::GetAllDevices(DeviceListReadyCallback callback) {
if (g_test_device_list.Get() != NULL) {
content::BrowserThread::PostTask(
content::BrowserThread::FILE,
FROM_HERE,
base::Bind(callback, g_test_device_list.Get(), true));
return;
}
scoped_refptr<StorageDeviceList> device_list(new StorageDeviceList);
// We need to do some file i/o to get the device block size
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::FILE,
FROM_HERE,
base::Bind(PopulateDeviceList, device_list),
base::Bind(callback, device_list));
}
void RemovableStorageProvider::SetDeviceListForTesting(
scoped_refptr<StorageDeviceList> device_list) {
g_test_device_list.Get() = device_list;
}
void RemovableStorageProvider::ClearDeviceListForTesting() {
g_test_device_list.Get() = NULL;
}
} // namespace extensions
......@@ -10,6 +10,8 @@
namespace extensions {
// TODO(haven): Clean up this class to remove refcounting. http://crbug/370590
typedef RefCountedVector<linked_ptr
<api::image_writer_private::RemovableStorageDevice> > StorageDeviceList;
......@@ -19,10 +21,21 @@ class RemovableStorageProvider {
public:
typedef base::Callback<void(scoped_refptr<StorageDeviceList>, bool)>
DeviceListReadyCallback;
// Gets the list of all available devices and returns it via callback.
static void GetAllDevices(DeviceListReadyCallback callback);
#if defined(OS_LINUX)
static bool GetDevicesOnFileThread(scoped_refptr<StorageDeviceList>);
#endif
// Sets the list of devices that will be returned by GetAllDevices during
// testing. All calls to |GetAllDevices| will return this list until
// |ClearDeviceListForTesting| is called.
static void SetDeviceListForTesting(
scoped_refptr<StorageDeviceList> device_list);
// Clears the list of devices that is used during testing.
static void ClearDeviceListForTesting();
private:
// Fills the provided empty device list with the available devices.
static bool PopulateDeviceList(scoped_refptr<StorageDeviceList> device_list);
};
} // namespace extensions
......
......@@ -17,9 +17,8 @@ using chromeos::disks::DiskMountManager;
// fixed disk. In fact, some SD cards will present themselves as fixed disks
// (see http://crbug.com/340761). Thus we just expose all USB and SD drives.
// static
void RemovableStorageProvider::GetAllDevices(DeviceListReadyCallback callback) {
scoped_refptr<StorageDeviceList> device_list(new StorageDeviceList());
bool RemovableStorageProvider::PopulateDeviceList(
scoped_refptr<StorageDeviceList> device_list) {
DiskMountManager* disk_mount_manager = DiskMountManager::GetInstance();
const DiskMountManager::DiskMap& disks = disk_mount_manager->disks();
......@@ -49,7 +48,7 @@ void RemovableStorageProvider::GetAllDevices(DeviceListReadyCallback callback) {
}
}
callback.Run(device_list, true);
return true;
}
} // namespace extensions
......@@ -3,8 +3,10 @@
// found in the LICENSE file.
#include "base/bind.h"
#include "base/run_loop.h"
#include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
#include "chromeos/disks/mock_disk_mount_manager.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
......@@ -109,6 +111,9 @@ class RemovableStorageProviderChromeOsUnitTest : public testing::Test {
MockDiskMountManager* disk_mount_manager_mock_;
scoped_refptr<StorageDeviceList> devices_;
private:
content::TestBrowserThreadBundle thread_bundle_;
};
} // namespace
......@@ -127,6 +132,8 @@ TEST_F(RemovableStorageProviderChromeOsUnitTest, GetAllDevices) {
base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(2U, devices_->data.size());
ExpectDevice(
......@@ -145,6 +152,8 @@ TEST_F(RemovableStorageProviderChromeOsUnitTest, EmptyProductAndModel) {
base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(2U, devices_->data.size());
ExpectDevice(devices_, kDevicePathUSB, "", kUnknownUSBDiskModel, kDeviceSize);
......
......@@ -10,7 +10,6 @@
#include "content/public/browser/browser_thread.h"
namespace extensions {
// TODO(haven): Udev code may be duplicated in the Chrome codebase.
// https://code.google.com/p/chromium/issues/detail?id=284898
......@@ -42,19 +41,7 @@ static int get_device_blk_size(const std::string& path) {
return blk_size;
}
void RemovableStorageProvider::GetAllDevices(
DeviceListReadyCallback callback) {
scoped_refptr<StorageDeviceList> device_list(new StorageDeviceList);
// We need to do some file i/o to get the device block size
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::FILE,
FROM_HERE,
base::Bind(GetDevicesOnFileThread, device_list),
base::Bind(callback, device_list));
}
bool RemovableStorageProvider::GetDevicesOnFileThread(
bool RemovableStorageProvider::PopulateDeviceList(
scoped_refptr<StorageDeviceList> device_list) {
struct udev* udev;
struct udev_enumerate* enumerate;
......
......@@ -6,11 +6,9 @@
namespace extensions {
void RemovableStorageProvider::GetAllDevices(
DeviceListReadyCallback callback) {
scoped_refptr<StorageDeviceList> device_list(new StorageDeviceList());
callback.Run(device_list, false);
bool RemovableStorageProvider::PopulateDeviceList(
scoped_refptr<StorageDeviceList> device_list) {
return false;
}
} // namespace extensions
......@@ -18,7 +18,7 @@ namespace {
bool AddDeviceInfo(HANDLE interface_enumerator,
SP_DEVICE_INTERFACE_DATA* interface_data,
StorageDeviceList* device_list) {
scoped_refptr<StorageDeviceList> device_list) {
// Get the required buffer size by calling with a null output buffer.
DWORD interface_detail_data_size;
BOOL status = SetupDiGetDeviceInterfaceDetail(
......@@ -149,7 +149,10 @@ bool AddDeviceInfo(HANDLE interface_enumerator,
return true;
}
bool PopulateList(StorageDeviceList* device_list) {
} // namespace
bool RemovableStorageProvider::PopulateDeviceList(
scoped_refptr<StorageDeviceList> device_list) {
HDEVINFO interface_enumerator = SetupDiGetClassDevs(
&DiskClassGuid,
NULL, // Enumerator.
......@@ -188,16 +191,4 @@ bool PopulateList(StorageDeviceList* device_list) {
return true;
}
} // namespace
void RemovableStorageProvider::GetAllDevices(DeviceListReadyCallback callback) {
scoped_refptr<StorageDeviceList> device_list(new StorageDeviceList());
if (PopulateList(device_list.get())) {
callback.Run(device_list, true);
} else {
callback.Run(device_list, false);
}
}
} // namespace extensions
......@@ -361,6 +361,7 @@
'browser/extensions/api/image_writer_private/image_writer_utility_client.h',
'browser/extensions/api/image_writer_private/image_writer_private_api.cc',
'browser/extensions/api/image_writer_private/image_writer_private_api.h',
'browser/extensions/api/image_writer_private/removable_storage_provider.cc',
'browser/extensions/api/image_writer_private/removable_storage_provider.h',
'browser/extensions/api/image_writer_private/removable_storage_provider_linux.cc',
'browser/extensions/api/image_writer_private/removable_storage_provider_chromeos.cc',
......
......@@ -1061,6 +1061,7 @@
'browser/extensions/api/identity/identity_apitest.cc',
'browser/extensions/api/idle/idle_apitest.cc',
'browser/extensions/api/idltest/idltest_apitest.cc',
'browser/extensions/api/image_writer_private/image_writer_private_apitest.cc',
'browser/extensions/api/input_ime/input_ime_apitest_chromeos.cc',
'browser/extensions/api/management/management_api_browsertest.cc',
'browser/extensions/api/management/management_apitest.cc',
......
......@@ -761,7 +761,8 @@
"extension_types": ["platform_app"],
"whitelist": [
"A291B26E088FA6BA53FFD72F0916F06EBA7C585A", // http://crbug.com/329088
"D7986543275120831B39EF28D1327552FC343960" // http://crbug.com/329088
"D7986543275120831B39EF28D1327552FC343960", // http://crbug.com/329088
"BCC85C0D7F85897F2699974421EA402144C35C0C" // API Test
]
},
"readingListPrivate": {
......
{
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6aedfP5QXnA176+/EKXFwbBoXl3smca9uaO1ytfLCRsH8Ja0xrjJG+/2tvcCL1JzBbc8/31cwWIOiNFawJiIc+nfZAi4rO27yakn4W83kHOhjr7hA4/a+CtmOPTTgpK1DCIpo0Xy+lpzQuqHBKL9/sXMCN4bKqcXMe7XA09VJYD6Rv+CTDfKkgN3oNYhm0KBOwkvJ/P7x7KeBUCusd+UOzJygBP4p2mDgIX/WfUZAuRGq1ty/Eu9dBm29Jhe1YBctFaARyR5FnMsr57Kw/mWrNXkZ2iewrLUzNh1FWLQUbiL4QdqaP9//Xxhsrf+LG1UcJN1HBnn/b0xYLfcH9W7RQIDAQAB",
"name": "Image Writer Private",
"version": "0.1",
"app": {
"background": {
"scripts": ["test.js"]
}
},
"permissions": ["imageWriterPrivate"]
}
// Copyright 2014 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.
var expectedDevices = [{
'vendor': 'Vendor 1',
'model': 'Model 1',
'capacity': 1 << 20,
'storageUnitId': '/test/id/1',
}, {
'vendor': 'Vendor 2',
'model': 'Model 2',
'capacity': 1 << 22,
'storageUnitId': '/test/id/2',
}];
function testDeviceList() {
chrome.imageWriterPrivate.listRemovableStorageDevices(
chrome.test.callback(listRemovableDevicesCallback));
}
function listRemovableDevicesCallback(deviceList) {
deviceList.sort(function (a, b) {
if (a.storageUnitId > b.storageUnitId) return 1;
if (a.storageUnitId < b.storageUnitId) return -1;
return 0;
});
chrome.test.assertEq(2, deviceList.length);
deviceList.forEach(function (dev, i) {
var expected = expectedDevices[i];
chrome.test.assertEq(expected.vendor, dev.vendor);
chrome.test.assertEq(expected.model, dev.model);
chrome.test.assertEq(expected.capacity, dev.capacity);
chrome.test.assertEq(expected.storageUnitId, dev.storageUnitId);
});
}
chrome.test.runTests([testDeviceList])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment