| 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 "sync/internal_api/public/attachments/attachment_service_proxy.h" | 5 #include "sync/internal_api/public/attachments/attachment_service_proxy.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 CalledOnValidThread(); | 41 CalledOnValidThread(); |
| 42 Increment(); | 42 Increment(); |
| 43 scoped_ptr<AttachmentMap> attachments(new AttachmentMap()); | 43 scoped_ptr<AttachmentMap> attachments(new AttachmentMap()); |
| 44 base::MessageLoop::current()->PostTask( | 44 base::MessageLoop::current()->PostTask( |
| 45 FROM_HERE, | 45 FROM_HERE, |
| 46 base::Bind(callback, | 46 base::Bind(callback, |
| 47 AttachmentService::GET_UNSPECIFIED_ERROR, | 47 AttachmentService::GET_UNSPECIFIED_ERROR, |
| 48 base::Passed(&attachments))); | 48 base::Passed(&attachments))); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void DropAttachments(const AttachmentIdList& attachment_ids, | |
| 52 const DropCallback& callback) override { | |
| 53 CalledOnValidThread(); | |
| 54 Increment(); | |
| 55 base::MessageLoop::current()->PostTask( | |
| 56 FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS)); | |
| 57 } | |
| 58 | |
| 59 void UploadAttachments(const AttachmentIdSet& attachments_ids) override { | 51 void UploadAttachments(const AttachmentIdSet& attachments_ids) override { |
| 60 CalledOnValidThread(); | 52 CalledOnValidThread(); |
| 61 Increment(); | 53 Increment(); |
| 62 } | 54 } |
| 63 | 55 |
| 64 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { | 56 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { |
| 65 return weak_ptr_factory_.GetWeakPtr(); | 57 return weak_ptr_factory_.GetWeakPtr(); |
| 66 } | 58 } |
| 67 | 59 |
| 68 // Return the number of method invocations. | 60 // Return the number of method invocations. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 94 CalledOnValidThread(); | 86 CalledOnValidThread(); |
| 95 stub_thread.reset(new base::Thread("attachment service stub thread")); | 87 stub_thread.reset(new base::Thread("attachment service stub thread")); |
| 96 stub_thread->Start(); | 88 stub_thread->Start(); |
| 97 stub.reset(new StubAttachmentService); | 89 stub.reset(new StubAttachmentService); |
| 98 proxy.reset(new AttachmentServiceProxy(stub_thread->message_loop_proxy(), | 90 proxy.reset(new AttachmentServiceProxy(stub_thread->message_loop_proxy(), |
| 99 stub->AsWeakPtr())); | 91 stub->AsWeakPtr())); |
| 100 | 92 |
| 101 callback_get_or_download = | 93 callback_get_or_download = |
| 102 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, | 94 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, |
| 103 base::Unretained(this)); | 95 base::Unretained(this)); |
| 104 callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop, | |
| 105 base::Unretained(this)); | |
| 106 count_callback_get_or_download = 0; | 96 count_callback_get_or_download = 0; |
| 107 count_callback_drop = 0; | |
| 108 } | 97 } |
| 109 | 98 |
| 110 void TearDown() override { | 99 void TearDown() override { |
| 111 // We must take care to call the stub's destructor on the stub_thread | 100 // We must take care to call the stub's destructor on the stub_thread |
| 112 // because that's the thread to which its WeakPtrs are bound. | 101 // because that's the thread to which its WeakPtrs are bound. |
| 113 if (stub) { | 102 if (stub) { |
| 114 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); | 103 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); |
| 115 WaitForStubThread(); | 104 WaitForStubThread(); |
| 116 } | 105 } |
| 117 stub_thread->Stop(); | 106 stub_thread->Stop(); |
| 118 } | 107 } |
| 119 | 108 |
| 120 // a GetOrDownloadCallback | 109 // a GetOrDownloadCallback |
| 121 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, | 110 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, |
| 122 scoped_ptr<AttachmentMap>) { | 111 scoped_ptr<AttachmentMap>) { |
| 123 CalledOnValidThread(); | 112 CalledOnValidThread(); |
| 124 ++count_callback_get_or_download; | 113 ++count_callback_get_or_download; |
| 125 } | 114 } |
| 126 | 115 |
| 127 // a DropCallback | |
| 128 void IncrementDrop(const AttachmentService::DropResult&) { | |
| 129 CalledOnValidThread(); | |
| 130 ++count_callback_drop; | |
| 131 } | |
| 132 | |
| 133 void WaitForStubThread() { | 116 void WaitForStubThread() { |
| 134 base::WaitableEvent done(false, false); | 117 base::WaitableEvent done(false, false); |
| 135 stub_thread->message_loop()->PostTask( | 118 stub_thread->message_loop()->PostTask( |
| 136 FROM_HERE, | 119 FROM_HERE, |
| 137 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | 120 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
| 138 done.Wait(); | 121 done.Wait(); |
| 139 } | 122 } |
| 140 | 123 |
| 141 base::MessageLoop loop; | 124 base::MessageLoop loop; |
| 142 scoped_ptr<base::Thread> stub_thread; | 125 scoped_ptr<base::Thread> stub_thread; |
| 143 scoped_ptr<StubAttachmentService> stub; | 126 scoped_ptr<StubAttachmentService> stub; |
| 144 scoped_ptr<AttachmentServiceProxy> proxy; | 127 scoped_ptr<AttachmentServiceProxy> proxy; |
| 145 | 128 |
| 146 AttachmentService::GetOrDownloadCallback callback_get_or_download; | 129 AttachmentService::GetOrDownloadCallback callback_get_or_download; |
| 147 AttachmentService::DropCallback callback_drop; | |
| 148 | 130 |
| 149 // number of times callback_get_or_download was invoked | 131 // number of times callback_get_or_download was invoked |
| 150 int count_callback_get_or_download; | 132 int count_callback_get_or_download; |
| 151 // number of times callback_drop was invoked | |
| 152 int count_callback_drop; | |
| 153 }; | 133 }; |
| 154 | 134 |
| 155 TEST_F(AttachmentServiceProxyTest, GetStore) { | 135 TEST_F(AttachmentServiceProxyTest, GetStore) { |
| 156 EXPECT_EQ(NULL, proxy->GetStore()); | 136 EXPECT_EQ(NULL, proxy->GetStore()); |
| 157 } | 137 } |
| 158 | 138 |
| 159 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub. | 139 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub. |
| 160 // Verify that the methods that take callbacks invoke passed callbacks on this | 140 // Verify that the methods that take callbacks invoke passed callbacks on this |
| 161 // thread. | 141 // thread. |
| 162 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { | 142 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { |
| 163 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 143 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
| 164 proxy->DropAttachments(AttachmentIdList(), callback_drop); | |
| 165 proxy->UploadAttachments(AttachmentIdSet()); | 144 proxy->UploadAttachments(AttachmentIdSet()); |
| 166 // Wait for the posted calls to execute in the stub thread. | 145 // Wait for the posted calls to execute in the stub thread. |
| 167 WaitForStubThread(); | 146 WaitForStubThread(); |
| 168 EXPECT_EQ(3, stub->GetCallCount()); | 147 EXPECT_EQ(2, stub->GetCallCount()); |
| 169 // At this point the stub thread has finished executed the calls. However, the | 148 // At this point the stub thread has finished executed the calls. However, the |
| 170 // result callbacks it has posted may not have executed yet. Wait a second | 149 // result callbacks it has posted may not have executed yet. Wait a second |
| 171 // time to ensure the stub thread has executed the posted result callbacks. | 150 // time to ensure the stub thread has executed the posted result callbacks. |
| 172 WaitForStubThread(); | 151 WaitForStubThread(); |
| 173 | 152 |
| 174 base::RunLoop().RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 175 EXPECT_EQ(1, count_callback_get_or_download); | 154 EXPECT_EQ(1, count_callback_get_or_download); |
| 176 EXPECT_EQ(1, count_callback_drop); | |
| 177 } | 155 } |
| 178 | 156 |
| 179 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped | 157 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped |
| 180 // AttachmentService has been destroyed. | 158 // AttachmentService has been destroyed. |
| 181 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { | 159 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { |
| 182 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 160 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
| 183 // Wait for the posted calls to execute in the stub thread. | 161 // Wait for the posted calls to execute in the stub thread. |
| 184 WaitForStubThread(); | 162 WaitForStubThread(); |
| 185 EXPECT_EQ(1, stub->GetCallCount()); | 163 EXPECT_EQ(1, stub->GetCallCount()); |
| 186 // Wait a second time ensure the stub thread has executed the posted result | 164 // Wait a second time ensure the stub thread has executed the posted result |
| (...skipping 10 matching lines...) Expand all Loading... |
| 197 // Now that the wrapped object has been destroyed, call again and see that we | 175 // Now that the wrapped object has been destroyed, call again and see that we |
| 198 // don't crash and the count remains the same. | 176 // don't crash and the count remains the same. |
| 199 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 177 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
| 200 WaitForStubThread(); | 178 WaitForStubThread(); |
| 201 WaitForStubThread(); | 179 WaitForStubThread(); |
| 202 base::RunLoop().RunUntilIdle(); | 180 base::RunLoop().RunUntilIdle(); |
| 203 EXPECT_EQ(1, count_callback_get_or_download); | 181 EXPECT_EQ(1, count_callback_get_or_download); |
| 204 } | 182 } |
| 205 | 183 |
| 206 } // namespace syncer | 184 } // namespace syncer |
| OLD | NEW |