summaryrefslogtreecommitdiff
path: root/AudioManagerUtilities/test/AmSerializerTest/CAmSerializerTest.cpp
blob: 508ed45a3933dde047ced467c3ae66784b234098 (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
/**
 * SPDX license identifier: MPL-2.0
 *
 * 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 2011,2012
 *
 * For further information see http://www.genivi.org/.
 *
 */

#include <cstdio>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <string.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/un.h>
#include <sys/poll.h>

#include "CAmSocketHandler.h"
#include "CAmSerializer.h"
#include "CAmSerializerTest.h"

using namespace testing;
using namespace am;

CAmTimerSockethandlerController::CAmTimerSockethandlerController(CAmSocketHandler *myHandler, const timespec &timeout) :
        MockIAmTimerCb(), mpSocketHandler(myHandler), mUpdateTimeout(timeout), pTimerCallback(this, &CAmTimerSockethandlerController::timerCallback)
{
}

am::CAmTimerSockethandlerController::~CAmTimerSockethandlerController()
{
}

void am::CAmTimerSockethandlerController::timerCallback(sh_timerHandle_t handle, void* userData)
{
    MockIAmTimerCb::timerCallback(handle, userData);
    mpSocketHandler->stop_listening();
}

CAmSerializerTest::CAmSerializerTest()
{
}

CAmSerializerTest::~CAmSerializerTest()
{
}

void CAmSerializerTest::SetUp()
{

}

void CAmSerializerTest::TearDown()
{
}

struct SerializerData
{

    std::string testStr;
    int result;
    MockIAmSerializerCb *pSerCb;
    CAmSocketHandler *pSocketHandler;
    V2::CAmSerializer *pSerializer;
};

void* ptSerializer(void* data)
{
    SerializerData *pData = (SerializerData*) data;
    std::string testStr(pData->testStr);
    bool result = false;
    int r = 0;
    const uint32_t ten = 10;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
    pData->pSerializer->syncCall(pData->pSerCb, &MockIAmSerializerCb::check);
    pData->pSerializer->syncCall(pData->pSerCb, &MockIAmSerializerCb::checkInt, pData->result);
    pData->pSerializer->syncCall(pData->pSerCb, &MockIAmSerializerCb::dispatchData, result, ten, pData->testStr);

    for (uint32_t i = 0; i < 5; i++)
        pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::dispatchData, i, testStr);

    pData->pSerializer->asyncInvocation(std::bind([]()->bool
    {   return 1;}));
    pData->pSerializer->asyncInvocation(std::bind([](const int i, int & result)
    {   result = i*10;}, 1, std::ref(r)));

    pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::check);
    pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::check);

    pData->pSerializer->asyncCall(pData->pSerCb, &MockIAmSerializerCb::checkInt);
#pragma GCC diagnostic pop
    return (NULL);
}

ACTION(ActionDispatchData){
arg1="DispatchData";
}

TEST(CAmSerializerTest, serializerTest)
{
    pthread_t serThread;

    MockIAmSerializerCb serCb;
    CAmSocketHandler myHandler;
    std::string testStr("testStr");
    V2::CAmSerializer serializer(&myHandler);
    sh_timerHandle_t handle;
    timespec timeout4;
    timeout4.tv_nsec = 0;
    timeout4.tv_sec = 3;
    CAmTimerSockethandlerController testCallback4(&myHandler, timeout4);
    myHandler.addTimer(timeout4, &testCallback4.pTimerCallback, handle, NULL);
    EXPECT_CALL(testCallback4,timerCallback(handle,NULL)).Times(1);

    SerializerData serializerData;
    serializerData.result = 0;
    serializerData.testStr = testStr;
    serializerData.pSerCb = &serCb;
    serializerData.pSocketHandler = &myHandler;
    serializerData.pSerializer = &serializer;
    pthread_create(&serThread, NULL, ptSerializer, &serializerData);

    EXPECT_CALL(serCb,check()).Times(3);
    EXPECT_CALL(serCb,checkInt()).Times(2).WillRepeatedly(Return(100));

    EXPECT_CALL(serCb,dispatchData(10,testStr)).WillOnce(DoAll(ActionDispatchData(), Return(true)));
    for (int i = 0; i < 5; i++)
        EXPECT_CALL(serCb,dispatchData(i,testStr)).WillOnce(DoAll(ActionDispatchData(), Return(true)));
    myHandler.start_listenting();

    pthread_join(serThread, NULL);
    ASSERT_TRUE(serializerData.testStr == "DispatchData");
    ASSERT_TRUE(serializerData.result == 100);
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}