File: drawing.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (276 lines) | stat: -rw-r--r-- 9,782 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/graphics/drawing.cpp
// Purpose:     Tests for wxGraphicsContent general drawing
// Author:      Armel Asselin
// Created:     2014-02-21
// Copyright:   (c) 2014 ElliƩ Computing <opensource@elliecomputing.com>
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"

#include "drawing.h"

#if wxUSE_TEST_GC_DRAWING

#include "wx/image.h"
#include "wx/wfstream.h"
#include "wx/stdpaths.h"
#include "wx/scopeguard.h"

#include "testimagefile.h"

#include <stdexcept>

// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------

//// ORGANIZATION /////////////////////////////////////////////////////////////

// This test suite is organized around two axes:
// - drawing test cases
// - drawing contexts life cycle
// => each drawing test case represent a serie of drawing primitives to execute
//  for whichever context
// => each drawing context life cycle represent a particular class of
//  wxGraphicsContext and a way to create, dispose of and save it so that it is
//  possible to compare it with a reference file

// A plugin system is implemented to let developers of contributed libraries
// test their library without the need to impact the test system and its
// dependencies. See RunPluginsDrawingCase.

// The crossing of drawing case and life cycles is implemented by
// RunIndividualDrawingCase

// The CPPUNIT test case class present a test per drawing case per life cycle
// so that it is easy to run a particular test

// The test requires reference files and must produce them when an
// implementation changed and new good references are known to be produced.
// Environment variables control where reference files are located and when to
// produce them:
//  - WX_TEST_SUITE_BUILD_REFERENCE must be "1" to request production of
//      reference files (by default only testing is done)
//  - WX_TEST_SUITE_REFERENCE_DIR must be a path to a directory containing the
//      sub-directory "gcdrawing-references" (by default the parent directory
//      of the directory of the test program is used)

//// WRITING NEW TEST CASES

// - add a new function to realize the drawing in the "cases functions" section
// - add a case structure declaration for it in the "test cases" section
// - use drawingbasic.cpp as a sample to add your own test case implementation

//// WRITING NEW FACTORIES

// - if the wxGraphicsContext is a class built-in wxWidgets, add a
//      DrawingTestGCFactory derived sub-class in drawing.h header
//      together with a declaration for it and its implementation
//      can be placed in drawing.cpp
//      Once this is done duplicate all the CPP UNIT test functions
//      and entries "DrawToImage_YYY" to your new GC "DrawTo<newGc>_YYYY"
//
// - if it is not built-in (contributed library/wxCode...), make a plugin for it
//      test.bkl contains a sample "test_drawingplugin" target, you can use
//      drawingplgsample.cpp as a start, see RunPluginsDrawingCase declaration
//      for information about how to run the tests

wxString GraphicsContextDrawingTestCase::ms_referenceDirectory;
bool GraphicsContextDrawingTestCase::ms_buildReference;
bool GraphicsContextDrawingTestCase::ms_buildReferenceDetermined;
GraphicsContextDrawingTestCase::ImageGraphicsContextLifeCycle
    GraphicsContextDrawingTestCase::ms_imageLifeCycle;

#if wxUSE_SVG
    GraphicsContextDrawingTestCase::SvgGraphicsContextLifeCycle
        GraphicsContextDrawingTestCase::ms_svgLifeCycle;
#endif // wxUSE_SVG

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( GraphicsContextDrawingTestCase );

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphicsContextDrawingTestCase,
    "GraphicsContextDrawingTestCase" );

// ----------------------------------------------------------------------------
// tests themselves
// ----------------------------------------------------------------------------

void GraphicsContextDrawingTestCase::RunIndividualDrawingCase (
    DrawingTestGCFactory& gcFactory,
    const DrawingTestCase & testCase)
{
    wxFileName fileName, refFileName;
    wxString testsOutputDirectory = wxStandardPaths::Get().GetTempDir();
    wxString refOutputDirectory = GetTestsReferenceDirectory();

    wxString platformTag;
    if (!testCase.platformIndependent && !gcFactory.PlatformIndependent())
        platformTag = wxString::Format("_%s", GetPlatformTag());

    fileName.Assign (testsOutputDirectory,
        wxString::Format("image_test_%s%s_%d", gcFactory.GetIdForFileName(),
                         platformTag, testCase.caseNumber),
        gcFactory.GetExtensionForFileName());

    refFileName.Assign (refOutputDirectory,
        wxString::Format("image_test_%s%s_%d_ref", gcFactory.GetIdForFileName(),
                        platformTag, testCase.caseNumber),
        gcFactory.GetExtensionForFileName());

    {
        wxGraphicsContext *gc = NULL;

        wxON_BLOCK_EXIT_OBJ1(gcFactory, DrawingTestGCFactory::CleanUp, gc);

        gc = gcFactory.BuildNewContext(
            wxSize(testCase.width, testCase.height),
            testCase.pointsPerInch, fileName);

        (this->*testCase.m_drawingF)(gc);

        gcFactory.SaveBuiltContext(gc);
    }

    if (GetBuildReference())
    {
         WX_ASSERT_MESSAGE(
             ("Cannot copy file \"%s\" to \"%s\".",
            fileName.GetFullPath(), refFileName.GetFullPath()),
            wxCopyFile (fileName.GetFullPath(),
                        refFileName.GetFullPath(), true));
    }
    else if (gcFactory.UseImageComparison())
    {
        WX_ASSERT_SAME_AS_IMAGE_FILE(fileName.GetFullPath(),
                                     refFileName.GetFullPath());
    }
    else
    {
        WX_ASSERT_SAME_AS_FILE(fileName.GetFullPath(),
                               refFileName.GetFullPath());
    }
}


