OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/public/cpp/application/application_impl.h" |
| 6 #include "mojo/public/cpp/application/application_test_base.h" |
| 7 #include "mojo/public/cpp/environment/logging.h" |
| 8 #include "mojo/public/cpp/system/macros.h" |
| 9 #include "mojo/public/cpp/utility/run_loop.h" |
| 10 #include "mojo/services/gfx/images/cpp/image_pipe_consumer_endpoint.h" |
| 11 #include "mojo/services/gfx/images/cpp/image_pipe_producer_endpoint.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace { |
| 15 |
| 16 using namespace image_pipe; |
| 17 |
| 18 class ImagePipeApplicationTest : public test::ApplicationTestBase { |
| 19 public: |
| 20 ImagePipeApplicationTest() : ApplicationTestBase() {} |
| 21 ~ImagePipeApplicationTest() override {} |
| 22 |
| 23 private: |
| 24 MOJO_DISALLOW_COPY_AND_ASSIGN(ImagePipeApplicationTest); |
| 25 }; |
| 26 |
| 27 class TestImagePipe : private ImagePipeConsumerDelegate { |
| 28 public: |
| 29 TestImagePipe(bool producer_checked, bool consumer_checked); |
| 30 ~TestImagePipe() override; |
| 31 mojo::gfx::ImagePtr CreateTestImage(); |
| 32 |
| 33 ImagePipeProducerEndpoint* producer_endpoint; |
| 34 ImagePipeConsumerEndpoint* consumer_endpoint; |
| 35 mojo::gfx::SupportedImagePropertiesPtr supported_properties_; |
| 36 |
| 37 bool HasExperiencedError() { return has_error_; } |
| 38 |
| 39 std::function<void(mojo::gfx::ImagePtr image, uint32_t id)> |
| 40 add_image_override; |
| 41 std::function<void(uint32_t id)> remove_image_override; |
| 42 std::function<void(uint32_t id)> present_image_override; |
| 43 |
| 44 private: |
| 45 // Inherited from ImagePipeConsumerDelegate // |
| 46 void AddImage(mojo::gfx::ImagePtr image, uint32_t id) override; |
| 47 void RemoveImage(uint32_t id) override; |
| 48 void PresentImage(uint32_t id) override; |
| 49 void HandleEndpointClosed() override { |
| 50 has_error_ = true; |
| 51 mojo::RunLoop::current()->Quit(); |
| 52 } |
| 53 bool has_error_; |
| 54 }; |
| 55 |
| 56 void TestImagePipe::AddImage(mojo::gfx::ImagePtr image, uint32_t id) { |
| 57 add_image_override(image.Pass(), id); |
| 58 } |
| 59 void TestImagePipe::RemoveImage(uint32_t id) { |
| 60 remove_image_override(id); |
| 61 } |
| 62 void TestImagePipe::PresentImage(uint32_t id) { |
| 63 present_image_override(id); |
| 64 } |
| 65 |
| 66 TestImagePipe::TestImagePipe(bool producer_checked, bool consumer_checked) { |
| 67 present_image_override = [](uint32_t id) {}; |
| 68 remove_image_override = [](uint32_t id) {}; |
| 69 add_image_override = [](mojo::gfx::ImagePtr image, uint32_t id) {}; |
| 70 |
| 71 has_error_ = false; |
| 72 supported_properties_ = mojo::gfx::SupportedImageProperties::New(); |
| 73 supported_properties_->size = mojo::Size::New(); |
| 74 supported_properties_->size->width = 256; |
| 75 supported_properties_->size->height = 256; |
| 76 |
| 77 supported_properties_->formats = |
| 78 mojo::Array<mojo::gfx::ColorFormatPtr>::New(0); |
| 79 mojo::gfx::ColorFormatPtr format = mojo::gfx::ColorFormat::New(); |
| 80 format->layout = mojo::gfx::PixelLayout::BGRA_8888; |
| 81 format->color_space = mojo::gfx::ColorSpace::SRGB; |
| 82 supported_properties_->formats.push_back(format.Pass()); |
| 83 |
| 84 mojo::gfx::ImagePipePtr image_pipe_ptr; |
| 85 consumer_endpoint = new ImagePipeConsumerEndpoint( |
| 86 GetProxy(&image_pipe_ptr), supported_properties_.Clone(), this, |
| 87 consumer_checked); |
| 88 |
| 89 producer_endpoint = |
| 90 new ImagePipeProducerEndpoint(image_pipe_ptr.Pass(), [this] { |
| 91 has_error_ = true; |
| 92 mojo::RunLoop::current()->Quit(); |
| 93 }, producer_checked); |
| 94 } |
| 95 |
| 96 TestImagePipe::~TestImagePipe() { |
| 97 delete producer_endpoint; |
| 98 delete consumer_endpoint; |
| 99 } |
| 100 |
| 101 mojo::gfx::ImagePtr TestImagePipe::CreateTestImage() { |
| 102 mojo::MessagePipe pipe; |
| 103 |
| 104 uint32_t bytes_per_pixel = 32; |
| 105 |
| 106 mojo::gfx::ImageBufferPtr image_buffer = mojo::gfx::ImageBuffer::New(); |
| 107 image_buffer->size = supported_properties_->size->width * |
| 108 supported_properties_->size->width * bytes_per_pixel; |
| 109 image_buffer->data = mojo::ScopedHandle(pipe.handle0.Pass()); |
| 110 |
| 111 mojo::gfx::ImagePtr image = mojo::gfx::Image::New(); |
| 112 image->buffer = image_buffer.Pass(); |
| 113 image->format = supported_properties_->formats[0].Clone(); |
| 114 image->size = supported_properties_->size.Clone(); |
| 115 image->pitch = supported_properties_->size->width; |
| 116 image->stride = image->pitch * bytes_per_pixel; |
| 117 |
| 118 return image; |
| 119 } |
| 120 |
| 121 // Tests that the usual flow for creating, adding, presenting, and removing |
| 122 // an image doesnt crash/break/cause-errors etc |
| 123 TEST_F(ImagePipeApplicationTest, NormalImageLifeCycle) { |
| 124 TestImagePipe image_pipe(true, true); |
| 125 |
| 126 image_pipe.present_image_override = [&image_pipe](uint32_t id) { |
| 127 uint32_t acquired_id; |
| 128 MOJO_CHECK(image_pipe.consumer_endpoint->AcquireNextImage(acquired_id)); |
| 129 MOJO_CHECK(acquired_id == id); |
| 130 image_pipe.consumer_endpoint->ReleaseImage( |
| 131 id, mojo::gfx::PresentationStatus::PRESENTED); |
| 132 }; |
| 133 |
| 134 uint32_t id = 0, acquired_id = UINT32_MAX; |
| 135 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 136 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 137 MOJO_CHECK(acquired_id == id); |
| 138 image_pipe.producer_endpoint->PresentImage( |
| 139 id, [&image_pipe](unsigned int id, mojo::gfx::PresentationStatus status) { |
| 140 EXPECT_EQ(mojo::gfx::PresentationStatus::PRESENTED, status); |
| 141 mojo::RunLoop::current()->Quit(); |
| 142 }); |
| 143 |
| 144 mojo::RunLoop::current()->Run(); |
| 145 acquired_id = UINT32_MAX; |
| 146 EXPECT_TRUE(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 147 MOJO_CHECK(acquired_id == id); |
| 148 image_pipe.producer_endpoint->RemoveImage(acquired_id); |
| 149 |
| 150 EXPECT_FALSE(image_pipe.HasExperiencedError()); |
| 151 } |
| 152 |
| 153 // Tests that flushing returns images to the producer with NOT_PRESENTED_FLUSHED |
| 154 TEST_F(ImagePipeApplicationTest, FlushImages) { |
| 155 TestImagePipe image_pipe(true, true); |
| 156 |
| 157 uint32_t id = 0, acquired_id = UINT32_MAX; |
| 158 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 159 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 160 MOJO_CHECK(acquired_id == id); |
| 161 image_pipe.producer_endpoint->PresentImage( |
| 162 id, |
| 163 [&id](unsigned int presented_id, mojo::gfx::PresentationStatus status) { |
| 164 EXPECT_EQ(mojo::gfx::PresentationStatus::NOT_PRESENTED_FLUSHED, status); |
| 165 EXPECT_TRUE(presented_id == id); |
| 166 mojo::RunLoop::current()->Quit(); |
| 167 }); |
| 168 image_pipe.producer_endpoint->FlushImages(); |
| 169 |
| 170 mojo::RunLoop::current()->Run(); |
| 171 acquired_id = UINT32_MAX; |
| 172 EXPECT_TRUE(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 173 EXPECT_TRUE(acquired_id == id); |
| 174 |
| 175 EXPECT_FALSE(image_pipe.HasExperiencedError()); |
| 176 } |
| 177 |
| 178 // Tests that you can safely try to acquire an image when none are available, |
| 179 // and that you will safely fail |
| 180 TEST_F(ImagePipeApplicationTest, AcquireImageFromEmptyPool) { |
| 181 TestImagePipe image_pipe(true, true); |
| 182 |
| 183 uint32_t id = 0xDEADBEEF, acquired_id = id; |
| 184 EXPECT_FALSE(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 185 EXPECT_TRUE(acquired_id == id); |
| 186 EXPECT_FALSE(image_pipe.HasExperiencedError()); |
| 187 } |
| 188 |
| 189 // Tests that adding an image with an existing ID causes the pipe to error |
| 190 TEST_F(ImagePipeApplicationTest, ProducerError_AddImageWithReusedID) { |
| 191 TestImagePipe image_pipe(false, true); |
| 192 |
| 193 uint32_t id = 0; |
| 194 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 195 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 196 |
| 197 mojo::RunLoop::current()->Run(); |
| 198 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 199 } |
| 200 |
| 201 // Tests that removing an image that hasnt been added causes the pipe to error |
| 202 TEST_F(ImagePipeApplicationTest, ProducerError_RemoveImageBeforeAdded) { |
| 203 TestImagePipe image_pipe(false, true); |
| 204 |
| 205 uint32_t id = 0; |
| 206 image_pipe.producer_endpoint->RemoveImage(id); |
| 207 |
| 208 mojo::RunLoop::current()->Run(); |
| 209 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 210 } |
| 211 |
| 212 // Tests that removing an image that has already been removed causes the pipe to |
| 213 // error (essentially that removing and image takes it out of the pool) |
| 214 TEST_F(ImagePipeApplicationTest, ProducerError_AddImageThenRemoveTwice) { |
| 215 TestImagePipe image_pipe(false, true); |
| 216 |
| 217 uint32_t id = 0; |
| 218 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 219 image_pipe.producer_endpoint->RemoveImage(id); |
| 220 image_pipe.producer_endpoint->RemoveImage(id); |
| 221 |
| 222 mojo::RunLoop::current()->Run(); |
| 223 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 224 } |
| 225 |
| 226 // Tests that removing an image owned by the consumer causes the pipe to error |
| 227 TEST_F(ImagePipeApplicationTest, ProducerError_RemoveImageOwnedByConsumer) { |
| 228 TestImagePipe image_pipe(false, true); |
| 229 |
| 230 uint32_t id = 0, acquired_id = UINT32_MAX; |
| 231 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 232 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 233 MOJO_CHECK(acquired_id == id); |
| 234 image_pipe.producer_endpoint->PresentImage( |
| 235 id, [](unsigned int, mojo::gfx::PresentationStatus) {}); |
| 236 image_pipe.producer_endpoint->RemoveImage(id); |
| 237 |
| 238 mojo::RunLoop::current()->Run(); |
| 239 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 240 } |
| 241 |
| 242 // Tests that presenting an image that hasnt been added causes the pipe to error |
| 243 TEST_F(ImagePipeApplicationTest, ProducerError_PresentImageNotAdded) { |
| 244 TestImagePipe image_pipe(false, true); |
| 245 |
| 246 uint32_t id = 0; |
| 247 image_pipe.producer_endpoint->PresentImage( |
| 248 id, [](unsigned int, mojo::gfx::PresentationStatus) {}); |
| 249 |
| 250 mojo::RunLoop::current()->Run(); |
| 251 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 252 } |
| 253 |
| 254 // Tests that presenting an image that has already been presented causes the |
| 255 // pipe to error |
| 256 TEST_F(ImagePipeApplicationTest, ProducerError_PresentImageTwice) { |
| 257 TestImagePipe image_pipe(false, true); |
| 258 |
| 259 uint32_t id = 0, acquired_id = UINT32_MAX; |
| 260 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 261 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 262 MOJO_CHECK(acquired_id == id); |
| 263 image_pipe.producer_endpoint->PresentImage( |
| 264 id, [](unsigned int, mojo::gfx::PresentationStatus) {}); |
| 265 image_pipe.producer_endpoint->PresentImage( |
| 266 id, [](unsigned int, mojo::gfx::PresentationStatus) {}); |
| 267 |
| 268 mojo::RunLoop::current()->Run(); |
| 269 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 270 } |
| 271 |
| 272 // Tests that releasing an image that hasnt been added causes the pipe to error |
| 273 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseImageNotInPool) { |
| 274 TestImagePipe image_pipe(true, false); |
| 275 |
| 276 image_pipe.present_image_override = [&image_pipe](uint32_t id) { |
| 277 image_pipe.consumer_endpoint->ReleaseImage( |
| 278 id + 1, mojo::gfx::PresentationStatus::PRESENTED); |
| 279 }; |
| 280 |
| 281 uint32_t id = 0, acquired_id = UINT32_MAX; |
| 282 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 283 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 284 MOJO_CHECK(acquired_id == id); |
| 285 image_pipe.producer_endpoint->PresentImage( |
| 286 id, [](unsigned int, mojo::gfx::PresentationStatus) {}); |
| 287 |
| 288 mojo::RunLoop::current()->Run(); |
| 289 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 290 } |
| 291 |
| 292 // Tests that releasing an image before acquiring it causes the pipe to error |
| 293 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseBeforeAcquire) { |
| 294 TestImagePipe image_pipe(true, false); |
| 295 |
| 296 image_pipe.present_image_override = [&image_pipe](uint32_t id) { |
| 297 image_pipe.consumer_endpoint->ReleaseImage( |
| 298 id, mojo::gfx::PresentationStatus::PRESENTED); |
| 299 }; |
| 300 |
| 301 uint32_t id = 0, acquired_id = UINT32_MAX; |
| 302 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 303 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 304 MOJO_CHECK(acquired_id == id); |
| 305 image_pipe.producer_endpoint->PresentImage( |
| 306 id, [](unsigned int, mojo::gfx::PresentationStatus) {}); |
| 307 |
| 308 mojo::RunLoop::current()->Run(); |
| 309 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 310 } |
| 311 |
| 312 // Tests that releasing an image before its presented causes the pipe to error |
| 313 TEST_F(ImagePipeApplicationTest, ConsumerError_ReleaseImageNotPresented) { |
| 314 TestImagePipe image_pipe(true, false); |
| 315 |
| 316 image_pipe.present_image_override = [&image_pipe](uint32_t id) { |
| 317 uint32_t acquired_id; |
| 318 MOJO_CHECK(image_pipe.consumer_endpoint->AcquireNextImage(acquired_id)); |
| 319 MOJO_CHECK(acquired_id == id); |
| 320 image_pipe.consumer_endpoint->ReleaseImage( |
| 321 id + 1, mojo::gfx::PresentationStatus::PRESENTED); |
| 322 }; |
| 323 |
| 324 uint32_t id = 0, acquired_id = UINT32_MAX; |
| 325 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id); |
| 326 image_pipe.producer_endpoint->AddImage(image_pipe.CreateTestImage(), id + 1); |
| 327 MOJO_CHECK(image_pipe.producer_endpoint->AcquireImage(acquired_id)); |
| 328 MOJO_CHECK(acquired_id == id); |
| 329 image_pipe.producer_endpoint->PresentImage( |
| 330 id, [](unsigned int, mojo::gfx::PresentationStatus) {}); |
| 331 |
| 332 mojo::RunLoop::current()->Run(); |
| 333 EXPECT_TRUE(image_pipe.HasExperiencedError()); |
| 334 } |
| 335 |
| 336 } // namespace image_pipe |
| 337 } // namespace mojo |
OLD | NEW |