summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/test/AmNodeStateCommunicatorDBusTest/CAmNodeStateCommunicatorTest.cpp
blob: 09e5f2b3b22ed6f81cb4e9d30bac51489f4331f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
 * Copyright (C) 2012, BMW AG
 *
 * This file is part of GENIVI Project AudioManager.
 *
 * Contributions are licensed to the GENIVI Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 *
 * \author Christian Linke, christian.linke@bmw.de BMW 2012
 *
 * \file CAmNodeStateCommunicatorTest.cpp
 * For further information see http://www.genivi.org/.
 *
 */

#include "CAmNodeStateCommunicatorTest.h"
#include "shared/CAmDltWrapper.h"
#include "shared/CAmSocketHandler.h"
#include "shared/CAmDbusWrapper.h"

#include <unistd.h>

using namespace testing;
using namespace am;

static CAmEnvironment* env;



CAmNodeStateCommunicatorTest::CAmNodeStateCommunicatorTest()
{

}

CAmNodeStateCommunicatorTest::~CAmNodeStateCommunicatorTest()
{
    // TODO Auto-generated destructor stub
}

/**This is the thread for the nsm python fake
 *
 * @param
 */
void* nsmThread (void*)
{
   system("python nsm.py");
   return (NULL);
}

/**this is the thread the mainloop runs in
 *
 * @param importHandler
 */
void* mainLoop(void* importHandler)
{
    CAmSocketHandler* handler=static_cast<CAmSocketHandler*>(importHandler);
    handler->start_listenting();
    return (NULL);
}



TEST_F(CAmNodeStateCommunicatorTest, nsmChangeNodeState)
{
    assert(true == env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface));
    NsmNodeState_e newstate(NsmNodeState_BaseRunning) ;
    EXPECT_CALL(pMockControlInterface,hookSystemNodeStateChanged(newstate));
    std::ostringstream send;
    send<<"python send2nsm.py nodeState "<<static_cast<std::int32_t>(newstate);
    system(send.str().c_str());
}

TEST_F(CAmNodeStateCommunicatorTest, nsmChangeApplicationMode)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    NsmApplicationMode_e appmode(NsmApplicationMode_Swl) ;
    EXPECT_CALL(pMockControlInterface,hookSystemNodeApplicationModeChanged(appmode));
    std::ostringstream send;
    send<<"python send2nsm.py appMode "<<static_cast<std::int32_t>(appmode);
    system(send.str().c_str());
}

TEST_F(CAmNodeStateCommunicatorTest, nsmChangeSessionState)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    std::string sessionName("mySession");
    NsmSeat_e seatID(NsmSeat_CoDriver);
    NsmSessionState_e sessionState(NsmSessionState_Inactive) ;
    EXPECT_CALL(pMockControlInterface,hookSystemSessionStateChanged(sessionName,seatID,sessionState));
    std::ostringstream send;
    send<<"python send2nsm.py sessionState "<<sessionName<<" "<<static_cast<std::int32_t>(seatID)<<" "<<static_cast<int32_t>(sessionState);
    system(send.str().c_str());
}

TEST_F(CAmNodeStateCommunicatorTest, getRestartReason)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    NsmRestartReason_e restartReason;
    ASSERT_EQ(E_OK,env->nsmController.nsmGetRestartReasonProperty(restartReason));
    ASSERT_EQ(restartReason,1);
}

TEST_F(CAmNodeStateCommunicatorTest, getShutdownReason)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    NsmShutdownReason_e ShutdownReason;
    ASSERT_EQ(E_OK,env->nsmController.nsmGetShutdownReasonProperty(ShutdownReason));
    ASSERT_EQ(ShutdownReason,2);
}

TEST_F(CAmNodeStateCommunicatorTest, getWakeUpReason)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    NsmRunningReason_e WakeUpReason;
    ASSERT_EQ(E_OK,env->nsmController.nsmGetRunningReasonProperty(WakeUpReason));
    ASSERT_EQ(WakeUpReason,3);
}

TEST_F(CAmNodeStateCommunicatorTest, getNodeState)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    NsmNodeState_e nodeState;
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Ok,env->nsmController.nsmGetNodeState(nodeState));
    ASSERT_EQ(nodeState,1);
}