bool GraphicsContextDrawingTestCase::GetBuildReference() const
{
    if (!ms_buildReferenceDetermined)
    {
        wxString value;
        if (wxGetEnv("WX_TEST_SUITE_BUILD_REFERENCE", &value))
            ms_buildReference = value == "1";
    }
    return ms_buildReference;
}

wxString GraphicsContextDrawingTestCase::GetTestsReferenceDirectory() const
{
    if (ms_referenceDirectory.empty())
    {
        wxFileName refDir;

        if ( !wxGetEnv("WX_TEST_SUITE_REFERENCE_DIR",
                        &ms_referenceDirectory) )
        {
            refDir = wxFileName(wxStandardPaths::Get().GetExecutablePath());
            refDir.RemoveLastDir();
        }
        else
        {
            refDir = wxFileName(ms_referenceDirectory, wxT(""));
        }
        refDir.AppendDir ("drawing");
        refDir.AppendDir ("references");
        ms_referenceDirectory = refDir.GetPath();
    }
    return ms_referenceDirectory;
}

wxString GraphicsContextDrawingTestCase::GetPlatformTag() const
{
    // We consider that the platform tag is the kind of default renderer plus
    // its major/minor versions.
    // The reason why including major/minor version is important, is that the
    // rendering engine typically evolves somewhat between two version
    // (i.e. font rendering is not the same in Windows XP and Windows 8)
    int major, minor;
    const wxGraphicsRenderer *defaultRenderer = wxGraphicsRenderer::GetDefaultRenderer();
    wxString rendererName = defaultRenderer->GetName();
    defaultRenderer->GetVersion (&major, &minor);

    return wxString::Format("%s-%d.%d", rendererName, major, minor);
}

///////////////////////////////////////////////////////////////////////////////
// BUILT-IN FACTORIES  ////////////////////////////////////////////////////////

wxGraphicsContext *
GraphicsContextDrawingTestCase::ImageGraphicsContextLifeCycle::
    BuildNewContext (wxSize expectedSize, double WXUNUSED(pointsPerInch),
    const wxFileName &targetFileName)
{
    m_image = new wxImage (expectedSize);
    m_image->InitAlpha();

    m_targetFileName = targetFileName.GetFullPath();

    // we should probably pass the number of points per inches somewhere...
    //  but I don't see where yet...
    return wxGraphicsContext::Create(*m_image);
}

void
GraphicsContextDrawingTestCase::ImageGraphicsContextLifeCycle::
    SaveBuiltContext (wxGraphicsContext *&gc)
{
    wxDELETE(gc);

    m_image->SaveFile (m_targetFileName);
}

void
GraphicsContextDrawingTestCase::ImageGraphicsContextLifeCycle::
    CleanUp (wxGraphicsContext *gc)
{
    delete gc;
    m_targetFileName.clear();
    wxDELETE(m_image);
}

#if wxUSE_SVG
wxGraphicsContext *
GraphicsContextDrawingTestCase::SvgGraphicsContextLifeCycle::
    BuildNewContext (wxSize WXUNUSED(expectedSize),
    double WXUNUSED(pointsPerInch),
    const wxFileName &WXUNUSED(targetFileName))
{
    m_svgFileDc = NULL;
    //m_svg_file_dc = new wxSVGFileDC (target_file_name.GetFullPath(),
    //    expected_size.GetWidth(), expected_size.GetHeight(), points_per_inch);

    // unfortunately cannot make GC over a DC yet :(
    throw std::runtime_error("SVG as no wxGC interface yet");
}

void
GraphicsContextDrawingTestCase::SvgGraphicsContextLifeCycle::
    SaveBuiltContext (wxGraphicsContext *&WXUNUSED(gc))
{
}

void
GraphicsContextDrawingTestCase::SvgGraphicsContextLifeCycle::
    CleanUp (wxGraphicsContext *WXUNUSED(gc))
{
    wxDELETE (m_svgFileDc);
}
#endif // wxUSE_SVG

#endif // wxUSE_TEST_GC_DRAWING