featuremgmt/featuremgr/test/rtest/src/t_fmgrnotify.cpp
branchRCL_3
changeset 8 fa9941cf3867
child 10 31a8f755b7fe
equal deleted inserted replaced
6:5ffdb8f2067f 8:fa9941cf3867
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <featmgr.h>
       
    18 #include <featureuids.h>
       
    19 #include <featurecontrol.h>
       
    20 #include <featurenotifier.h>
       
    21 
       
    22 using namespace NFeature;
       
    23 
       
    24 const TUid KNewFeatureUid = {0x7888ABCD}; 
       
    25 const TUid KNewFeatureUid2 = {0x7888ABCF}; 
       
    26 
       
    27 ///////////////////////////////////////////////////////////////////////////////////////
       
    28 
       
    29 static RTest TheTest(_L("t_fmgrnotify"));
       
    30 
       
    31 ///////////////////////////////////////////////////////////////////////////////////////
       
    32 
       
    33 //Deletes all created test files.
       
    34 void DestroyTestEnv()
       
    35     {
       
    36     }
       
    37 
       
    38 ///////////////////////////////////////////////////////////////////////////////////////
       
    39 ///////////////////////////////////////////////////////////////////////////////////////
       
    40 //Test macros and functions
       
    41 void Check1(TInt aValue, TInt aLine)
       
    42     {
       
    43     if(!aValue)
       
    44         {
       
    45         DestroyTestEnv();
       
    46         RDebug::Print(_L("*** Expression evaluated to false. Line %d\r\n"), aLine);
       
    47         TheTest(EFalse, aLine);
       
    48         }
       
    49     }
       
    50 void Check2(TInt aValue, TInt aExpected, TInt aLine)
       
    51     {
       
    52     if(aValue != aExpected)
       
    53         {
       
    54         DestroyTestEnv();
       
    55         RDebug::Print(_L("*** Expected: %d, got: %d. Line %d\r\n"), aExpected, aValue, aLine);
       
    56         TheTest(EFalse, aLine);
       
    57         }
       
    58     }
       
    59 #define TEST(arg) ::Check1((arg), __LINE__)
       
    60 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
       
    61 
       
    62 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    63 
       
    64 _LIT(KFeatureNoChange,          "FeatureNoChange");
       
    65 _LIT(KFeatureStatusUpdated,     "FeatureStatusUpdated");
       
    66 _LIT(KFeatureDataUpdated,       "FeatureDataUpdated");
       
    67 _LIT(KFeatureStatusDataUpdated, "FeatureStatusDataUpdated");
       
    68 _LIT(KFeatureRediscover,        "FeatureRediscover");
       
    69 _LIT(KFeatureCreated,           "FeatureCreated");
       
    70 _LIT(KFeatureDeleted,           "FeatureDeleted");
       
    71 
       
    72 const TPtrC KNotificationText[] = {KFeatureNoChange(), KFeatureStatusUpdated(), KFeatureDataUpdated(), 
       
    73                                    KFeatureStatusDataUpdated(), KFeatureRediscover(), KFeatureCreated(), 
       
    74                                    KFeatureDeleted()};
       
    75 
       
    76 class TTestFeatureObserver : public MFeatureObserver
       
    77     {
       
    78 public:
       
    79     TTestFeatureObserver() :
       
    80         iType(static_cast <TFeatureChangeType> (-1)),
       
    81         iFeatureUid(KNullUid)    
       
    82         {
       
    83         }
       
    84     void SetExpected(TFeatureChangeType aType, TUid aFeatureUid)
       
    85         {
       
    86         iType = aType;
       
    87         iFeatureUid = aFeatureUid;    
       
    88         }
       
    89     void Reset()
       
    90         {
       
    91         iType = static_cast <TFeatureChangeType> (-1);
       
    92         iFeatureUid = KNullUid;    
       
    93         }
       
    94     virtual void HandleNotifyChange(TFeatureChangeType aType, TFeatureEntry aFeature)
       
    95         {
       
    96         TheTest.Printf(_L("=== HandleNotifyChange() called with aType=\"%S\" and aFeature.FeatureUid()=0x%X\r\n"), &KNotificationText[aType], aFeature.FeatureUid());
       
    97         CActiveScheduler::Stop();
       
    98         TEST2(aType, iType);
       
    99         TEST(aFeature.FeatureUid() == iFeatureUid);
       
   100         }
       
   101     virtual void HandleNotifyError(TInt aError)
       
   102         {
       
   103         TheTest.Printf(_L("=== HandleNotifyError() called with error=%d\r\n"), aError);
       
   104         CActiveScheduler::Stop();
       
   105         TEST2(aError, KErrNone);
       
   106         }
       
   107 private:
       
   108         TFeatureChangeType  iType;
       
   109         TUid                iFeatureUid;    
       
   110     };
       
   111 
       
   112 ///////////////////////////////////////////////////////////////////////////////////////
       
   113 
       
   114 void AddFeature(RFeatureControl& aCtrl, TUid aFeatureUid)
       
   115     {
       
   116     TBitFlags32 flags;
       
   117     flags.ClearAll();
       
   118     flags.Set(EFeatureSupported);
       
   119     flags.Set(EFeatureModifiable);
       
   120     TFeatureEntry fentry(aFeatureUid, flags, 0x0);
       
   121     TInt err = aCtrl.AddFeature(fentry);
       
   122     TEST2(err, KErrNone);
       
   123     }
       
   124 
       
   125 void DeleteFeature(RFeatureControl& aCtrl, TUid aFeatureUid)
       
   126     {
       
   127     TInt err = aCtrl.DeleteFeature(aFeatureUid);
       
   128     TEST2(err, KErrNone);
       
   129     }
       
   130 
       
   131 /**
       
   132 @SYMTestCaseID          PDS-FEATMGR-CT-????
       
   133 @SYMTestCaseDesc        
       
   134 @SYMTestPriority        High
       
   135 @SYMTestActions         
       
   136 @SYMTestExpectedResults Test must not fail
       
   137 @SYMDEF                 DEF144262
       
   138 */
       
   139 void NotificationsTest1()
       
   140     {
       
   141     TTestFeatureObserver observer;
       
   142     CFeatureNotifier* notifier = NULL;
       
   143     TRAPD(err, notifier = CFeatureNotifier::NewL(observer));
       
   144     TEST2(err, KErrNone);
       
   145     //Request notification for feature with KNewFeatureUid uid.
       
   146     err = notifier->NotifyRequest(KNewFeatureUid);
       
   147     TEST2(err, KErrNone);
       
   148     err = notifier->NotifyRequest(KNewFeatureUid);
       
   149     TEST2(err, KErrAlreadyExists);
       
   150     
       
   151     RFeatureControl ctrl;
       
   152     err = ctrl.Connect();
       
   153     TEST2(err, KErrNone);
       
   154     //Add a feature with KNewFeatureUid uid and check the notification
       
   155     AddFeature(ctrl, KNewFeatureUid);
       
   156     observer.SetExpected(EFeatureFeatureCreated, KNewFeatureUid);
       
   157     CActiveScheduler::Start();
       
   158     //Set the feature status and data and check the notification
       
   159     err = ctrl.SetFeature(KNewFeatureUid, ETrue, 100);
       
   160     TEST2(err, KErrNone);
       
   161     observer.SetExpected(EFeatureStatusDataUpdated, KNewFeatureUid);
       
   162     CActiveScheduler::Start();
       
   163     //Set the feature data and check the notification
       
   164     err = ctrl.SetFeature(KNewFeatureUid, 200);
       
   165     TEST2(err, KErrNone);
       
   166     observer.SetExpected(EFeatureDataUpdated, KNewFeatureUid);
       
   167     CActiveScheduler::Start();
       
   168     //Enable the feature (it is already enabled) and check the notification
       
   169     err = ctrl.EnableFeature(KNewFeatureUid);
       
   170     TEST2(err, KErrNone);
       
   171     observer.SetExpected(EFeatureNoChange, KNewFeatureUid);//the feature is enabled - no status change
       
   172     CActiveScheduler::Start();
       
   173     //Disable the feature and check the notification
       
   174     err = ctrl.DisableFeature(KNewFeatureUid);
       
   175     TEST2(err, KErrNone);
       
   176     observer.SetExpected(EFeatureStatusUpdated, KNewFeatureUid);
       
   177     CActiveScheduler::Start();
       
   178     //Cancel notifications
       
   179     err = notifier->NotifyCancel(KNewFeatureUid);
       
   180     TEST2(err, KErrNone);
       
   181     err = notifier->NotifyCancel(KNewFeatureUid);
       
   182     TEST2(err, KErrNotFound);
       
   183     //Request notifications again    
       
   184     err = notifier->NotifyRequest(KNewFeatureUid);
       
   185     TEST2(err, KErrNone);
       
   186     //Delete the feature and check the notification
       
   187     DeleteFeature(ctrl, KNewFeatureUid);
       
   188     observer.SetExpected(EFeatureFeatureDeleted, KNewFeatureUid);
       
   189     CActiveScheduler::Start();
       
   190     //Cleanup
       
   191     ctrl.Close();
       
   192     delete notifier;
       
   193     }
       
   194 
       
   195 /**
       
   196 @SYMTestCaseID          PDS-FEATMGR-CT-????
       
   197 @SYMTestCaseDesc        
       
   198 @SYMTestPriority        High
       
   199 @SYMTestActions         
       
   200 @SYMTestExpectedResults Test must not fail
       
   201 @SYMDEF                 DEF144262
       
   202 */
       
   203 void NotificationsTest2()
       
   204     {
       
   205     TTestFeatureObserver observer;
       
   206     CFeatureNotifier* notifier = NULL;
       
   207     TRAPD(err, notifier = CFeatureNotifier::NewL(observer));
       
   208     TEST2(err, KErrNone);
       
   209     
       
   210     RFeatureControl ctrl;
       
   211     err = ctrl.Connect();
       
   212     TEST2(err, KErrNone);
       
   213     //Add two features
       
   214     AddFeature(ctrl, KNewFeatureUid);
       
   215     AddFeature(ctrl, KNewFeatureUid2);
       
   216     //Request notifications for the added features. One of them - duplicated in the array.
       
   217     RFeatureUidArray farray;
       
   218     err = farray.Append(KNewFeatureUid);
       
   219     TEST2(err, KErrNone);
       
   220     err = farray.Append(KNewFeatureUid2);
       
   221     TEST2(err, KErrNone);
       
   222     err = farray.Append(KNewFeatureUid);
       
   223     TEST2(err, KErrNone);
       
   224     //
       
   225     err = notifier->NotifyRequest(farray);
       
   226     TEST2(err, KErrNone);
       
   227     //Enable one of the features (already enabled) and check the notification
       
   228     err = ctrl.EnableFeature(KNewFeatureUid);
       
   229     TEST2(err, KErrNone);
       
   230     observer.SetExpected(EFeatureNoChange, KNewFeatureUid);//the feature is enabled - no status change
       
   231     CActiveScheduler::Start();
       
   232     //Disable the second feature and check the notification
       
   233     err = ctrl.DisableFeature(KNewFeatureUid2);
       
   234     TEST2(err, KErrNone);
       
   235     observer.SetExpected(EFeatureStatusUpdated, KNewFeatureUid2);
       
   236     CActiveScheduler::Start();
       
   237     //Cancel notifications for the second feature
       
   238     err = notifier->NotifyCancel(KNewFeatureUid2);
       
   239     TEST2(err, KErrNone);
       
   240     //Disable the first feature and check notification
       
   241     err = ctrl.DisableFeature(KNewFeatureUid);
       
   242     TEST2(err, KErrNone);
       
   243     observer.SetExpected(EFeatureStatusUpdated, KNewFeatureUid);
       
   244     CActiveScheduler::Start();
       
   245     //Cancel all notifications
       
   246     err = notifier->NotifyCancelAll();
       
   247     TEST2(err, KErrNone);
       
   248     err = notifier->NotifyCancelAll();
       
   249     TEST2(err, KErrNone);
       
   250     //Cleanup
       
   251     farray.Close();
       
   252     ctrl.Close();
       
   253     delete notifier;
       
   254     }
       
   255 
       
   256 void DoTestsL()
       
   257     {
       
   258     CActiveScheduler* scheduler = new CActiveScheduler;
       
   259     TEST(scheduler != NULL);
       
   260     CActiveScheduler::Install(scheduler);
       
   261     
       
   262     TheTest.Start(_L("@SYMTestCaseID:PDS-EFM-CT-4066 Notifications test 1"));
       
   263     NotificationsTest1();
       
   264 
       
   265     TheTest.Next(_L("@SYMTestCaseID:PDS-EFM-CT-4067 Notifications test 2"));
       
   266     NotificationsTest2();
       
   267     
       
   268     delete scheduler;
       
   269     }
       
   270 
       
   271 TInt E32Main()
       
   272     {
       
   273     TheTest.Title();
       
   274     
       
   275     CTrapCleanup* tc = CTrapCleanup::New();
       
   276     TheTest(tc != NULL);
       
   277     
       
   278     __UHEAP_MARK;
       
   279     
       
   280     TRAPD(err, DoTestsL());
       
   281     DestroyTestEnv();
       
   282     TEST2(err, KErrNone);
       
   283 
       
   284     __UHEAP_MARKEND;
       
   285     
       
   286     TheTest.End();
       
   287     TheTest.Close();
       
   288     
       
   289     delete tc;
       
   290 
       
   291     User::Heap().Check();
       
   292     return KErrNone;
       
   293     }