summaryrefslogtreecommitdiffstats
path: root/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/performance/Test_org_eclipse_swt_graphics_ImageLoader.java
blob: 5c97fb75d634a6b00c2571d65722d0c1764f02fd (plain)
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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tests.junit.performance;


import java.io.*;

import junit.framework.*;
import junit.textui.*;

import org.eclipse.swt.graphics.*;
import org.eclipse.test.performance.PerformanceMeter;

/**
 * Automated Performance Test Suite for class org.eclipse.swt.graphics.ImageLoader
 *
 * @see org.eclipse.swt.graphics.ImageLoader
 */
public class Test_org_eclipse_swt_graphics_ImageLoader extends SwtPerformanceTestCase {

public Test_org_eclipse_swt_graphics_ImageLoader(String name) {
	super(name);
}

public static void main(String[] args) {
	TestRunner.run(suite());
}

public void test_loadLjava_lang_String() {
	final int COUNT = 4500;
	
	// j2se and j2me(cdc) can load from a filename but, j2me(cldc) throws an exception
	if (isJ2ME()) return;

	ImageLoader loader = new ImageLoader();
	String fileName = getPath(imageFilenames[0] + "." + imageFormats[0]);
	
	PerformanceMeter meter = createMeter("ImageLoader load(String)");
	meter.start();
	for (int i = 0; i < COUNT; i++) {
		loader.load(fileName);
	}
	meter.stop();
	
	disposeMeter(meter);
}

public void test_saveLjava_io_OutputStreamI() {
	final int COUNT = 30000;
	
	ImageLoader loader = new ImageLoader();
	boolean jpgSupported = false;
	for (int i=0; i<imageFormats.length; i++) {
		if (imageFormats[i].equals("jpg")) {
			jpgSupported = true;
			break;
		}
	}
	if (!jpgSupported) return;
	
	String filename = imageFilenames[0];
	// must use jpg since save is not implemented yet in png format		
	String filetype = "jpg";
	FileInputStream inStream = null;
	try {
		inStream = new FileInputStream(getPath(filename + "." + filetype));
	} catch (FileNotFoundException e1) {
		e1.printStackTrace();
	}	
	loader.load(inStream);
	try {
		inStream.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
	ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	try {
		for (int i = 0; i < imageFormats.length; i++) {
			if (imageFormats[i].equals(filetype)) {

				PerformanceMeter meter = createMeter("ImageLoader save(OutputStream,I) - " + i);
				meter.start();
				for (int j = 0; j < COUNT; j++) {
					loader.save(outStream, i);
				}
				meter.stop();
				
				disposeMeter(meter);
				
				break;
			}
		}
	} finally {
		try {
			outStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

public static Test suite() {
	TestSuite suite = new TestSuite();
	java.util.Vector methodNames = methodNames();
	java.util.Enumeration e = methodNames.elements();
	while (e.hasMoreElements()) {
		suite.addTest(new Test_org_eclipse_swt_graphics_ImageLoader((String)e.nextElement()));
	}
	return suite;
}
public static java.util.Vector methodNames() {
	java.util.Vector methodNames = new java.util.Vector();
	methodNames.addElement("test_loadLjava_lang_String");
	methodNames.addElement("test_saveLjava_io_OutputStreamI");
	return methodNames;
}
protected void runTest() throws Throwable {
	if (getName().equals("test_loadLjava_lang_String")) test_loadLjava_lang_String();
	else if (getName().equals("test_saveLjava_io_OutputStreamI")) test_saveLjava_io_OutputStreamI();
}
}