cmmanager/connection_settings_shim/tsrc/ut/testcmmgrshim.cpp
branchRCL_3
changeset 58 83ca720e2b9a
parent 57 05bc53fe583b
child 62 bb1f80fb7db2
equal deleted inserted replaced
57:05bc53fe583b 58:83ca720e2b9a
     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 * CM Application Settings UI unit testing.
       
    16 */
       
    17 
       
    18 #include <QList>
       
    19 #include <QString>
       
    20 
       
    21 #include <HbApplication>
       
    22 #include <HbMainWindow>
       
    23 #include <HbView>
       
    24 #include <HbLabel>
       
    25 
       
    26 #include <QtTest/QtTest>
       
    27 
       
    28 #include "cmdestination_shim.h"
       
    29 #include "cmconnectionmethod_shim.h"
       
    30 
       
    31 #include "testcmmgrshim.h"
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // STATIC TEST DATA
       
    35 // -----------------------------------------------------------------------------
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // TEST MACROES
       
    39 // -----------------------------------------------------------------------------
       
    40 
       
    41 // Test macro for verifying an exception throwing code block
       
    42 #define TEST_CATCH_AND_VERIFY(code, error) \
       
    43     { \
       
    44     int error_code; \
       
    45     QT_TRYCATCH_ERROR(error_code, code); \
       
    46     QCOMPARE(error_code, error); \
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // FRAMEWORK FUNCTIONS
       
    51 // -----------------------------------------------------------------------------
       
    52 
       
    53 /**
       
    54  * Test main function. Runs all test cases.
       
    55  */
       
    56 int main(int argc, char *argv[])
       
    57 {
       
    58     Q_UNUSED(argc);
       
    59     
       
    60     char *pass[3];  
       
    61     pass[0] = argv[0];
       
    62     pass[1] = "-o"; 
       
    63     pass[2] = "c:\\data\\TestCmMgrShim.txt";
       
    64  
       
    65     TestCmMgrShim tc;
       
    66     int res = QTest::qExec(&tc, 3, pass);
       
    67  
       
    68     return res;
       
    69 }
       
    70 
       
    71 /**
       
    72  * This function is be called before the first test case is executed.
       
    73  */
       
    74 void TestCmMgrShim::initTestCase()
       
    75 {
       
    76 }
       
    77 
       
    78 /**
       
    79  * This function is be called after the last test case was executed.
       
    80  */
       
    81 void TestCmMgrShim::cleanupTestCase()
       
    82 {
       
    83 }
       
    84 
       
    85 /**
       
    86  * This function is be called before each test case is executed.
       
    87  */
       
    88 void TestCmMgrShim::init()
       
    89 {
       
    90     // Initialize the CmManagerShim object
       
    91     mCmManagerShim = new CmManagerShim;
       
    92 }
       
    93 
       
    94 /**
       
    95  * This function is be called after each test case is executed.
       
    96  */
       
    97 void TestCmMgrShim::cleanup()
       
    98 {
       
    99     // There should be no destinations
       
   100     QList<uint> destinations;
       
   101     mCmManagerShim->allDestinations(destinations);
       
   102     int destinationCount = destinations.count(); 
       
   103     
       
   104     // Delete the destinations if there were any
       
   105     deleteDestinations();
       
   106 
       
   107     // There should be no connection methods
       
   108     QList<uint> connMethods;
       
   109     mCmManagerShim->connectionMethod(connMethods, false);
       
   110     int connMethodCount = connMethods.count(); 
       
   111     
       
   112     // Delete the connection methods if there were any
       
   113     deleteConnectionMethods();
       
   114 
       
   115     // Validate after the deletions are done
       
   116     QCOMPARE(destinationCount, 0);
       
   117     QCOMPARE(connMethodCount, 0);
       
   118 
       
   119     delete mCmManagerShim;
       
   120 }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // TEST CASES
       
   124 // -----------------------------------------------------------------------------
       
   125 
       
   126 /**
       
   127  * Test case for testing initial state.
       
   128  * -Reads AND DELETES all destinations and connection methods.
       
   129  * -Verifies that there are no destinations and connection methods.
       
   130  */
       
   131 void TestCmMgrShim::tcResetSettings()
       
   132 {
       
   133     // Read all destinations
       
   134     QList<uint> destinations;
       
   135     mCmManagerShim->allDestinations(destinations);
       
   136     
       
   137     // Delete the destinations
       
   138     for (int i=0; i < destinations.count(); i++) {
       
   139         CmDestinationShim *dest = mCmManagerShim->destination(destinations[i]);
       
   140         dest->deleteDestination();
       
   141         
       
   142         // Update should leave
       
   143         TEST_CATCH_AND_VERIFY(
       
   144             dest->update(),
       
   145             KErrBadHandle);
       
   146         
       
   147         delete dest;
       
   148     }
       
   149     
       
   150     // Check that there no longer are any destinations
       
   151     mCmManagerShim->allDestinations(destinations);
       
   152     QCOMPARE(destinations.count(), 0);
       
   153     
       
   154     // Read all connection methods
       
   155     QList<uint> connMethods;
       
   156     mCmManagerShim->connectionMethod(connMethods, false);
       
   157     
       
   158     // Remove possible uncategorized connection methods
       
   159     for (int i=0; i < connMethods.count(); i++){
       
   160         CmConnectionMethodShim *cm = 
       
   161             mCmManagerShim->connectionMethod(connMethods[i]);
       
   162         QVERIFY(cm != NULL);
       
   163         bool ok = cm->deleteConnectionMethod();
       
   164         QVERIFY(ok == true);
       
   165         cm->update();
       
   166         delete cm;
       
   167     }
       
   168     
       
   169     // Check that tere is no longer any connection methods
       
   170     mCmManagerShim->connectionMethod(connMethods, false);
       
   171     QCOMPARE(connMethods.count(), 0);
       
   172     
       
   173     // Try reading a Connection Method with an erroneous ID
       
   174     TEST_CATCH_AND_VERIFY(
       
   175         mCmManagerShim->connectionMethod(0),
       
   176         KErrArgument);
       
   177     
       
   178     // Try reading a non-existent Connection Method
       
   179     TEST_CATCH_AND_VERIFY(
       
   180         mCmManagerShim->connectionMethod(1),
       
   181         KErrNotFound);
       
   182 }
       
   183 
       
   184 /**
       
   185  * Test case for testing basic legacy WLAN connection method handling.
       
   186  * -Creates a legacy WLAN connection method (i.e. does not belong to
       
   187  *  any destination).
       
   188  * -Deletes the connection method. 
       
   189  */
       
   190 void TestCmMgrShim::tcLegacyConnMethodWlan()
       
   191 {
       
   192     // Create the connection method
       
   193     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   194         CMManagerShim::BearerTypeWlan);
       
   195     QVERIFY(cm != NULL);
       
   196     
       
   197     // Update to CommsDat
       
   198     cm->update();
       
   199     
       
   200     // Check bearer type
       
   201     int bearer = cm->getIntAttribute(CMManagerShim::CmBearerType);
       
   202     QCOMPARE(bearer, (int)CMManagerShim::BearerTypeWlan);
       
   203     
       
   204     // Check ID
       
   205     int id = cm->getIntAttribute(CMManagerShim::CmId);
       
   206     QCOMPARE(id, 1);
       
   207     
       
   208     // Delete the connection method reference
       
   209     delete cm;
       
   210     
       
   211     // Refetch the connection method
       
   212     cm = mCmManagerShim->connectionMethod(id);
       
   213     QVERIFY(cm != NULL);
       
   214     
       
   215     // Delete the connection method
       
   216     cm->deleteConnectionMethod();
       
   217     
       
   218     // Check that double deletion throws an exception
       
   219     TEST_CATCH_AND_VERIFY(
       
   220         cm->deleteConnectionMethod(),
       
   221         KErrBadHandle);
       
   222     
       
   223     delete cm;
       
   224 }
       
   225 
       
   226 /**
       
   227  * Test case for testing basic legacy GPRS connection method handling.
       
   228  * -Creates a legacy GPRS connection method (i.e. does not belong to
       
   229  *  any destination).
       
   230  * -Deletes the connection method. 
       
   231  */
       
   232 void TestCmMgrShim::tcLegacyConnMethodGprs()
       
   233 {
       
   234     // Create the connection method
       
   235     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   236         CMManagerShim::BearerTypePacketData);
       
   237     QVERIFY(cm != NULL);
       
   238     
       
   239     // Update to CommsDat
       
   240     cm->update();
       
   241     
       
   242     // Check ID
       
   243     int id = cm->getIntAttribute(CMManagerShim::CmId);
       
   244     QCOMPARE(id, 1);
       
   245     
       
   246     // Check bearer type
       
   247     int bearer = cm->getIntAttribute(CMManagerShim::CmBearerType);
       
   248     QCOMPARE(bearer, (int)CMManagerShim::BearerTypePacketData);
       
   249     
       
   250     // Delete the connection method reference
       
   251     delete cm;
       
   252     
       
   253     // Refetch the connection method
       
   254     cm = mCmManagerShim->connectionMethod(id);
       
   255     QVERIFY(cm != NULL);
       
   256     
       
   257     // Delete the connection method
       
   258     cm->deleteConnectionMethod();
       
   259     delete cm;
       
   260 }
       
   261 
       
   262 /**
       
   263  * Basic error case testing for CmManagerShim.
       
   264  * All test call should throw an exception since they are invalid.
       
   265  */
       
   266 void TestCmMgrShim::tcCmManagerBasicFails()
       
   267 {
       
   268     // Try creating a destination with an invalid name
       
   269     CmDestinationShim *dest = 0;
       
   270     TEST_CATCH_AND_VERIFY(
       
   271         dest = mCmManagerShim->createDestination(QString("")),
       
   272         KErrArgument);
       
   273     QVERIFY(dest == NULL);
       
   274     
       
   275     // Try creating a connection method with an invalid bearer type
       
   276     CmConnectionMethodShim *cm = 0;
       
   277     TEST_CATCH_AND_VERIFY(
       
   278         cm = mCmManagerShim->createConnectionMethod(0),
       
   279         KErrArgument);
       
   280     QVERIFY(cm == NULL);
       
   281     
       
   282     // Try fetching a connection method with an invalid ID
       
   283     cm = 0;
       
   284     TEST_CATCH_AND_VERIFY(
       
   285         cm = mCmManagerShim->connectionMethod(42),
       
   286         KErrNotFound);
       
   287     QVERIFY(cm == NULL);
       
   288     
       
   289     // Try fetching a destination with an invalid ID
       
   290     dest = 0;
       
   291     TEST_CATCH_AND_VERIFY(
       
   292         dest = mCmManagerShim->destination(42),
       
   293         KErrArgument);
       
   294     QVERIFY(dest == NULL);
       
   295 }
       
   296 
       
   297 /**
       
   298  * Test case for testing basic destination handling
       
   299  * -Create a destination
       
   300  * -Validate the default destination content
       
   301  * -Delete the destination
       
   302  */
       
   303 void TestCmMgrShim::tcBasicDestination()
       
   304 {
       
   305     // Create a new destination
       
   306     CmDestinationShim *dest;
       
   307     dest = mCmManagerShim->createDestination("TestDestination");
       
   308     QVERIFY(dest != NULL);
       
   309     
       
   310     // Update to CommsDat
       
   311     dest->update();
       
   312     
       
   313     // Check the name
       
   314     QString name(dest->name());
       
   315     QCOMPARE(name, QString("TestDestination"));
       
   316 
       
   317     // Check ID
       
   318     uint destinationId = dest->id();
       
   319     QVERIFY(destinationId >= 4000);
       
   320     
       
   321     // Check protection level default value
       
   322     CMManagerShim::CmmProtectionLevel prot = dest->protectionLevel();
       
   323     QCOMPARE(prot, CMManagerShim::ProtLevel0);
       
   324     
       
   325     // Check hidden flag default value
       
   326     bool isHidden = dest->isHidden(); 
       
   327     QCOMPARE(isHidden, false);
       
   328     
       
   329     // Check destination content
       
   330     int connMethodCount = dest->connectionMethodCount();
       
   331     QCOMPARE(connMethodCount, 0);
       
   332     
       
   333     // Delete the destination
       
   334     dest->deleteDestination();
       
   335 
       
   336     // Check that double deletion throws an exception
       
   337     TEST_CATCH_AND_VERIFY(
       
   338         dest->deleteDestination(),
       
   339         KErrBadHandle);
       
   340 
       
   341     delete dest;
       
   342 }
       
   343 
       
   344 /**
       
   345  * Test case for testing basic modifications for a destination.
       
   346  * -Create a destination
       
   347  * -Add a GPRS connection method to it.
       
   348  * -Add a WLAN connection method to it.
       
   349  * -Delete the GPRS connection method.
       
   350  * -Delete the Destination (and thus also the WLAN connection method).
       
   351  */
       
   352 void TestCmMgrShim::tcDestinationModify()
       
   353 {
       
   354     // Create a new destination
       
   355     CmDestinationShim *dest;
       
   356     dest = mCmManagerShim->createDestination("TestDestination");
       
   357     QVERIFY(dest != NULL);
       
   358     uint destId = dest->id();
       
   359     QVERIFY(destId >= 4000);
       
   360     
       
   361     // Check destination initial content
       
   362     int connMethodCount = dest->connectionMethodCount();
       
   363     QCOMPARE(connMethodCount, 0);
       
   364     
       
   365     // Create a GPRS connection method
       
   366     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   367         CMManagerShim::BearerTypePacketData);
       
   368     QVERIFY(cm != NULL);
       
   369 
       
   370     // Update to CommsDat
       
   371     cm->update();
       
   372     
       
   373     // Add the connection method to the destination
       
   374     int index = dest->addConnectionMethod(cm);
       
   375     QCOMPARE(index, 0);
       
   376 
       
   377     // Update to CommsDat
       
   378     dest->update();
       
   379     
       
   380     // Create a WLAN connection method
       
   381     CmConnectionMethodShim *cmWlan = mCmManagerShim->createConnectionMethod(
       
   382         CMManagerShim::BearerTypeWlan);
       
   383     QVERIFY(cmWlan != NULL);
       
   384 
       
   385     // Update to CommsDat
       
   386     cmWlan->update();
       
   387 
       
   388     // Add the connection method to the destination
       
   389     index = dest->addConnectionMethod(cmWlan);
       
   390     QCOMPARE(index, 0);
       
   391 
       
   392     // Update to CommsDat
       
   393     dest->update();
       
   394     
       
   395     // Delete the WLAN connection method reference
       
   396     delete cmWlan;
       
   397     cmWlan = 0;
       
   398     
       
   399     // Delete the GPRS connection method
       
   400     dest->deleteConnectionMethod(cm);
       
   401     delete cm;
       
   402     cm = 0;
       
   403     
       
   404     // Update to CommsDat
       
   405     dest->update();
       
   406     
       
   407     // Verify the delete
       
   408     connMethodCount = dest->connectionMethodCount();
       
   409     QCOMPARE(connMethodCount, 1);
       
   410     
       
   411     // Delete the whole destination including the remaining WLAN conn method.
       
   412     dest->deleteDestination();
       
   413     delete dest;
       
   414     
       
   415     // Check that the destination is gone
       
   416     dest = NULL;
       
   417     TEST_CATCH_AND_VERIFY(
       
   418         dest = mCmManagerShim->destination(destId),
       
   419         KErrNotFound);
       
   420     QCOMPARE(dest, (CmDestinationShim *)0);
       
   421 }
       
   422 
       
   423 /**
       
   424  * Test case for adding and reading boolean attribute for a connection method.
       
   425  * -Creates a legacy WLAN connection method (i.e. does not belong to
       
   426  *  any destination).
       
   427  * -Sets a boolean attribute.
       
   428  * -Gets the boolean attribute.
       
   429  * -Deletes the connection method. 
       
   430  */
       
   431 void TestCmMgrShim::tcConnMethodSetBoolAttribute()
       
   432 {
       
   433     // Create the connection method
       
   434     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   435         CMManagerShim::BearerTypeWlan);
       
   436     QVERIFY(cm != NULL);
       
   437     
       
   438     // Update to CommsDat
       
   439     cm->update();
       
   440     
       
   441     // Set bool attribute value
       
   442     cm->setBoolAttribute(CMManagerShim::CmProxyUsageEnabled, true);
       
   443         
       
   444     // Update to CommsDat
       
   445     cm->update();
       
   446      
       
   447     // Get bool attribute value
       
   448     bool testBool = false;
       
   449     testBool = cm->getBoolAttribute(CMManagerShim::CmProxyUsageEnabled);
       
   450     QCOMPARE(testBool, true);
       
   451         
       
   452     // Delete the connection method
       
   453     cm->deleteConnectionMethod();
       
   454     delete cm;
       
   455 }
       
   456 
       
   457 /**
       
   458  * Test case for adding and reading integer attribute for a connection method.
       
   459  * -Creates a legacy WLAN connection method (i.e. does not belong to
       
   460  *  any destination).
       
   461  * -Sets an int attribute.
       
   462  * -Gets the int attribute.
       
   463  * -Deletes the connection method. 
       
   464  */
       
   465 void TestCmMgrShim::tcConnMethodSetIntAttribute()
       
   466 {
       
   467     // Create the connection method
       
   468     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   469         CMManagerShim::BearerTypeWlan);
       
   470     QVERIFY(cm != NULL);
       
   471     
       
   472     // Update to CommsDat
       
   473     cm->update();
       
   474     
       
   475     // Set int attribute value
       
   476     uint testInt = 99;
       
   477     cm->setIntAttribute(CMManagerShim::CmProxyPortNumber, testInt);
       
   478     
       
   479     // Update to CommsDat
       
   480     cm->update();
       
   481      
       
   482     // Get int attribute value
       
   483     uint resultInt = cm->getIntAttribute(CMManagerShim::CmProxyPortNumber);
       
   484     QCOMPARE(resultInt, testInt);    
       
   485     
       
   486     // Delete the connection method
       
   487     cm->deleteConnectionMethod();
       
   488     delete cm;
       
   489 }
       
   490 
       
   491 /**
       
   492  * Test case for adding and reading string attributes for a connection method.
       
   493  * -Creates a legacy WLAN connection method (i.e. does not belong to
       
   494  *  any destination).
       
   495  * -Sets a string attribute.
       
   496  * -Gets the string attribute.
       
   497  * -Deletes the connection method. 
       
   498  */
       
   499 void TestCmMgrShim::tcConnMethodSetStringAttribute()
       
   500 {
       
   501     // Create the connection method
       
   502     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   503         CMManagerShim::BearerTypeWlan);
       
   504     QVERIFY(cm != NULL);
       
   505     
       
   506     // Update to CommsDat
       
   507     cm->update();
       
   508 
       
   509     // Set string attribute value
       
   510     QString testString("TestProxyServerName");
       
   511     cm->setStringAttribute(CMManagerShim::CmProxyServerName, testString);
       
   512 
       
   513     // Set string8 attribute value
       
   514     QString testString2("key12");
       
   515     cm->setString8Attribute(CMManagerShim::WlanWepKey1InAscii, testString2);
       
   516     
       
   517     // Update to CommsDat
       
   518     cm->update();
       
   519      
       
   520     // Check string attribute value
       
   521     QString resultString = cm->getStringAttribute(CMManagerShim::CmProxyServerName);
       
   522     QCOMPARE(resultString, testString);
       
   523 
       
   524     // Check string8 attribute value
       
   525     resultString = cm->getString8Attribute(CMManagerShim::WlanWepKey1InAscii);
       
   526     QCOMPARE(resultString, testString2);
       
   527 
       
   528     // Delete the connection method
       
   529     cm->deleteConnectionMethod();
       
   530     delete cm;
       
   531 }
       
   532 
       
   533 /**
       
   534  * Test case for testing attribute reads using a wrong attribute type.
       
   535  * All test reads in this case should fail to an exception.
       
   536  */
       
   537 void TestCmMgrShim::tcConnMethodWrongTypeAttributeRead()
       
   538 {
       
   539     // Create the connection method
       
   540     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   541         CMManagerShim::BearerTypeWlan);
       
   542     QVERIFY(cm != NULL);
       
   543     
       
   544     // Update to CommsDat
       
   545     cm->update();
       
   546 
       
   547     // Try reading name erroneously as an int attribute
       
   548     uint intResult = 0;
       
   549     TEST_CATCH_AND_VERIFY(
       
   550         intResult = cm->getIntAttribute(CMManagerShim::CmName),
       
   551         KErrNotSupported);
       
   552     QCOMPARE(intResult, (uint)0);
       
   553     
       
   554     // Try reading name erroneously as a bool attribute
       
   555     bool boolResult = false;
       
   556     TEST_CATCH_AND_VERIFY(
       
   557         boolResult = cm->getBoolAttribute(CMManagerShim::CmName),
       
   558         KErrNotSupported);
       
   559     QCOMPARE(boolResult, false);
       
   560     
       
   561     // Try reading ID erroneously as a string attribute
       
   562     QString stringResult;
       
   563     TEST_CATCH_AND_VERIFY(
       
   564         stringResult = cm->getStringAttribute(CMManagerShim::CmId),
       
   565         KErrNotSupported);
       
   566     QCOMPARE(stringResult, QString(""));
       
   567     
       
   568     // Try reading ID erroneously as a string 8 attribute
       
   569     stringResult = "";
       
   570     TEST_CATCH_AND_VERIFY(
       
   571         stringResult = cm->getString8Attribute(CMManagerShim::CmId),
       
   572         KErrNotSupported);
       
   573     QCOMPARE(stringResult, QString(""));
       
   574     
       
   575     // Delete the connection method
       
   576     cm->deleteConnectionMethod();
       
   577     delete cm;
       
   578 }
       
   579 
       
   580 /**
       
   581  * Test case for testing attribute reads using a wrong attribute type.
       
   582  * All test reads in this case should fail to an exception.
       
   583  */
       
   584 void TestCmMgrShim::tcConnMethodWrongTypeAttributeWrite()
       
   585 {
       
   586     // Create the connection method
       
   587     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   588         CMManagerShim::BearerTypeWlan);
       
   589     QVERIFY(cm != NULL);
       
   590     
       
   591     // Update to CommsDat
       
   592     cm->update();
       
   593 
       
   594     // Try writing name erroneously as an int attribute
       
   595     TEST_CATCH_AND_VERIFY(
       
   596         cm->setIntAttribute(CMManagerShim::CmName, 0),
       
   597         KErrNotSupported);
       
   598     
       
   599     // Try writing name erroneously as a bool attribute
       
   600     TEST_CATCH_AND_VERIFY(
       
   601         cm->setBoolAttribute(CMManagerShim::CmName, false),
       
   602         KErrNotSupported);
       
   603     
       
   604     // Try writing ID erroneously as a string attribute
       
   605     TEST_CATCH_AND_VERIFY(
       
   606         cm->setStringAttribute(CMManagerShim::CmId, QString("dada")),
       
   607         KErrNotSupported);
       
   608     
       
   609     // Try reading ID erroneously as a string 8 attribute
       
   610     TEST_CATCH_AND_VERIFY(
       
   611         cm->setString8Attribute(CMManagerShim::CmId, QString("dada")),
       
   612         KErrNotSupported);
       
   613     
       
   614     // Delete the connection method
       
   615     cm->deleteConnectionMethod();
       
   616     delete cm;
       
   617 }
       
   618 
       
   619 /**
       
   620  * Test case for testing connection method refreshing.  
       
   621  * -Create a connection method
       
   622  * -Read the name of the connection method
       
   623  * -Change the name, but don't update CommsDat
       
   624  * -Refsesh connection method
       
   625  * -Check that the name is the original one
       
   626  * -Delete the connection method
       
   627  */
       
   628 void TestCmMgrShim::tcConnectionMethodRefresh()
       
   629 {
       
   630     // Create the connection method
       
   631     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
   632         CMManagerShim::BearerTypeWlan);
       
   633     QVERIFY(cm != NULL);
       
   634         
       
   635     // Update to CommsDat
       
   636     cm->update();
       
   637 
       
   638     // Check ID
       
   639     int id = cm->getIntAttribute(CMManagerShim::CmId);
       
   640     QCOMPARE(id, 1);
       
   641         
       
   642     // Check the default name
       
   643     QString name = cm->getStringAttribute(CMManagerShim::CmName);
       
   644     QVERIFY(name != QString(""));
       
   645         
       
   646     // Change name
       
   647     cm->setStringAttribute(CMManagerShim::CmName, "WlanBlaaBlaa");
       
   648     // Do not update CommsDat
       
   649 
       
   650     // Check the changed name
       
   651     QString newName = cm->getStringAttribute(CMManagerShim::CmName);
       
   652     QCOMPARE(newName, QString("WlanBlaaBlaa"));
       
   653 
       
   654     // Refresh connection method
       
   655     cm->refresh();
       
   656 
       
   657     // Delete the connection method reference
       
   658     delete cm;
       
   659 
       
   660     // Refetch the connection method
       
   661     cm = mCmManagerShim->connectionMethod(id);
       
   662     QVERIFY(cm != NULL);        
       
   663 
       
   664     // Check that the bearer name in database is the original one
       
   665     newName = cm->getStringAttribute(CMManagerShim::CmName);
       
   666     QCOMPARE(newName, name);
       
   667         
       
   668     // Delete the connection method
       
   669     cm->deleteConnectionMethod();
       
   670     delete cm;
       
   671 }
       
   672 
       
   673 /**
       
   674  * Test case for testing reading destination's connection methods.
       
   675  * -Create a destination
       
   676  * -Add a GPRS connection method to it.
       
   677  * -Add a WLAN connection method to it.
       
   678  * -Read number of connection methods.
       
   679  * -Read connection method by index.
       
   680  * -Read connection method by unknown index.
       
   681  * -Read connection method by id.
       
   682  * -Read connection method by unknown id.
       
   683  * -Delete the Destination (and connection methods).
       
   684  */
       
   685 void TestCmMgrShim::tcDestinationReadConnectionMethods()
       
   686 {
       
   687     // Create a new destination
       
   688     CmDestinationShim *dest;
       
   689     dest = mCmManagerShim->createDestination("TestDestination");
       
   690     QVERIFY(dest != NULL);
       
   691     uint destId = dest->id();
       
   692     QVERIFY(destId >= 4000);
       
   693     
       
   694     // Check destination initial content
       
   695     int connMethodCount = dest->connectionMethodCount();
       
   696     QCOMPARE(connMethodCount, 0);
       
   697     
       
   698     // Create a GPRS connection method
       
   699     CmConnectionMethodShim *cmGprs = mCmManagerShim->createConnectionMethod(
       
   700         CMManagerShim::BearerTypePacketData);
       
   701     QVERIFY(cmGprs != NULL);
       
   702     // Update to CommsDat
       
   703     cmGprs->update();
       
   704     
       
   705     // Add the connection method to the destination
       
   706     int index = dest->addConnectionMethod(cmGprs);
       
   707     QCOMPARE(index, 0);
       
   708     // Update to CommsDat
       
   709     dest->update();
       
   710     
       
   711     delete cmGprs;
       
   712     cmGprs = 0;
       
   713     
       
   714     // Create a WLAN connection method
       
   715     CmConnectionMethodShim *cmWlan = mCmManagerShim->createConnectionMethod(
       
   716         CMManagerShim::BearerTypeWlan);
       
   717     QVERIFY(cmWlan != NULL);
       
   718     // Update to CommsDat
       
   719     cmWlan->update();
       
   720 
       
   721     // Add the connection method to the destination
       
   722     index = dest->addConnectionMethod(cmWlan);
       
   723     QCOMPARE(index, 0);
       
   724     // Update to CommsDat
       
   725     dest->update();
       
   726 
       
   727     
       
   728     // Check number of destination's connection methods
       
   729     connMethodCount = dest->connectionMethodCount();
       
   730     QCOMPARE(connMethodCount, 2);
       
   731 
       
   732     // Read connection method by index.
       
   733     CmConnectionMethodShim *cm = dest->connectionMethod(1);
       
   734     QVERIFY(cm != NULL);
       
   735     
       
   736     // Try to read connection method by unknown index.
       
   737     TEST_CATCH_AND_VERIFY(
       
   738             dest->connectionMethod(2),
       
   739             KErrArgument);
       
   740     
       
   741     // Read connection method by id.
       
   742     uint id = cmWlan->getIntAttribute(CMManagerShim::CmId);
       
   743     cm = dest->connectionMethodByID(id);
       
   744     QVERIFY(cm != NULL);
       
   745     
       
   746     delete cmWlan;
       
   747     cmWlan = 0;
       
   748     
       
   749     delete cm;
       
   750     cm = 0;
       
   751     
       
   752     // Try to read connection method by unknown id.    
       
   753     TEST_CATCH_AND_VERIFY(
       
   754         dest->connectionMethodByID(100),
       
   755         KErrNotFound);
       
   756    
       
   757     // Delete the destination (and connection methods).
       
   758     dest->deleteDestination();
       
   759     delete dest;
       
   760 }
       
   761 
       
   762 /**
       
   763  * Test case for testing removing connection method from single destination.
       
   764  * -Create 2 destinations
       
   765  * -Add a WLAN connection method to both.
       
   766  * -Remove the WLAN connection method from the first destination.
       
   767  * -Remove the WLAN connection method from the other destination.
       
   768  * -Delete the Destinations.
       
   769  * -Delete connection method.
       
   770  */
       
   771 void TestCmMgrShim::tcDestinationRemoveConnectionMethod()
       
   772 {
       
   773     // Create a new destination
       
   774     CmDestinationShim *dest1;
       
   775     dest1 = mCmManagerShim->createDestination("TestDestination1");
       
   776     QVERIFY(dest1 != NULL);
       
   777     uint destId1 = dest1->id();
       
   778     QVERIFY(destId1 >= 4000);
       
   779 
       
   780     // Create another destination
       
   781     CmDestinationShim *dest2;
       
   782     dest2 = mCmManagerShim->createDestination("TestDestination2");
       
   783     QVERIFY(dest2 != NULL);
       
   784     uint destId2 = dest2->id();
       
   785     QVERIFY(destId2 >= 4000);
       
   786     
       
   787     // Check destinations initial content
       
   788     int connMethodCount = dest1->connectionMethodCount();
       
   789     QCOMPARE(connMethodCount, 0);
       
   790     connMethodCount = dest2->connectionMethodCount();
       
   791     QCOMPARE(connMethodCount, 0);
       
   792     
       
   793     // Create a WLAN connection method
       
   794     CmConnectionMethodShim *cmWlan = mCmManagerShim->createConnectionMethod(
       
   795         CMManagerShim::BearerTypeWlan);
       
   796     QVERIFY(cmWlan != NULL);
       
   797     // Update to CommsDat
       
   798     cmWlan->update();
       
   799 
       
   800     uint cmWlanId = cmWlan->getIntAttribute(CMManagerShim::CmId);
       
   801     QCOMPARE(cmWlanId, (uint)1);
       
   802 
       
   803     // Add the connection method to the destination 1
       
   804     int index = dest1->addConnectionMethod(cmWlan);
       
   805     QCOMPARE(index, 0);
       
   806     // Update to CommsDat
       
   807     dest1->update();
       
   808 
       
   809     // Add the connection method to the destination 2
       
   810     index = dest2->addConnectionMethod(cmWlan);
       
   811     QCOMPARE(index, 0);
       
   812     // Update to CommsDat
       
   813     dest2->update();
       
   814         
       
   815     // Remove the WLAN connection method from destination 1
       
   816     dest1->removeConnectionMethod(cmWlan);
       
   817     // Update to CommsDat
       
   818     dest1->update();
       
   819     
       
   820     // Verify the delete
       
   821     connMethodCount = dest1->connectionMethodCount();
       
   822     QCOMPARE(connMethodCount, 0);
       
   823     
       
   824     // Check that WLAN connection method still exists for destination 2
       
   825     connMethodCount = dest2->connectionMethodCount();
       
   826     QCOMPARE(connMethodCount, 1);
       
   827 
       
   828     // Remove the WLAN connection method from destination 2
       
   829     dest2->removeConnectionMethod(cmWlan);
       
   830     // Update to CommsDat
       
   831     dest2->update();
       
   832     
       
   833     // Verify the delete
       
   834     connMethodCount = dest2->connectionMethodCount();
       
   835     QCOMPARE(connMethodCount, 0);
       
   836 
       
   837     // Delete destinations
       
   838     dest1->deleteDestination();
       
   839     delete dest1;
       
   840     dest2->deleteDestination();
       
   841     delete dest2;
       
   842     
       
   843     // Check that WLAN connection method still exists
       
   844     delete cmWlan;
       
   845     cmWlan = mCmManagerShim->connectionMethod(cmWlanId);
       
   846     QVERIFY(cmWlan != NULL);
       
   847     
       
   848     // Remove WLAN connection method.
       
   849     cmWlan->deleteConnectionMethod();
       
   850     delete cmWlan;
       
   851 }
       
   852 
       
   853 /**
       
   854  * Test case for testing destination's cm priority reading and changing. 
       
   855  * -Create a destination
       
   856  * -Add a GPRS connection method to it.
       
   857  * -Add a WLAN connection method to it.
       
   858  * -Read priority values of the connection methods
       
   859  * -Change the priority values
       
   860  * -Read the changed priority values
       
   861  * -Try to change the priority value to unsupported value
       
   862  * -Delete the destination (and connection methods)
       
   863  */
       
   864 void TestCmMgrShim::tcDestinationPriority()
       
   865 {
       
   866     // Create a new destination
       
   867     CmDestinationShim *dest;
       
   868     dest = mCmManagerShim->createDestination("TestDestination");
       
   869     QVERIFY(dest != NULL);
       
   870     uint destId = dest->id();
       
   871     QVERIFY(destId >= 4000);
       
   872       
       
   873     // Create a GPRS connection method
       
   874     CmConnectionMethodShim *cmGprs = mCmManagerShim->createConnectionMethod(
       
   875         CMManagerShim::BearerTypePacketData);
       
   876     QVERIFY(cmGprs != NULL);
       
   877     // Update to CommsDat
       
   878     cmGprs->update();
       
   879     
       
   880     // Add the connection method to the destination
       
   881     int index = dest->addConnectionMethod(cmGprs);
       
   882     QCOMPARE(index, 0);
       
   883     // Update to CommsDat
       
   884     dest->update();
       
   885     
       
   886     // Create a WLAN connection method
       
   887     CmConnectionMethodShim *cmWlan = mCmManagerShim->createConnectionMethod(
       
   888         CMManagerShim::BearerTypeWlan);
       
   889     QVERIFY(cmWlan != NULL);
       
   890     // Update to CommsDat
       
   891     cmWlan->update();
       
   892 
       
   893     // Add the connection method to the destination
       
   894     index = dest->addConnectionMethod(cmWlan);
       
   895     QCOMPARE(index, 0);
       
   896     // Update to CommsDat
       
   897     dest->update();
       
   898         
       
   899     // Get the priority of connection methods
       
   900     int priority = dest->priority(cmWlan);
       
   901     QCOMPARE(priority, 1);
       
   902     priority = dest->priority(cmGprs);
       
   903     QCOMPARE(priority, 2);
       
   904     
       
   905     // Modify the priority of GPRS connection method
       
   906     dest->modifyPriority(cmGprs, 0);
       
   907     dest->modifyPriority(cmWlan, 1);
       
   908     // Update to CommsDat
       
   909     dest->update();
       
   910     
       
   911     // Check the priorities
       
   912     priority = dest->priority(cmGprs);
       
   913     QCOMPARE(priority, 1);
       
   914     priority = dest->priority(cmWlan);
       
   915     QCOMPARE(priority, 2);
       
   916 
       
   917     // Try to modify the priority to forbidden value  
       
   918     TEST_CATCH_AND_VERIFY(
       
   919         dest->modifyPriority(cmGprs, 2),
       
   920         KErrArgument);
       
   921 
       
   922     // Delete the whole destination including the remaining WLAN conn method.
       
   923     dest->deleteDestination();
       
   924     
       
   925     delete dest;
       
   926     delete cmGprs;
       
   927     delete cmWlan;
       
   928 }
       
   929 
       
   930 /**
       
   931  * Test case for testing miscellaneous methods for destination. 
       
   932  * -Create a destination
       
   933  * -Change the name of the destination
       
   934  * -Check if the destination is hidden
       
   935  * -Check if destination is "internet" with metadata.
       
   936  * -Delete the destination
       
   937  */
       
   938 void TestCmMgrShim::tcDestinationMisc()
       
   939 {
       
   940     // Create a new destination
       
   941     CmDestinationShim *dest;
       
   942     dest = mCmManagerShim->createDestination("TestDestination");
       
   943     QVERIFY(dest != NULL);
       
   944     uint destId = dest->id();
       
   945     QVERIFY(destId >= 4000);
       
   946           
       
   947     // Change the name of the destination
       
   948     dest->setName("NewName");
       
   949     // Update to CommsDat
       
   950     dest->update();
       
   951     
       
   952     // Check the name
       
   953     QString name(dest->name());
       
   954     QCOMPARE(name, QString("NewName"));
       
   955 
       
   956     // Check if destination is hidden
       
   957     bool hidden = dest->isHidden();
       
   958     QCOMPARE(hidden, false);
       
   959     
       
   960     // Check "internet" metadata field
       
   961     uint metadata = dest->metadata(CMManagerShim::SnapMetadataInternet);
       
   962     QCOMPARE(metadata, (uint)false);
       
   963     
       
   964     // Delete the destination
       
   965     dest->deleteDestination();
       
   966     delete dest;
       
   967 }
       
   968 
       
   969 /**
       
   970  * Test case for testing destination refreshing method.  
       
   971  * -Create a destination
       
   972  * -Add a GPRS connection method, but don't update CommsDat
       
   973  * -Change the name of the destination, but don't update CommsDat
       
   974  * -Refresh destination
       
   975  * -Check that the changes are not valid anymore
       
   976  * -Delete the destination
       
   977  * -Delete the connection method
       
   978  */
       
   979 void TestCmMgrShim::tcDestinationRefresh()
       
   980 {
       
   981     // Create a new destination
       
   982     CmDestinationShim *dest;
       
   983     dest = mCmManagerShim->createDestination("TestDestination");
       
   984     QVERIFY(dest != NULL);
       
   985     uint destId = dest->id();
       
   986     QVERIFY(destId >= 4000);
       
   987     dest->update();
       
   988 
       
   989     // Create a GPRS connection method
       
   990     CmConnectionMethodShim *cmGprs = mCmManagerShim->createConnectionMethod(
       
   991         CMManagerShim::BearerTypePacketData);
       
   992     QVERIFY(cmGprs != NULL);
       
   993     // Update to CommsDat
       
   994     cmGprs->update();
       
   995     
       
   996     // Add the connection method to the destination
       
   997     int index = dest->addConnectionMethod(cmGprs);
       
   998     QCOMPARE(index, 0);
       
   999     // Don't update CommsDat
       
  1000     
       
  1001     // Change the name of the destination
       
  1002     dest->setName("NewName");
       
  1003     // Don't update to CommsDat
       
  1004 
       
  1005     // Refresh destination
       
  1006     dest->refresh();
       
  1007     
       
  1008     // Delete the destination reference
       
  1009     delete dest;
       
  1010     
       
  1011     // Refetch the destination
       
  1012     dest = mCmManagerShim->destination(destId);
       
  1013     QVERIFY(dest != NULL);
       
  1014     
       
  1015     // Check the name that it is the original one
       
  1016     QString name(dest->name());
       
  1017     QCOMPARE(name, QString("TestDestination"));
       
  1018 
       
  1019     // Check destination content
       
  1020     int connMethodCount = dest->connectionMethodCount();
       
  1021     QCOMPARE(connMethodCount, 0);
       
  1022     
       
  1023     // Delete the destination
       
  1024     dest->deleteDestination();
       
  1025     delete dest;
       
  1026 
       
  1027     // Delete connection method
       
  1028     bool ok = cmGprs->deleteConnectionMethod();
       
  1029     delete cmGprs;
       
  1030     QCOMPARE(ok, true);
       
  1031 }
       
  1032 
       
  1033 /**
       
  1034  * Test case for testing data reads using a invalid input data.
       
  1035  * All test reads in this case should fail to an exception.
       
  1036  */
       
  1037 void TestCmMgrShim::tcDestinationInvalidParams()
       
  1038 {
       
  1039     // Create a new destination
       
  1040     CmDestinationShim *dest;
       
  1041     dest = mCmManagerShim->createDestination("TestDestination");
       
  1042     QVERIFY(dest != NULL);
       
  1043     
       
  1044     // Update to CommsDat
       
  1045     dest->update();
       
  1046 
       
  1047     // Try reading with an invalid connection method index
       
  1048     CmConnectionMethodShim *cm = 0;
       
  1049     TEST_CATCH_AND_VERIFY(
       
  1050         dest->connectionMethod(42),
       
  1051         KErrArgument);
       
  1052     QVERIFY(cm == 0);
       
  1053     
       
  1054     // Try reading with an invalid connection method ID
       
  1055     cm = 0;
       
  1056     TEST_CATCH_AND_VERIFY(
       
  1057         dest->connectionMethodByID(42),
       
  1058         KErrNotFound);
       
  1059     QVERIFY(cm == 0);
       
  1060     
       
  1061     // Delete the destination
       
  1062     dest->deleteDestination();
       
  1063     delete dest;
       
  1064 }
       
  1065 
       
  1066 /**
       
  1067  * Test case for setting and getting icon from CM Manager
       
  1068  * -Creates a legacy WLAN connection method (i.e. does not belong to
       
  1069  *  any destination).
       
  1070  * -Sets & Gets Icon
       
  1071  * -Creates destination
       
  1072  * -Sets & Gets Icon
       
  1073  * -Deletes the connection method. 
       
  1074  */
       
  1075 void TestCmMgrShim::tcIconOperations()
       
  1076 {
       
  1077     // Create the connection method
       
  1078     CmConnectionMethodShim *cm = mCmManagerShim->createConnectionMethod(
       
  1079         CMManagerShim::BearerTypeWlan);
       
  1080     QVERIFY(cm != NULL);
       
  1081     
       
  1082     // Update to CommsDat
       
  1083     cm->update();
       
  1084     
       
  1085     // Create a new destination
       
  1086     CmDestinationShim *dest;
       
  1087     dest = mCmManagerShim->createDestination("TestDestination");
       
  1088     QVERIFY(dest != NULL);
       
  1089        
       
  1090     // Update to CommsDat
       
  1091     dest->update();
       
  1092 
       
  1093     // Set icon
       
  1094     QString testString("TestIconName");
       
  1095     cm->setIcon(testString);
       
  1096     
       
  1097     // Update to CommsDat
       
  1098     cm->update();
       
  1099     
       
  1100     // Set icon
       
  1101     dest->setIcon(testString);
       
  1102     
       
  1103     // Update to CommsDat
       
  1104     dest->update();
       
  1105      
       
  1106     // Check icon
       
  1107     QString resultString = cm->getIcon();
       
  1108     QCOMPARE(resultString, testString);
       
  1109 
       
  1110     // Check destination icon
       
  1111     QString resultDestString = dest->getIcon();
       
  1112     QCOMPARE(resultDestString, testString);
       
  1113 
       
  1114     // Delete the connection method
       
  1115     cm->deleteConnectionMethod();
       
  1116     delete cm;
       
  1117     
       
  1118     // Delete the destination
       
  1119     dest->deleteDestination();
       
  1120     delete dest;
       
  1121 }
       
  1122 
       
  1123 // -----------------------------------------------------------------------------
       
  1124 // SUB TEST CASES
       
  1125 // -----------------------------------------------------------------------------
       
  1126 
       
  1127 /**
       
  1128  * Case for deleting all destinations.
       
  1129  */
       
  1130 void TestCmMgrShim::deleteDestinations()
       
  1131 {
       
  1132     // Read all destinations
       
  1133     QList<uint> destinations;
       
  1134     mCmManagerShim->allDestinations(destinations);
       
  1135     
       
  1136     // Delete the destinations
       
  1137     for (int i=0; i < destinations.count(); i++) {
       
  1138         CmDestinationShim *dest = mCmManagerShim->destination(destinations[i]);
       
  1139         dest->deleteDestination();
       
  1140         delete dest;
       
  1141     }
       
  1142 }
       
  1143 
       
  1144 /**
       
  1145  * Case for deleting all connection methods.
       
  1146  */
       
  1147 void TestCmMgrShim::deleteConnectionMethods()
       
  1148 {
       
  1149     // Read all connection methods
       
  1150     QList<uint> connMethods;
       
  1151     mCmManagerShim->connectionMethod(connMethods, false);
       
  1152     
       
  1153     // Remove all connection methods
       
  1154     for (int i=0; i < connMethods.count(); i++){
       
  1155         CmConnectionMethodShim *cm = 
       
  1156             mCmManagerShim->connectionMethod(connMethods[i]);
       
  1157         QVERIFY(cm != NULL);
       
  1158         bool ok = cm->deleteConnectionMethod();
       
  1159         QVERIFY(ok == true);
       
  1160         cm->update();
       
  1161         delete cm;
       
  1162     }
       
  1163 }