javaextensions/midprms_db/tsrc/rmsplugin/src/testplugin.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     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: RmsExtensionPlugin unit tests with Java Captain
       
    15 *
       
    16 */
       
    17 
       
    18 #include <string>
       
    19 #include "TestHarness.h"
       
    20 #include "comms.h"
       
    21 #include "commsmessage.h"
       
    22 #include "commsclientendpoint.h"
       
    23 #include "monitor.h"
       
    24 
       
    25 #include "rmsextensionplugin.h"
       
    26 
       
    27 using namespace java::rms;
       
    28 using namespace java::util;
       
    29 using namespace java::comms;
       
    30 
       
    31 class NotifyListener : public CommsListener
       
    32 {
       
    33 public:
       
    34     NotifyListener(Monitor& aMonitor, int aMsgCount) : mListenerCountMsg(0), mListenerCount(0),
       
    35             mMonitor(aMonitor), mMsgCount(aMsgCount) {};
       
    36     virtual void processMessage(CommsMessage& aMessage)
       
    37     {
       
    38         int id = aMessage.getMessageId();
       
    39         switch (id)
       
    40         {
       
    41         case MSG_ID_RECORD_CHANGE:
       
    42             if (--mMsgCount == 0)
       
    43             {
       
    44                 mMonitor.notify();
       
    45             }
       
    46             break;
       
    47         case MSG_ID_LISTENER_COUNT:
       
    48         {
       
    49             mListenerCountMsg++;
       
    50             std::string name;
       
    51             aMessage >> name >> mListenerCount;
       
    52             break;
       
    53         }
       
    54         default:
       
    55             break;
       
    56         }
       
    57     }
       
    58 
       
    59     int mListenerCountMsg;
       
    60     int mListenerCount;
       
    61 private:
       
    62     Monitor& mMonitor;
       
    63     int mMsgCount;
       
    64 };
       
    65 
       
    66 
       
    67 CommsMessage createAddListenerMsg()
       
    68 {
       
    69     CommsMessage addMsg;
       
    70     addMsg.setModuleId(PLUGIN_ID_RMS_C);
       
    71     addMsg.setMessageId(MSG_ID_LISTENER_CHANGE);
       
    72     addMsg << "aaa" << ADD_LISTENER;
       
    73     return addMsg;
       
    74 }
       
    75 
       
    76 CommsMessage createRemoveListenerMsg()
       
    77 {
       
    78     CommsMessage removeMsg;
       
    79     removeMsg.setModuleId(PLUGIN_ID_RMS_C);
       
    80     removeMsg.setMessageId(MSG_ID_LISTENER_CHANGE);
       
    81     removeMsg << "aaa" << REMOVE_LISTENER;
       
    82     return removeMsg;
       
    83 }
       
    84 
       
    85 CommsMessage createRecordChangedMsg()
       
    86 {
       
    87     CommsMessage recordMsg;
       
    88     recordMsg.setModuleId(PLUGIN_ID_RMS_C);
       
    89     recordMsg.setMessageId(MSG_ID_RECORD_CHANGE);
       
    90     recordMsg << "aaa";
       
    91     return recordMsg;
       
    92 }
       
    93 
       
    94 TEST_GROUP(TestPlugin)
       
    95 {
       
    96     TEST_SETUP()
       
    97     {
       
    98     }
       
    99 
       
   100     TEST_TEARDOWN()
       
   101     {
       
   102     }
       
   103 };
       
   104 
       
   105 
       
   106 TEST(TestPlugin, oneListener)
       
   107 {
       
   108     CommsClientEndpoint a;
       
   109     std::auto_ptr<Monitor> monitor(Monitor::createMonitor());
       
   110     NotifyListener listener(*monitor, 1);
       
   111     a.registerDefaultListener(&listener);
       
   112 
       
   113     int rc = a.connect(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   114     CHECK(rc == 0);
       
   115     CommsMessage msg = createAddListenerMsg();
       
   116     CommsMessage receivedMsg;
       
   117     rc = a.sendReceive(msg, receivedMsg, WAIT_FOR_EVER);
       
   118     CHECK(rc == 0);
       
   119     listener.processMessage(receivedMsg);
       
   120     CHECK(listener.mListenerCountMsg == 1);
       
   121 
       
   122     CommsClientEndpoint dummy;
       
   123     dummy.connect(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   124     msg = createRecordChangedMsg();
       
   125     rc = dummy.send(msg);
       
   126     CHECK(rc == 0);
       
   127 
       
   128     monitor->wait();
       
   129 
       
   130     msg = createRemoveListenerMsg();
       
   131     rc = a.send(msg);
       
   132     CHECK(rc == 0);
       
   133 }
       
   134 
       
   135 TEST(TestPlugin, multipleListeners)
       
   136 {
       
   137     // 1st listener
       
   138     CommsClientEndpoint a;
       
   139     std::auto_ptr<Monitor> monitorA(Monitor::createMonitor());
       
   140     NotifyListener listenerA(*monitorA, 2);
       
   141     a.registerDefaultListener(&listenerA);
       
   142     int rc = a.connect(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   143     CHECK(rc == 0);
       
   144 
       
   145     // 2nd listener
       
   146     CommsClientEndpoint b;
       
   147     std::auto_ptr<Monitor> monitorB(Monitor::createMonitor());
       
   148     NotifyListener listenerB(*monitorB, 2);
       
   149     b.registerDefaultListener(&listenerB);
       
   150     rc = b.connect(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   151     CHECK(rc == 0);
       
   152 
       
   153     // 3rd listener
       
   154     CommsClientEndpoint c;
       
   155     std::auto_ptr<Monitor> monitorC(Monitor::createMonitor());
       
   156     NotifyListener listenerC(*monitorC, 2);
       
   157     c.registerDefaultListener(&listenerC);
       
   158     rc = c.connect(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   159     CHECK(rc == 0);
       
   160 
       
   161     // add listeners
       
   162     CommsMessage msg = createAddListenerMsg();
       
   163     rc = a.send(msg);
       
   164     CHECK(rc == 0);
       
   165     rc = b.send(msg);
       
   166     CHECK(rc == 0);
       
   167     rc = c.send(msg);
       
   168     CHECK(rc == 0);
       
   169 
       
   170     // send record modified
       
   171     msg = createRecordChangedMsg();
       
   172     rc = a.send(msg);
       
   173     CHECK(rc == 0);
       
   174     rc = b.send(msg);
       
   175     CHECK(rc == 0);
       
   176     rc = c.send(msg);
       
   177     CHECK(rc == 0);
       
   178 
       
   179     monitorA->wait();
       
   180     monitorB->wait();
       
   181     monitorC->wait();
       
   182 
       
   183     CHECK(listenerA.mListenerCountMsg == 3);
       
   184     CHECK(listenerB.mListenerCountMsg == 2);
       
   185     CHECK(listenerC.mListenerCountMsg == 1);
       
   186 
       
   187     CHECK(listenerA.mListenerCount == 3);
       
   188     CHECK(listenerB.mListenerCount == 3);
       
   189     CHECK(listenerC.mListenerCount == 3);
       
   190 
       
   191     // remove listeners
       
   192     msg = createRemoveListenerMsg();
       
   193     rc = a.send(msg);
       
   194     CHECK(rc == 0);
       
   195     rc = b.send(msg);
       
   196     CHECK(rc == 0);
       
   197     rc = c.send(msg);
       
   198     CHECK(rc == 0);
       
   199 }