|
| 1 | +package jUnit; |
| 2 | + |
| 3 | +import static org.junit.Assert.fail; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.LinkedList; |
| 9 | + |
| 10 | +import org.eclipse.core.resources.IProject; |
| 11 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 12 | +import org.eclipse.core.runtime.CoreException; |
| 13 | +import org.eclipse.core.runtime.IPath; |
| 14 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.junit.runners.Parameterized; |
| 18 | +import org.junit.runners.Parameterized.Parameters; |
| 19 | + |
| 20 | +import io.sloeber.core.api.BoardsManager; |
| 21 | +import io.sloeber.core.api.CodeDescriptor; |
| 22 | +import io.sloeber.core.api.CompileOptions; |
| 23 | +import io.sloeber.core.api.ConfigurationDescriptor; |
| 24 | +import io.sloeber.core.api.Sketch; |
| 25 | +import jUnit.boards.Due; |
| 26 | +import jUnit.boards.IBoard; |
| 27 | +import jUnit.boards.NodeMCUBoard; |
| 28 | +import jUnit.boards.UnoBoard; |
| 29 | +import jUnit.boards.Zero; |
| 30 | +import jUnit.boards.megaBoard; |
| 31 | + |
| 32 | +@SuppressWarnings("nls") |
| 33 | +@RunWith(Parameterized.class) |
| 34 | +public class CompileAndUpload { |
| 35 | + private static final boolean reinstall_boards_and_libraries = false; |
| 36 | + private static int mCounter = 0; |
| 37 | + private IBoard myBoard; |
| 38 | + private String myName; |
| 39 | + |
| 40 | + public CompileAndUpload(String name, IBoard board) { |
| 41 | + this.myBoard = board; |
| 42 | + this.myName = name; |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("rawtypes") |
| 47 | + @Parameters(name = "{index}: {0}") |
| 48 | + public static Collection examples() { |
| 49 | + WaitForInstallerToFinish(); |
| 50 | + |
| 51 | + IBoard unoBoard = new UnoBoard(); |
| 52 | + IBoard nodeMCUBoard = new NodeMCUBoard(); |
| 53 | + IBoard megaBoard = new megaBoard(); |
| 54 | + IBoard zeroBoard = new Zero(); |
| 55 | + IBoard dueBoard = new Due(); |
| 56 | + LinkedList<Object[]> examples = new LinkedList<>(); |
| 57 | + |
| 58 | + examples.add(new Object[] { "uno", unoBoard }); |
| 59 | + examples.add(new Object[] { "mega", megaBoard }); |
| 60 | + examples.add(new Object[] { "zero", zeroBoard }); |
| 61 | + examples.add(new Object[] { "due", dueBoard }); |
| 62 | + |
| 63 | + return examples; |
| 64 | + // new leonardoBoard(), new EsploraBoard(), new AdafruitnRF52idBoard(), |
| 65 | + // new AdafruitnCirquitPlaygroundBoard(), new Primo(), |
| 66 | + } |
| 67 | + |
| 68 | + /* |
| 69 | + * In new new installations (of the Sloeber development environment) the |
| 70 | + * installer job will trigger downloads These mmust have finished before we |
| 71 | + * can start testing |
| 72 | + */ |
| 73 | + |
| 74 | + public static void WaitForInstallerToFinish() { |
| 75 | + |
| 76 | + installAdditionalBoards(); |
| 77 | + |
| 78 | + Shared.waitForAllJobsToFinish(); |
| 79 | + } |
| 80 | + |
| 81 | + public static void installAdditionalBoards() { |
| 82 | + String[] packageUrlsToAdd = { "http://arduino.esp8266.com/stable/package_esp8266com_index.json", |
| 83 | + "https://raw.githubusercontent.com/stm32duino/BoardManagerFiles/master/STM32/package_stm_index.json" }; |
| 84 | + BoardsManager.addPackageURLs(new HashSet<>(Arrays.asList(packageUrlsToAdd)), true); |
| 85 | + if (reinstall_boards_and_libraries) { |
| 86 | + BoardsManager.installAllLatestPlatforms(); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testExamples() { |
| 93 | + IPath templateFolder = Shared.getTemplateFolder("fastBlink"); |
| 94 | + Build_Verify_upload(CodeDescriptor.createCustomTemplate(templateFolder)); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + public void Build_Verify_upload(CodeDescriptor codeDescriptor) { |
| 99 | + |
| 100 | + IProject theTestProject = null; |
| 101 | + |
| 102 | + NullProgressMonitor monitor = new NullProgressMonitor(); |
| 103 | + String projectName = String.format("%05d_%s", new Integer(mCounter++), this.myName); |
| 104 | + try { |
| 105 | + |
| 106 | + theTestProject = this.myBoard.getBoardDescriptor().createProject(projectName, null, |
| 107 | + ConfigurationDescriptor.getDefaultDescriptors(), codeDescriptor, new CompileOptions(null), monitor); |
| 108 | + Shared.waitForAllJobsToFinish(); // for the indexer |
| 109 | + } catch (Exception e) { |
| 110 | + e.printStackTrace(); |
| 111 | + fail("Failed to create the project:" + projectName); |
| 112 | + return; |
| 113 | + } |
| 114 | + try { |
| 115 | + theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor); |
| 116 | + if (Shared.hasBuildErrors(theTestProject)) { |
| 117 | + // try again because the libraries may not yet been added |
| 118 | + Shared.waitForAllJobsToFinish(); // for the indexer |
| 119 | + try { |
| 120 | + Thread.sleep(3000);// seen sometimes the libs were still not |
| 121 | + // added |
| 122 | + } catch (InterruptedException e) { |
| 123 | + // ignore |
| 124 | + } |
| 125 | + theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor); |
| 126 | + if (Shared.hasBuildErrors(theTestProject)) { |
| 127 | + // give up |
| 128 | + fail("Failed to compile the project:" + projectName + " build errors"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + } catch (CoreException e) { |
| 133 | + e.printStackTrace(); |
| 134 | + fail("Failed to compile the project:" + projectName + " exception"); |
| 135 | + } |
| 136 | + Sketch.upload(theTestProject); |
| 137 | + } |
| 138 | +} |
0 commit comments