~ubuntu-branches/ubuntu/utopic/mir/utopic-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-03-10 19:28:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: package-import@ubuntu.com-20140310192846-rq9qm3ec26yrelo2
Tags: upstream-0.1.6+14.04.20140310
Import upstream version 0.1.6+14.04.20140310

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014 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: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "src/server/compositor/screencast_display_buffer.h"
 
20
 
 
21
#include "mir_test_doubles/mock_buffer.h"
 
22
#include "mir_test_doubles/stub_buffer.h"
 
23
#include "mir_test_doubles/mock_gl.h"
 
24
#include "mir_test_doubles/stub_renderable.h"
 
25
 
 
26
#include <gtest/gtest.h>
 
27
#include <gmock/gmock.h>
 
28
 
 
29
namespace mc = mir::compositor;
 
30
namespace mtd = mir::test::doubles;
 
31
namespace geom = mir::geometry;
 
32
namespace mg = mir::graphics;
 
33
 
 
34
namespace
 
35
{
 
36
 
 
37
struct MockRenderFunctor
 
38
{
 
39
    void operator()(mg::Renderable const& r)
 
40
    {
 
41
        operator_call(&r);
 
42
    }
 
43
 
 
44
    MOCK_METHOD1(operator_call, void(mg::Renderable const*));
 
45
};
 
46
 
 
47
struct ScreencastDisplayBufferTest : testing::Test
 
48
{
 
49
    testing::NiceMock<mtd::MockGL> mock_gl;
 
50
};
 
51
 
 
52
}
 
53
 
 
54
TEST_F(ScreencastDisplayBufferTest, cleans_up_gl_resources)
 
55
{
 
56
    using namespace testing;
 
57
    GLuint const texture{11};
 
58
    GLuint const renderbuffer{12};
 
59
    GLuint const framebuffer{13};
 
60
 
 
61
    EXPECT_CALL(mock_gl, glGenTextures(1,_))
 
62
        .WillOnce(SetArgPointee<1>(texture));
 
63
    EXPECT_CALL(mock_gl, glGenRenderbuffers(1,_))
 
64
        .WillOnce(SetArgPointee<1>(renderbuffer));
 
65
    EXPECT_CALL(mock_gl, glGenFramebuffers(1,_))
 
66
        .WillOnce(SetArgPointee<1>(framebuffer));
 
67
 
 
68
    EXPECT_CALL(mock_gl, glDeleteTextures(1,Pointee(texture)));
 
69
    EXPECT_CALL(mock_gl, glDeleteRenderbuffers(1,Pointee(renderbuffer)));
 
70
    EXPECT_CALL(mock_gl, glDeleteFramebuffers(1,Pointee(framebuffer)));
 
71
 
 
72
    geom::Rectangle const rect{{100,100}, {800,600}};
 
73
    mtd::StubBuffer stub_buffer;
 
74
 
 
75
    mc::ScreencastDisplayBuffer db{rect, stub_buffer};
 
76
}
 
77
 
 
78
TEST_F(ScreencastDisplayBufferTest, cleans_up_gl_resources_on_construction_failure)
 
79
{
 
80
    using namespace testing;
 
81
    GLuint const texture{11};
 
82
    GLuint const renderbuffer{12};
 
83
    GLuint const framebuffer{13};
 
84
 
 
85
    EXPECT_CALL(mock_gl, glGenTextures(1,_))
 
86
        .WillOnce(SetArgPointee<1>(texture));
 
87
    EXPECT_CALL(mock_gl, glGenRenderbuffers(1,_))
 
88
        .WillOnce(SetArgPointee<1>(renderbuffer));
 
89
    EXPECT_CALL(mock_gl, glGenFramebuffers(1,_))
 
90
        .WillOnce(SetArgPointee<1>(framebuffer));
 
91
 
 
92
    EXPECT_CALL(mock_gl, glCheckFramebufferStatus(_))
 
93
        .WillOnce(Return(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT));
 
94
 
 
95
    EXPECT_CALL(mock_gl, glDeleteTextures(1,Pointee(texture)));
 
96
    EXPECT_CALL(mock_gl, glDeleteRenderbuffers(1,Pointee(renderbuffer)));
 
97
    EXPECT_CALL(mock_gl, glDeleteFramebuffers(1,Pointee(framebuffer)));
 
98
 
 
99
    geom::Rectangle const rect{{100,100}, {800,600}};
 
100
    mtd::StubBuffer stub_buffer;
 
101
 
 
102
    EXPECT_THROW({
 
103
        mc::ScreencastDisplayBuffer db(rect, stub_buffer);
 
104
    }, std::runtime_error);
 
105
}
 
106
 
 
107
TEST_F(ScreencastDisplayBufferTest, renders_to_supplied_buffer)
 
108
{
 
109
    using namespace testing;
 
110
 
 
111
    geom::Rectangle const rect{{100,100}, {800,600}};
 
112
    testing::NiceMock<mtd::MockBuffer> mock_buffer{
 
113
        rect.size, geom::Stride{100}, mir_pixel_format_xbgr_8888};
 
114
 
 
115
    InSequence s;
 
116
    /* Set the buffer as rendering target */
 
117
    EXPECT_CALL(mock_buffer, bind_to_texture());
 
118
    EXPECT_CALL(mock_gl,
 
119
                glViewport(0, 0,
 
120
                           mock_buffer.size().width.as_int(),
 
121
                           mock_buffer.size().height.as_int()));
 
122
    /* Restore previous viewport on exit */
 
123
    EXPECT_CALL(mock_gl, glViewport(0, 0, 0, 0));
 
124
 
 
125
    mc::ScreencastDisplayBuffer db{rect, mock_buffer};
 
126
    db.make_current();
 
127
}
 
128
 
 
129
TEST_F(ScreencastDisplayBufferTest, forces_rendering_to_complete_on_post_update)
 
130
{
 
131
    using namespace testing;
 
132
 
 
133
    geom::Rectangle const rect{{100,100}, {800,600}};
 
134
    mtd::StubBuffer stub_buffer;
 
135
 
 
136
    mc::ScreencastDisplayBuffer db{rect, stub_buffer};
 
137
 
 
138
    Mock::VerifyAndClearExpectations(&mock_gl);
 
139
    EXPECT_CALL(mock_gl, glFinish());
 
140
 
 
141
    db.post_update();
 
142
}
 
143
 
 
144
TEST_F(ScreencastDisplayBufferTest, renders_renderables_on_render_and_post_update)
 
145
{
 
146
    using namespace testing;
 
147
 
 
148
    geom::Rectangle const rect{{100,100}, {800,600}};
 
149
    mtd::StubBuffer stub_buffer;
 
150
 
 
151
    std::list<std::shared_ptr<mg::Renderable>> renderables{
 
152
        std::make_shared<mtd::StubRenderable>(),
 
153
        std::make_shared<mtd::StubRenderable>(),
 
154
        std::make_shared<mtd::StubRenderable>()};
 
155
 
 
156
    mc::ScreencastDisplayBuffer db{rect, stub_buffer};
 
157
 
 
158
    Mock::VerifyAndClearExpectations(&mock_gl);
 
159
    MockRenderFunctor mock_render_functor;
 
160
 
 
161
    InSequence s;
 
162
 
 
163
    for (auto const& renderable : renderables)
 
164
        EXPECT_CALL(mock_render_functor, operator_call(renderable.get()));
 
165
 
 
166
    EXPECT_CALL(mock_gl, glFinish());
 
167
 
 
168
    db.render_and_post_update(renderables, std::ref(mock_render_functor));
 
169
}