~alan-griffiths/mir/fix-1654023

« back to all changes in this revision

Viewing changes to tests/unit-tests/graphics/test_egl_error.cpp

  • Committer: Daniel van Vugt
  • Date: 2015-04-28 07:54:10 UTC
  • mfrom: (2517 development-branch)
  • mto: This revision was merged to the branch mainline in revision 2673.
  • Revision ID: daniel.van.vugt@canonical.com-20150428075410-rwskshfuar7voesp
Merge latest trunk and fix conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as 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 "mir/graphics/egl_error.h"
 
20
#include "mir_test_doubles/mock_egl.h"
 
21
 
 
22
#include <gtest/gtest.h>
 
23
#include <gmock/gmock.h>
 
24
 
 
25
namespace mg = mir::graphics;
 
26
namespace mtd = mir::test::doubles;
 
27
 
 
28
namespace
 
29
{
 
30
 
 
31
std::string to_hex_string(int n)
 
32
{
 
33
    std::stringstream ss;
 
34
    ss << std::showbase << std::hex << n;
 
35
    return ss.str();
 
36
}
 
37
 
 
38
struct EGLErrorTest : ::testing::Test
 
39
{
 
40
    testing::NiceMock<mtd::MockEGL> mock_egl;
 
41
 
 
42
    std::string const user_message{"Something failed"};
 
43
    std::string const egl_error_code_str{"EGL_BAD_MATCH"};
 
44
    int const egl_error_code{EGL_BAD_MATCH};
 
45
};
 
46
 
 
47
}
 
48
 
 
49
TEST_F(EGLErrorTest, produces_correct_error_code)
 
50
{
 
51
    using namespace testing;
 
52
 
 
53
    EXPECT_CALL(mock_egl, eglGetError())
 
54
        .WillOnce(Return(egl_error_code));
 
55
 
 
56
    std::error_code ec;
 
57
 
 
58
    try
 
59
    {
 
60
        throw mg::egl_error("");
 
61
    }
 
62
    catch (std::system_error const& error)
 
63
    {
 
64
        ec = error.code();
 
65
    }
 
66
 
 
67
    EXPECT_THAT(ec.value(), Eq(egl_error_code));
 
68
}
 
69
 
 
70
TEST_F(EGLErrorTest, produces_message_with_user_string_and_error_code_for_known_error)
 
71
{
 
72
    using namespace testing;
 
73
 
 
74
    EXPECT_CALL(mock_egl, eglGetError())
 
75
        .WillOnce(Return(egl_error_code));
 
76
 
 
77
    std::string error_message;
 
78
 
 
79
    try
 
80
    {
 
81
        throw mg::egl_error(user_message);
 
82
    }
 
83
    catch (std::system_error const& error)
 
84
    {
 
85
        error_message = error.what();
 
86
    }
 
87
 
 
88
    EXPECT_THAT(error_message, HasSubstr(user_message));
 
89
    EXPECT_THAT(error_message, HasSubstr(egl_error_code_str));
 
90
    EXPECT_THAT(error_message, HasSubstr(to_hex_string(egl_error_code)));
 
91
}
 
92
 
 
93
TEST_F(EGLErrorTest, produces_message_with_user_string_and_error_code_for_unknown_error)
 
94
{
 
95
    using namespace testing;
 
96
 
 
97
    int const unknown_egl_error_code{0x131313};
 
98
 
 
99
    EXPECT_CALL(mock_egl, eglGetError())
 
100
        .WillOnce(Return(unknown_egl_error_code));
 
101
 
 
102
    std::string error_message;
 
103
 
 
104
    try
 
105
    {
 
106
        throw mg::egl_error(user_message);
 
107
    }
 
108
    catch (std::system_error const& error)
 
109
    {
 
110
        error_message = error.what();
 
111
    }
 
112
 
 
113
    EXPECT_THAT(error_message, HasSubstr(user_message));
 
114
    EXPECT_THAT(error_message, HasSubstr(to_hex_string(unknown_egl_error_code)));
 
115
}