messagingapp/msgui/conversationview/tsrc/unittests/unittest_msgconversationwidget/unittest_msgconversationwidget.cpp
changeset 56 f42d9a78f435
parent 55 5b3b2fa8c3ec
child 58 5401a102f08b
equal deleted inserted replaced
55:5b3b2fa8c3ec 56:f42d9a78f435
     1 /*
       
     2  * Copyright (c) 2010 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:
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QtTest/QtTest>
       
    19 
       
    20 #include "msgconversationwidgetheaders.h"
       
    21 
       
    22 /*
       
    23     Global variables that are used to control the test environment and/or
       
    24     observe the individual tests. The naming convention can be described with
       
    25     a ternary template
       
    26     
       
    27         <class>_<member>[_<suffix>],
       
    28     
       
    29     where <class> is the name of a mocked class, <member> is the name of a
       
    30     variable, a function, or a property under consideration, and <suffix> is
       
    31     an optional description. For example, if we wish to observe the number of
       
    32     A::B() calls, we declare a global integer a_b_callcount and increment it
       
    33     every time A::B() is called. The variable is also declared in the header
       
    34     file of the mocked class by using the extern keyword. Typically, these
       
    35     global variables are defined and reset in the unit-test class' helper
       
    36     functions.
       
    37     
       
    38     Mocked HbLabel class' global variables:
       
    39     
       
    40         • argument of setNumber() and/or setPlainText(),
       
    41         • number of setNumber() calls, and
       
    42         • number of setPlaintText() calls.
       
    43  */
       
    44 
       
    45 int hbframeitem_framedrawer_callcount;
       
    46 int hbstyle_setitemname_callcount;
       
    47 int hbframedrawer_setframetype_callcount;
       
    48 int hbtextedit_setreadonly_callcount;
       
    49 int hbtextedit_setsmileysenabled_callcount;
       
    50 const QString SUBJECT = "Subject";
       
    51 const QString BODYTEXT = "Body Text";
       
    52 
       
    53 QGraphicsWidget *hbwidget_parent;
       
    54 
       
    55 /*
       
    56     The unit-test class. The class definition contains four helper functions
       
    57     and a number of test functions. The helper functions are responsible for
       
    58     the initialization and cleanup of the test environment, whereas the test
       
    59     functions implement the actual testing. Although the test functions are
       
    60     executed in the order they are defined, they should not depend on each
       
    61     other in any way. In other words, one should be able to scramble the test
       
    62     functions and still compile a working unit test.
       
    63  */
       
    64 class TestMsgConversationWidget : public QObject
       
    65 {
       
    66     Q_OBJECT
       
    67 private slots:
       
    68     void initTestCase();
       
    69     void cleanupTestCase();
       
    70     void init();
       
    71     void cleanup();
       
    72     void testConstructor();
       
    73     void testSetSubject();
       
    74     void testSetBodyText();
       
    75 };
       
    76 
       
    77 /*
       
    78     Initializes the test environment. This function is automatically invoked
       
    79     before the first test function.
       
    80  */
       
    81 void TestMsgConversationWidget::initTestCase()
       
    82 {
       
    83 }
       
    84 
       
    85 /*
       
    86     Cleans up the test environment. This function is automatically invoked
       
    87     after the last test function.
       
    88  */
       
    89 void TestMsgConversationWidget::cleanupTestCase()
       
    90 {
       
    91 }
       
    92 
       
    93 /*
       
    94     Performs initialization for a single test function. This function is
       
    95     automatically invoked before each test function.
       
    96  */
       
    97 void TestMsgConversationWidget::init()
       
    98 {
       
    99     hbframeitem_framedrawer_callcount = 0;
       
   100     hbstyle_setitemname_callcount = 0;
       
   101     hbframedrawer_setframetype_callcount = 0;
       
   102     hbtextedit_setsmileysenabled_callcount = 0;
       
   103     hbtextedit_setreadonly_callcount = 0;
       
   104     hbwidget_parent = NULL;
       
   105 }
       
   106 
       
   107 /*
       
   108     Performs cleanup for a single test function. This function is auto-
       
   109     matically invoked after each test function.
       
   110  */
       
   111 void TestMsgConversationWidget::cleanup()
       
   112 {
       
   113 }
       
   114 
       
   115 /*
       
   116     Test the constructor with different parent arguments:
       
   117     
       
   118         1. null parent (i.e. default parent argument) and
       
   119         2. non-null parent.
       
   120  */
       
   121 void TestMsgConversationWidget::testConstructor()
       
   122 {
       
   123     // 1.
       
   124     MsgConversationWidget *widget = new MsgConversationWidget();
       
   125     QVERIFY(!hbwidget_parent);
       
   126     delete widget;
       
   127     
       
   128     // 2.
       
   129     MsgConversationWidget parent(NULL);
       
   130     widget = new MsgConversationWidget(&parent);
       
   131     QVERIFY(hbwidget_parent == &parent);
       
   132     
       
   133     QVERIFY(6 == hbframeitem_framedrawer_callcount);
       
   134     QVERIFY(6 == hbframedrawer_setframetype_callcount);
       
   135     QVERIFY(6 == hbstyle_setitemname_callcount);
       
   136     
       
   137     delete widget;
       
   138 }
       
   139 
       
   140 void TestMsgConversationWidget::testSetSubject()
       
   141 {
       
   142     // 1.
       
   143     MsgConversationWidget* myWidget = new MsgConversationWidget();
       
   144     myWidget->setSubject(SUBJECT);
       
   145     QVERIFY(1 == hbtextedit_setreadonly_callcount);
       
   146     QVERIFY(1 == hbtextedit_setsmileysenabled_callcount);
       
   147     
       
   148     delete myWidget;
       
   149 }
       
   150 
       
   151 void TestMsgConversationWidget::testSetBodyText()
       
   152 {
       
   153     // 1.
       
   154     MsgConversationWidget* myWidget = new MsgConversationWidget();
       
   155     myWidget->setBodyText(BODYTEXT);
       
   156     QVERIFY(1 == hbtextedit_setreadonly_callcount);
       
   157     QVERIFY(1 == hbtextedit_setsmileysenabled_callcount);
       
   158     
       
   159     delete myWidget;
       
   160 }
       
   161 
       
   162 /*
       
   163     Implement a main() function that initializes the test environment,
       
   164     executes all tests in the order they were defined, and finally cleans up
       
   165     the test environment.
       
   166  */
       
   167 QTEST_APPLESS_MAIN(TestMsgConversationWidget)
       
   168 
       
   169 /*
       
   170     Because both the declaration and the implementation of our test class are
       
   171     in a single .cpp file, we also need to include the generated moc file to
       
   172     make Qt's introspection work.
       
   173  */
       
   174 #include "unittest_msgconversationwidget.moc"