~chasedouglas/+junk/client-lib

« back to all changes in this revision

Viewing changes to unit-tests/compositor/test_buffer_manager.cpp

  • Committer: Alan Griffiths
  • Date: 2012-07-06 16:48:49 UTC
  • mfrom: (28.4.63 add-bm-client)
  • Revision ID: alan@octopull.co.uk-20120706164849-4g2i72hplzgbvuwa
Refine interfaces surrounding the BufferBundle

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * Authored by: Thomas Voss <thomas.voss@canonical.com>
17
17
 */
18
18
 
 
19
#include "mock_buffer.h"
 
20
#include "mock_graphic_buffer_allocator.h"
 
21
 
19
22
#include "mir/compositor/buffer.h"
20
 
#include "mir/compositor/buffer_manager.h"
21
 
#include "mir/compositor/buffer_manager_client.h"
 
23
#include "mir/compositor/buffer_allocation_strategy.h"
 
24
#include "mir/compositor/buffer_bundle_manager.h"
 
25
#include "mir/compositor/buffer_bundle.h"
22
26
#include "mir/compositor/graphic_buffer_allocator.h"
23
27
 
24
28
#include <gmock/gmock.h>
38
42
    }    
39
43
};
40
44
 
41
 
struct MockGraphicBufferAllocator : mc::GraphicBufferAllocator
 
45
struct MockBufferAllocationStrategy : public mc::BufferAllocationStrategy
42
46
{
43
 
 public:
44
 
    MOCK_METHOD3(alloc_buffer, std::shared_ptr<mc::Buffer>(geom::Width, geom::Height, mc::PixelFormat));
45
 
    MOCK_METHOD1(free_buffer, void(std::shared_ptr<mc::Buffer>));
 
47
    MockBufferAllocationStrategy(std::shared_ptr<mc::GraphicBufferAllocator> allocator)
 
48
            : mc::BufferAllocationStrategy(allocator)
 
49
    {
 
50
    }
 
51
 
 
52
    MOCK_METHOD4(
 
53
        allocate_buffers_for_bundle,
 
54
        void(geom::Width, geom::Height, mc::PixelFormat, mc::BufferBundle* bundle));
46
55
};
47
56
 
48
57
const geom::Width width{1024};
50
59
const geom::Stride stride{geom::dim_cast<geom::Stride>(width)};
51
60
const mc::PixelFormat pixel_format{mc::PixelFormat::rgba_8888};
52
61
 
53
 
struct MockBuffer : public mc::Buffer
54
 
{
55
 
 public:
56
 
        MockBuffer()
57
 
        {
58
 
            using namespace testing;
59
 
                ON_CALL(*this, width()).       WillByDefault(Return(::width));
60
 
                ON_CALL(*this, height()).      WillByDefault(Return(::height));
61
 
                ON_CALL(*this, stride()).      WillByDefault(Return(::stride));
62
 
                ON_CALL(*this, pixel_format()).WillByDefault(Return(::pixel_format));
63
 
        }
64
 
 
65
 
    MOCK_CONST_METHOD0(width, geom::Width());
66
 
    MOCK_CONST_METHOD0(height, geom::Height());
67
 
    MOCK_CONST_METHOD0(stride, geom::Stride());
68
 
    MOCK_CONST_METHOD0(pixel_format, mc::PixelFormat());
69
 
};
70
62
}
71
63
 
72
64
TEST(buffer_manager, create_buffer)
73
65
{
74
66
    using namespace testing;
75
67
    
76
 
    MockBuffer mock_buffer;
77
 
    std::shared_ptr<MockBuffer> default_buffer(
 
68
    mc::MockBuffer mock_buffer{width, height, stride, pixel_format};
 
69
    std::shared_ptr<mc::MockBuffer> default_buffer(
78
70
        &mock_buffer,
79
71
        EmptyDeleter()); 
80
 
    MockGraphicBufferAllocator graphic_allocator;
81
 
    mc::BufferManager buffer_manager(&graphic_allocator);
82
 
 
83
 
    EXPECT_CALL(graphic_allocator, alloc_buffer(Eq(width), Eq(height), Eq(pixel_format))).
84
 
                Times(AtLeast(1)).WillRepeatedly(Return(default_buffer));
85
 
 
86
 
    mc::BufferManagerClient *client = nullptr;
87
 
    client = buffer_manager.create_client(
88
 
        width,
89
 
        height,
90
 
        pixel_format);
91
 
 
92
 
    EXPECT_TRUE(client != nullptr);
93
 
 
94
 
    //TODO this is a frig to keep valgrind happy
95
 
    delete client;
 
72
    mc::MockGraphicBufferAllocator graphic_allocator;
 
73
    std::shared_ptr<mc::GraphicBufferAllocator> allocator(&graphic_allocator, EmptyDeleter());
 
74
    MockBufferAllocationStrategy allocation_strategy(allocator);
 
75
 
 
76
    mc::BufferBundleManager buffer_bundle_manager(
 
77
        &allocation_strategy);
 
78
 
 
79
    /* note: this is somewhat of a weak test, some create_clients will create a varied amount
 
80
             of buffers */
 
81
    EXPECT_CALL(
 
82
        graphic_allocator,
 
83
        alloc_buffer(Eq(width), Eq(height), Eq(pixel_format)))
 
84
            .Times(0);
 
85
 
 
86
    EXPECT_CALL(allocation_strategy, allocate_buffers_for_bundle(Eq(width), Eq(height), Eq(pixel_format), _)).Times(AtLeast(1));
 
87
    
 
88
    std::shared_ptr<mc::BufferBundle> bundle{
 
89
        buffer_bundle_manager.create_buffer_bundle(
 
90
            width,
 
91
            height,
 
92
            pixel_format)};
 
93
    
 
94
    EXPECT_TRUE(bundle != nullptr);
 
95
    
96
96
}