Skip to content

Commit 4e262a5

Browse files
author
Federico Fissore
committed
Introducing "defaultTarget" board is the one selected in preferences is not available. Closes #1731
1 parent b530742 commit 4e262a5

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

app/src/processing/app/debug/TargetPlatform.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class TargetPlatform {
4444
* Contains preferences for every defined board
4545
*/
4646
private Map<String, TargetBoard> boards = new LinkedHashMap<String, TargetBoard>();
47+
private TargetBoard defaultBoard;
4748

4849
/**
4950
* Contains preferences for every defined programmer
@@ -86,11 +87,16 @@ public TargetPlatform(String _name, File _folder, TargetPackage parent)
8687
boardsPreferences.remove("menu");
8788

8889
// Create boards
89-
for (String id : boardsPreferences.keySet()) {
90+
Set<String> boardIDs = boardsPreferences.keySet();
91+
for (String id : boardIDs) {
9092
PreferencesMap preferences = boardsPreferences.get(id);
9193
TargetBoard board = new TargetBoard(id, preferences, this);
9294
boards.put(id, board);
9395
}
96+
if (!boardIDs.isEmpty()) {
97+
PreferencesMap preferences = boardsPreferences.get(boardIDs.iterator().next());
98+
defaultBoard = new TargetBoard(id, preferences, this);
99+
}
94100
} catch (IOException e) {
95101
throw new TargetPlatformException(format(_("Error loading {0}"),
96102
boardsFile.getAbsolutePath()), e);
@@ -156,7 +162,10 @@ public PreferencesMap getPreferences() {
156162
}
157163

158164
public TargetBoard getBoard(String boardId) {
159-
return boards.get(boardId);
165+
if (boards.containsKey(boardId)) {
166+
return boards.get(boardId);
167+
}
168+
return defaultBoard;
160169
}
161170

162171
public TargetPackage getContainerPackage() {
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package processing.app;
22

3-
import org.junit.BeforeClass;
3+
import org.junit.Before;
44

55
public abstract class AbstractWithPreferencesTest {
66

7-
@BeforeClass
8-
public static void init() throws Exception {
7+
@Before
8+
public void init() throws Exception {
99
Base.initPlatform();
1010
Preferences.init(null);
11+
Theme.init();
12+
13+
Base.untitledFolder = Base.createTempFolder("untitled");
14+
Base.untitledFolder.deleteOnExit();
15+
1116
}
1217
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package processing.app;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import processing.app.debug.TargetBoard;
7+
8+
import static org.junit.Assert.assertNotEquals;
9+
10+
public class DefaultTargetTest extends AbstractWithPreferencesTest {
11+
12+
private String oldBoardID;
13+
14+
@Before
15+
public void saveBoardFromPreferences() throws Exception {
16+
oldBoardID = Preferences.get("board");
17+
}
18+
19+
@After
20+
public void restoreBoardIntoPreferences() throws Exception {
21+
Preferences.set("board", oldBoardID);
22+
Preferences.save();
23+
}
24+
25+
@Test
26+
public void testDefaultTarget() throws Exception {
27+
Preferences.set("board", "unreal_board");
28+
29+
// should not raise an exception
30+
new Base(new String[0]);
31+
32+
TargetBoard targetBoard = Base.getTargetBoard();
33+
assertNotEquals("unreal_board", targetBoard.getId());
34+
}
35+
}

0 commit comments

Comments
 (0)