Skip to content

Commit 86441e4

Browse files
committed
Added first tests for library manager engine
1 parent 31de5b6 commit 86441e4

File tree

139 files changed

+20707
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+20707
-6
lines changed

app/src/cc/arduino/contributions/ContributionsSelfCheck.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,9 @@ public void run() {
7373
updateContributionIndex();
7474
updateLibrariesIndex();
7575

76-
boolean updatablePlatforms = BaseNoGui.indexer.getPackages().stream()
77-
.flatMap(pack -> pack.getPlatforms().stream())
78-
.anyMatch(new UpdatablePlatformPredicate());
76+
boolean updatablePlatforms = checkForUpdatablePlatforms();
7977

80-
boolean updatableLibraries = BaseNoGui.librariesIndexer.getIndex().getLibraries().stream()
81-
.anyMatch(new UpdatableLibraryPredicate());
78+
boolean updatableLibraries = checkForUpdatableLibraries();
8279

8380
if (!updatableLibraries && !updatablePlatforms) {
8481
return;
@@ -125,6 +122,17 @@ public void windowGainedFocus(WindowEvent evt) {
125122
});
126123
}
127124

125+
static boolean checkForUpdatablePlatforms() {
126+
return BaseNoGui.indexer.getPackages().stream()
127+
.flatMap(pack -> pack.getPlatforms().stream())
128+
.anyMatch(new UpdatablePlatformPredicate());
129+
}
130+
131+
static boolean checkForUpdatableLibraries() {
132+
return BaseNoGui.librariesIndexer.getIndex().getLibraries().stream()
133+
.anyMatch(new UpdatableLibraryPredicate());
134+
}
135+
128136
@Override
129137
public boolean cancel() {
130138
cancelled = true;

app/src/cc/arduino/contributions/libraries/filters/UpdatableLibraryPredicate.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import cc.arduino.contributions.VersionComparator;
3333
import cc.arduino.contributions.libraries.ContributedLibrary;
34+
import cc.arduino.contributions.libraries.LibrariesIndexer;
3435
import processing.app.BaseNoGui;
3536
import processing.app.packages.UserLibrary;
3637

@@ -40,6 +41,16 @@
4041

4142
public class UpdatableLibraryPredicate implements Predicate<ContributedLibrary> {
4243

44+
LibrariesIndexer librariesIndexer;
45+
46+
public UpdatableLibraryPredicate() {
47+
librariesIndexer = BaseNoGui.librariesIndexer;
48+
}
49+
50+
public UpdatableLibraryPredicate(LibrariesIndexer indexer) {
51+
librariesIndexer = indexer;
52+
}
53+
4354
@Override
4455
public boolean test(ContributedLibrary lib) {
4556
Optional<UserLibrary> installed = lib.getInstalledLibrary();
@@ -48,7 +59,7 @@ public boolean test(ContributedLibrary lib) {
4859
}
4960
String installedVersion = installed.get().getVersion();
5061
String libraryName = lib.getName();
51-
List<ContributedLibrary> libraries = BaseNoGui.librariesIndexer.getIndex().find(libraryName);
62+
List<ContributedLibrary> libraries = librariesIndexer.getIndex().find(libraryName);
5263
return libraries.stream()
5364
.anyMatch(library -> VersionComparator.greaterThan(library.getParsedVersion(), installedVersion));
5465
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cc.arduino.contributions;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.File;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import org.junit.Test;
12+
13+
import cc.arduino.contributions.libraries.ContributedLibrary;
14+
import cc.arduino.contributions.libraries.LibrariesIndexer;
15+
import processing.app.BaseNoGui;
16+
import processing.app.packages.UserLibraryFolder;
17+
import processing.app.packages.UserLibraryFolder.Location;
18+
19+
public class UpdatableLibraryTest {
20+
21+
File testdata = new File(
22+
UpdatableLibraryTest.class.getResource("/").getFile(),
23+
"../testdata/libraries");
24+
File index_SD_only = new File(testdata, "index_SD_only");
25+
File SD111 = new File(testdata, "SD_1.1.1");
26+
File SD121 = new File(testdata, "SD_1.2.1");
27+
File index_Bridge_only = new File(testdata, "index_Bridge_only");
28+
File Bridge163 = new File(testdata, "Bridge_1.6.3");
29+
File Bridge170 = new File(testdata, "Bridge_1.7.0");
30+
31+
@Test
32+
public void testUpdatableLibrary() throws Exception {
33+
List<UserLibraryFolder> folders = new ArrayList<>();
34+
folders.add(new UserLibraryFolder(SD111, Location.IDE_BUILTIN));
35+
36+
LibrariesIndexer indexer = new LibrariesIndexer(index_SD_only);
37+
BaseNoGui.librariesIndexer = indexer;
38+
indexer.parseIndex();
39+
indexer.setLibrariesFolders(folders);
40+
41+
ContributedLibrary sdLib = indexer.getIndex().getInstalled("SD").get();
42+
assertTrue("SD lib is installed", sdLib.isLibraryInstalled());
43+
assertEquals("SD installed version", "1.1.1", sdLib.getParsedVersion());
44+
45+
assertTrue(ContributionsSelfCheck.checkForUpdatableLibraries());
46+
47+
folders.add(new UserLibraryFolder(SD121, Location.SKETCHBOOK));
48+
indexer.setLibrariesFolders(folders);
49+
50+
sdLib = indexer.getIndex().getInstalled("SD").get();
51+
assertTrue("SD lib is installed", sdLib.isLibraryInstalled());
52+
assertEquals("SD installed version", "1.2.1", sdLib.getParsedVersion());
53+
54+
assertFalse(ContributionsSelfCheck.checkForUpdatableLibraries());
55+
}
56+
57+
@Test
58+
public void testUpdatableLibraryWithBundled() throws Exception {
59+
List<UserLibraryFolder> folders = new ArrayList<>();
60+
folders.add(new UserLibraryFolder(Bridge163, Location.IDE_BUILTIN));
61+
62+
LibrariesIndexer indexer = new LibrariesIndexer(index_Bridge_only);
63+
BaseNoGui.librariesIndexer = indexer;
64+
indexer.parseIndex();
65+
indexer.setLibrariesFolders(folders);
66+
67+
ContributedLibrary l = indexer.getIndex().getInstalled("Bridge").get();
68+
assertTrue("Bridge lib is installed", l.isLibraryInstalled());
69+
assertEquals("Bridge installed version", "1.6.3", l.getParsedVersion());
70+
71+
assertTrue(ContributionsSelfCheck.checkForUpdatableLibraries());
72+
73+
folders.add(new UserLibraryFolder(Bridge170, Location.SKETCHBOOK));
74+
indexer.setLibrariesFolders(folders);
75+
76+
l = indexer.getIndex().getInstalled("Bridge").get();
77+
assertTrue("Bridge lib is installed", l.isLibraryInstalled());
78+
assertEquals("Bridge installed version", "1.7.0", l.getParsedVersion());
79+
80+
assertFalse(ContributionsSelfCheck.checkForUpdatableLibraries());
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
= Bridge Library for Arduino =
2+
3+
The Bridge library simplifies communication between the ATmega32U4 and the AR9331.
4+
5+
For more information about this library please visit us at
6+
http://www.arduino.cc/en/Reference/YunBridgeLibrary
7+
8+
== License ==
9+
10+
Copyright (c) 2014 Arduino LLC. All right reserved.
11+
12+
This library is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU Lesser General Public
14+
License as published by the Free Software Foundation; either
15+
version 2.1 of the License, or (at your option) any later version.
16+
17+
This library is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20+
Lesser General Public License for more details.
21+
22+
You should have received a copy of the GNU Lesser General Public
23+
License along with this library; if not, write to the Free Software
24+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
Arduino Yún Bridge example
3+
4+
This example for the YunShield/Yún shows how
5+
to use the Bridge library to access the digital and
6+
analog pins on the board through REST calls.
7+
It demonstrates how you can create your own API when
8+
using REST style calls through the browser.
9+
10+
Possible commands created in this shetch:
11+
12+
"/arduino/digital/13" -> digitalRead(13)
13+
"/arduino/digital/13/1" -> digitalWrite(13, HIGH)
14+
"/arduino/analog/2/123" -> analogWrite(2, 123)
15+
"/arduino/analog/2" -> analogRead(2)
16+
"/arduino/mode/13/input" -> pinMode(13, INPUT)
17+
"/arduino/mode/13/output" -> pinMode(13, OUTPUT)
18+
19+
This example code is part of the public domain
20+
21+
http://www.arduino.cc/en/Tutorial/Bridge
22+
23+
*/
24+
25+
#include <Bridge.h>
26+
#include <BridgeServer.h>
27+
#include <BridgeClient.h>
28+
29+
// Listen to the default port 5555, the Yún webserver
30+
// will forward there all the HTTP requests you send
31+
BridgeServer server;
32+
33+
void setup() {
34+
// Bridge startup
35+
pinMode(13, OUTPUT);
36+
digitalWrite(13, LOW);
37+
Bridge.begin();
38+
digitalWrite(13, HIGH);
39+
40+
// Listen for incoming connection only from localhost
41+
// (no one from the external network could connect)
42+
server.listenOnLocalhost();
43+
server.begin();
44+
}
45+
46+
void loop() {
47+
// Get clients coming from server
48+
BridgeClient client = server.accept();
49+
50+
// There is a new client?
51+
if (client) {
52+
// Process request
53+
process(client);
54+
55+
// Close connection and free resources.
56+
client.stop();
57+
}
58+
59+
delay(50); // Poll every 50ms
60+
}
61+
62+
void process(BridgeClient client) {
63+
// read the command
64+
String command = client.readStringUntil('/');
65+
66+
// is "digital" command?
67+
if (command == "digital") {
68+
digitalCommand(client);
69+
}
70+
71+
// is "analog" command?
72+
if (command == "analog") {
73+
analogCommand(client);
74+
}
75+
76+
// is "mode" command?
77+
if (command == "mode") {
78+
modeCommand(client);
79+
}
80+
}
81+
82+
void digitalCommand(BridgeClient client) {
83+
int pin, value;
84+
85+
// Read pin number
86+
pin = client.parseInt();
87+
88+
// If the next character is a '/' it means we have an URL
89+
// with a value like: "/digital/13/1"
90+
if (client.read() == '/') {
91+
value = client.parseInt();
92+
digitalWrite(pin, value);
93+
} else {
94+
value = digitalRead(pin);
95+
}
96+
97+
// Send feedback to client
98+
client.print(F("Pin D"));
99+
client.print(pin);
100+
client.print(F(" set to "));
101+
client.println(value);
102+
103+
// Update datastore key with the current pin value
104+
String key = "D";
105+
key += pin;
106+
Bridge.put(key, String(value));
107+
}
108+
109+
void analogCommand(BridgeClient client) {
110+
int pin, value;
111+
112+
// Read pin number
113+
pin = client.parseInt();
114+
115+
// If the next character is a '/' it means we have an URL
116+
// with a value like: "/analog/5/120"
117+
if (client.read() == '/') {
118+
// Read value and execute command
119+
value = client.parseInt();
120+
analogWrite(pin, value);
121+
122+
// Send feedback to client
123+
client.print(F("Pin D"));
124+
client.print(pin);
125+
client.print(F(" set to analog "));
126+
client.println(value);
127+
128+
// Update datastore key with the current pin value
129+
String key = "D";
130+
key += pin;
131+
Bridge.put(key, String(value));
132+
} else {
133+
// Read analog pin
134+
value = analogRead(pin);
135+
136+
// Send feedback to client
137+
client.print(F("Pin A"));
138+
client.print(pin);
139+
client.print(F(" reads analog "));
140+
client.println(value);
141+
142+
// Update datastore key with the current pin value
143+
String key = "A";
144+
key += pin;
145+
Bridge.put(key, String(value));
146+
}
147+
}
148+
149+
void modeCommand(BridgeClient client) {
150+
int pin;
151+
152+
// Read pin number
153+
pin = client.parseInt();
154+
155+
// If the next character is not a '/' we have a malformed URL
156+
if (client.read() != '/') {
157+
client.println(F("error"));
158+
return;
159+
}
160+
161+
String mode = client.readStringUntil('\r');
162+
163+
if (mode == "input") {
164+
pinMode(pin, INPUT);
165+
// Send feedback to client
166+
client.print(F("Pin D"));
167+
client.print(pin);
168+
client.print(F(" configured as INPUT!"));
169+
return;
170+
}
171+
172+
if (mode == "output") {
173+
pinMode(pin, OUTPUT);
174+
// Send feedback to client
175+
client.print(F("Pin D"));
176+
client.print(pin);
177+
client.print(F(" configured as OUTPUT!"));
178+
return;
179+
}
180+
181+
client.print(F("error: invalid mode "));
182+
client.print(mode);
183+
}

0 commit comments

Comments
 (0)