JAL-3599 gradle tasks to test JalviewJS launches in a chrome or chromium browser
authorBen Soares <b.soares@dundee.ac.uk>
Thu, 8 Jun 2023 19:58:49 +0000 (20:58 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Thu, 8 Jun 2023 19:58:49 +0000 (20:58 +0100)
build.gradle
gradle.properties
src/jalview/gui/Desktop.java
utils/jalviewjs/chromium_test/jalview_bin_Jalview-stderr.html [new file with mode: 0644]

index ca599a8..8a7e0d7 100644 (file)
@@ -568,11 +568,13 @@ ext {
   jalviewjsJ2sAltSettingsFileName = string("${jalviewDir}/${jalviewjs_j2s_alt_settings}")
   jalviewjsJ2sProps = null
   jalviewjsJ2sPlugin = jalviewjs_j2s_plugin
+  jalviewjsStderrLaunchFilename = "${jalviewjsSiteDir}/"+(file(jalviewjs_stderr_launch).getName())
 
   eclipseWorkspace = null
   eclipseBinary = string("")
   eclipseVersion = string("")
   eclipseDebug = false
+  
   // ENDEXT
 }
 
@@ -4207,3 +4209,73 @@ task jalviewjs {
   dependsOn jalviewjsBuildSite
 }
 
+
+task jalviewjsCopyStderrLaunchFile(type: Copy) {
+  from file(jalviewjs_stderr_launch)
+  into jalviewjsSiteDir
+
+  inputs.file jalviewjs_stderr_launch
+  outputs.file jalviewjsStderrLaunchFilename
+}
+
+task jalviewjsChromiumProfile {
+  def profileDir = "${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}/${jalviewjs_chromium_profile_name}"
+  def firstRun = file("${profileDir}/First Run")
+
+  doFirst {
+    mkdir profileDir
+    firstRun.text = ""
+  }
+  
+  outputs.file firstRun
+}
+
+task jalviewjsLaunchTest(type: Exec) {
+  group "Test"
+  description "Check JalviewJS opens in a browser"
+  dependsOn jalviewjsBuildSite
+  dependsOn jalviewjsCopyStderrLaunchFile
+  dependsOn jalviewjsChromiumProfile
+
+  def chromiumBinary = jalviewjs_chromium_binary
+  if (chromiumBinary.startsWith("~/")) {
+    chromiumBinary = System.getProperty("user.home") + chromiumBinary.substring(1)
+  }
+  
+  doFirst {
+    def exec = file(chromiumBinary)
+    if (!exec.exists()) {
+      throw new GradleException("Could not find chromium binary '${chromiumBinary}'. Cannot run task ${name}.")
+    }
+  }
+
+  executable(chromiumBinary)
+  args([
+    "--headless=new",
+    "--timeout=15000",
+    "--virtual-time-budget=15000",
+    "--user-data-dir=${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}",
+    "--profile-directory=${jalviewjs_chromium_profile_name}",
+    "--allow-file-access-from-files",
+    "--enable-logging=stderr",
+    jalviewjsStderrLaunchFilename
+  ])
+
+  standardOutput = new ByteArrayOutputStream()
+  errorOutput = new ByteArrayOutputStream()
+  
+  doLast {
+    def found = false
+    def stderr = errorOutput.toString()
+    stderr.eachLine { line ->
+      if (line.contains(jalviewjs_desktop_init_string)) {
+        println("Found line '"+line+"'")
+        found = true
+        return
+      }
+    }
+    if (!found) {
+      throw new GradleException("Could not find evidence of Desktop launch in JalviewJS.")
+    }
+  }
+}
index 7b3a031..93bf118 100644 (file)
@@ -260,5 +260,11 @@ jalviewjs_j2s_to_console = true
 jalviewjs_closure_compiler = tools/closure_compiler.jar
 jalviewjs_j2s_closure_stdout = j2s-closure.out
 
+# for checking jalviewjs launches okay
+jalviewjs_chromium_binary = ~/buildtools/chromium/chrome
+jalviewjs_chromium_user_dir = chromium
+jalviewjs_chromium_profile_name = BUILD
+jalviewjs_stderr_launch = utils/jalviewjs/chromium_test/jalview_bin_Jalview-stderr.html
+jalviewjs_desktop_init_string = JALVIEWJS: CREATED DESKTOP
 
 testp=gradle.properties
index bfd700f..2dfe820 100644 (file)
@@ -623,6 +623,12 @@ public class Desktop extends jalview.jbgui.GDesktop
       }
     });
     desktop.addMouseListener(ma);
+
+    if (Platform.isJS())
+    {
+      // used for jalviewjsTest
+      jalview.bin.Console.info("JALVIEWJS: CREATED DESKTOP");
+    }
   }
 
   /**
@@ -3132,10 +3138,14 @@ public class Desktop extends jalview.jbgui.GDesktop
     String title = "View of desktop";
     ImageExporter exporter = new ImageExporter(writer, null, TYPE.EPS,
             title);
-    try {
+    try
+    {
       exporter.doExport(of, this, width, height, title);
-    } catch (ImageOutputException ioex) {
-      jalview.bin.Console.error("Unexpected error whilst writing Jalview desktop snapshot as EPS",ioex);
+    } catch (ImageOutputException ioex)
+    {
+      jalview.bin.Console.error(
+              "Unexpected error whilst writing Jalview desktop snapshot as EPS",
+              ioex);
     }
   }
 
@@ -3582,7 +3592,8 @@ public class Desktop extends jalview.jbgui.GDesktop
    */
   public static void closeDesktop()
   {
-    if (Desktop.instance != null) {
+    if (Desktop.instance != null)
+    {
       Desktop.instance.closeAll_actionPerformed(null);
       Desktop.instance.setVisible(false);
       Desktop.instance.dispose();
diff --git a/utils/jalviewjs/chromium_test/jalview_bin_Jalview-stderr.html b/utils/jalviewjs/chromium_test/jalview_bin_Jalview-stderr.html
new file mode 100644 (file)
index 0000000..bf7a678
--- /dev/null
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>SwingJS test Jalview</title><meta charset="utf-8" />
+<script src="swingjs/swingjs2.js"></script>
+<script>
+if (!self.SwingJS)alert('swingjs2.js was not found. It needs to be in swingjs folder in the same directory as ' + document.location.href)
+Info = {
+  code: null,
+  main: "jalview.bin.Jalview",
+  core: "NONE",
+       width: 850,
+       height: 550,
+  readyFunction: null,
+       serverURL: 'https://chemapps.stolaf.edu/jmol/jsmol/php/jsmol.php',
+       j2sPath: 'swingjs/j2s',
+       console: window.console,
+       allowjavascript: true
+}
+</script>
+</head>
+<body>
+<script>
+// we define console.err because swingjs2.js calls it instead of console.error
+window.console.err = function() {
+       this.error.apply(this,arguments);
+}
+SwingJS.getApplet('testApplet', Info)
+getClassList = function(){J2S._saveFile('_j2sclasslist.txt', Clazz.ClassFilesLoaded.sort().join('\n'))}
+</script>
+<div style="position:absolute;left:900px;top:30px;width:600px;height:300px;">
+<div id="sysoutdiv" contentEditable="true" style="border:1px solid green;width:100%;height:95%;overflow:auto"></div>
+This is System.out. <a href="javascript:testApplet._clearConsole()">clear it</a> <br>Add ?j2snocore to URL to see full class list; ?j2sdebug to use uncompressed j2s/core files <br><a href="javascript:getClassList()">get _j2sClassList.txt</a>
+</div>
+</body>
+</html>