OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "chrome/browser/chromeos/file_system_provider/operations/close_file.h" | 12 #include "chrome/browser/chromeos/file_system_provider/operations/unmount.h" |
13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
14 #include "chrome/common/extensions/api/file_system_provider.h" | 14 #include "chrome/common/extensions/api/file_system_provider.h" |
15 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 15 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
16 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "webkit/browser/fileapi/async_file_util.h" | |
19 | 18 |
20 namespace chromeos { | 19 namespace chromeos { |
21 namespace file_system_provider { | 20 namespace file_system_provider { |
22 namespace operations { | 21 namespace operations { |
23 namespace { | 22 namespace { |
24 | 23 |
25 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 24 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
26 const char kFileSystemId[] = "testing-file-system"; | 25 const char kFileSystemId[] = "testing-file-system"; |
27 const int kRequestId = 2; | 26 const int kRequestId = 2; |
28 const int kOpenRequestId = 3; | |
29 | 27 |
30 // Fake event dispatcher implementation with extra logging capability. Acts as | 28 // Fake event dispatcher implementation with extra logging capability. Acts as |
31 // a providing extension end-point. | 29 // a providing extension end-point. |
32 class LoggingDispatchEventImpl { | 30 class LoggingDispatchEventImpl { |
33 public: | 31 public: |
34 explicit LoggingDispatchEventImpl(bool dispatch_reply) | 32 explicit LoggingDispatchEventImpl(bool dispatch_reply) |
35 : dispatch_reply_(dispatch_reply) {} | 33 : dispatch_reply_(dispatch_reply) {} |
36 virtual ~LoggingDispatchEventImpl() {} | 34 virtual ~LoggingDispatchEventImpl() {} |
37 | 35 |
38 bool OnDispatchEventImpl(scoped_ptr<extensions::Event> event) { | 36 bool OnDispatchEventImpl(scoped_ptr<extensions::Event> event) { |
39 events_.push_back(event->DeepCopy()); | 37 events_.push_back(event->DeepCopy()); |
40 return dispatch_reply_; | 38 return dispatch_reply_; |
41 } | 39 } |
42 | 40 |
43 ScopedVector<extensions::Event>& events() { return events_; } | 41 ScopedVector<extensions::Event>& events() { return events_; } |
44 | 42 |
45 private: | 43 private: |
46 ScopedVector<extensions::Event> events_; | 44 ScopedVector<extensions::Event> events_; |
47 bool dispatch_reply_; | 45 bool dispatch_reply_; |
48 | 46 |
49 DISALLOW_COPY_AND_ASSIGN(LoggingDispatchEventImpl); | 47 DISALLOW_COPY_AND_ASSIGN(LoggingDispatchEventImpl); |
50 }; | 48 }; |
51 | 49 |
52 // Callback invocation logger. Acts as a fileapi end-point. | 50 // Callback invocation logger. Acts as a fileapi end-point. |
53 class CallbackLogger { | 51 class CallbackLogger { |
54 public: | 52 public: |
55 CallbackLogger() : weak_ptr_factory_(this) {} | 53 CallbackLogger() : weak_ptr_factory_(this) {} |
56 virtual ~CallbackLogger() {} | 54 virtual ~CallbackLogger() {} |
57 | 55 |
58 void OnCloseFile(base::File::Error result) { events_.push_back(result); } | 56 void OnUnmount(base::File::Error result) { events_.push_back(result); } |
59 | 57 |
60 std::vector<base::File::Error>& events() { return events_; } | 58 std::vector<base::File::Error>& events() { return events_; } |
61 | 59 |
62 base::WeakPtr<CallbackLogger> GetWeakPtr() { | 60 base::WeakPtr<CallbackLogger> GetWeakPtr() { |
63 return weak_ptr_factory_.GetWeakPtr(); | 61 return weak_ptr_factory_.GetWeakPtr(); |
64 } | 62 } |
65 | 63 |
66 private: | 64 private: |
67 std::vector<base::File::Error> events_; | 65 std::vector<base::File::Error> events_; |
| 66 bool dispatch_reply_; |
68 base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_; | 67 base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_; |
69 | 68 |
70 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); | 69 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); |
71 }; | 70 }; |
72 | 71 |
73 } // namespace | 72 } // namespace |
74 | 73 |
75 class FileSystemProviderOperationsCloseFileTest : public testing::Test { | 74 class FileSystemProviderOperationsUnmountTest : public testing::Test { |
76 protected: | 75 protected: |
77 FileSystemProviderOperationsCloseFileTest() {} | 76 FileSystemProviderOperationsUnmountTest() {} |
78 virtual ~FileSystemProviderOperationsCloseFileTest() {} | 77 virtual ~FileSystemProviderOperationsUnmountTest() {} |
79 | 78 |
80 virtual void SetUp() OVERRIDE { | 79 virtual void SetUp() OVERRIDE { |
81 file_system_info_ = | 80 file_system_info_ = |
82 ProvidedFileSystemInfo(kExtensionId, | 81 ProvidedFileSystemInfo(kExtensionId, |
83 kFileSystemId, | 82 kFileSystemId, |
84 "" /* file_system_name */, | 83 "" /* file_system_name */, |
85 base::FilePath() /* mount_path */); | 84 base::FilePath() /* mount_path */); |
86 } | 85 } |
87 | 86 |
88 ProvidedFileSystemInfo file_system_info_; | 87 ProvidedFileSystemInfo file_system_info_; |
89 }; | 88 }; |
90 | 89 |
91 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) { | 90 TEST_F(FileSystemProviderOperationsUnmountTest, Execute) { |
92 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 91 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
93 CallbackLogger callback_logger; | 92 CallbackLogger callback_logger; |
94 | 93 |
95 CloseFile close_file( | 94 Unmount unmount( |
96 NULL, | 95 NULL, |
97 file_system_info_, | 96 file_system_info_, |
98 kOpenRequestId, | 97 base::Bind(&CallbackLogger::OnUnmount, callback_logger.GetWeakPtr())); |
99 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 98 unmount.SetDispatchEventImplForTesting( |
100 close_file.SetDispatchEventImplForTesting( | |
101 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 99 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
102 base::Unretained(&dispatcher))); | 100 base::Unretained(&dispatcher))); |
103 | 101 |
104 EXPECT_TRUE(close_file.Execute(kRequestId)); | 102 EXPECT_TRUE(unmount.Execute(kRequestId)); |
105 | 103 |
106 ASSERT_EQ(1u, dispatcher.events().size()); | 104 ASSERT_EQ(1u, dispatcher.events().size()); |
107 extensions::Event* event = dispatcher.events()[0]; | 105 extensions::Event* event = dispatcher.events()[0]; |
108 EXPECT_EQ( | 106 EXPECT_EQ( |
109 extensions::api::file_system_provider::OnCloseFileRequested::kEventName, | 107 extensions::api::file_system_provider::OnUnmountRequested::kEventName, |
110 event->event_name); | 108 event->event_name); |
111 base::ListValue* event_args = event->event_args.get(); | 109 base::ListValue* event_args = event->event_args.get(); |
112 ASSERT_EQ(3u, event_args->GetSize()); | 110 ASSERT_EQ(2u, event_args->GetSize()); |
113 | 111 |
114 std::string event_file_system_id; | 112 std::string event_file_system_id; |
115 EXPECT_TRUE(event_args->GetString(0, &event_file_system_id)); | 113 EXPECT_TRUE(event_args->GetString(0, &event_file_system_id)); |
116 EXPECT_EQ(kFileSystemId, event_file_system_id); | 114 EXPECT_EQ(kFileSystemId, event_file_system_id); |
117 | 115 |
118 int event_request_id = -1; | 116 int event_request_id = -1; |
119 EXPECT_TRUE(event_args->GetInteger(1, &event_request_id)); | 117 EXPECT_TRUE(event_args->GetInteger(1, &event_request_id)); |
120 EXPECT_EQ(kRequestId, event_request_id); | 118 EXPECT_EQ(kRequestId, event_request_id); |
121 | |
122 int event_open_request_id = -1; | |
123 EXPECT_TRUE(event_args->GetInteger(2, &event_open_request_id)); | |
124 EXPECT_EQ(kOpenRequestId, event_open_request_id); | |
125 } | 119 } |
126 | 120 |
127 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) { | 121 TEST_F(FileSystemProviderOperationsUnmountTest, Execute_NoListener) { |
128 LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 122 LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
129 CallbackLogger callback_logger; | 123 CallbackLogger callback_logger; |
130 | 124 |
131 CloseFile close_file( | 125 Unmount unmount( |
132 NULL, | 126 NULL, |
133 file_system_info_, | 127 file_system_info_, |
134 kOpenRequestId, | 128 base::Bind(&CallbackLogger::OnUnmount, callback_logger.GetWeakPtr())); |
135 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 129 unmount.SetDispatchEventImplForTesting( |
136 close_file.SetDispatchEventImplForTesting( | |
137 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 130 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
138 base::Unretained(&dispatcher))); | 131 base::Unretained(&dispatcher))); |
139 | 132 |
140 EXPECT_FALSE(close_file.Execute(kRequestId)); | 133 EXPECT_FALSE(unmount.Execute(kRequestId)); |
141 } | 134 } |
142 | 135 |
143 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) { | 136 TEST_F(FileSystemProviderOperationsUnmountTest, OnSuccess) { |
144 using extensions::api::file_system_provider_internal:: | 137 using extensions::api::file_system_provider_internal:: |
145 CloseFileRequestedSuccess::Params; | 138 UnmountRequestedSuccess::Params; |
146 | 139 |
147 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 140 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
148 CallbackLogger callback_logger; | 141 CallbackLogger callback_logger; |
149 | 142 |
150 CloseFile close_file( | 143 Unmount unmount( |
151 NULL, | 144 NULL, |
152 file_system_info_, | 145 file_system_info_, |
153 kOpenRequestId, | 146 base::Bind(&CallbackLogger::OnUnmount, callback_logger.GetWeakPtr())); |
154 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 147 unmount.SetDispatchEventImplForTesting( |
155 close_file.SetDispatchEventImplForTesting( | |
156 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 148 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
157 base::Unretained(&dispatcher))); | 149 base::Unretained(&dispatcher))); |
158 | 150 |
159 EXPECT_TRUE(close_file.Execute(kRequestId)); | 151 EXPECT_TRUE(unmount.Execute(kRequestId)); |
160 | 152 |
161 close_file.OnSuccess(kRequestId, | 153 unmount.OnSuccess(kRequestId, |
162 scoped_ptr<RequestValue>(new RequestValue()), | 154 scoped_ptr<RequestValue>(new RequestValue()), |
163 false /* has_next */); | 155 false /* has_more */); |
164 ASSERT_EQ(1u, callback_logger.events().size()); | 156 ASSERT_EQ(1u, callback_logger.events().size()); |
165 EXPECT_EQ(base::File::FILE_OK, callback_logger.events()[0]); | 157 base::File::Error event_result = callback_logger.events()[0]; |
| 158 EXPECT_EQ(base::File::FILE_OK, event_result); |
166 } | 159 } |
167 | 160 |
168 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) { | 161 TEST_F(FileSystemProviderOperationsUnmountTest, OnError) { |
169 using extensions::api::file_system_provider_internal:: | 162 using extensions::api::file_system_provider_internal::UnmountRequestedError:: |
170 CloseFileRequestedError::Params; | 163 Params; |
171 | 164 |
172 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 165 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
173 CallbackLogger callback_logger; | 166 CallbackLogger callback_logger; |
174 | 167 |
175 CloseFile close_file( | 168 Unmount unmount( |
176 NULL, | 169 NULL, |
177 file_system_info_, | 170 file_system_info_, |
178 kOpenRequestId, | 171 base::Bind(&CallbackLogger::OnUnmount, callback_logger.GetWeakPtr())); |
179 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 172 unmount.SetDispatchEventImplForTesting( |
180 close_file.SetDispatchEventImplForTesting( | |
181 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 173 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
182 base::Unretained(&dispatcher))); | 174 base::Unretained(&dispatcher))); |
183 | 175 |
184 EXPECT_TRUE(close_file.Execute(kRequestId)); | 176 EXPECT_TRUE(unmount.Execute(kRequestId)); |
185 | 177 |
186 close_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED); | 178 unmount.OnError(kRequestId, base::File::FILE_ERROR_NOT_FOUND); |
187 ASSERT_EQ(1u, callback_logger.events().size()); | 179 ASSERT_EQ(1u, callback_logger.events().size()); |
188 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, | 180 base::File::Error event_result = callback_logger.events()[0]; |
189 callback_logger.events()[0]); | 181 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, event_result); |
190 } | 182 } |
191 | 183 |
192 } // namespace operations | 184 } // namespace operations |
193 } // namespace file_system_provider | 185 } // namespace file_system_provider |
194 } // namespace chromeos | 186 } // namespace chromeos |
OLD | NEW |