Added qmafw-gst-subtitles-renderer-0.0.55 for Meego Harmattan 1.2
[mafwsubrenderer] / qmafw-gst-subtitles-renderer / unittests / common / MafwStubHelper.cpp
1 /* 
2  * This file is part of QMAFW 
3  *
4  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights
5  * reserved.
6  *
7  * Contact: Visa Smolander <visa.smolander@nokia.com>
8  *
9  * This software, including documentation, is protected by copyright controlled
10  * by Nokia Corporation. All rights are reserved. Copying, including
11  * reproducing, storing, adapting or translating, any or all of this material
12  * requires the prior written consent of Nokia Corporation. This material also
13  * contains confidential information which may not be disclosed to others
14  * without the prior written consent of Nokia.
15  *
16  */
17
18
19 #include <QDebug>
20 #include <QStringList>
21
22 #include "MafwStubHelper.h"
23
24 MafwStubHelper::MafwStubHelper()
25 {
26     m_expectedCalls = QQueue<ExpectedItem*>();
27 }
28
29 MafwStubHelper::~MafwStubHelper()
30 {
31         clear();
32 }
33
34 void MafwStubHelper::printExpects() const
35 {
36     QStringList expectedNames;
37     for(int i=0; i<m_expectedCalls.size(); i++ )
38     {
39         expectedNames << m_expectedCalls[i]->expectedName;
40     }
41     qDebug() << "Expected calls enqueued (" << m_expectedCalls.size() << "): \n\t" << expectedNames.join(",\n\t ");
42 }
43
44 void MafwStubHelper::expect(const QString& function, QVariant returnVal)
45 {
46     QList<QVariant> emptyParams;
47     QList<QVariant> returnList;
48     returnList << returnVal;
49     expect(function, emptyParams, returnList);
50 }
51
52
53 void MafwStubHelper::expect(const QString function, const QList<QVariant> params,
54                                                         const QList<QVariant> returns)
55 {
56         qDebug() << "MafwStubHelper::expect, function = " << function << " " << returns.at(0);
57         
58         ExpectedItem* item = new ExpectedItem;
59         item->expectedName = function;
60         item->expectedParams = params;
61         item->returnValues = returns;
62         m_expectedCalls.enqueue(item);
63 }
64
65 QVariant MafwStubHelper::getReturn(const QString& function)
66 {
67     QList<QVariant> emptyParams;
68     QList<QVariant> returnList;
69     getReturn(function, emptyParams, returnList);
70     if (returnList.isEmpty())
71     {
72         return QVariant();
73     }
74     else
75     {
76         return returnList.first();
77     }
78 }
79
80 void MafwStubHelper::getReturn(const QString function, const QList<QVariant> params,
81                                                            QList<QVariant>& returns)
82 {
83     // Check if the call is expected
84     if ( m_expectedCalls.isEmpty() )
85     {
86         qDebug() << "MafwStubHelper::getReturn, function = " << function <<", no expected calls";
87         return;
88     }
89     if (!m_expectedCalls.isEmpty() && function.compare(m_expectedCalls.head()->expectedName))
90     {
91         qDebug() << "MafwStubHelper::getReturn:  " << function << ", not expected (2)";
92         printExpects();
93         return;
94     }
95     ExpectedItem* item = m_expectedCalls.dequeue();
96     
97     // Check if the parameters match
98     if (!item->expectedParams.isEmpty())
99     {
100         for (int i = 0; i < item->expectedParams.count() && item->expectedParams.count() == params.count(); ++i)
101         {
102             if (item->expectedParams.at(i) != params.at(i))
103             {
104                 qDebug() << "MafwStubHelper::getReturn:  " << function <<", not expected (2)";
105                 return;
106             }
107         }
108     }
109     // Expected parameters list was empty but given was not
110     else if (!params.isEmpty())
111     {
112         qDebug() << "MafwStubHelper::getReturn: " << function <<", not expected (3)";
113         return;
114     }
115     else
116     {
117     }
118     
119     // Everything ok, let's find the return values
120     returns = item->returnValues;
121     
122     qDebug() << "MafwStubHelper::getReturn, function: " << function << ", returns: " << returns;
123     
124     delete item;
125 }
126
127 bool MafwStubHelper::allCallsConsumed() const
128 {       
129     return m_expectedCalls.isEmpty();
130 }
131
132 void MafwStubHelper::clear()
133 {
134     qDebug() << "MafwStubHelper::clear()";
135     
136     qDeleteAll(m_expectedCalls);
137     m_expectedCalls.clear();
138 }
139
140 // End of file