| 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 "mojo/application/public/interfaces/service_provider.mojom.h" | 9 #include "mojo/application/public/interfaces/service_provider.mojom.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 uint32_t num_bytes = 1; | 26 uint32_t num_bytes = 1; |
| 27 result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes, | 27 result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes, |
| 28 MOJO_WRITE_DATA_FLAG_NONE); | 28 MOJO_WRITE_DATA_FLAG_NONE); |
| 29 EXPECT_EQ(MOJO_RESULT_OK, result); | 29 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 30 EXPECT_EQ(1u, num_bytes); | 30 EXPECT_EQ(1u, num_bytes); |
| 31 return mojo::ScopedHandle::From(consumer_handle.Pass()); | 31 return mojo::ScopedHandle::From(consumer_handle.Pass()); |
| 32 } | 32 } |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 class StashServiceTest : public testing::Test { | 36 class StashServiceTest : public testing::Test, public mojo::ErrorHandler { |
| 37 public: | 37 public: |
| 38 enum Event { | 38 enum Event { |
| 39 EVENT_NONE, | 39 EVENT_NONE, |
| 40 EVENT_STASH_RETRIEVED, | 40 EVENT_STASH_RETRIEVED, |
| 41 EVENT_HANDLE_READY, | 41 EVENT_HANDLE_READY, |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 StashServiceTest() {} | 44 StashServiceTest() {} |
| 45 | 45 |
| 46 void SetUp() override { | 46 void SetUp() override { |
| 47 expecting_error_ = false; | 47 expecting_error_ = false; |
| 48 expected_event_ = EVENT_NONE; | 48 expected_event_ = EVENT_NONE; |
| 49 stash_backend_.reset(new StashBackend(base::Bind( | 49 stash_backend_.reset(new StashBackend(base::Bind( |
| 50 &StashServiceTest::OnHandleReadyToRead, base::Unretained(this)))); | 50 &StashServiceTest::OnHandleReadyToRead, base::Unretained(this)))); |
| 51 stash_backend_->BindToRequest(mojo::GetProxy(&stash_service_)); | 51 stash_backend_->BindToRequest(mojo::GetProxy(&stash_service_)); |
| 52 stash_service_.set_connection_error_handler(base::Bind(&OnConnectionError)); | 52 stash_service_.set_error_handler(this); |
| 53 handles_ready_ = 0; | 53 handles_ready_ = 0; |
| 54 } | 54 } |
| 55 | 55 |
| 56 static void OnConnectionError() { FAIL() << "Unexpected connection error"; } | 56 void OnConnectionError() override { FAIL() << "Unexpected connection error"; } |
| 57 | 57 |
| 58 mojo::Array<StashedObjectPtr> RetrieveStash() { | 58 mojo::Array<StashedObjectPtr> RetrieveStash() { |
| 59 mojo::Array<StashedObjectPtr> stash; | 59 mojo::Array<StashedObjectPtr> stash; |
| 60 stash_service_->RetrieveStash(base::Bind( | 60 stash_service_->RetrieveStash(base::Bind( |
| 61 &StashServiceTest::StashRetrieved, base::Unretained(this), &stash)); | 61 &StashServiceTest::StashRetrieved, base::Unretained(this), &stash)); |
| 62 WaitForEvent(EVENT_STASH_RETRIEVED); | 62 WaitForEvent(EVENT_STASH_RETRIEVED); |
| 63 return stash.Pass(); | 63 return stash.Pass(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void StashRetrieved(mojo::Array<StashedObjectPtr>* output, | 66 void StashRetrieved(mojo::Array<StashedObjectPtr>* output, |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 | 264 |
| 265 stash_service_->AddToStash(RetrieveStash()); | 265 stash_service_->AddToStash(RetrieveStash()); |
| 266 WaitForEvent(EVENT_HANDLE_READY); | 266 WaitForEvent(EVENT_HANDLE_READY); |
| 267 EXPECT_EQ(2, handles_ready_); | 267 EXPECT_EQ(2, handles_ready_); |
| 268 } | 268 } |
| 269 | 269 |
| 270 // Test that a stash service discards stashed objects when the backend no longer | 270 // Test that a stash service discards stashed objects when the backend no longer |
| 271 // exists. | 271 // exists. |
| 272 TEST_F(StashServiceTest, ServiceWithDeletedBackend) { | 272 TEST_F(StashServiceTest, ServiceWithDeletedBackend) { |
| 273 stash_backend_.reset(); | 273 stash_backend_.reset(); |
| 274 stash_service_.set_connection_error_handler(base::Bind(&OnConnectionError)); | 274 stash_service_.set_error_handler(this); |
| 275 | 275 |
| 276 mojo::Array<StashedObjectPtr> stashed_objects; | 276 mojo::Array<StashedObjectPtr> stashed_objects; |
| 277 StashedObjectPtr stashed_object(StashedObject::New()); | 277 StashedObjectPtr stashed_object(StashedObject::New()); |
| 278 stashed_object->id = "test type"; | 278 stashed_object->id = "test type"; |
| 279 stashed_object->data.push_back(1); | 279 stashed_object->data.push_back(1); |
| 280 mojo::MessagePipe message_pipe; | 280 mojo::MessagePipe message_pipe; |
| 281 stashed_object->stashed_handles.push_back( | 281 stashed_object->stashed_handles.push_back( |
| 282 mojo::ScopedHandle::From(message_pipe.handle0.Pass())); | 282 mojo::ScopedHandle::From(message_pipe.handle0.Pass())); |
| 283 stashed_objects.push_back(stashed_object.Pass()); | 283 stashed_objects.push_back(stashed_object.Pass()); |
| 284 stash_service_->AddToStash(stashed_objects.Pass()); | 284 stash_service_->AddToStash(stashed_objects.Pass()); |
| 285 stashed_objects = RetrieveStash(); | 285 stashed_objects = RetrieveStash(); |
| 286 ASSERT_EQ(0u, stashed_objects.size()); | 286 ASSERT_EQ(0u, stashed_objects.size()); |
| 287 // Check that the stashed handle has been closed. | 287 // Check that the stashed handle has been closed. |
| 288 MojoResult result = | 288 MojoResult result = |
| 289 mojo::Wait(message_pipe.handle1.get(), | 289 mojo::Wait(message_pipe.handle1.get(), |
| 290 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READABLE, | 290 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READABLE, |
| 291 MOJO_DEADLINE_INDEFINITE, nullptr); | 291 MOJO_DEADLINE_INDEFINITE, nullptr); |
| 292 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); | 292 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 293 } | 293 } |
| 294 | 294 |
| 295 } // namespace extensions | 295 } // namespace extensions |
| OLD | NEW |