phoneapp/phoneuiview2/tsrc/ut_phoneuiqtview/unit_tests.cpp
changeset 21 92ab7f8d0eab
child 22 6bb1b21d2484
equal deleted inserted replaced
4:c84cf270c54f 21:92ab7f8d0eab
       
     1 /*!
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Unit tests for PhoneUIQtView.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QtTest/QtTest>
       
    19 #include <QtGui>
       
    20 #include <QVariant>
       
    21 #include <QKeyEvent>
       
    22 #include <hbapplication.h>
       
    23 #include <QSignalSpy>
       
    24 #include <hbmainwindow.h>
       
    25 #include <hbinstance.h>
       
    26 #include <hbmainwindow.h>
       
    27 #include <bubblemanagerif.h>
       
    28 #include "phoneuiqtview.h"
       
    29 #include "phoneaction.h"
       
    30 #include <hbtoolbar.h>
       
    31 #include <hbvolumesliderpopup.h>
       
    32 #include <hbaction.h>
       
    33 #include "xqserviceutil.h"
       
    34 #include "phoneaction.h"
       
    35 
       
    36 //CONSTANTS
       
    37 static const int KMAXVOLUME = 10;
       
    38 static const int KMINVOLUME = 0;
       
    39 static const int KVOLUMECOMMAND = 5;
       
    40 
       
    41 
       
    42 #define PHONE_QT_VIEW_TEST_MAIN(TestObject) \
       
    43 int main(int argc, char *argv[]) \
       
    44 { \
       
    45     HbApplication app(argc, argv); \
       
    46     TestObject tc; \
       
    47     QResource::registerResource("../hbcore.rcc"); \
       
    48     int ret = QTest::qExec(&tc, argc, argv); \
       
    49     /* Core dump if HbIconLoader instance is not destroyed before the application instance. */ \
       
    50     /* HbIconLoader uses QCoreApplication::aboutToQuit() signal to destroy itself. */ \
       
    51     /* app.exec() where the signal is normally emitted is not called here. */ \
       
    52     /* So, invoking the signal explicitly. */ \
       
    53     QMetaObject::invokeMethod(&app, "aboutToQuit", Qt::DirectConnection); \
       
    54     return ret; \
       
    55 }
       
    56 
       
    57 class TestPhoneUIQtView : public QObject
       
    58 {
       
    59     Q_OBJECT
       
    60     
       
    61 public:
       
    62     TestPhoneUIQtView ();
       
    63     ~TestPhoneUIQtView ();
       
    64 
       
    65 
       
    66 public slots:
       
    67     void initTestCase ();
       
    68     void cleanupTestCase (); 
       
    69     void init ();
       
    70     void cleanup ();
       
    71     
       
    72 private slots:
       
    73     void testAddBubbleCommand ();
       
    74     void testHideToolbar ();
       
    75     void testShowToolbar ();
       
    76     void testAddToolbarActions ();
       
    77     void testSetVolumeSliderValue ();
       
    78     void testRemoveVolumeSlider ();
       
    79     void testVolumeSliderValue ();
       
    80     void testVolumeSliderChanged ();
       
    81     void testParticipantListAction ();
       
    82     void testSetExpandAction ();
       
    83     void testPhoneAction ();
       
    84     void testSetOrientation ();
       
    85     void testBackButtonClicked();
       
    86     void testShowDialpad();
       
    87     void testDialpadText();
       
    88     void testSetMenuActions();
       
    89 
       
    90 private:
       
    91     int createCallHeader();
       
    92 
       
    93 private:
       
    94     PhoneUIQtView *m_view; // class under test
       
    95     HbMainWindow *m_main_window;
       
    96 
       
    97 };
       
    98 
       
    99 
       
   100 TestPhoneUIQtView::TestPhoneUIQtView ()
       
   101 {    
       
   102 }
       
   103 
       
   104 TestPhoneUIQtView::~TestPhoneUIQtView ()
       
   105 {
       
   106 }
       
   107 
       
   108 void TestPhoneUIQtView::initTestCase ()
       
   109 {	
       
   110 }
       
   111 
       
   112 void TestPhoneUIQtView::cleanupTestCase ()
       
   113 {
       
   114 }
       
   115 
       
   116 void TestPhoneUIQtView::init ()
       
   117 {
       
   118     m_main_window = new HbMainWindow(); 
       
   119     m_view = new PhoneUIQtView(*m_main_window);
       
   120 }
       
   121 
       
   122 void TestPhoneUIQtView::cleanup ()
       
   123 {
       
   124     delete m_view;
       
   125     delete m_main_window;
       
   126 }
       
   127 
       
   128 void TestPhoneUIQtView::testAddBubbleCommand ()
       
   129 {
       
   130     m_view->bubbleManager ().startChanges ();
       
   131     int bubbleId = createCallHeader();
       
   132     int bubbleId2 = createCallHeader();
       
   133     
       
   134     PhoneAction action1;
       
   135     action1.setText("Command 1");
       
   136     action1.setCommand(1);
       
   137     action1.setActionRole(PhoneAction::None);
       
   138     
       
   139     PhoneAction action2;
       
   140     action2.setText("Command 2");
       
   141     action2.setCommand(2);
       
   142     action2.setActionRole(PhoneAction::Accept);
       
   143     
       
   144     PhoneAction action3;
       
   145     action3.setText("Command 3");
       
   146     action3.setCommand(3);
       
   147     action3.setActionRole(PhoneAction::Decline);
       
   148     
       
   149     m_view->addBubbleCommand (bubbleId, action1);
       
   150     m_view->addBubbleCommand (bubbleId, action2);
       
   151     m_view->addBubbleCommand (bubbleId2, action3);
       
   152     m_view->bubbleManager ().endChanges ();
       
   153     
       
   154     m_view->bubbleManager ().startChanges ();
       
   155     m_view->clearBubbleCommands (bubbleId);
       
   156     m_view->clearBubbleCommands (bubbleId2);
       
   157     m_view->clearBubbleCommands (bubbleId2);
       
   158     m_view->bubbleManager ().endChanges ();
       
   159 }
       
   160 
       
   161 void TestPhoneUIQtView::testHideToolbar ()
       
   162 {
       
   163     m_view->hideToolbar ();
       
   164     QCOMPARE (m_hideCalled, true);	
       
   165 }
       
   166 
       
   167 void TestPhoneUIQtView::testShowToolbar ()
       
   168 {
       
   169     m_view->showToolbar ();
       
   170     QCOMPARE (m_showCalled, true);
       
   171 }
       
   172 void TestPhoneUIQtView::testAddToolbarActions ()
       
   173 {
       
   174     m_actions.clear();
       
   175     HbIcon icon ("foo.jpg");
       
   176     PhoneAction *testAction = new PhoneAction ();
       
   177     testAction->setIcon(icon);
       
   178     testAction->setText("foo");
       
   179     testAction->setCommand(20);
       
   180     QList<PhoneAction*> actions;
       
   181     actions.append(testAction);
       
   182     
       
   183     m_view->setToolbarActions(actions);
       
   184     QCOMPARE (m_action->icon(), icon);
       
   185     QCOMPARE(true, m_toolBarUpdateCalled);
       
   186     
       
   187     m_view->setToolbarActions(actions);
       
   188     
       
   189     m_toolBarUpdateCalled = false;
       
   190     PhoneAction *testAction2 = new PhoneAction ();
       
   191     testAction2->setIcon(icon);
       
   192     testAction2->setText("foo2");
       
   193     testAction2->setCommand(21);
       
   194     actions.append(testAction2);
       
   195     
       
   196     m_view->setToolbarActions(actions);
       
   197     QCOMPARE(true, m_toolBarUpdateCalled);
       
   198     actions.removeOne(testAction2);
       
   199     
       
   200     m_toolBarUpdateCalled = false;
       
   201     m_view->setToolbarActions(actions);
       
   202     QCOMPARE(true, m_toolBarUpdateCalled);
       
   203     
       
   204     actions.clear();
       
   205     delete testAction;
       
   206     delete testAction2;
       
   207 }
       
   208 
       
   209 void TestPhoneUIQtView::testSetVolumeSliderValue ()
       
   210 {
       
   211     m_view->setVolumeSliderValue (1, KVOLUMECOMMAND, KMAXVOLUME, KMINVOLUME);
       
   212     QCOMPARE (m_volumeSliderContructorCalled, true);
       
   213     QCOMPARE (m_sliderRangeMin, KMINVOLUME);
       
   214     QCOMPARE (m_sliderRangeMax, KMAXVOLUME);
       
   215     QCOMPARE (m_volumeSliderSetTimeoutCalled, true);
       
   216     QCOMPARE (m_sliderTimeout, 10000);
       
   217     QCOMPARE (m_volumeSliderSetValueCalled, true);
       
   218     QCOMPARE (m_sliderValue, 1);
       
   219     QCOMPARE (m_volumeSliderShowCalled, true);
       
   220     
       
   221     // reset values
       
   222     m_volumeSliderContructorCalled = false;
       
   223     m_volumeSliderSetValueCalled = false;
       
   224     m_sliderRangeMin = -1;
       
   225     m_sliderRangeMax = -1;
       
   226     m_volumeSliderSetTimeoutCalled = false;
       
   227     m_sliderTimeout = -1;
       
   228     m_volumeSliderSetValueCalled = false;
       
   229     m_sliderValue = -1;
       
   230     m_volumeSliderShowCalled = false;
       
   231 
       
   232     // The second call shouldn't create a new instance
       
   233     m_view->setVolumeSliderValue (2, KVOLUMECOMMAND, KMAXVOLUME, KMINVOLUME);
       
   234     QCOMPARE (m_volumeSliderContructorCalled, false);
       
   235     QCOMPARE (m_sliderRangeMin, KMINVOLUME);
       
   236     QCOMPARE (m_sliderRangeMax, KMAXVOLUME);
       
   237     QCOMPARE (m_volumeSliderSetTimeoutCalled, false);
       
   238     QCOMPARE (m_sliderTimeout, -1);
       
   239     QCOMPARE (m_volumeSliderSetValueCalled, true);
       
   240     QCOMPARE (m_sliderValue, 2);
       
   241     QCOMPARE (m_volumeSliderShowCalled, false);
       
   242 }
       
   243 
       
   244 void TestPhoneUIQtView::testRemoveVolumeSlider ()
       
   245 {
       
   246     m_view->setVolumeSliderValue (1, KVOLUMECOMMAND, KMAXVOLUME, KMINVOLUME);
       
   247     m_view->removeVolumeSlider ();
       
   248     QCOMPARE (m_volumeSliderHideCalled, true);
       
   249 }
       
   250 
       
   251 void TestPhoneUIQtView::testVolumeSliderValue ()
       
   252 {
       
   253     m_sliderValue = -1;
       
   254     int value = m_view->volumeSliderValue ();
       
   255     QCOMPARE (m_sliderValue, -1);
       
   256     m_view->setVolumeSliderValue (3, KVOLUMECOMMAND, KMAXVOLUME, KMINVOLUME);
       
   257     value = m_view->volumeSliderValue ();
       
   258     QCOMPARE (m_volumeSliderValueCalled, true);
       
   259     QCOMPARE (value, 3);
       
   260 }
       
   261 
       
   262 void TestPhoneUIQtView::testVolumeSliderChanged ()
       
   263 {
       
   264     m_view->setVolumeSliderValue (3, KVOLUMECOMMAND, KMAXVOLUME, KMINVOLUME);
       
   265     QSignalSpy spy (m_view, SIGNAL(command(int)));
       
   266     m_view->volumeSliderChanged (1);
       
   267     QCOMPARE (spy.count(), 1);
       
   268     int command = qvariant_cast<int>(spy.at (0).at (0));
       
   269     QCOMPARE (command, (int)KVOLUMECOMMAND);
       
   270 }
       
   271 
       
   272 void TestPhoneUIQtView::testParticipantListAction ()
       
   273 {
       
   274     m_view->bubbleManager ().startChanges ();
       
   275     m_view->addParticipantListAction (1,"Command 1", HbIcon ());
       
   276     m_view->addParticipantListAction (2,"Command 2", HbIcon ());
       
   277     m_view->bubbleManager ().endChanges ();
       
   278 
       
   279     m_view->bubbleManager ().startChanges ();
       
   280     m_view->clearParticipantListActions ();
       
   281     m_view->clearParticipantListActions ();
       
   282     m_view->bubbleManager ().endChanges ();
       
   283 }
       
   284 
       
   285 void TestPhoneUIQtView::testSetExpandAction ()
       
   286 {
       
   287     m_view->bubbleManager ().startChanges ();
       
   288     int bubbleId = createCallHeader();
       
   289     int bubbleId2 = createCallHeader();
       
   290 
       
   291     m_view->setExpandAction(bubbleId, 1);
       
   292     m_view->setExpandAction(bubbleId2, 2);
       
   293     m_view->bubbleManager ().endChanges ();
       
   294 
       
   295     m_view->bubbleManager ().startChanges ();
       
   296     m_view->removeExpandAction(bubbleId);
       
   297     m_view->removeExpandAction(bubbleId2);
       
   298     m_view->bubbleManager().removeCallHeader(bubbleId);
       
   299     m_view->bubbleManager().removeCallHeader(bubbleId2);
       
   300     m_view->bubbleManager ().endChanges ();
       
   301 }
       
   302 
       
   303 void TestPhoneUIQtView::testPhoneAction ()
       
   304 {
       
   305     PhoneAction *action = new PhoneAction ();
       
   306     QString string("Action");
       
   307     action->setText(string);
       
   308     HbIcon icon;
       
   309     action->setIcon(icon);
       
   310     int commandId(1);
       
   311     action->setCommand(commandId);
       
   312     action->setDisabled(true);
       
   313     QCOMPARE( string, action->text() );
       
   314     QCOMPARE( icon, action->icon() );
       
   315     QCOMPARE( commandId, action->command() );
       
   316     QCOMPARE( true, action->isDisabled() );
       
   317     delete action;
       
   318 }
       
   319 
       
   320 void TestPhoneUIQtView::testSetOrientation ()
       
   321 {
       
   322 
       
   323     m_setOrientationCalled = false;
       
   324     
       
   325     m_view->handleOrientationChange(Qt::Vertical);
       
   326     QVERIFY( false == m_setOrientationCalled );
       
   327     
       
   328     m_view->handleOrientationChange(Qt::Horizontal);
       
   329     QVERIFY( true == m_setOrientationCalled );
       
   330     
       
   331 }
       
   332 
       
   333 void TestPhoneUIQtView::testBackButtonClicked()
       
   334 {
       
   335     // not possible to mock xqservice because it is in \epoc32\include
       
   336     // calling backButtonClicked would send this test background.
       
   337     // m_view->backButtonClicked();
       
   338 }
       
   339 
       
   340 void TestPhoneUIQtView::testShowDialpad()
       
   341 {
       
   342     m_view->hideDialpad();
       
   343     m_view->showDialpad();
       
   344     m_view->showDialpad();
       
   345     QVERIFY(true == m_view->isDialpadVisible());
       
   346     m_view->hideDialpad();
       
   347 }
       
   348 
       
   349 void TestPhoneUIQtView::testDialpadText()
       
   350 {
       
   351     m_view->showDialpad();
       
   352     QVERIFY(QString("") == m_view->dialpadText());
       
   353     m_view->clearAndHideDialpad();
       
   354 }
       
   355 
       
   356 void TestPhoneUIQtView::testSetMenuActions()
       
   357 {
       
   358     QList<PhoneAction*> actions;
       
   359     m_view->setMenuActions(actions);
       
   360     
       
   361     PhoneAction* phoneAction = new PhoneAction; 
       
   362     phoneAction->setText(QString("test"));
       
   363     phoneAction->setCommand(1);
       
   364     actions.append(phoneAction);
       
   365     
       
   366     m_view->setMenuActions(actions);
       
   367     qDeleteAll(actions);
       
   368     actions.clear();
       
   369     
       
   370     m_view->setMenuActions(actions);
       
   371 }
       
   372 
       
   373 //Private methods
       
   374 int TestPhoneUIQtView::createCallHeader ()
       
   375 {
       
   376     int bubble = m_view->bubbleManager().createCallHeader ();
       
   377 
       
   378     return bubble;
       
   379 }
       
   380 
       
   381 PHONE_QT_VIEW_TEST_MAIN(TestPhoneUIQtView)
       
   382 Q_DECLARE_METATYPE(QKeyEvent *)
       
   383 #include "unit_tests.moc"