radiohswidget/tsrc/src/t_radiohswidget.cpp
branchRCL_3
changeset 19 cce62ebc198e
equal deleted inserted replaced
18:1a6714c53019 19:cce62ebc198e
       
     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 // System includes
       
    19 #include <QVariant>
       
    20 
       
    21 // User Includes
       
    22 #include "t_radiohswidget.h"
       
    23 #include "xqsettingsmanager.h"
       
    24 #include "xqapplicationmanager.h"
       
    25 #include "xqaiwrequest.h"
       
    26 #include "xqaiwinterfacedescriptor.h"
       
    27 #include "radioservicedef.h"
       
    28 #include "radiologger.h"
       
    29 
       
    30 // Constants
       
    31 /**
       
    32 * From ProfileEngineSDKCRKeys.h
       
    33 * The id of the currently active profile, integer value.
       
    34 * Possible values:
       
    35 *
       
    36 * 0 = General profile (default value)<br>
       
    37 * 1 = Silent profile<br>
       
    38 * 2 = Meeting profile<br>
       
    39 * 3 = Outdoor profile<br>
       
    40 * 4 = Pager profile<br>
       
    41 * 5 = Off-line profile<br>
       
    42 * 6 = Drive profile<br>
       
    43 * 30-49 = User-created profiles<br>
       
    44 **/
       
    45 const int PROFILE_GENERAL(0); 
       
    46 const int PROFILE_SILENT(1); 
       
    47 const int PROFILE_MEETING(2); 
       
    48 const int PROFILE_OUTDOOR(3); 
       
    49 const int PROFILE_PAGER(4); 
       
    50 const int PROFILE_OFFLINE(5); 
       
    51 const int PROFILE_DRIVE(6); 
       
    52 const int PROFILE_USER_CREATED(35);
       
    53 
       
    54 /** Requests are not embedded. */
       
    55 const bool EMBEDDED_REQUEST(false);
       
    56 
       
    57 /** Last request should be answered with empty reply. */
       
    58 int EMPTY_REPLY(-5);
       
    59 
       
    60 /** Test values for favorite station count. */
       
    61 const int FAVORITE_STATION_COUNT_INVALID_TOO_LOW1(-56);
       
    62 const int FAVORITE_STATION_COUNT_INVALID_TOO_LOW2(-1);
       
    63 const int FAVORITE_STATION_COUNT_VALID_LOWER_BOUNDARY(0);
       
    64 const int FAVORITE_STATION_COUNT_VALID1(1);
       
    65 const int FAVORITE_STATION_COUNT_VALID2(2);
       
    66 const int FAVORITE_STATION_COUNT_VALID3(3);
       
    67 const int FAVORITE_STATION_COUNT_VALID4(9);
       
    68 const int FAVORITE_STATION_COUNT_VALID5(78);
       
    69 const int FAVORITE_STATION_COUNT_VALID_UPPER_BOUNDARY(100);
       
    70 const int FAVORITE_STATION_COUNT_INVALID_TOO_HIGH1(101);
       
    71 const int FAVORITE_STATION_COUNT_INVALID_TOO_HIGH2(1256);
       
    72 
       
    73 /** Test values for textual information. */
       
    74 const QString TEST_FREQUENCY("99.9");
       
    75 const QString TEST_NAME("Radio Test");
       
    76 const QString TEST_GENRE("Test GenRe");
       
    77 const QString TEST_RADIO_TEXT("Test RDS");
       
    78 const QString TEST_DYNAMIC_PSNAME("Dynamic PS Name test");
       
    79 const QString TEST_EMPTY_STRING("");
       
    80 const QString TEST_INVALID_STRING;
       
    81 /** This test string contains 1000 characters. */
       
    82 const QString TEST_TOO_LONG_STRING("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
       
    83 
       
    84 /** Test values for service notification types. */
       
    85 const int RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_LOW1(-357);
       
    86 const int RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_LOW2(-1);
       
    87 const int RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_LOW3(0);
       
    88 const int RADIO_SERVICE_NOTIFICATION_ID_VALID_LOWER_BOUNDARY(1);
       
    89 const int RADIO_SERVICE_NOTIFICATION_ID_VALID_UPPER_BOUNDARY(8);
       
    90 const int RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_HIGH1(9);
       
    91 const int RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_HIGH2(57982);
       
    92 const int RADIO_STATUS_INVALID_TOO_LOW1(-581);
       
    93 const int RADIO_STATUS_INVALID_TOO_LOW2(-1);
       
    94 const int RADIO_STATUS_VALID_LOWER_BOUNDARY(0);
       
    95 const int RADIO_STATUS_VALID_UPPER_BOUNDARY(5);
       
    96 const int RADIO_STATUS_INVALID_TOO_HIGH1(6);
       
    97 const int RADIO_STATUS_INVALID_TOO_HIGH2(456);
       
    98 
       
    99 /** Mock returns for settings manager stub. */
       
   100 QVariant settings_manager_mock_return_profile;
       
   101 QVariant settings_manager_mock_return_2nd_profile;
       
   102 bool settings_manager_mock_bool_return;
       
   103 
       
   104 /** These request are defined in stub xqapplicationmanager.cpp. */
       
   105 extern XQAiwRequest *application_manager_mock_refresh_request;
       
   106 extern XQAiwRequest *application_manager_mock_monitor_request;
       
   107 
       
   108 /** List for request mocks. */
       
   109 QVariantList request_mocks;
       
   110 /** Amount of requests send. */
       
   111 int send_count;
       
   112 
       
   113 
       
   114 /*!
       
   115     \class TestRadioHsWidget
       
   116     \brief Unit testing for FM Radio hs widget.
       
   117 
       
   118     TestRadioHsWidget implements needed functions for the FM Radio home screen
       
   119     widget unit tests.
       
   120 */
       
   121 
       
   122 // ======== MEMBER FUNCTIONS ========
       
   123 
       
   124 /*!
       
   125     Main function.
       
   126     
       
   127     There's been problems with QTEST_MAIN macro, so main function is defined
       
   128     here.
       
   129 */
       
   130 int main(int /* argc*/, char *argv[])
       
   131 {
       
   132     LOG_METHOD_ENTER;
       
   133     TestRadioHsWidget tv;
       
   134 
       
   135     char *pass[3];
       
   136     pass[0] = argv[0];
       
   137     pass[1] = "-o";
       
   138     pass[2] = "c:\\data\\testradiohswidget.txt";
       
   139     
       
   140     int res = QTest::qExec(&tv, 3, pass);
       
   141 
       
   142     return res;
       
   143 }
       
   144 
       
   145 /*!
       
   146     Constructor.
       
   147  */
       
   148 TestRadioHsWidget::TestRadioHsWidget()
       
   149 {
       
   150     LOG_METHOD_ENTER;
       
   151 }
       
   152 
       
   153 
       
   154 /*!
       
   155     Destructor.
       
   156  */
       
   157 TestRadioHsWidget::~TestRadioHsWidget()
       
   158 {
       
   159     LOG_METHOD_ENTER;
       
   160 }
       
   161 
       
   162 /*!
       
   163     Called before each testfunction is executed.
       
   164  */
       
   165 void TestRadioHsWidget::init()
       
   166 {
       
   167     LOG_METHOD_ENTER;
       
   168     settings_manager_mock_return_profile = PROFILE_GENERAL;
       
   169     request_mocks.clear();
       
   170     mSendInfos.clear();
       
   171     send_count = -1;
       
   172     mRadioHsWidgetStub.data()->reset();
       
   173 }
       
   174 
       
   175 /*!
       
   176     Called after every testfunction.
       
   177  */
       
   178 void TestRadioHsWidget::cleanup()
       
   179 {
       
   180     LOG_METHOD_ENTER;
       
   181 }
       
   182 
       
   183 /*!
       
   184     Called before the first testfunction is executed
       
   185  */
       
   186 void TestRadioHsWidget::initTestCase()
       
   187 {
       
   188     LOG_METHOD_ENTER;
       
   189     mRadioHsWidgetStub.reset(new RadioHsWidget());
       
   190     QVERIFY(mRadioHsWidgetStub);
       
   191     mProfileReader.reset(new RadioHsWidgetProfileReader(mRadioHsWidgetStub.data()));
       
   192     QVERIFY(mProfileReader);
       
   193     mServiceClient.reset(new RadioHsWidgetRadioServiceClient(mRadioHsWidgetStub.data()));
       
   194     QVERIFY(mServiceClient);
       
   195 }
       
   196 
       
   197 /*!
       
   198     Called after the last testfunction was executed.
       
   199  */
       
   200 void TestRadioHsWidget::cleanupTestCase()
       
   201 {
       
   202     LOG_METHOD_ENTER;
       
   203 }
       
   204 
       
   205 /*!
       
   206     Tests RadioHsWidgetProfileReader and it's isCurrentProfileOffline()
       
   207     function.
       
   208  */
       
   209 void TestRadioHsWidget::offlineModeTest()
       
   210 {
       
   211     LOG_METHOD;
       
   212     settings_manager_mock_return_profile = PROFILE_GENERAL;
       
   213     bool isOffline = mProfileReader.data()->isCurrentProfileOffline();
       
   214     QVERIFY2(( isOffline == false ), "FmRadioHsWidget:RadioHsWidget profile is offline");
       
   215     settings_manager_mock_return_profile = PROFILE_OFFLINE;
       
   216     isOffline = mProfileReader.data()->isCurrentProfileOffline();
       
   217     QVERIFY2(( isOffline == true ), "FmRadioHsWidget:RadioHsWidget profile is not offline");
       
   218     settings_manager_mock_return_profile = PROFILE_USER_CREATED;
       
   219     isOffline = mProfileReader.data()->isCurrentProfileOffline();
       
   220     QVERIFY2(( isOffline == false ), "FmRadioHsWidget:RadioHsWidget profile is offline");
       
   221 }
       
   222 
       
   223 /*!
       
   224     Tests RadioHsWidgetRadioServiceClient class and it's ability to receive
       
   225     and forward valid information.
       
   226  */
       
   227 void TestRadioHsWidget::validReplies()
       
   228 {
       
   229     LOG_METHOD;
       
   230     // Replies that should be ok.
       
   231     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_VALID_LOWER_BOUNDARY));
       
   232     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_VALID1));
       
   233     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_VALID2));
       
   234     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_VALID3));
       
   235     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_VALID4));
       
   236     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_VALID5));
       
   237     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_VALID_UPPER_BOUNDARY));
       
   238     createSendInfo(RadioServiceNotification::CurrentIsFavorite, QVariant(true));
       
   239     createSendInfo(RadioServiceNotification::CurrentIsFavorite, QVariant(false));
       
   240     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RadioStatus::UnSpecified));
       
   241     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RadioStatus::Playing));
       
   242     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RadioStatus::Muted));
       
   243     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RadioStatus::Seeking));
       
   244     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RadioStatus::NoAntenna));
       
   245     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RadioStatus::PoweringOff));
       
   246     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RADIO_STATUS_VALID_LOWER_BOUNDARY));
       
   247     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RADIO_STATUS_VALID_UPPER_BOUNDARY));
       
   248     createSendInfo(RadioServiceNotification::Frequency, QVariant(TEST_FREQUENCY));
       
   249     createSendInfo(RadioServiceNotification::Name, QVariant(TEST_NAME));
       
   250     createSendInfo(RadioServiceNotification::Name, QVariant(TEST_EMPTY_STRING));
       
   251     createSendInfo(RadioServiceNotification::Genre, QVariant(TEST_GENRE));
       
   252     createSendInfo(RadioServiceNotification::RadioText, QVariant(TEST_RADIO_TEXT));
       
   253     createSendInfo(RadioServiceNotification::DynamicPS, QVariant(TEST_DYNAMIC_PSNAME));
       
   254     // Empty reply should be last to put RServiceClient to make an request, which is not replied. 
       
   255     createSendInfo(EMPTY_REPLY, QVariant(EMPTY_REPLY));
       
   256 
       
   257     send_count = mSendInfos.count();
       
   258     
       
   259     mServiceClient.data()->startMonitoring(FmRadio::VisibiltyToBackground);
       
   260     mServiceClient.data()->stopMonitoring();
       
   261     
       
   262     int receivedCount = mRadioHsWidgetStub.data()->receivedInformationCount();
       
   263     QVERIFY2((receivedCount == send_count - 1), "Send and received counts doesn't match");
       
   264     
       
   265     ReceivedInfo temp;
       
   266     for (int i = 0; i < send_count - 1; i++) {
       
   267         temp = mRadioHsWidgetStub.data()->receivedInformation();
       
   268         QVERIFY(temp.informationType == mSendInfos.at(i).informationType);
       
   269         verifyReceivedInformation(temp.informationType, temp.information, mSendInfos.at(i).information);
       
   270     }
       
   271 }
       
   272 
       
   273 /*!
       
   274     Tests RadioHsWidgetRadioServiceClient class and it's ability to receive
       
   275     and forward invalid information.
       
   276  */
       
   277 void TestRadioHsWidget::invalidReplies()
       
   278 {
       
   279     LOG_METHOD;
       
   280     QList<int> skipSendInfo;
       
   281     // Replies that should not be ok.
       
   282     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_INVALID_TOO_LOW1));
       
   283     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_INVALID_TOO_LOW2));
       
   284     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_INVALID_TOO_HIGH1));
       
   285     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant(FAVORITE_STATION_COUNT_INVALID_TOO_HIGH2));
       
   286     createSendInfo(RadioServiceNotification::FavoriteCount, QVariant());
       
   287     skipSendInfo.append(request_mocks.count() - 1);
       
   288     createSendInfo(RadioServiceNotification::CurrentIsFavorite, QVariant());
       
   289     skipSendInfo.append(request_mocks.count() - 1);
       
   290     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RADIO_STATUS_INVALID_TOO_LOW1));
       
   291     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RADIO_STATUS_INVALID_TOO_LOW1));
       
   292     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RADIO_STATUS_INVALID_TOO_HIGH1));
       
   293     createSendInfo(RadioServiceNotification::RadioStatus, QVariant(RADIO_STATUS_INVALID_TOO_HIGH2));
       
   294     createSendInfo(RadioServiceNotification::RadioStatus, QVariant());
       
   295     skipSendInfo.append(request_mocks.count() - 1);
       
   296     createSendInfo(RadioServiceNotification::Frequency, QVariant());
       
   297     skipSendInfo.append(request_mocks.count() - 1);
       
   298     createSendInfo(RadioServiceNotification::Name, QVariant(TEST_INVALID_STRING));
       
   299     createSendInfo(RadioServiceNotification::Name, QVariant(TEST_TOO_LONG_STRING));
       
   300     createSendInfo(RadioServiceNotification::Name, QVariant());
       
   301     skipSendInfo.append(request_mocks.count() - 1);
       
   302     createSendInfo(RadioServiceNotification::Genre, QVariant());
       
   303     skipSendInfo.append(request_mocks.count() - 1);
       
   304     createSendInfo(RadioServiceNotification::RadioText, QVariant());
       
   305     skipSendInfo.append(request_mocks.count() - 1);
       
   306     createSendInfo(RadioServiceNotification::DynamicPS, QVariant());
       
   307     skipSendInfo.append(request_mocks.count() - 1);
       
   308     // Replies that should generate a warning.
       
   309     createSendInfo(RADIO_SERVICE_NOTIFICATION_ID_VALID_LOWER_BOUNDARY, QVariant(TEST_EMPTY_STRING));
       
   310     createSendInfo(RADIO_SERVICE_NOTIFICATION_ID_VALID_UPPER_BOUNDARY, QVariant(TEST_EMPTY_STRING));
       
   311     createSendInfo(RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_LOW1, QVariant(TEST_EMPTY_STRING));
       
   312     createSendInfo(RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_LOW2, QVariant(TEST_EMPTY_STRING));
       
   313     createSendInfo(RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_LOW3, QVariant(TEST_EMPTY_STRING));
       
   314     createSendInfo(RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_HIGH1, QVariant(TEST_EMPTY_STRING));
       
   315     createSendInfo(RADIO_SERVICE_NOTIFICATION_ID_INVALID_TOO_HIGH2, QVariant(TEST_EMPTY_STRING));
       
   316     // Empty reply should be last to put RServiceClient to make an request, which is not replied. 
       
   317     createSendInfo(EMPTY_REPLY, QVariant(EMPTY_REPLY));
       
   318     
       
   319     send_count = mSendInfos.count();
       
   320     
       
   321     mServiceClient.data()->startMonitoring(FmRadio::VisibiltyToBackground);
       
   322     mServiceClient.data()->stopMonitoring();
       
   323     
       
   324     int receivedCount = mRadioHsWidgetStub.data()->receivedInformationCount();
       
   325     int skippedCount = skipSendInfo.count();
       
   326     QVERIFY2((receivedCount == send_count - skippedCount - 1), "Send and received counts doesn't match");
       
   327     
       
   328     ReceivedInfo temp;
       
   329     for (int i = 0; i < send_count - 1; i++) {
       
   330         if (skipSendInfo.contains(i)) {
       
   331             // Skip verification for information that was filtered in
       
   332             // RadioHsWidgetRadioServiceClient.
       
   333             continue;
       
   334         }
       
   335         temp = mRadioHsWidgetStub.data()->receivedInformation();
       
   336         QVERIFY(temp.informationType == mSendInfos.at(i).informationType);
       
   337         verifyReceivedInformation(temp.informationType, temp.information, mSendInfos.at(i).information);
       
   338     }
       
   339 }
       
   340 
       
   341 /*!
       
   342     Helper function to store send information and append it to a request
       
   343     mocks. 
       
   344  */
       
   345 void TestRadioHsWidget::createSendInfo(int informationType, const QVariant information)
       
   346 {
       
   347     SendInfo info;
       
   348     QVariantList notificationList;
       
   349     
       
   350     info.informationType = informationType;
       
   351     info.information = information;
       
   352     info.data = QVariant(mServiceClient.data()->
       
   353         createRadioNotificationData(
       
   354             info.informationType,
       
   355             info.information
       
   356             ));
       
   357     
       
   358     mSendInfos.append(info);
       
   359 
       
   360     notificationList.append(info.data);
       
   361     request_mocks.append(QVariant(notificationList));
       
   362 }
       
   363 
       
   364 /*!
       
   365     Helper function to verify receved information.
       
   366  */
       
   367 void TestRadioHsWidget::verifyReceivedInformation(int informationType,
       
   368     const QVariant &receivedInformation, const QVariant &sendInformation)
       
   369 {
       
   370     switch (informationType) {
       
   371     case RadioServiceNotification::CurrentIsFavorite:
       
   372         QVERIFY(receivedInformation.canConvert(QVariant::Bool));
       
   373         QCOMPARE(receivedInformation.toBool(), sendInformation.toBool());
       
   374         break;
       
   375     case RadioServiceNotification::FavoriteCount:
       
   376         // Fall through because of similar handling
       
   377     case RadioServiceNotification::RadioStatus:
       
   378         QVERIFY(receivedInformation.canConvert(QVariant::Int));
       
   379         QCOMPARE(receivedInformation.toInt(), sendInformation.toInt());
       
   380         break;
       
   381     case RadioServiceNotification::Frequency:
       
   382         // Fall through because of similar type of information and thus
       
   383         // similar handling
       
   384     case RadioServiceNotification::Name:
       
   385         // Fall through because of similar type of information and thus
       
   386         // similar handling
       
   387     case RadioServiceNotification::Genre:
       
   388         // Fall through because of similar type of information and thus
       
   389         // similar handling
       
   390     case RadioServiceNotification::RadioText:
       
   391         // Fall through because of similar type of information and thus
       
   392         // similar handling
       
   393     case RadioServiceNotification::DynamicPS:
       
   394         QVERIFY(receivedInformation.canConvert(QVariant::String));
       
   395         QCOMPARE(receivedInformation.toString(), sendInformation.toString());
       
   396         break;
       
   397     default:
       
   398         QWARN("Default case, shouldn't be here except if specifically testing RadioServiceNotification id out of bounds.");
       
   399         QVERIFY(receivedInformation.canConvert(QVariant::String));
       
   400         QCOMPARE(receivedInformation.toString(), sendInformation.toString());
       
   401         break;
       
   402     }
       
   403 }
       
   404 
       
   405 //QTEST_MAIN(t_radiohswidget)