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" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 private: | 45 private: |
46 ScopedVector<extensions::Event> events_; | 46 ScopedVector<extensions::Event> events_; |
47 bool dispatch_reply_; | 47 bool dispatch_reply_; |
48 | 48 |
49 DISALLOW_COPY_AND_ASSIGN(LoggingDispatchEventImpl); | 49 DISALLOW_COPY_AND_ASSIGN(LoggingDispatchEventImpl); |
50 }; | 50 }; |
51 | 51 |
52 // Callback invocation logger. Acts as a fileapi end-point. | 52 // Callback invocation logger. Acts as a fileapi end-point. |
53 class CallbackLogger { | 53 class CallbackLogger { |
54 public: | 54 public: |
55 CallbackLogger() : weak_ptr_factory_(this) {} | 55 CallbackLogger() {} |
56 virtual ~CallbackLogger() {} | 56 virtual ~CallbackLogger() {} |
57 | 57 |
58 void OnCloseFile(base::File::Error result) { events_.push_back(result); } | 58 void OnCloseFile(base::File::Error result) { events_.push_back(result); } |
59 | 59 |
60 std::vector<base::File::Error>& events() { return events_; } | 60 std::vector<base::File::Error>& events() { return events_; } |
61 | 61 |
62 base::WeakPtr<CallbackLogger> GetWeakPtr() { | |
63 return weak_ptr_factory_.GetWeakPtr(); | |
64 } | |
65 | |
66 private: | 62 private: |
67 std::vector<base::File::Error> events_; | 63 std::vector<base::File::Error> events_; |
68 base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); | 64 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); |
71 }; | 65 }; |
72 | 66 |
73 } // namespace | 67 } // namespace |
74 | 68 |
75 class FileSystemProviderOperationsCloseFileTest : public testing::Test { | 69 class FileSystemProviderOperationsCloseFileTest : public testing::Test { |
76 protected: | 70 protected: |
77 FileSystemProviderOperationsCloseFileTest() {} | 71 FileSystemProviderOperationsCloseFileTest() {} |
78 virtual ~FileSystemProviderOperationsCloseFileTest() {} | 72 virtual ~FileSystemProviderOperationsCloseFileTest() {} |
79 | 73 |
80 virtual void SetUp() OVERRIDE { | 74 virtual void SetUp() OVERRIDE { |
81 file_system_info_ = | 75 file_system_info_ = |
82 ProvidedFileSystemInfo(kExtensionId, | 76 ProvidedFileSystemInfo(kExtensionId, |
83 kFileSystemId, | 77 kFileSystemId, |
84 "" /* file_system_name */, | 78 "" /* file_system_name */, |
85 base::FilePath() /* mount_path */); | 79 base::FilePath() /* mount_path */); |
86 } | 80 } |
87 | 81 |
88 ProvidedFileSystemInfo file_system_info_; | 82 ProvidedFileSystemInfo file_system_info_; |
89 }; | 83 }; |
90 | 84 |
91 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) { | 85 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) { |
92 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 86 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
93 CallbackLogger callback_logger; | 87 CallbackLogger callback_logger; |
94 | 88 |
95 CloseFile close_file( | 89 CloseFile close_file(NULL, |
96 NULL, | 90 file_system_info_, |
97 file_system_info_, | 91 kOpenRequestId, |
98 kOpenRequestId, | 92 base::Bind(&CallbackLogger::OnCloseFile, |
99 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 93 base::Unretained(&callback_logger))); |
100 close_file.SetDispatchEventImplForTesting( | 94 close_file.SetDispatchEventImplForTesting( |
101 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 95 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
102 base::Unretained(&dispatcher))); | 96 base::Unretained(&dispatcher))); |
103 | 97 |
104 EXPECT_TRUE(close_file.Execute(kRequestId)); | 98 EXPECT_TRUE(close_file.Execute(kRequestId)); |
105 | 99 |
106 ASSERT_EQ(1u, dispatcher.events().size()); | 100 ASSERT_EQ(1u, dispatcher.events().size()); |
107 extensions::Event* event = dispatcher.events()[0]; | 101 extensions::Event* event = dispatcher.events()[0]; |
108 EXPECT_EQ( | 102 EXPECT_EQ( |
109 extensions::api::file_system_provider::OnCloseFileRequested::kEventName, | 103 extensions::api::file_system_provider::OnCloseFileRequested::kEventName, |
(...skipping 14 matching lines...) Expand all Loading... |
124 | 118 |
125 int event_open_request_id = -1; | 119 int event_open_request_id = -1; |
126 EXPECT_TRUE(options->GetInteger("openRequestId", &event_open_request_id)); | 120 EXPECT_TRUE(options->GetInteger("openRequestId", &event_open_request_id)); |
127 EXPECT_EQ(kOpenRequestId, event_open_request_id); | 121 EXPECT_EQ(kOpenRequestId, event_open_request_id); |
128 } | 122 } |
129 | 123 |
130 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) { | 124 TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) { |
131 LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 125 LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
132 CallbackLogger callback_logger; | 126 CallbackLogger callback_logger; |
133 | 127 |
134 CloseFile close_file( | 128 CloseFile close_file(NULL, |
135 NULL, | 129 file_system_info_, |
136 file_system_info_, | 130 kOpenRequestId, |
137 kOpenRequestId, | 131 base::Bind(&CallbackLogger::OnCloseFile, |
138 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 132 base::Unretained(&callback_logger))); |
139 close_file.SetDispatchEventImplForTesting( | 133 close_file.SetDispatchEventImplForTesting( |
140 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 134 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
141 base::Unretained(&dispatcher))); | 135 base::Unretained(&dispatcher))); |
142 | 136 |
143 EXPECT_FALSE(close_file.Execute(kRequestId)); | 137 EXPECT_FALSE(close_file.Execute(kRequestId)); |
144 } | 138 } |
145 | 139 |
146 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) { | 140 TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) { |
147 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 141 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
148 CallbackLogger callback_logger; | 142 CallbackLogger callback_logger; |
149 | 143 |
150 CloseFile close_file( | 144 CloseFile close_file(NULL, |
151 NULL, | 145 file_system_info_, |
152 file_system_info_, | 146 kOpenRequestId, |
153 kOpenRequestId, | 147 base::Bind(&CallbackLogger::OnCloseFile, |
154 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 148 base::Unretained(&callback_logger))); |
155 close_file.SetDispatchEventImplForTesting( | 149 close_file.SetDispatchEventImplForTesting( |
156 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 150 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
157 base::Unretained(&dispatcher))); | 151 base::Unretained(&dispatcher))); |
158 | 152 |
159 EXPECT_TRUE(close_file.Execute(kRequestId)); | 153 EXPECT_TRUE(close_file.Execute(kRequestId)); |
160 | 154 |
161 close_file.OnSuccess(kRequestId, | 155 close_file.OnSuccess(kRequestId, |
162 scoped_ptr<RequestValue>(new RequestValue()), | 156 scoped_ptr<RequestValue>(new RequestValue()), |
163 false /* has_more */); | 157 false /* has_more */); |
164 ASSERT_EQ(1u, callback_logger.events().size()); | 158 ASSERT_EQ(1u, callback_logger.events().size()); |
165 EXPECT_EQ(base::File::FILE_OK, callback_logger.events()[0]); | 159 EXPECT_EQ(base::File::FILE_OK, callback_logger.events()[0]); |
166 } | 160 } |
167 | 161 |
168 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) { | 162 TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) { |
169 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 163 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
170 CallbackLogger callback_logger; | 164 CallbackLogger callback_logger; |
171 | 165 |
172 CloseFile close_file( | 166 CloseFile close_file(NULL, |
173 NULL, | 167 file_system_info_, |
174 file_system_info_, | 168 kOpenRequestId, |
175 kOpenRequestId, | 169 base::Bind(&CallbackLogger::OnCloseFile, |
176 base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr())); | 170 base::Unretained(&callback_logger))); |
177 close_file.SetDispatchEventImplForTesting( | 171 close_file.SetDispatchEventImplForTesting( |
178 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 172 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
179 base::Unretained(&dispatcher))); | 173 base::Unretained(&dispatcher))); |
180 | 174 |
181 EXPECT_TRUE(close_file.Execute(kRequestId)); | 175 EXPECT_TRUE(close_file.Execute(kRequestId)); |
182 | 176 |
183 close_file.OnError(kRequestId, | 177 close_file.OnError(kRequestId, |
184 scoped_ptr<RequestValue>(new RequestValue()), | 178 scoped_ptr<RequestValue>(new RequestValue()), |
185 base::File::FILE_ERROR_TOO_MANY_OPENED); | 179 base::File::FILE_ERROR_TOO_MANY_OPENED); |
186 ASSERT_EQ(1u, callback_logger.events().size()); | 180 ASSERT_EQ(1u, callback_logger.events().size()); |
187 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, | 181 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, |
188 callback_logger.events()[0]); | 182 callback_logger.events()[0]); |
189 } | 183 } |
190 | 184 |
191 } // namespace operations | 185 } // namespace operations |
192 } // namespace file_system_provider | 186 } // namespace file_system_provider |
193 } // namespace chromeos | 187 } // namespace chromeos |
OLD | NEW |