From 9044197eeff670bc7cc5e4d617478f55d11d346f Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Tue, 31 Oct 2017 22:08:40 +0000
Subject: [PATCH 3/4] JOSMTestRules: add fakeImagery() method(s) for setting up
 a TileSourceRule as part of the mock JOSM setup process

---
 .../josm/testutils/JOSMTestRules.java              | 54 ++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
index 238ed200e..22e9e95f7 100644
--- a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
+++ b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
@@ -7,6 +7,8 @@ import java.security.GeneralSecurityException;
 import java.text.MessageFormat;
 import java.util.TimeZone;
 
+import java.awt.Color;
+
 import org.junit.rules.TemporaryFolder;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
@@ -56,6 +58,7 @@ public class JOSMTestRules implements TestRule {
     private boolean usePreferences = false;
     private APIType useAPI = APIType.NONE;
     private String i18n = null;
+    private TileSourceRule tileSourceRule;
     private boolean platform;
     private boolean useProjection;
     private boolean useProjectionNadGrids;
@@ -242,6 +245,36 @@ public class JOSMTestRules implements TestRule {
     }
 
     /**
+     * Replace imagery sources with a default set of mock tile sources
+     *
+     * @return this instance, for easy chaining
+     */
+    public JOSMTestRules fakeImagery() {
+        return this.fakeImagery(
+            new TileSourceRule(
+                true,
+                true,
+                true,
+                new TileSourceRule.ColorSource(Color.WHITE, "White Tiles", 256),
+                new TileSourceRule.ColorSource(Color.BLACK, "Black Tiles", 256),
+                new TileSourceRule.ColorSource(Color.MAGENTA, "Magenta Tiles", 256),
+                new TileSourceRule.ColorSource(Color.GREEN, "Green Tiles", 256)
+            )
+        );
+    }
+
+    /**
+     * Replace imagery sources with those from specific mock tile server setup
+     *
+     * @return this instance, for easy chaining
+     */
+    public JOSMTestRules fakeImagery(TileSourceRule tileSourceRule) {
+        this.preferences();
+        this.tileSourceRule = tileSourceRule;
+        return this;
+    }
+
+    /**
      * Use the {@link Main#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel},
      *         {@link Main#menu}, {@link Main#toolbar} global variables in this test.
      * @return this instance, for easy chaining
@@ -256,14 +289,28 @@ public class JOSMTestRules implements TestRule {
     @Override
     public Statement apply(Statement base, Description description) {
         Statement statement = base;
+        // counter-intuitively, Statements which need to have their setup routines performed *after* another one need to
+        // be added into the chain *before* that one, so that it ends up on the "inside".
         if (timeout > 0) {
             // TODO: new DisableOnDebug(timeout)
             statement = new FailOnTimeoutStatement(statement, timeout);
         }
+
+        // this half of TileSourceRule's initialization must happen after josm is set up
+        if (this.tileSourceRule != null) {
+            statement = this.tileSourceRule.applyRegisterLayers(statement, description);
+        }
+
         statement = new CreateJosmEnvironment(statement);
         if (josmHome != null) {
             statement = josmHome.apply(statement, description);
         }
+
+        // run mock tile server as the outermost Statement (started first) so it can hopefully be initializing in
+        // parallel with other setup
+        if (this.tileSourceRule != null) {
+            statement = this.tileSourceRule.applyRunServer(statement, description);
+        }
         return statement;
     }
 
@@ -409,6 +456,13 @@ public class JOSMTestRules implements TestRule {
     }
 
     /**
+     * @return TileSourceRule which is automatically started by this rule
+     */
+    public TileSourceRule getTileSourceRule() {
+        return this.tileSourceRule;
+    }
+
+    /**
      * Clean up after running a test
      */
     @SuppressFBWarnings("DM_GC")
-- 
2.11.0

