File: ValidateImageHandling.java

package info (click to toggle)
java-gnome 4.1.3-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 9,840 kB
  • sloc: java: 27,002; ansic: 4,517; perl: 1,651; python: 1,187; makefile: 136
file content (223 lines) | stat: -rw-r--r-- 6,833 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
/*
 * java-gnome, a UI library for writing GTK and GNOME programs from Java!
 *
 * Copyright © 2007-2010 Operational Dynamics Consulting, Pty Ltd
 *
 * The code in this file, and the program it is a part of, is made available
 * to you by its authors as open source software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License version
 * 2 ("GPL") as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GPL for more details.
 *
 * You should have received a copy of the GPL along with this program. If not,
 * see http://www.gnu.org/licenses/. The authors of this program may be
 * contacted through http://java-gnome.sourceforge.net/.
 */
package org.gnome.gdk;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.gnome.gtk.Button;
import org.gnome.gtk.GraphicalTestCase;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.IconSize;
import org.gnome.gtk.Stock;

/**
 * @author Andrew Cowie
 */
public class ValidateImageHandling extends GraphicalTestCase
{
    public final void testPixbufFromFile() {
        try {
            new Pixbuf("foo.jpg");
            fail("Should have complained not being able to find file");
        } catch (FileNotFoundException fnfe) {
            // good
        }

        try {
            new Pixbuf("README.markdown");
            fail("Specified file not an image, should have thrown");
        } catch (RuntimeException re) {
            // good
        } catch (FileNotFoundException fnfe) {
            fail("Should have found non-image file");
        }

        try {
            new Pixbuf("src/bindings/java-gnome_Icon.png");
        } catch (FileNotFoundException fnfe) {
            fail("Target file should exist. Did someone refactor the logo image?");
        }
    }

    public final void testPixbufGetPixels() {
        final Pixbuf pixbuf;
        final byte[] data;
        final int width, height, num;

        try {
            pixbuf = new Pixbuf("src/bindings/java-gnome_Icon.png");
        } catch (FileNotFoundException fnfe) {
            fail("Target file should exist. Did someone refactor the logo image?");
            return;
        }

        width = pixbuf.getWidth();
        height = pixbuf.getHeight();
        num = pixbuf.getNumChannels();
        assertEquals("Expecting a 48x48 image", 48, width);
        assertEquals("Expecting a 48x48 image", 48, height);
        assertEquals("Where did the alpha channel go?", 4, num);

        data = pixbuf.getPixels();

        assertEquals(width * height * num, data.length);

        int sum;

        sum = 0;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {
                for (int c = 0; c < num; c++) {
                    int pixel;

                    pixel = data[j * width + i * num + c] & 0xFF;

                    assertTrue((pixel >= 0) && (pixel <= 255));

                    sum += pixel;
                }
            }
        }

        /*
         * Ok, this isn't much of a test, but it sorta catches the flavour of
         * things, and is the sort of calculation we were doing during our
         * performance tests.
         */
        assertEquals(515687, sum);
    }

    public final void testPixbufCopying() {
        final Pixbuf original;
        final Pixbuf copy;
        byte[] data;
        int O, C;

        original = Gtk.renderIcon(new Button(), Stock.OK, IconSize.BUTTON);
        data = original.getPixels();
        O = 0;
        for (int i = 0; i < data.length; i++) {
            O += data[i];
        }

        copy = original.copy();

        assertNotSame(copy, original);

        data = copy.getPixels();
        C = 0;
        for (int i = 0; i < data.length; i++) {
            C += data[i];
        }

        assertEquals(C, O);
    }

    /*
     * Obviously we can't assess quality of transformed image, but we can at
     * least exercise the code path.
     */
    public final void testPixbufScaling() {
        final Pixbuf original;
        final Pixbuf result;

        original = Gtk.renderIcon(new Button(), Stock.HELP, IconSize.MENU);

        assertTrue(original.getWidth() < 100);
        assertTrue(original.getHeight() < 100);

        result = original.scale(300, 500, InterpType.NEAREST);

        assertEquals(300, result.getWidth());
        assertEquals(500, result.getHeight());
    }

    public final void testPixbufFromData() throws IOException {
        Pixbuf target;
        byte[] data;
        final FileInputStream png;

        target = null;
        data = null;

        try {
            target = new Pixbuf(data);
            fail("Shouldn't be able to pass a null byte[]");
        } catch (IllegalArgumentException iae) {
            // good
        }

        data = new byte[0];
        try {
            target = new Pixbuf(data);
            fail("Something should break if you pass in an empty byte[]!");
        } catch (IOException iae) {
            // good
        }

        try {
            target = new Pixbuf(data);
            fail("The byte[] isn't an image.");
        } catch (IOException iae) {
            // good
        }

        data = new byte[4017];
        png = new FileInputStream("src/bindings/java-gnome_Icon.png");
        png.read(data);

        target = new Pixbuf(data);

        assertEquals(48, target.getWidth());
        assertEquals(48, target.getHeight());
    }

    public final void testPixbufInfoFromDisk() throws IOException {
        final Pixbuf pixbuf;
        final int loadedWidth, loadedHeight;
        final int infoWidth, infoHeight;

        /*
         * This file specifically picked to be a non-square image.
         */

        pixbuf = new Pixbuf("tests/prototype/MapleSyrup.jpg");

        loadedWidth = pixbuf.getWidth();
        loadedHeight = pixbuf.getHeight();

        assertEquals("Expecting a 200x398 image", 200, loadedWidth);
        assertEquals("Expecting a 200x398 image", 398, loadedHeight);

        /*
         * Now test the static functions used for probing a file on disk. It's
         * hard to imagine that this would come up with different answers than
         * loading an image, but the point is to exercise these code paths,
         * not validate gdk-pixbuf as such.
         */

        infoWidth = Pixbuf.getFileInfoWidth("tests/prototype/MapleSyrup.jpg");
        infoHeight = Pixbuf.getFileInfoHeight("tests/prototype/MapleSyrup.jpg");

        assertEquals("File info width should be 200", 200, infoWidth);
        assertEquals("File info height should be 398", 398, infoHeight);
    }
}