~robertcarr/mir/client-focus-notifications

« back to all changes in this revision

Viewing changes to tests/unit-tests/frontend/test_socket_session.cpp

  • Committer: Robert Carr
  • Date: 2013-08-01 22:01:20 UTC
  • mfrom: (706.2.208 trunk)
  • Revision ID: robert.carr@canonical.com-20130801220120-6m230b3g6x0xflzd
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
 
17
 */
 
18
 
 
19
#include "src/server/frontend/socket_session.h"
 
20
#include "src/server/frontend/message_receiver.h"
 
21
#include "src/server/frontend/message_processor.h"
 
22
 
 
23
#include "mir_test/fake_shared.h"
 
24
 
 
25
#include <gmock/gmock.h>
 
26
#include <gtest/gtest.h>
 
27
 
 
28
namespace mfd = mir::frontend::detail;
 
29
namespace ba = boost::asio;
 
30
namespace mt = mir::test;
 
31
 
 
32
namespace
 
33
{
 
34
struct MockReceiver : public mfd::MessageReceiver
 
35
{
 
36
    MOCK_METHOD3(async_receive_msg, void(std::function<void(boost::system::error_code const&, size_t)> const&,
 
37
                                         boost::asio::streambuf&, size_t)); 
 
38
    MOCK_METHOD0(client_pid, pid_t());
 
39
};
 
40
 
 
41
struct MockProcessor : public mfd::MessageProcessor
 
42
{
 
43
    MOCK_METHOD1(process_message, bool(std::istream&));
 
44
};
 
45
}
 
46
struct SocketSessionTest : public ::testing::Test
 
47
{
 
48
    testing::NiceMock<MockProcessor> mock_processor;
 
49
    testing::NiceMock<MockReceiver> mock_receiver;
 
50
};
 
51
 
 
52
TEST_F(SocketSessionTest, basic_msg)
 
53
{
 
54
    using namespace testing;
 
55
 
 
56
    std::shared_ptr<mfd::ConnectedSessions<mfd::SocketSession>> null_sessions;
 
57
    std::function<void(boost::system::error_code const&, size_t)> header_read, body_read;
 
58
 
 
59
    size_t header_size = 2;
 
60
    EXPECT_CALL(mock_receiver, async_receive_msg(_,_, header_size))
 
61
        .Times(1)
 
62
        .WillOnce(SaveArg<0>(&header_read));
 
63
 
 
64
    mfd::SocketSession session(mt::fake_shared(mock_receiver), 0, null_sessions, mt::fake_shared(mock_processor));
 
65
 
 
66
    //trigger wait for header
 
67
    session.read_next_message();
 
68
    testing::Mock::VerifyAndClearExpectations(&mock_receiver);
 
69
 
 
70
    //trigger body read
 
71
    EXPECT_CALL(mock_receiver, async_receive_msg(_,_,_))
 
72
        .Times(1)
 
73
        .WillOnce(SaveArg<0>(&body_read));
 
74
 
 
75
    boost::system::error_code code;
 
76
    header_read(code, 2);
 
77
 
 
78
    testing::Mock::VerifyAndClearExpectations(&mock_receiver);
 
79
 
 
80
    //trigger message process
 
81
    EXPECT_CALL(mock_processor, process_message(_))
 
82
        .Times(1)
 
83
        .WillOnce(Return(true));
 
84
    body_read(code, 9);
 
85
}