Ticket #15508: v2-0003-JOSMTestRules-add-fakeImagery-method-s-for-setting-u.patch

File v2-0003-JOSMTestRules-add-fakeImagery-method-s-for-setting-u.patch, 4.2 KB (added by ris, 6 years ago)
  • test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    From e506c66c36ff6f104f60e5908c896f54fd0e6251 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 b import java.security.GeneralSecurityException;  
    77import java.text.MessageFormat;
    88import java.util.TimeZone;
    99
     10import java.awt.Color;
     11
    1012import org.junit.rules.TemporaryFolder;
    1113import org.junit.rules.TestRule;
    1214import org.junit.runner.Description;
    public class JOSMTestRules implements TestRule {  
    5658    private boolean usePreferences = false;
    5759    private APIType useAPI = APIType.NONE;
    5860    private String i18n = null;
     61    private TileSourceRule tileSourceRule;
    5962    private boolean platform;
    6063    private boolean useProjection;
    6164    private boolean useProjectionNadGrids;
    public class JOSMTestRules implements TestRule {  
    242245    }
    243246
    244247    /**
     248     * Replace imagery sources with a default set of mock tile sources
     249     *
     250     * @return this instance, for easy chaining
     251     */
     252    public JOSMTestRules fakeImagery() {
     253        return this.fakeImagery(
     254            new TileSourceRule(
     255                true,
     256                true,
     257                true,
     258                new TileSourceRule.ColorSource(Color.WHITE, "White Tiles", 256),
     259                new TileSourceRule.ColorSource(Color.BLACK, "Black Tiles", 256),
     260                new TileSourceRule.ColorSource(Color.MAGENTA, "Magenta Tiles", 256),
     261                new TileSourceRule.ColorSource(Color.GREEN, "Green Tiles", 256)
     262            )
     263        );
     264    }
     265
     266    /**
     267     * Replace imagery sources with those from specific mock tile server setup
     268     *
     269     * @return this instance, for easy chaining
     270     */
     271    public JOSMTestRules fakeImagery(TileSourceRule tileSourceRule) {
     272        this.preferences();
     273        this.tileSourceRule = tileSourceRule;
     274        return this;
     275    }
     276
     277    /**
    245278     * Use the {@link Main#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel},
    246279     *         {@link Main#menu}, {@link Main#toolbar} global variables in this test.
    247280     * @return this instance, for easy chaining
    public class JOSMTestRules implements TestRule {  
    256289    @Override
    257290    public Statement apply(Statement base, Description description) {
    258291        Statement statement = base;
     292        // counter-intuitively, Statements which need to have their setup routines performed *after* another one need to
     293        // be added into the chain *before* that one, so that it ends up on the "inside".
    259294        if (timeout > 0) {
    260295            // TODO: new DisableOnDebug(timeout)
    261296            statement = new FailOnTimeoutStatement(statement, timeout);
    262297        }
     298
     299        // this half of TileSourceRule's initialization must happen after josm is set up
     300        if (this.tileSourceRule != null) {
     301            statement = this.tileSourceRule.applyRegisterLayers(statement, description);
     302        }
     303
    263304        statement = new CreateJosmEnvironment(statement);
    264305        if (josmHome != null) {
    265306            statement = josmHome.apply(statement, description);
    266307        }
     308
     309        // run mock tile server as the outermost Statement (started first) so it can hopefully be initializing in
     310        // parallel with other setup
     311        if (this.tileSourceRule != null) {
     312            statement = this.tileSourceRule.applyRunServer(statement, description);
     313        }
    267314        return statement;
    268315    }
    269316
    public class JOSMTestRules implements TestRule {  
    409456    }
    410457
    411458    /**
     459     * @return TileSourceRule which is automatically started by this rule
     460     */
     461    public TileSourceRule getTileSourceRule() {
     462        return this.tileSourceRule;
     463    }
     464
     465    /**
    412466     * Clean up after running a test
    413467     */
    414468    @SuppressFBWarnings("DM_GC")