|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, the original author(s). |
| 3 | + * |
| 4 | + * This software is distributable under the BSD license. See the terms of the |
| 5 | + * BSD license in the documentation provided with this software. |
| 6 | + * |
| 7 | + * https://opensource.org/licenses/BSD-3-Clause |
| 8 | + */ |
| 9 | +package org.jline.widget; |
| 10 | + |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +import org.jline.reader.LineReader; |
| 16 | +import org.jline.reader.impl.LineReaderImpl; |
| 17 | +import org.jline.terminal.Terminal; |
| 18 | +import org.jline.terminal.impl.DumbTerminal; |
| 19 | +import org.jline.utils.InfoCmp.Capability; |
| 20 | +import org.jline.utils.Status; |
| 21 | +import org.jline.widget.TailTipWidgets.TipType; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.*; |
| 25 | + |
| 26 | +public final class TailTipWidgetsTest { |
| 27 | + |
| 28 | + /** A simple extension of {@link DumbTerminal} doing the minimal amount of work so that a {@link Status} constructed |
| 29 | + * from it is "supported". */ |
| 30 | + private static final class SupportedDumbTerminal extends DumbTerminal { |
| 31 | + private SupportedDumbTerminal() throws IOException { |
| 32 | + super(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()); |
| 33 | + strings.put(Capability.change_scroll_region, ""); |
| 34 | + strings.put(Capability.save_cursor, ""); |
| 35 | + strings.put(Capability.restore_cursor, ""); |
| 36 | + strings.put(Capability.cursor_address, ""); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** Subclass of {@link Status} exposing the {@code supported} field. For testing only. */ |
| 41 | + private static final class TestStatus extends Status { |
| 42 | + private TestStatus(Terminal terminal) { |
| 43 | + super(terminal); |
| 44 | + } |
| 45 | + |
| 46 | + private boolean isSupported() { |
| 47 | + return supported; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** A dummy {@link LineReader} that's immediately resized to 0x0. */ |
| 52 | + private static final class ZeroSizeLineReader extends LineReaderImpl { |
| 53 | + private ZeroSizeLineReader(Terminal terminal) throws IOException { |
| 54 | + super(terminal); |
| 55 | + display.resize(0, 0); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void enableTest() throws Exception { |
| 61 | + Terminal terminal = new SupportedDumbTerminal(); |
| 62 | + assertTrue(new TestStatus(terminal).isSupported()); |
| 63 | + LineReader reader = new ZeroSizeLineReader(terminal); |
| 64 | + new TailTipWidgets(reader, __ -> null, 1, TipType.COMBINED).enable(); |
| 65 | + } |
| 66 | +} |