1/*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17package com.badlogic.gdx.jglfw.tests;
18
19import java.awt.BorderLayout;
20import java.awt.HeadlessException;
21import java.awt.event.ActionEvent;
22import java.awt.event.ActionListener;
23import java.awt.event.KeyAdapter;
24import java.awt.event.KeyEvent;
25import java.awt.event.MouseAdapter;
26import java.awt.event.MouseEvent;
27
28import javax.swing.DefaultListSelectionModel;
29import javax.swing.JButton;
30import javax.swing.JFrame;
31import javax.swing.JList;
32import javax.swing.JPanel;
33import javax.swing.JScrollPane;
34import javax.swing.ListSelectionModel;
35import javax.swing.UIManager;
36
37import com.badlogic.gdx.LifecycleListener;
38import com.badlogic.gdx.Preferences;
39import com.badlogic.gdx.backends.jglfw.JglfwApplication;
40import com.badlogic.gdx.backends.jglfw.JglfwApplicationConfiguration;
41import com.badlogic.gdx.backends.jglfw.JglfwFiles;
42import com.badlogic.gdx.backends.jglfw.JglfwPreferences;
43import com.badlogic.gdx.files.FileHandle;
44import com.badlogic.gdx.tests.utils.GdxTest;
45import com.badlogic.gdx.tests.utils.GdxTests;
46import com.badlogic.gdx.utils.GdxRuntimeException;
47
48public class JglfwTestStarter extends JFrame {
49	public JglfwTestStarter () throws HeadlessException {
50		super("JGLFW libgdx Tests");
51		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52		setContentPane(new TestList());
53		pack();
54		setSize(getWidth(), 600);
55		setLocationRelativeTo(null);
56		setVisible(true);
57	}
58
59	/** Runs the {@link GdxTest} with the given name.
60	 * @param testName the name of a test class
61	 * @return {@code true} if the test was found and run, {@code false} otherwise */
62	public static JglfwApplication runTest (String testName) {
63		final GdxTest test = GdxTests.newTest(testName);
64		if (test == null) throw new GdxRuntimeException("Test not found: " + testName);
65
66		final JglfwApplicationConfiguration config = new JglfwApplicationConfiguration();
67		config.width = 640;
68		config.height = 480;
69		config.title = testName;
70		config.forceExit = false;
71		return new JglfwApplication(test, config);
72	}
73
74	class TestList extends JPanel {
75		public TestList () {
76			setLayout(new BorderLayout());
77
78			final JButton button = new JButton("Run Test");
79
80			final JList list = new JList(GdxTests.getNames().toArray());
81			JScrollPane pane = new JScrollPane(list);
82
83			DefaultListSelectionModel m = new DefaultListSelectionModel();
84			m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
85			m.setLeadAnchorNotificationEnabled(false);
86			list.setSelectionModel(m);
87
88			list.addMouseListener(new MouseAdapter() {
89				public void mouseClicked (MouseEvent event) {
90					if (event.getClickCount() == 2) button.doClick();
91				}
92			});
93
94			list.addKeyListener(new KeyAdapter() {
95				public void keyPressed (KeyEvent e) {
96					if (e.getKeyCode() == KeyEvent.VK_ENTER) button.doClick();
97				}
98			});
99
100			final Preferences prefs = new JglfwPreferences(new FileHandle(new JglfwFiles().getExternalStoragePath()
101				+ ".prefs/jglfw-tests"));
102			list.setSelectedValue(prefs.getString("last", null), true);
103
104			button.addActionListener(new ActionListener() {
105				@Override
106				public void actionPerformed (ActionEvent e) {
107					String testName = (String)list.getSelectedValue();
108					prefs.putString("last", testName);
109					prefs.flush();
110					JglfwTestStarter.this.setVisible(false);
111					runTest(testName).addLifecycleListener(new LifecycleListener() {
112						public void resume () {
113						}
114
115						public void pause () {
116						}
117
118						public void dispose () {
119							JglfwTestStarter.this.setVisible(true);
120						}
121					});
122				}
123			});
124
125			add(pane, BorderLayout.CENTER);
126			add(button, BorderLayout.SOUTH);
127		}
128	}
129
130	/** Runs a libgdx test.
131	 *
132	 * If no arguments are provided on the command line, shows a list of tests to choose from. If an argument is present, the test
133	 * with that name will immediately be run.
134	 *
135	 * @param argv command line arguments */
136	public static void main (String[] argv) throws Exception {
137		if (argv.length > 0) {
138			runTest(argv[0]);
139			return;
140		}
141		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
142		new JglfwTestStarter();
143	}
144}
145