| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | //TESTED_COMPONENT=src/multimedia |
| 30 | |
| 31 | #include <QtTest/QtTest> |
| 32 | #include <QDebug> |
| 33 | |
| 34 | #include <qabstractvideosurface.h> |
| 35 | #include <qcameracontrol.h> |
| 36 | #include <qcameralockscontrol.h> |
| 37 | #include <qcameraexposurecontrol.h> |
| 38 | #include <qcameraflashcontrol.h> |
| 39 | #include <qcamerafocuscontrol.h> |
| 40 | #include <qcameraimagecapturecontrol.h> |
| 41 | #include <qimageencodercontrol.h> |
| 42 | #include <qcameraimageprocessingcontrol.h> |
| 43 | #include <qcameracapturebufferformatcontrol.h> |
| 44 | #include <qcameracapturedestinationcontrol.h> |
| 45 | #include <qmediaservice.h> |
| 46 | #include <qcamera.h> |
| 47 | #include <qcameraimagecapture.h> |
| 48 | #include <qgraphicsvideoitem.h> |
| 49 | #include <qvideorenderercontrol.h> |
| 50 | #include <qvideowidget.h> |
| 51 | #include <qvideowindowcontrol.h> |
| 52 | |
| 53 | #include "mockcameraservice.h" |
| 54 | |
| 55 | #include "mockmediaserviceprovider.h" |
| 56 | #include "mockvideosurface.h" |
| 57 | #include "mockvideorenderercontrol.h" |
| 58 | #include "mockvideowindowcontrol.h" |
| 59 | |
| 60 | QT_USE_NAMESPACE |
| 61 | |
| 62 | |
| 63 | class tst_QCameraWidgets: public QObject |
| 64 | { |
| 65 | Q_OBJECT |
| 66 | |
| 67 | public slots: |
| 68 | void initTestCase(); |
| 69 | void init(); |
| 70 | void cleanup(); |
| 71 | void cleanupTestCase(); |
| 72 | |
| 73 | private slots: |
| 74 | void testCameraEncodingProperyChange(); |
| 75 | void testSetVideoOutput(); |
| 76 | void testSetVideoOutputNoService(); |
| 77 | void testSetVideoOutputNoControl(); |
| 78 | |
| 79 | private: |
| 80 | MockCameraService *mockCameraService; |
| 81 | MockMediaServiceProvider *provider; |
| 82 | }; |
| 83 | |
| 84 | void tst_QCameraWidgets::initTestCase() |
| 85 | { |
| 86 | provider = new MockMediaServiceProvider; |
| 87 | QMediaServiceProvider::setDefaultServiceProvider(provider); |
| 88 | } |
| 89 | |
| 90 | void tst_QCameraWidgets::init() |
| 91 | { |
| 92 | mockCameraService = new MockCameraService; |
| 93 | provider->service = mockCameraService; |
| 94 | } |
| 95 | |
| 96 | void tst_QCameraWidgets::cleanup() |
| 97 | { |
| 98 | delete mockCameraService; |
| 99 | provider->service = 0; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void tst_QCameraWidgets::cleanupTestCase() |
| 104 | { |
| 105 | delete provider; |
| 106 | } |
| 107 | |
| 108 | void tst_QCameraWidgets::testCameraEncodingProperyChange() |
| 109 | { |
| 110 | QCamera camera; |
| 111 | QCameraImageCapture imageCapture(&camera); |
| 112 | |
| 113 | QSignalSpy stateChangedSignal(&camera, SIGNAL(stateChanged(QCamera::State))); |
| 114 | QSignalSpy statusChangedSignal(&camera, SIGNAL(statusChanged(QCamera::Status))); |
| 115 | |
| 116 | camera.start(); |
| 117 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 118 | QCOMPARE(camera.status(), QCamera::ActiveStatus); |
| 119 | |
| 120 | QCOMPARE(stateChangedSignal.count(), 1); |
| 121 | QCOMPARE(statusChangedSignal.count(), 1); |
| 122 | stateChangedSignal.clear(); |
| 123 | statusChangedSignal.clear(); |
| 124 | |
| 125 | |
| 126 | camera.setCaptureMode(QCamera::CaptureVideo); |
| 127 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 128 | QCOMPARE(camera.status(), QCamera::LoadedStatus); |
| 129 | |
| 130 | QCOMPARE(stateChangedSignal.count(), 0); |
| 131 | QCOMPARE(statusChangedSignal.count(), 1); |
| 132 | stateChangedSignal.clear(); |
| 133 | statusChangedSignal.clear(); |
| 134 | |
| 135 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 136 | QTRY_COMPARE(camera.status(), QCamera::ActiveStatus); |
| 137 | |
| 138 | QCOMPARE(stateChangedSignal.count(), 0); |
| 139 | QCOMPARE(statusChangedSignal.count(), 1); |
| 140 | stateChangedSignal.clear(); |
| 141 | statusChangedSignal.clear(); |
| 142 | |
| 143 | //backens should not be stopped since the capture mode is Video |
| 144 | imageCapture.setEncodingSettings(QImageEncoderSettings()); |
| 145 | QCOMPARE(stateChangedSignal.count(), 0); |
| 146 | QCOMPARE(statusChangedSignal.count(), 0); |
| 147 | |
| 148 | camera.setCaptureMode(QCamera::CaptureStillImage); |
| 149 | QTRY_COMPARE(camera.status(), QCamera::ActiveStatus); |
| 150 | stateChangedSignal.clear(); |
| 151 | statusChangedSignal.clear(); |
| 152 | |
| 153 | //the settings change should trigger camera stop/start |
| 154 | imageCapture.setEncodingSettings(QImageEncoderSettings()); |
| 155 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 156 | QCOMPARE(camera.status(), QCamera::LoadedStatus); |
| 157 | |
| 158 | QCOMPARE(stateChangedSignal.count(), 0); |
| 159 | QCOMPARE(statusChangedSignal.count(), 1); |
| 160 | stateChangedSignal.clear(); |
| 161 | statusChangedSignal.clear(); |
| 162 | |
| 163 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 164 | QTRY_COMPARE(camera.status(), QCamera::ActiveStatus); |
| 165 | |
| 166 | QCOMPARE(stateChangedSignal.count(), 0); |
| 167 | QCOMPARE(statusChangedSignal.count(), 1); |
| 168 | stateChangedSignal.clear(); |
| 169 | statusChangedSignal.clear(); |
| 170 | |
| 171 | //the settings change should trigger camera stop/start only once |
| 172 | camera.setCaptureMode(QCamera::CaptureVideo); |
| 173 | camera.setCaptureMode(QCamera::CaptureStillImage); |
| 174 | imageCapture.setEncodingSettings(QImageEncoderSettings()); |
| 175 | imageCapture.setEncodingSettings(QImageEncoderSettings()); |
| 176 | |
| 177 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 178 | QCOMPARE(camera.status(), QCamera::LoadedStatus); |
| 179 | |
| 180 | QCOMPARE(stateChangedSignal.count(), 0); |
| 181 | QCOMPARE(statusChangedSignal.count(), 1); |
| 182 | stateChangedSignal.clear(); |
| 183 | statusChangedSignal.clear(); |
| 184 | |
| 185 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 186 | QTRY_COMPARE(camera.status(), QCamera::ActiveStatus); |
| 187 | |
| 188 | QCOMPARE(stateChangedSignal.count(), 0); |
| 189 | QCOMPARE(statusChangedSignal.count(), 1); |
| 190 | stateChangedSignal.clear(); |
| 191 | statusChangedSignal.clear(); |
| 192 | |
| 193 | //setting the viewfinder should also trigger backend to be restarted: |
| 194 | camera.setViewfinder(new QGraphicsVideoItem()); |
| 195 | QCOMPARE(camera.state(), QCamera::ActiveState); |
| 196 | QCOMPARE(camera.status(), QCamera::LoadedStatus); |
| 197 | |
| 198 | QCOMPARE(stateChangedSignal.count(), 0); |
| 199 | QCOMPARE(statusChangedSignal.count(), 1); |
| 200 | |
| 201 | QTRY_COMPARE(camera.status(), QCamera::ActiveStatus); |
| 202 | |
| 203 | mockCameraService->mockControl->m_propertyChangesSupported = true; |
| 204 | //the changes to encoding settings, |
| 205 | //capture mode and encoding parameters should not trigger service restart |
| 206 | stateChangedSignal.clear(); |
| 207 | statusChangedSignal.clear(); |
| 208 | |
| 209 | camera.setCaptureMode(QCamera::CaptureVideo); |
| 210 | camera.setCaptureMode(QCamera::CaptureStillImage); |
| 211 | imageCapture.setEncodingSettings(QImageEncoderSettings()); |
| 212 | imageCapture.setEncodingSettings(QImageEncoderSettings()); |
| 213 | camera.setViewfinder(new QGraphicsVideoItem()); |
| 214 | |
| 215 | QCOMPARE(stateChangedSignal.count(), 0); |
| 216 | QCOMPARE(statusChangedSignal.count(), 0); |
| 217 | } |
| 218 | |
| 219 | void tst_QCameraWidgets::testSetVideoOutput() |
| 220 | { |
| 221 | QVideoWidget widget; |
| 222 | QGraphicsVideoItem item; |
| 223 | MockVideoSurface surface; |
| 224 | QCamera camera; |
| 225 | |
| 226 | camera.setViewfinder(&widget); |
| 227 | qDebug() << widget.mediaObject(); |
| 228 | QVERIFY(widget.mediaObject() == &camera); |
| 229 | |
| 230 | camera.setViewfinder(&item); |
| 231 | QVERIFY(widget.mediaObject() == 0); |
| 232 | QVERIFY(item.mediaObject() == &camera); |
| 233 | |
| 234 | camera.setViewfinder(reinterpret_cast<QVideoWidget *>(0)); |
| 235 | QVERIFY(item.mediaObject() == 0); |
| 236 | |
| 237 | camera.setViewfinder(&widget); |
| 238 | QVERIFY(widget.mediaObject() == &camera); |
| 239 | |
| 240 | camera.setViewfinder(reinterpret_cast<QGraphicsVideoItem *>(0)); |
| 241 | QVERIFY(widget.mediaObject() == 0); |
| 242 | |
| 243 | camera.setViewfinder(&surface); |
| 244 | QVERIFY(mockCameraService->rendererControl->surface() == &surface); |
| 245 | |
| 246 | camera.setViewfinder(reinterpret_cast<QAbstractVideoSurface *>(0)); |
| 247 | QVERIFY(mockCameraService->rendererControl->surface() == 0); |
| 248 | |
| 249 | camera.setViewfinder(&surface); |
| 250 | QVERIFY(mockCameraService->rendererControl->surface() == &surface); |
| 251 | |
| 252 | camera.setViewfinder(&widget); |
| 253 | QVERIFY(mockCameraService->rendererControl->surface() == 0); |
| 254 | QVERIFY(widget.mediaObject() == &camera); |
| 255 | |
| 256 | camera.setViewfinder(&surface); |
| 257 | QVERIFY(mockCameraService->rendererControl->surface() == &surface); |
| 258 | QVERIFY(widget.mediaObject() == 0); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void tst_QCameraWidgets::testSetVideoOutputNoService() |
| 263 | { |
| 264 | QVideoWidget widget; |
| 265 | QGraphicsVideoItem item; |
| 266 | MockVideoSurface surface; |
| 267 | |
| 268 | provider->service = 0; |
| 269 | QCamera camera; |
| 270 | |
| 271 | camera.setViewfinder(&widget); |
| 272 | QVERIFY(widget.mediaObject() == 0); |
| 273 | |
| 274 | camera.setViewfinder(&item); |
| 275 | QVERIFY(item.mediaObject() == 0); |
| 276 | |
| 277 | camera.setViewfinder(&surface); |
| 278 | // Nothing we can verify here other than it doesn't assert. |
| 279 | } |
| 280 | |
| 281 | void tst_QCameraWidgets::testSetVideoOutputNoControl() |
| 282 | { |
| 283 | QVideoWidget widget; |
| 284 | QGraphicsVideoItem item; |
| 285 | MockVideoSurface surface; |
| 286 | |
| 287 | mockCameraService->rendererRef = 1; |
| 288 | mockCameraService->windowRef = 1; |
| 289 | |
| 290 | QCamera camera; |
| 291 | |
| 292 | camera.setViewfinder(&widget); |
| 293 | QVERIFY(widget.mediaObject() == 0); |
| 294 | |
| 295 | camera.setViewfinder(&item); |
| 296 | QVERIFY(item.mediaObject() == 0); |
| 297 | |
| 298 | camera.setViewfinder(&surface); |
| 299 | QVERIFY(mockCameraService->rendererControl->surface() == 0); |
| 300 | } |
| 301 | |
| 302 | QTEST_MAIN(tst_QCameraWidgets) |
| 303 | |
| 304 | #include "tst_qcamerawidgets.moc" |
| 305 | |