~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
ImportĀ upstreamĀ versionĀ 0.8.0+14.10.20141010

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
17
17
 */
18
18
 
 
19
#include "mir_test_doubles/fd_matcher.h"
19
20
#include "src/server/frontend/protobuf_buffer_packer.h"
20
21
#include "src/server/frontend/resource_cache.h"
21
22
 
28
29
namespace mfd=mir::frontend::detail;
29
30
namespace mp=mir::protobuf;
30
31
namespace geom=mir::geometry;
31
 
 
32
 
namespace
33
 
{
34
 
 
35
 
struct MockResourceCache : public mf::MessageResourceCache
36
 
{
37
 
    MOCK_METHOD2(save_resource, void(google::protobuf::Message*, std::shared_ptr<void> const&));
38
 
    MOCK_METHOD2(save_fd, void(google::protobuf::Message*, mir::Fd const&));
39
 
    MOCK_METHOD1(free_resource, void(google::protobuf::Message*));
40
 
};
41
 
 
42
 
struct ProtobufBufferPacker : public testing::Test
43
 
{
44
 
    ProtobufBufferPacker() :
45
 
        mock_resource_cache(std::make_shared<testing::NiceMock<MockResourceCache>>())
46
 
    {
47
 
    }
48
 
    std::shared_ptr<MockResourceCache> mock_resource_cache;
49
 
};
50
 
}
51
 
 
52
 
TEST_F(ProtobufBufferPacker, packing)
 
32
namespace mtd=mir::test::doubles;
 
33
 
 
34
TEST(ProtobufBufferPacker, packing)
53
35
{
54
36
    geom::Stride dummy_stride(4);
55
37
 
56
38
    mp::Buffer response;
57
 
    mfd::ProtobufBufferPacker packer(&response, mock_resource_cache);
 
39
    mfd::ProtobufBufferPacker packer(&response);
58
40
 
59
41
    int num_fd = 33, num_int = 44;
60
42
    std::vector<mir::Fd> raw_fds;
83
65
    EXPECT_EQ(789, response.height());
84
66
}
85
67
 
86
 
TEST_F(ProtobufBufferPacker, fd_packing_saves_using_the_resource_cache)
87
 
{
88
 
    using namespace testing;
89
 
    mir::Fd fake_fd0{fileno(tmpfile())};
90
 
    mir::Fd fake_fd1{fileno(tmpfile())};
91
 
 
92
 
    mp::Buffer response;
93
 
 
94
 
    EXPECT_CALL(*mock_resource_cache, save_fd(&response, Ref(fake_fd0)));
95
 
    EXPECT_CALL(*mock_resource_cache, save_fd(&response, Ref(fake_fd1)));
96
 
 
97
 
    mfd::ProtobufBufferPacker packer(&response, mock_resource_cache);
98
 
 
99
 
    packer.pack_fd(fake_fd0);
100
 
    packer.pack_fd(fake_fd1);
 
68
TEST(ProtobufBufferPacker, data_and_fds_are_the_same_as_packed)
 
69
{
 
70
    using namespace testing;
 
71
 
 
72
    mp::Buffer response;
 
73
    unsigned int const num_fds{3};
 
74
    unsigned int const num_data{9};
 
75
    for(auto i = 0u; i < num_fds; i++)
 
76
        response.add_fd(mir::Fd{fileno(tmpfile())});
 
77
    for(auto i = 0u; i < num_data; i++)
 
78
        response.add_data(i*3);
 
79
 
 
80
    mfd::ProtobufBufferPacker packer(&response);
 
81
 
 
82
    mir::Fd additional_fd{fileno(tmpfile())};
 
83
    packer.pack_fd(additional_fd);
 
84
 
 
85
    auto fds = packer.fds();
 
86
    EXPECT_THAT(fds.size(), Eq(num_fds + 1));
 
87
 
 
88
    auto data = packer.data();
 
89
    EXPECT_THAT(data, ElementsAreArray(response.data().data(), num_data));
 
90
}
 
91
 
 
92
TEST(ProtobufBufferPacker, message_takes_ownership_of_fds)
 
93
{
 
94
    using namespace testing;
 
95
 
 
96
    mp::Buffer response;
 
97
    unsigned int const num_fds{3};
 
98
    for(auto i = 0u; i < num_fds; i++)
 
99
        response.add_fd(fileno(tmpfile()));
 
100
 
 
101
    mir::Fd additional_fd{fileno(tmpfile())};
 
102
 
 
103
    {
 
104
        mfd::ProtobufBufferPacker packer(&response);
 
105
        packer.pack_fd(additional_fd);
 
106
    }
 
107
 
 
108
    EXPECT_THAT(response.fd().size(), Eq(num_fds+1));
 
109
    auto i = 0u;
 
110
    for(; i < num_fds; i++)
 
111
        EXPECT_THAT(response.fd().Get(i), Not(mtd::RawFdIsValid()));
 
112
    EXPECT_THAT(response.fd().Get(i), mtd::RawFdIsValid());
101
113
}