1
/*
2
 * Copyright © 2022 Uli Schlachter
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 *
24
 * Author: Uli Schlachter <psychon@znc.in>
25
 */
26

            
27
#include "cairo-test.h"
28
#include "cairo-script.h"
29

            
30
struct write_data {
31
    cairo_bool_t finished;
32
    cairo_test_status_t test_status;
33
};
34

            
35
static cairo_surface_t*
36
1
create_recording_surface ()
37
{
38
    /* Create a non-empty recording surface with arbitrary content */
39
1
    cairo_surface_t *surf = cairo_recording_surface_create (CAIRO_CONTENT_COLOR, NULL);
40
1
    cairo_t *cr = cairo_create (surf);
41

            
42
1
    cairo_move_to (cr, 0, 0);
43
1
    cairo_line_to (cr, 10, 0);
44
1
    cairo_stroke (cr);
45

            
46
1
    cairo_destroy (cr);
47
1
    return surf;
48
}
49

            
50
static cairo_status_t
51
30
write_func(void *closure, const unsigned char* bytes, unsigned int length)
52
{
53
30
    struct write_data *data = closure;
54
    (void) bytes; (void) length;
55

            
56
30
    if (data->finished)
57
	data->test_status = CAIRO_TEST_ERROR;
58

            
59
30
    return CAIRO_STATUS_SUCCESS;
60
}
61

            
62
static cairo_test_status_t
63
1
preamble (cairo_test_context_t *ctx)
64
{
65
1
    struct write_data write_data = { FALSE, CAIRO_TEST_SUCCESS };
66
1
    cairo_device_t *script_device = cairo_script_create_for_stream (write_func, &write_data);
67
1
    cairo_surface_t *recording = create_recording_surface ();
68
    cairo_surface_t *script;
69
    cairo_t *cr;
70

            
71
    /* Draw the recording surface to a script surface */
72
1
    script = cairo_script_surface_create (script_device, CAIRO_CONTENT_COLOR, 5, 5);
73
1
    cr = cairo_test_create (script, ctx);
74
1
    cairo_set_source_surface (cr, recording, 0, 0);
75
1
    cairo_paint (cr);
76
1
    cairo_destroy (cr);
77
1
    cairo_surface_destroy (script);
78

            
79
    /* Finish the script device; no further writing allowed afterwards */
80
1
    cairo_device_finish (script_device);
81
1
    write_data.finished = TRUE;
82
1
    cairo_device_destroy (script_device);
83

            
84
1
    cairo_surface_destroy (recording);
85

            
86
1
    return write_data.test_status;
87
}
88

            
89
1
CAIRO_TEST (bug_277,
90
	    "Regression test: Script surface emitting test after finish()",
91
	    NULL, /* keywords */
92
	    NULL, /* requirements */
93
	    0, 0,
94
	    preamble, NULL)