tests/auto/qmediaplayer/tst_qmediaplayer.h
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #ifndef TST_QMEDIAPLAYER_H
       
    42 #define TST_QMEDIAPLAYER_H
       
    43 
       
    44 #include <QtTest/QtTest>
       
    45 #include <QtCore/qdebug.h>
       
    46 #include <QtCore/qbuffer.h>
       
    47 
       
    48 #include <qmediaplayer.h>
       
    49 #include <qmediaplayercontrol.h>
       
    50 #include <qmediaplaylist.h>
       
    51 #include <qmediaservice.h>
       
    52 #include <qmediastreamscontrol.h>
       
    53 
       
    54 QT_USE_NAMESPACE
       
    55 
       
    56 class AutoConnection
       
    57 {
       
    58 public:
       
    59     AutoConnection(QObject *sender, const char *signal, QObject *receiver, const char *method)
       
    60             : sender(sender), signal(signal), receiver(receiver), method(method)
       
    61     {
       
    62         QObject::connect(sender, signal, receiver, method);
       
    63     }
       
    64 
       
    65     ~AutoConnection()
       
    66     {
       
    67         QObject::disconnect(sender, signal, receiver, method);
       
    68     }
       
    69 
       
    70 private:
       
    71     QObject *sender;
       
    72     const char *signal;
       
    73     QObject *receiver;
       
    74     const char *method;
       
    75 };
       
    76 
       
    77 
       
    78 class MockPlayerControl : public QMediaPlayerControl
       
    79 {
       
    80     friend class MockPlayerService;
       
    81 
       
    82 public:
       
    83     MockPlayerControl():QMediaPlayerControl(0) {}
       
    84 
       
    85     QMediaPlayer::State state() const { return _state; }
       
    86     QMediaPlayer::MediaStatus mediaStatus() const { return _mediaStatus; }
       
    87 
       
    88     qint64 duration() const { return _duration; }
       
    89 
       
    90     qint64 position() const { return _position; }
       
    91 
       
    92     void setPosition(qint64 position) { if (position != _position) emit positionChanged(_position = position); }
       
    93 
       
    94     int volume() const { return _volume; }
       
    95     void setVolume(int volume) { emit volumeChanged(_volume = volume); }
       
    96 
       
    97     bool isMuted() const { return _muted; }
       
    98     void setMuted(bool muted) { if (muted != _muted) emit mutedChanged(_muted = muted); }
       
    99 
       
   100     int bufferStatus() const { return _bufferStatus; }
       
   101 
       
   102     bool isAudioAvailable() const { return _audioAvailable; }
       
   103     bool isVideoAvailable() const { return _videoAvailable; }
       
   104 
       
   105     bool isSeekable() const { return _isSeekable; }
       
   106     QMediaTimeRange availablePlaybackRanges() const { return QMediaTimeRange(_seekRange.first, _seekRange.second); }
       
   107     void setSeekRange(qint64 minimum, qint64 maximum) { _seekRange = qMakePair(minimum, maximum); }
       
   108 
       
   109     qreal playbackRate() const { return _playbackRate; }
       
   110     void setPlaybackRate(qreal rate) { if (rate != _playbackRate) emit playbackRateChanged(_playbackRate = rate); }
       
   111 
       
   112     QMediaContent media() const { return _media; }
       
   113     void setMedia(const QMediaContent &content, QIODevice *stream)
       
   114     {
       
   115         _stream = stream;
       
   116         _media = content;
       
   117         if (_state != QMediaPlayer::StoppedState) {
       
   118             _mediaStatus = _media.isNull() ? QMediaPlayer::NoMedia : QMediaPlayer::LoadingMedia;
       
   119             emit stateChanged(_state = QMediaPlayer::StoppedState);
       
   120             emit mediaStatusChanged(_mediaStatus);
       
   121         }
       
   122         emit mediaChanged(_media = content);
       
   123     }
       
   124     QIODevice *mediaStream() const { return _stream; }
       
   125 
       
   126     void play() { if (_isValid && !_media.isNull() && _state != QMediaPlayer::PlayingState) emit stateChanged(_state = QMediaPlayer::PlayingState); }
       
   127     void pause() { if (_isValid && !_media.isNull() && _state != QMediaPlayer::PausedState) emit stateChanged(_state = QMediaPlayer::PausedState); }
       
   128     void stop() { if (_state != QMediaPlayer::StoppedState) emit stateChanged(_state = QMediaPlayer::StoppedState); }
       
   129 
       
   130     QMediaPlayer::State _state;
       
   131     QMediaPlayer::MediaStatus _mediaStatus;
       
   132     QMediaPlayer::Error _error;
       
   133     qint64 _duration;
       
   134     qint64 _position;
       
   135     int _volume;
       
   136     bool _muted;
       
   137     int _bufferStatus;
       
   138     bool _audioAvailable;
       
   139     bool _videoAvailable;
       
   140     bool _isSeekable;
       
   141     QPair<qint64, qint64> _seekRange;
       
   142     qreal _playbackRate;
       
   143     QMediaContent _media;
       
   144     QIODevice *_stream;
       
   145     bool _isValid;
       
   146     QString _errorString;
       
   147 };
       
   148 
       
   149 class MockStreamsControl : public QMediaStreamsControl
       
   150 {
       
   151 public:
       
   152     MockStreamsControl(QObject *parent = 0) : QMediaStreamsControl(parent) {}
       
   153 
       
   154     int streamCount() { return _streams.count(); }
       
   155     void setStreamCount(int count) { _streams.resize(count); }
       
   156 
       
   157     StreamType streamType(int index) { return _streams.at(index).type; }
       
   158     void setStreamType(int index, StreamType type) { _streams[index].type = type; }
       
   159 
       
   160     QVariant metaData(int index, QtMultimediaKit::MetaData key) {
       
   161         return _streams.at(index).metaData.value(key); }
       
   162     void setMetaData(int index, QtMultimediaKit::MetaData key, const QVariant &value) {
       
   163         _streams[index].metaData.insert(key, value); }
       
   164 
       
   165     bool isActive(int index) { return _streams.at(index).active; }
       
   166     void setActive(int index, bool state) { _streams[index].active = state; }
       
   167 
       
   168 private:
       
   169     struct Stream
       
   170     {
       
   171         Stream() : type(UnknownStream), active(false) {}
       
   172         StreamType type;
       
   173         QMap<QtMultimediaKit::MetaData, QVariant> metaData;
       
   174         bool active;
       
   175     };
       
   176 
       
   177     QVector<Stream> _streams;
       
   178 };
       
   179 
       
   180 class MockPlayerService : public QMediaService
       
   181 {
       
   182     Q_OBJECT
       
   183 
       
   184 public:
       
   185     MockPlayerService():QMediaService(0)
       
   186     {
       
   187         mockControl = new MockPlayerControl;
       
   188         mockStreamsControl = new MockStreamsControl;
       
   189     }
       
   190 
       
   191     ~MockPlayerService()
       
   192     {
       
   193         delete mockControl;
       
   194         delete mockStreamsControl;
       
   195     }
       
   196 
       
   197     QMediaControl* requestControl(const char *iid)
       
   198     {
       
   199         if (qstrcmp(iid, QMediaPlayerControl_iid) == 0)
       
   200             return mockControl;
       
   201 
       
   202         return 0;
       
   203     }
       
   204 
       
   205     void releaseControl(QMediaControl *)
       
   206     {
       
   207     }
       
   208 
       
   209     void setState(QMediaPlayer::State state) { emit mockControl->stateChanged(mockControl->_state = state); }
       
   210     void setState(QMediaPlayer::State state, QMediaPlayer::MediaStatus status) {
       
   211         mockControl->_state = state;
       
   212         mockControl->_mediaStatus = status;
       
   213         emit mockControl->mediaStatusChanged(status);
       
   214         emit mockControl->stateChanged(state);
       
   215     }
       
   216     void setMediaStatus(QMediaPlayer::MediaStatus status) { emit mockControl->mediaStatusChanged(mockControl->_mediaStatus = status); }
       
   217     void setIsValid(bool isValid) { mockControl->_isValid = isValid; }
       
   218     void setMedia(QMediaContent media) { mockControl->_media = media; }
       
   219     void setDuration(qint64 duration) { mockControl->_duration = duration; }
       
   220     void setPosition(qint64 position) { mockControl->_position = position; }
       
   221     void setSeekable(bool seekable) { mockControl->_isSeekable = seekable; }
       
   222     void setVolume(int volume) { mockControl->_volume = volume; }
       
   223     void setMuted(bool muted) { mockControl->_muted = muted; }
       
   224     void setVideoAvailable(bool videoAvailable) { mockControl->_videoAvailable = videoAvailable; }
       
   225     void setBufferStatus(int bufferStatus) { mockControl->_bufferStatus = bufferStatus; }
       
   226     void setPlaybackRate(qreal playbackRate) { mockControl->_playbackRate = playbackRate; }
       
   227     void setError(QMediaPlayer::Error error) { mockControl->_error = error; emit mockControl->error(mockControl->_error, mockControl->_errorString); }
       
   228     void setErrorString(QString errorString) { mockControl->_errorString = errorString; emit mockControl->error(mockControl->_error, mockControl->_errorString); }
       
   229 
       
   230     void reset()
       
   231     {
       
   232         mockControl->_state = QMediaPlayer::StoppedState;
       
   233         mockControl->_mediaStatus = QMediaPlayer::UnknownMediaStatus;
       
   234         mockControl->_error = QMediaPlayer::NoError;
       
   235         mockControl->_duration = 0;
       
   236         mockControl->_position = 0;
       
   237         mockControl->_volume = 0;
       
   238         mockControl->_muted = false;
       
   239         mockControl->_bufferStatus = 0;
       
   240         mockControl->_videoAvailable = false;
       
   241         mockControl->_isSeekable = false;
       
   242         mockControl->_playbackRate = 0.0;
       
   243         mockControl->_media = QMediaContent();
       
   244         mockControl->_stream = 0;
       
   245         mockControl->_isValid = false;
       
   246         mockControl->_errorString = QString();
       
   247     }
       
   248 
       
   249     MockPlayerControl *mockControl;
       
   250     MockStreamsControl *mockStreamsControl;
       
   251 };
       
   252 
       
   253 class MockProvider : public QMediaServiceProvider
       
   254 {
       
   255 public:
       
   256     MockProvider(MockPlayerService *service):mockService(service) {}
       
   257     QMediaService *requestService(const QByteArray &, const QMediaServiceProviderHint &)
       
   258     {
       
   259         return mockService;
       
   260     }
       
   261 
       
   262     void releaseService(QMediaService *service) { delete service; }
       
   263 
       
   264     MockPlayerService *mockService;
       
   265 };
       
   266 
       
   267 
       
   268 
       
   269 
       
   270 
       
   271 class tst_QMediaPlayer: public QObject
       
   272 {
       
   273     Q_OBJECT
       
   274 
       
   275 public slots:
       
   276     void initTestCase_data();
       
   277     void initTestCase();
       
   278     void cleanupTestCase();
       
   279     void init();
       
   280     void cleanup();
       
   281 
       
   282 private slots:
       
   283     void testNullService();
       
   284     void testValid();
       
   285     void testMedia();
       
   286     void testDuration();
       
   287     void testPosition();
       
   288     void testVolume();
       
   289     void testMuted();
       
   290     void testIsAvailable();
       
   291     void testVideoAvailable();
       
   292     void testBufferStatus();
       
   293     void testSeekable();
       
   294     void testPlaybackRate();
       
   295     void testError();
       
   296     void testErrorString();
       
   297     void testService();
       
   298     void testPlay();
       
   299     void testPause();
       
   300     void testStop();
       
   301     void testMediaStatus();
       
   302     void testPlaylist();
       
   303 
       
   304 private:
       
   305     MockProvider *mockProvider;
       
   306     MockPlayerService  *mockService;
       
   307     QMediaPlayer *player;
       
   308 };
       
   309 
       
   310 #endif //TST_QMEDIAPLAYER_H