Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / system_storage / system_storage_apitest.cc
blobaf7262f30de40206c5d32993557f7519d1832aee
1 // Copyright 2013 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 <vector>
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/storage_monitor/storage_monitor.h"
10 #include "components/storage_monitor/test_storage_monitor.h"
11 #include "extensions/browser/api/system_storage/storage_api_test_util.h"
12 #include "extensions/browser/api/system_storage/storage_info_provider.h"
13 #include "extensions/shell/test/shell_apitest.h"
14 #include "extensions/test/extension_test_message_listener.h"
15 #include "extensions/test/result_catcher.h"
17 namespace {
19 using extensions::StorageUnitInfoList;
20 using extensions::test::TestStorageUnitInfo;
21 using extensions::test::kRemovableStorageData;
22 using storage_monitor::StorageMonitor;
23 using storage_monitor::TestStorageMonitor;
25 const struct TestStorageUnitInfo kTestingData[] = {
26 {"dcim:device:001", "0xbeaf", 4098, 1},
27 {"path:device:002", "/home", 4098, 2},
28 {"path:device:003", "/data", 10000, 3}};
30 } // namespace
32 class TestStorageInfoProvider : public extensions::StorageInfoProvider {
33 public:
34 TestStorageInfoProvider(const struct TestStorageUnitInfo* testing_data,
35 size_t n);
37 private:
38 ~TestStorageInfoProvider() override;
40 // StorageInfoProvider implementations.
41 double GetStorageFreeSpaceFromTransientIdOnFileThread(
42 const std::string& transient_id) override;
44 std::vector<struct TestStorageUnitInfo> testing_data_;
47 TestStorageInfoProvider::TestStorageInfoProvider(
48 const struct TestStorageUnitInfo* testing_data,
49 size_t n)
50 : testing_data_(testing_data, testing_data + n) {
53 TestStorageInfoProvider::~TestStorageInfoProvider() {
56 double TestStorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread(
57 const std::string& transient_id) {
58 std::string device_id =
59 StorageMonitor::GetInstance()->GetDeviceIdForTransientId(transient_id);
60 for (size_t i = 0; i < testing_data_.size(); ++i) {
61 if (testing_data_[i].device_id == device_id) {
62 return static_cast<double>(testing_data_[i].available_capacity);
65 return -1;
68 class SystemStorageApiTest : public extensions::ShellApiTest {
69 public:
70 SystemStorageApiTest() {}
71 ~SystemStorageApiTest() override {}
73 void SetUpOnMainThread() override {
74 ShellApiTest::SetUpOnMainThread();
75 TestStorageMonitor::CreateForBrowserTests();
78 void SetUpInProcessBrowserTestFixture() override {
79 ShellApiTest::SetUpInProcessBrowserTestFixture();
80 message_loop_.reset(new base::MessageLoopForUI);
83 void SetUpAllMockStorageDevices() {
84 for (size_t i = 0; i < arraysize(kTestingData); ++i) {
85 AttachRemovableStorage(kTestingData[i]);
89 void AttachRemovableStorage(
90 const struct TestStorageUnitInfo& removable_storage_info) {
91 StorageMonitor::GetInstance()->receiver()->ProcessAttach(
92 extensions::test::BuildStorageInfoFromTestStorageUnitInfo(
93 removable_storage_info));
96 void DetachRemovableStorage(const std::string& id) {
97 StorageMonitor::GetInstance()->receiver()->ProcessDetach(id);
100 private:
101 scoped_ptr<base::MessageLoop> message_loop_;
104 IN_PROC_BROWSER_TEST_F(SystemStorageApiTest, Storage) {
105 SetUpAllMockStorageDevices();
106 TestStorageInfoProvider* provider =
107 new TestStorageInfoProvider(kTestingData, arraysize(kTestingData));
108 extensions::StorageInfoProvider::InitializeForTesting(provider);
109 std::vector<linked_ptr<ExtensionTestMessageListener>> device_ids_listeners;
110 for (size_t i = 0; i < arraysize(kTestingData); ++i) {
111 linked_ptr<ExtensionTestMessageListener> listener(
112 new ExtensionTestMessageListener(
113 StorageMonitor::GetInstance()->GetTransientIdForDeviceId(
114 kTestingData[i].device_id),
115 false));
116 device_ids_listeners.push_back(listener);
118 ASSERT_TRUE(RunAppTest("system/storage")) << message_;
119 for (size_t i = 0; i < device_ids_listeners.size(); ++i)
120 EXPECT_TRUE(device_ids_listeners[i]->WaitUntilSatisfied());
123 IN_PROC_BROWSER_TEST_F(SystemStorageApiTest, StorageAttachment) {
124 extensions::ResultCatcher catcher;
125 ExtensionTestMessageListener attach_listener("attach", false);
126 ExtensionTestMessageListener detach_listener("detach", false);
128 EXPECT_TRUE(LoadApp("system/storage_attachment"));
129 // Simulate triggering onAttached event.
130 ASSERT_TRUE(attach_listener.WaitUntilSatisfied());
132 AttachRemovableStorage(kRemovableStorageData);
134 std::string removable_storage_transient_id =
135 StorageMonitor::GetInstance()->GetTransientIdForDeviceId(
136 kRemovableStorageData.device_id);
137 ExtensionTestMessageListener detach_device_id_listener(
138 removable_storage_transient_id, false);
140 // Simulate triggering onDetached event.
141 ASSERT_TRUE(detach_listener.WaitUntilSatisfied());
142 DetachRemovableStorage(kRemovableStorageData.device_id);
144 ASSERT_TRUE(detach_device_id_listener.WaitUntilSatisfied());
146 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();