Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "base/bind.h" | |
| 6 #include "base/memory/ref_counted.h" | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "content/child/child_process.h" | |
| 10 #include "content/renderer/media/video_capture_impl_manager.h" | |
| 11 #include "content/renderer/media/video_capture_message_filter.h" | |
| 12 #include "media/base/bind_to_loop.h" | |
| 13 #include "media/video/capture/mock_video_capture_event_handler.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using ::testing::_; | |
| 18 using ::testing::DoAll; | |
| 19 using ::testing::SaveArg; | |
| 20 using media::BindToCurrentLoop; | |
| 21 using media::MockVideoCaptureEventHandler; | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 ACTION_P(RunClosure, closure) { | |
| 26 closure.Run(); | |
| 27 } | |
| 28 | |
| 29 class VideoCaptureImplManagerTest : public ::testing::Test { | |
| 30 public: | |
| 31 VideoCaptureImplManagerTest() { | |
| 32 params_.requested_format = media::VideoCaptureFormat( | |
| 33 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); | |
| 34 child_process_.reset(new ChildProcess()); | |
| 35 } | |
| 36 | |
| 37 void FakeChannelSetup() { | |
| 38 scoped_refptr<base::MessageLoopProxy> loop = | |
| 39 child_process_->io_message_loop_proxy(); | |
| 40 if (!loop->BelongsToCurrentThread()) { | |
| 41 loop->PostTask( | |
| 42 FROM_HERE, | |
| 43 base::Bind( | |
| 44 &VideoCaptureImplManagerTest::FakeChannelSetup, | |
| 45 base::Unretained(this))); | |
| 46 return; | |
| 47 } | |
| 48 manager_.video_capture_message_filter()->OnFilterAdded(NULL); | |
| 49 } | |
| 50 | |
| 51 void Quit(base::RunLoop* run_loop) { | |
| 52 message_loop_.PostTask(FROM_HERE, run_loop->QuitClosure()); | |
| 53 } | |
| 54 | |
| 55 protected: | |
| 56 base::MessageLoop message_loop_; | |
| 57 scoped_ptr<ChildProcess> child_process_; | |
| 58 media::VideoCaptureParams params_; | |
| 59 VideoCaptureImplManager manager_; | |
| 60 | |
| 61 private: | |
| 62 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); | |
| 63 }; | |
| 64 | |
| 65 // Multiple clients with the same session id. There is only one | |
| 66 // media::VideoCapture object. | |
| 67 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { | |
| 68 scoped_ptr<MockVideoCaptureEventHandler> client1( | |
| 69 new MockVideoCaptureEventHandler); | |
| 70 scoped_ptr<MockVideoCaptureEventHandler> client2( | |
| 71 new MockVideoCaptureEventHandler); | |
| 72 | |
| 73 media::VideoCapture* device1 = NULL; | |
| 74 media::VideoCapture* device2 = NULL; | |
| 75 | |
| 76 scoped_ptr<VideoCaptureHandle> handle1; | |
| 77 scoped_ptr<VideoCaptureHandle> handle2; | |
| 78 { | |
| 79 base::RunLoop run_loop; | |
| 80 base::Closure quit_closure = BindToCurrentLoop( | |
| 81 run_loop.QuitClosure()); | |
| 82 | |
| 83 EXPECT_CALL(*client1, OnStarted(_)).WillOnce(SaveArg<0>(&device1)); | |
| 84 EXPECT_CALL(*client2, OnStarted(_)).WillOnce( | |
| 85 DoAll( | |
| 86 SaveArg<0>(&device2), | |
| 87 RunClosure(quit_closure))); | |
| 88 handle1 = manager_.UseDevice(1); | |
| 89 handle2 = manager_.UseDevice(1); | |
| 90 handle1->StartCapture(client1.get(), params_); | |
| 91 handle2->StartCapture(client2.get(), params_); | |
| 92 FakeChannelSetup(); | |
| 93 run_loop.Run(); | |
| 94 } | |
| 95 | |
| 96 { | |
| 97 base::RunLoop run_loop; | |
| 98 base::Closure quit_closure = BindToCurrentLoop( | |
| 99 run_loop.QuitClosure()); | |
| 100 | |
| 101 EXPECT_CALL(*client1, OnStopped(_)); | |
| 102 EXPECT_CALL(*client1, OnRemoved(_)); | |
| 103 EXPECT_CALL(*client2, OnStopped(_)); | |
| 104 EXPECT_CALL(*client2, OnRemoved(_)).WillOnce( | |
| 105 RunClosure(quit_closure)); | |
| 106 handle1->StopCapture(client1.get()); | |
| 107 handle2->StopCapture(client2.get()); | |
| 108 run_loop.Run(); | |
| 109 } | |
| 110 | |
| 111 EXPECT_TRUE(device1 == device2); | |
| 112 } | |
| 113 | |
| 114 // Two clients at different time. There are two media::VideoCapture | |
| 115 // objects created. | |
| 116 TEST_F(VideoCaptureImplManagerTest, ConsecutiveClients) { | |
| 117 scoped_ptr<MockVideoCaptureEventHandler> client1( | |
| 118 new MockVideoCaptureEventHandler); | |
| 119 scoped_ptr<MockVideoCaptureEventHandler> client2( | |
| 120 new MockVideoCaptureEventHandler); | |
| 121 | |
| 122 media::VideoCapture* device1 = NULL; | |
| 123 media::VideoCapture* device2 = NULL; | |
| 124 | |
| 125 // Start and stop the device for |handle1|. | |
| 126 scoped_ptr<VideoCaptureHandle> handle1 = manager_.UseDevice(1); | |
| 127 { | |
| 128 base::RunLoop run_loop; | |
| 129 base::Closure quit_closure = BindToCurrentLoop( | |
| 130 run_loop.QuitClosure()); | |
| 131 | |
| 132 EXPECT_CALL(*client1, OnStarted(_)).WillOnce( | |
| 133 DoAll( | |
| 134 SaveArg<0>(&device1), | |
| 135 RunClosure(quit_closure))); | |
| 136 handle1->StartCapture(client1.get(), params_); | |
| 137 FakeChannelSetup(); | |
| 138 run_loop.Run(); | |
| 139 } | |
| 140 | |
| 141 { | |
| 142 base::RunLoop run_loop; | |
| 143 base::Closure quit_closure = BindToCurrentLoop( | |
| 144 run_loop.QuitClosure()); | |
| 145 | |
| 146 EXPECT_CALL(*client1, OnStopped(_)); | |
| 147 EXPECT_CALL(*client1, OnRemoved(_)).WillOnce( | |
| 148 RunClosure(quit_closure)); | |
| 149 handle1->StopCapture(client1.get()); | |
| 150 run_loop.Run(); | |
| 151 } | |
| 152 | |
| 153 // This will cause the media::VideoCapture object to be destroyed. | |
| 154 { | |
| 155 base::RunLoop run_loop; | |
| 156 handle1.reset(); | |
| 157 run_loop.RunUntilIdle(); | |
| 158 } | |
| 159 | |
| 160 // Start and stop the device for |handle2| with the same session id. | |
| 161 scoped_ptr<VideoCaptureHandle> handle2 = manager_.UseDevice(1); | |
| 162 { | |
| 163 base::RunLoop run_loop; | |
| 164 base::Closure quit_closure = BindToCurrentLoop( | |
| 165 run_loop.QuitClosure()); | |
| 166 | |
| 167 EXPECT_CALL(*client2, OnStarted(_)).WillOnce( | |
| 168 DoAll( | |
| 169 SaveArg<0>(&device2), | |
| 170 RunClosure(quit_closure))); | |
| 171 handle2->StartCapture(client2.get(), params_); | |
| 172 FakeChannelSetup(); | |
| 173 run_loop.Run(); | |
| 174 } | |
| 175 | |
| 176 { | |
| 177 base::RunLoop run_loop; | |
| 178 base::Closure quit_closure = BindToCurrentLoop( | |
| 179 run_loop.QuitClosure()); | |
| 180 | |
| 181 EXPECT_CALL(*client2, OnStopped(_)); | |
| 182 EXPECT_CALL(*client2, OnRemoved(_)).WillOnce( | |
| 183 RunClosure(quit_closure)); | |
| 184 handle2->StopCapture(client2.get()); | |
| 185 run_loop.Run(); | |
| 186 } | |
| 187 handle2.reset(); | |
| 188 | |
| 189 EXPECT_FALSE(device1 == device2); | |
|
Ami GONE FROM CHROMIUM
2014/01/06 23:37:14
How do you guarantee that operator new() doesn't r
Alpha Left Google
2014/01/08 00:23:36
This test verifies that VidoeCaptureImpl is destro
| |
| 190 } | |
| 191 | |
| 192 } // namespace content | |
| OLD | NEW |