Chromium Code Reviews| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "extensions/browser/mojo/stash_backend.h" | 8 #include "extensions/browser/mojo/stash_backend.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 namespace { | |
| 13 | |
| 14 // Create a data pipe, write some data to the proucer handle and return the | |
|
Anand Mistry (off Chromium)
2015/02/05 11:10:33
proucer
Sam McNally
2015/02/05 23:35:31
Done.
| |
| 15 // consumer handle. | |
| 16 mojo::ScopedHandle CreateReadableHandle() { | |
| 17 mojo::ScopedDataPipeConsumerHandle consumer_handle; | |
| 18 mojo::ScopedDataPipeProducerHandle producer_handle; | |
| 19 MojoCreateDataPipeOptions options = { | |
| 20 sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, 1, | |
| 21 }; | |
| 22 MojoResult result = | |
| 23 mojo::CreateDataPipe(&options, &producer_handle, &consumer_handle); | |
| 24 EXPECT_EQ(MOJO_RESULT_OK, result); | |
| 25 uint32_t num_bytes = 1; | |
| 26 result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes, | |
| 27 MOJO_WRITE_DATA_FLAG_NONE); | |
| 28 EXPECT_EQ(MOJO_RESULT_OK, result); | |
| 29 EXPECT_EQ(1u, num_bytes); | |
| 30 return mojo::ScopedHandle::From(consumer_handle.Pass()); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 12 | 34 |
| 13 class StashServiceTest : public testing::Test, public mojo::ErrorHandler { | 35 class StashServiceTest : public testing::Test, public mojo::ErrorHandler { |
| 14 public: | 36 public: |
| 15 enum Event { | 37 enum Event { |
| 16 EVENT_NONE, | 38 EVENT_NONE, |
| 17 EVENT_STASH_RETRIEVED, | 39 EVENT_STASH_RETRIEVED, |
| 40 EVENT_HANDLE_READY, | |
| 18 }; | 41 }; |
| 19 | 42 |
| 20 StashServiceTest() {} | 43 StashServiceTest() {} |
| 21 | 44 |
| 22 void SetUp() override { | 45 void SetUp() override { |
| 23 expecting_error_ = false; | 46 expecting_error_ = false; |
| 24 expected_event_ = EVENT_NONE; | 47 expected_event_ = EVENT_NONE; |
| 25 stash_backend_.reset(new StashBackend); | 48 stash_backend_.reset(new StashBackend(base::Bind( |
| 49 &StashServiceTest::OnHandleReadyToRead, base::Unretained(this)))); | |
| 26 stash_backend_->BindToRequest(mojo::GetProxy(&stash_service_)); | 50 stash_backend_->BindToRequest(mojo::GetProxy(&stash_service_)); |
| 27 stash_service_.set_error_handler(this); | 51 stash_service_.set_error_handler(this); |
| 52 handles_ready_ = 0; | |
| 28 } | 53 } |
| 29 | 54 |
| 30 void OnConnectionError() override { FAIL() << "Unexpected connection error"; } | 55 void OnConnectionError() override { FAIL() << "Unexpected connection error"; } |
| 31 | 56 |
| 32 mojo::Array<StashedObjectPtr> RetrieveStash() { | 57 mojo::Array<StashedObjectPtr> RetrieveStash() { |
| 33 mojo::Array<StashedObjectPtr> stash; | 58 mojo::Array<StashedObjectPtr> stash; |
| 34 stash_service_->RetrieveStash(base::Bind( | 59 stash_service_->RetrieveStash(base::Bind( |
| 35 &StashServiceTest::StashRetrieved, base::Unretained(this), &stash)); | 60 &StashServiceTest::StashRetrieved, base::Unretained(this), &stash)); |
| 36 WaitForEvent(EVENT_STASH_RETRIEVED); | 61 WaitForEvent(EVENT_STASH_RETRIEVED); |
| 37 return stash.Pass(); | 62 return stash.Pass(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 48 base::RunLoop run_loop; | 73 base::RunLoop run_loop; |
| 49 stop_run_loop_ = run_loop.QuitClosure(); | 74 stop_run_loop_ = run_loop.QuitClosure(); |
| 50 run_loop.Run(); | 75 run_loop.Run(); |
| 51 } | 76 } |
| 52 | 77 |
| 53 void EventReceived(Event event) { | 78 void EventReceived(Event event) { |
| 54 if (event == expected_event_ && !stop_run_loop_.is_null()) | 79 if (event == expected_event_ && !stop_run_loop_.is_null()) |
| 55 stop_run_loop_.Run(); | 80 stop_run_loop_.Run(); |
| 56 } | 81 } |
| 57 | 82 |
| 83 void OnHandleReadyToRead() { | |
| 84 handles_ready_++; | |
| 85 EventReceived(EVENT_HANDLE_READY); | |
| 86 } | |
| 87 | |
| 58 protected: | 88 protected: |
| 59 base::MessageLoop message_loop_; | 89 base::MessageLoop message_loop_; |
| 60 base::Closure stop_run_loop_; | 90 base::Closure stop_run_loop_; |
| 61 scoped_ptr<StashBackend> stash_backend_; | 91 scoped_ptr<StashBackend> stash_backend_; |
| 62 Event expected_event_; | 92 Event expected_event_; |
| 63 bool expecting_error_; | 93 bool expecting_error_; |
| 64 mojo::InterfacePtr<StashService> stash_service_; | 94 mojo::InterfacePtr<StashService> stash_service_; |
| 95 int handles_ready_; | |
| 65 | 96 |
| 66 private: | 97 private: |
| 67 DISALLOW_COPY_AND_ASSIGN(StashServiceTest); | 98 DISALLOW_COPY_AND_ASSIGN(StashServiceTest); |
| 68 }; | 99 }; |
| 69 | 100 |
| 70 // Test that adding stashed objects in multiple calls can all be retrieved by a | 101 // Test that adding stashed objects in multiple calls can all be retrieved by a |
| 71 // Retrieve call. | 102 // Retrieve call. |
| 72 TEST_F(StashServiceTest, AddTwiceAndRetrieve) { | 103 TEST_F(StashServiceTest, AddTwiceAndRetrieve) { |
| 73 mojo::Array<StashedObjectPtr> stashed_objects; | 104 mojo::Array<StashedObjectPtr> stashed_objects; |
| 74 StashedObjectPtr stashed_object(StashedObject::New()); | 105 StashedObjectPtr stashed_object(StashedObject::New()); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 ASSERT_EQ(1u, num_bytes); | 171 ASSERT_EQ(1u, num_bytes); |
| 141 EXPECT_EQ('1', data); | 172 EXPECT_EQ('1', data); |
| 142 } | 173 } |
| 143 | 174 |
| 144 TEST_F(StashServiceTest, RetrieveWithoutStashing) { | 175 TEST_F(StashServiceTest, RetrieveWithoutStashing) { |
| 145 mojo::Array<StashedObjectPtr> stashed_objects = RetrieveStash(); | 176 mojo::Array<StashedObjectPtr> stashed_objects = RetrieveStash(); |
| 146 ASSERT_TRUE(!stashed_objects.is_null()); | 177 ASSERT_TRUE(!stashed_objects.is_null()); |
| 147 EXPECT_EQ(0u, stashed_objects.size()); | 178 EXPECT_EQ(0u, stashed_objects.size()); |
| 148 } | 179 } |
| 149 | 180 |
| 181 TEST_F(StashServiceTest, NotifyOnReadableHandle) { | |
| 182 mojo::Array<StashedObjectPtr> stash_entries; | |
| 183 StashedObjectPtr stashed_object(StashedObject::New()); | |
| 184 stashed_object->id = "test type"; | |
| 185 stashed_object->data.push_back(0); | |
| 186 stashed_object->monitor_handles = true; | |
| 187 mojo::InterfacePtr<StashService> stash_service; | |
|
Anand Mistry (off Chromium)
2015/02/05 11:10:33
stash_service and stash_service_ is confusing. I h
Sam McNally
2015/02/05 23:35:31
Done.
| |
| 188 | |
| 189 stashed_object->stashed_handles.push_back(mojo::ScopedHandle::From( | |
| 190 mojo::GetProxy(&stash_service).PassMessagePipe())); | |
| 191 | |
| 192 stash_entries.push_back(stashed_object.Pass()); | |
| 193 stash_service_->AddToStash(stash_entries.Pass()); | |
| 194 stash_entries.resize(0); | |
| 195 stash_service->AddToStash(stash_entries.Pass()); | |
| 196 WaitForEvent(EVENT_HANDLE_READY); | |
| 197 EXPECT_EQ(1, handles_ready_); | |
| 198 } | |
| 199 | |
| 200 TEST_F(StashServiceTest, NotifyOnReadableDataPipeHandle) { | |
| 201 mojo::Array<StashedObjectPtr> stash_entries; | |
| 202 StashedObjectPtr stashed_object(StashedObject::New()); | |
| 203 stashed_object->id = "test type"; | |
| 204 stashed_object->monitor_handles = true; | |
| 205 | |
| 206 MojoCreateDataPipeOptions options = { | |
| 207 sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, 1, | |
| 208 }; | |
| 209 mojo::ScopedDataPipeConsumerHandle consumer_handle; | |
| 210 mojo::ScopedDataPipeProducerHandle producer_handle; | |
| 211 uint32_t num_bytes = 1; | |
| 212 MojoResult result = | |
| 213 mojo::CreateDataPipe(&options, &producer_handle, &consumer_handle); | |
| 214 ASSERT_EQ(MOJO_RESULT_OK, result); | |
| 215 result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes, | |
| 216 MOJO_WRITE_DATA_FLAG_NONE); | |
| 217 ASSERT_EQ(MOJO_RESULT_OK, result); | |
| 218 ASSERT_EQ(1u, num_bytes); | |
| 219 stashed_object->stashed_handles.push_back( | |
| 220 mojo::ScopedHandle::From(producer_handle.Pass())); | |
| 221 stashed_object->stashed_handles.push_back( | |
| 222 mojo::ScopedHandle::From(consumer_handle.Pass())); | |
| 223 stashed_object->data.push_back(1); | |
| 224 | |
| 225 stash_entries.push_back(stashed_object.Pass()); | |
| 226 stash_service_->AddToStash(stash_entries.Pass()); | |
| 227 WaitForEvent(EVENT_HANDLE_READY); | |
| 228 EXPECT_EQ(1, handles_ready_); | |
| 229 } | |
| 230 | |
| 231 TEST_F(StashServiceTest, NotifyOncePerStashOnReadableHandles) { | |
| 232 mojo::Array<StashedObjectPtr> stash_entries; | |
| 233 StashedObjectPtr stashed_object(StashedObject::New()); | |
| 234 stashed_object->id = "test type"; | |
| 235 stashed_object->data.push_back(1); | |
| 236 stashed_object->monitor_handles = true; | |
| 237 stashed_object->stashed_handles.push_back(CreateReadableHandle()); | |
| 238 stashed_object->stashed_handles.push_back(CreateReadableHandle()); | |
| 239 stash_entries.push_back(stashed_object.Pass()); | |
| 240 stashed_object = StashedObject::New(); | |
| 241 stashed_object->id = "another test type"; | |
| 242 stashed_object->data.push_back(2); | |
| 243 stashed_object->monitor_handles = true; | |
| 244 stashed_object->stashed_handles.push_back(CreateReadableHandle()); | |
| 245 stashed_object->stashed_handles.push_back(CreateReadableHandle()); | |
| 246 stash_entries.push_back(stashed_object.Pass()); | |
| 247 stash_service_->AddToStash(stash_entries.Pass()); | |
| 248 WaitForEvent(EVENT_HANDLE_READY); | |
| 249 EXPECT_EQ(1, handles_ready_); | |
| 250 | |
| 251 stashed_object = StashedObject::New(); | |
| 252 stashed_object->id = "yet another test type"; | |
| 253 stashed_object->data.push_back(3); | |
| 254 stashed_object->monitor_handles = true; | |
| 255 stashed_object->stashed_handles.push_back(CreateReadableHandle()); | |
| 256 stashed_object->stashed_handles.push_back(CreateReadableHandle()); | |
| 257 stash_entries.push_back(stashed_object.Pass()); | |
| 258 stash_service_->AddToStash(stash_entries.Pass()); | |
| 259 | |
| 260 stash_service_->AddToStash(RetrieveStash()); | |
| 261 WaitForEvent(EVENT_HANDLE_READY); | |
| 262 EXPECT_EQ(2, handles_ready_); | |
| 263 } | |
| 264 | |
| 150 // Test that a stash service discards stashed objects when the backend no longer | 265 // Test that a stash service discards stashed objects when the backend no longer |
| 151 // exists. | 266 // exists. |
| 152 TEST_F(StashServiceTest, ServiceWithDeletedBackend) { | 267 TEST_F(StashServiceTest, ServiceWithDeletedBackend) { |
| 153 stash_backend_.reset(); | 268 stash_backend_.reset(); |
| 154 stash_service_.set_error_handler(this); | 269 stash_service_.set_error_handler(this); |
| 155 | 270 |
| 156 mojo::Array<StashedObjectPtr> stashed_objects; | 271 mojo::Array<StashedObjectPtr> stashed_objects; |
| 157 StashedObjectPtr stashed_object(StashedObject::New()); | 272 StashedObjectPtr stashed_object(StashedObject::New()); |
| 158 stashed_object->id = "test type"; | 273 stashed_object->id = "test type"; |
| 159 stashed_object->data.push_back(1); | 274 stashed_object->data.push_back(1); |
| 160 mojo::MessagePipe message_pipe; | 275 mojo::MessagePipe message_pipe; |
| 161 stashed_object->stashed_handles.push_back( | 276 stashed_object->stashed_handles.push_back( |
| 162 mojo::ScopedHandle::From(message_pipe.handle0.Pass())); | 277 mojo::ScopedHandle::From(message_pipe.handle0.Pass())); |
| 163 stashed_objects.push_back(stashed_object.Pass()); | 278 stashed_objects.push_back(stashed_object.Pass()); |
| 164 stash_service_->AddToStash(stashed_objects.Pass()); | 279 stash_service_->AddToStash(stashed_objects.Pass()); |
| 165 stashed_objects = RetrieveStash(); | 280 stashed_objects = RetrieveStash(); |
| 166 ASSERT_EQ(0u, stashed_objects.size()); | 281 ASSERT_EQ(0u, stashed_objects.size()); |
| 167 // Check that the stashed handle has been closed. | 282 // Check that the stashed handle has been closed. |
| 168 MojoResult result = | 283 MojoResult result = |
| 169 mojo::Wait(message_pipe.handle1.get(), | 284 mojo::Wait(message_pipe.handle1.get(), |
| 170 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READABLE, | 285 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READABLE, |
| 171 MOJO_DEADLINE_INDEFINITE, nullptr); | 286 MOJO_DEADLINE_INDEFINITE, nullptr); |
| 172 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); | 287 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 173 } | 288 } |
| 174 | 289 |
| 175 } // namespace extensions | 290 } // namespace extensions |
| OLD | NEW |