TEST_F(CAmNodeStateCommunicatorTest, getApplicationMode)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    NsmApplicationMode_e applicationMode;
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Error,env->nsmController.nsmGetApplicationMode(applicationMode));
    ASSERT_EQ(applicationMode,5);
}

TEST_F(CAmNodeStateCommunicatorTest, getSessionState)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    std::string sessionName("mySession");
    NsmSeat_e seatID(NsmSeat_Driver);
    NsmSessionState_e sessionState;
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Ok,env->nsmController.nsmGetSessionState(sessionName,seatID,sessionState));
    ASSERT_EQ(sessionState,5);
}

TEST_F(CAmNodeStateCommunicatorTest, RegisterShutdownClient)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    uint32_t shutdownmode(1), timeoutMs(100);
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Ok,env->nsmController.nsmRegisterShutdownClient(shutdownmode,timeoutMs));
}

TEST_F(CAmNodeStateCommunicatorTest, receiveLifecycleRequest)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    uint32_t shutdownmode(1);
    uint32_t timeoutMs(100);
    int32_t Request(1);
    int32_t RequestID(4);
    EXPECT_CALL(pMockControlInterface,hookSystemLifecycleRequest(_,RequestID)).WillOnce(Return(NsmErrorStatus_e::NsmErrorStatus_Ok));
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Ok,env->nsmController.nsmRegisterShutdownClient(shutdownmode,timeoutMs));
    std::ostringstream send;
    send << "python send2nsm.py LifecycleRequest "<<Request<<" "<<RequestID;
    system(send.str().c_str());
}

TEST_F(CAmNodeStateCommunicatorTest, UnRegisterShutdownClient)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    uint32_t shutdownmode(1),timeoutMs(100);
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Ok,env->nsmController.nsmRegisterShutdownClient(shutdownmode,timeoutMs));
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Ok,env->nsmController.nsmUnRegisterShutdownClient(shutdownmode));

}

TEST_F(CAmNodeStateCommunicatorTest, sendLifecycleRequestComplete)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    uint32_t RequestID(22);
    NsmErrorStatus_e errorStatus(NsmErrorStatus_Internal);
    ASSERT_EQ(NsmErrorStatus_e::NsmErrorStatus_Ok,env->nsmController.nsmSendLifecycleRequestComplete(RequestID,errorStatus));
}

TEST_F(CAmNodeStateCommunicatorTest, getInterfaceVersion)
{
    env->pControlInterfaceBackdoor.replaceController(&env->pControlSender,&pMockControlInterface);
    uint32_t version(0);
    ASSERT_EQ(E_OK,env->nsmController.nsmGetInterfaceVersion(version));
    ASSERT_EQ(version,static_cast<uint32_t>(23));
}

void CAmNodeStateCommunicatorTest::SetUp()
{
}

void CAmNodeStateCommunicatorTest::TearDown()
{
}

int main(int argc, char **argv)
{
    CAmDltWrapper::instance()->registerApp("nsm", "nsmtest");
    logInfo("nsmtest Test started ");
    ::testing::InitGoogleTest(&argc, argv);
    ::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new CAmEnvironment);
    (void) env;
    return RUN_ALL_TESTS();
}

CAmEnvironment::CAmEnvironment() :
    pControlInterfaceBackdoor(),
    pControlSender(),
    iSocketHandler(),
    wrapper(&iSocketHandler,DBusBusType::DBUS_BUS_SESSION),
    nsmController(&wrapper)
{
    env=this;
}

CAmEnvironment::~CAmEnvironment()
{
}

void CAmEnvironment::SetUp()
{
    //create the nsm thread
    pthread_create(&pNsmThread, NULL, nsmThread, NULL);
    nsmController.registerControlSender(&pControlSender);
    //create the mainloop thread
    pthread_create(&pMainLoopThread, NULL, mainLoop, (void*)&iSocketHandler);
    printf("[----------] Waiting for interface to be ready....\r\n");
    sleep(2);
}

void CAmEnvironment::TearDown()
{
    //end the nsm per dbus
    system("python send2nsm.py finish");
    pthread_join(pNsmThread, NULL);
    //end the mainloop
    iSocketHandler.exit_mainloop();
    pthread_join(pMainLoopThread, NULL);
}