diff --git a/acceptance/src/test/java/net/silthus/schat/cucumber/ChannelSteps.java b/acceptance/src/test/java/net/silthus/schat/cucumber/ChannelSteps.java index 2f432b68d..d911eaa6d 100644 --- a/acceptance/src/test/java/net/silthus/schat/cucumber/ChannelSteps.java +++ b/acceptance/src/test/java/net/silthus/schat/cucumber/ChannelSteps.java @@ -34,7 +34,7 @@ import static net.silthus.schat.channel.ChannelHelper.ConfiguredSetting.set; import static net.silthus.schat.channel.ChannelSettings.GLOBAL; import static net.silthus.schat.channel.ChannelSettings.PROTECTED; -import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG; +import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG; public class ChannelSteps { @@ -71,6 +71,6 @@ public void configureChannel(ChannelHelper.ConfiguredSetting setting, Channel @Given("the channel {channel} has a custom format") public void theChannelGlobalHasACustomFormat(Channel channel) { - channel.get(FORMAT_CONFIG).messageFormat(new MiniMessageFormat(": ")); + channel.get(TAB_FORMAT_CONFIG).messageFormat(new MiniMessageFormat(": ")); } } diff --git a/core/src/main/java/net/silthus/schat/eventbus/EmptyEventBus.java b/core/src/main/java/net/silthus/schat/eventbus/EmptyEventBus.java index ea03c64c7..40c354a46 100644 --- a/core/src/main/java/net/silthus/schat/eventbus/EmptyEventBus.java +++ b/core/src/main/java/net/silthus/schat/eventbus/EmptyEventBus.java @@ -47,6 +47,11 @@ public E post(@NonNull E event) { return new HashSet<>(); } + @Override + public void unregister(Object listener) { + + } + @Override public @NonNull @Unmodifiable Set> subscriptions(@NonNull Class eventClass) { return Set.of(); diff --git a/core/src/main/java/net/silthus/schat/eventbus/EventBus.java b/core/src/main/java/net/silthus/schat/eventbus/EventBus.java index eb0488e7b..837cc393e 100644 --- a/core/src/main/java/net/silthus/schat/eventbus/EventBus.java +++ b/core/src/main/java/net/silthus/schat/eventbus/EventBus.java @@ -108,6 +108,16 @@ static EventBus eventBus(boolean debug) { */ @NonNull @Unmodifiable Set> register(Object listener); + /** + * Unregisters the given listener from this event bus. + * + *

All event handlers that are annotated with {@link Subscribe} in the listener are unregistered.

+ * + * @param listener the listener to unregister + * @since next + */ + void unregister(Object listener); + /** * Gets a set of all registered handlers for a given event. * diff --git a/core/src/main/java/net/silthus/schat/eventbus/EventBusImpl.java b/core/src/main/java/net/silthus/schat/eventbus/EventBusImpl.java index fd38e9dc6..595fe3c18 100644 --- a/core/src/main/java/net/silthus/schat/eventbus/EventBusImpl.java +++ b/core/src/main/java/net/silthus/schat/eventbus/EventBusImpl.java @@ -36,6 +36,7 @@ import net.kyori.event.SimpleEventBus; import net.silthus.schat.events.SChatEvent; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Unmodifiable; @Accessors(fluent = true) @@ -68,6 +69,11 @@ public E post(final @NonNull E event) { .collect(Collectors.toUnmodifiableSet()); } + @Override + public void unregister(@NonNull Object listener) { + bus.unregister(sub -> ((EventSubscriptionImpl) sub).owner() == listener); + } + @NotNull @SuppressWarnings("unchecked") private EventSubscription registerSubscription(Object listener, Method method) { @@ -78,20 +84,29 @@ private EventSubscription registerSubscription(Object list } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } - }); + }, listener); } private EventSubscription registerSubscription(final Class eventClass, final Consumer handler) { - final EventSubscriptionImpl eventHandler = createSubscription(eventClass, handler); + final EventSubscriptionImpl eventHandler = createSubscription(eventClass, handler, null); + this.bus.register(eventClass, eventHandler); + + return eventHandler; + } + + private EventSubscription registerSubscription(final Class eventClass, + final Consumer handler, + final @Nullable Object owner) { + final EventSubscriptionImpl eventHandler = createSubscription(eventClass, handler, owner); this.bus.register(eventClass, eventHandler); return eventHandler; } @NotNull - protected EventSubscriptionImpl createSubscription(Class eventClass, Consumer handler) { - return new EventSubscriptionImpl<>(this, eventClass, handler); + protected EventSubscriptionImpl createSubscription(Class eventClass, Consumer handler, @Nullable Object owner) { + return new EventSubscriptionImpl<>(this, eventClass, handler, owner); } @Override @@ -150,8 +165,8 @@ public E post(@NonNull E event) { } @Override - protected @NotNull EventSubscriptionImpl createSubscription(Class eventClass, Consumer handler) { - return new EventSubscriptionImpl.Logging<>(this, eventClass, handler); + protected @NotNull EventSubscriptionImpl createSubscription(Class eventClass, Consumer handler, Object owner) { + return new EventSubscriptionImpl.Logging<>(this, eventClass, handler, owner); } } } diff --git a/core/src/main/java/net/silthus/schat/eventbus/EventSubscriptionImpl.java b/core/src/main/java/net/silthus/schat/eventbus/EventSubscriptionImpl.java index 831072868..b30f4f949 100644 --- a/core/src/main/java/net/silthus/schat/eventbus/EventSubscriptionImpl.java +++ b/core/src/main/java/net/silthus/schat/eventbus/EventSubscriptionImpl.java @@ -31,6 +31,7 @@ import lombok.extern.java.Log; import net.kyori.event.EventSubscriber; import net.silthus.schat.events.SChatEvent; +import org.jetbrains.annotations.Nullable; @Getter @Log(topic = "sChat:EventBus") @@ -40,14 +41,17 @@ class EventSubscriptionImpl implements EventSubscription eventClass; private final Consumer handler; + private final @Nullable Object owner; private final AtomicBoolean active = new AtomicBoolean(true); EventSubscriptionImpl(final EventBusImpl eventBus, final Class eventClass, - final Consumer consumer) { + final Consumer consumer, + final @Nullable Object owner) { this.eventBus = eventBus; this.eventClass = eventClass; this.handler = consumer; + this.owner = owner; } @Override @@ -81,8 +85,8 @@ public void invoke(final @NonNull E event) { @Log(topic = "sChat:EventBus") static final class Logging extends EventSubscriptionImpl { - Logging(EventBusImpl eventBus, Class eventClass, Consumer consumer) { - super(eventBus, eventClass, consumer); + Logging(EventBusImpl eventBus, Class eventClass, Consumer consumer, @Nullable Object owner) { + super(eventBus, eventClass, consumer, owner); } @Override diff --git a/core/src/testFixtures/java/net/silthus/schat/chatter/ChatterMock.java b/core/src/testFixtures/java/net/silthus/schat/chatter/ChatterMock.java index d53da0563..316d6822e 100644 --- a/core/src/testFixtures/java/net/silthus/schat/chatter/ChatterMock.java +++ b/core/src/testFixtures/java/net/silthus/schat/chatter/ChatterMock.java @@ -85,6 +85,10 @@ public void assertReceivedRawMessage(Component component) { assertThat(receivesMessages).contains(component); } + public void assertReceivedNoRawMessage() { + assertThat(receivesMessages).isEmpty(); + } + public void assertLastRawMessage(Component render) { assertThat(receivesMessages).last() .isEqualTo(render); diff --git a/platform/src/test/java/net/silthus/schat/platform/config/ConfigTests.java b/platform/src/test/java/net/silthus/schat/platform/config/ConfigTests.java index 7aeb9cee1..67ad39231 100644 --- a/platform/src/test/java/net/silthus/schat/platform/config/ConfigTests.java +++ b/platform/src/test/java/net/silthus/schat/platform/config/ConfigTests.java @@ -52,7 +52,7 @@ import static net.silthus.schat.platform.config.ConfigKeys.CHANNELS; import static net.silthus.schat.platform.config.ConfigKeys.VIEW_CONFIG; import static net.silthus.schat.platform.config.TestConfigurationAdapter.testConfigAdapter; -import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG; +import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG; import static org.assertj.core.api.Assertions.assertThat; class ConfigTests { @@ -118,7 +118,7 @@ void channel_name_is_loaded_with_color() { @Test void loads_message_format() { final Component format = getTestChannelConfig() - .get(FORMAT_CONFIG) + .get(TAB_FORMAT_CONFIG) .messageFormat() .format(View.empty(), message("Hey").source(of(identity("Notch"))).create()); assertThat(format).isEqualTo(text("Notch", AQUA).append(text(": Hey", GREEN))); @@ -126,7 +126,7 @@ void loads_message_format() { @Test void loads_active_color() { - final TextColor activeColor = getTestChannelConfig().get(FORMAT_CONFIG).activeColor(); + final TextColor activeColor = getTestChannelConfig().get(TAB_FORMAT_CONFIG).activeColor(); assertThat(activeColor).isEqualTo(RED); } } diff --git a/ui/src/main/java/net/silthus/schat/ui/ViewModule.java b/ui/src/main/java/net/silthus/schat/ui/ViewModule.java index 951642fc1..e26387ee6 100644 --- a/ui/src/main/java/net/silthus/schat/ui/ViewModule.java +++ b/ui/src/main/java/net/silthus/schat/ui/ViewModule.java @@ -48,8 +48,8 @@ import net.silthus.schat.ui.views.tabbed.TabbedChannelsView; import net.silthus.schat.util.gson.GsonProvider; -import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG; import static net.silthus.schat.ui.view.ViewProvider.cachingViewProvider; +import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG; @Getter @Accessors(fluent = true) @@ -89,7 +89,7 @@ private void onChatterJoin(ChatterJoinedServerEvent event) { } private void configurePrivateChats() { - PrivateChannel.configure(builder -> builder.set(FORMAT_CONFIG, config().privateChatFormat())); + PrivateChannel.configure(builder -> builder.set(TAB_FORMAT_CONFIG, config().privateChatFormat())); } private static final class MiniMessageFormatSerializer implements JsonSerializer, JsonDeserializer { diff --git a/ui/src/main/java/net/silthus/schat/ui/format/ChannelFormatConfig.java b/ui/src/main/java/net/silthus/schat/ui/format/ChannelFormatConfig.java new file mode 100644 index 000000000..3639739c5 --- /dev/null +++ b/ui/src/main/java/net/silthus/schat/ui/format/ChannelFormatConfig.java @@ -0,0 +1,63 @@ +/* + * This file is part of sChat, licensed under the MIT License. + * Copyright (C) Silthus + * Copyright (C) sChat team and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.silthus.schat.ui.format; + +import lombok.Data; +import lombok.experimental.Accessors; +import net.kyori.adventure.text.Component; +import net.silthus.schat.message.Message; +import net.silthus.schat.message.MessageSource; +import org.spongepowered.configurate.objectmapping.ConfigSerializable; +import org.spongepowered.configurate.objectmapping.meta.Setting; + +import static net.kyori.adventure.text.Component.text; +import static net.kyori.adventure.text.Component.translatable; +import static net.kyori.adventure.text.format.NamedTextColor.DARK_AQUA; +import static net.kyori.adventure.text.format.NamedTextColor.GRAY; +import static net.kyori.adventure.text.format.NamedTextColor.YELLOW; + +@Data +@Accessors(fluent = true) +@ConfigSerializable +public class ChannelFormatConfig { + + /** + * Controls the formatting of a channel tab. + */ + public static final net.silthus.schat.pointer.Setting FORMAT_CONFIG = net.silthus.schat.pointer.Setting.dynamicSetting(ChannelFormatConfig.class, "format", ChannelFormatConfig::new); + + @Setting("message_format") + private Format messageFormat = (view, msg) -> + msg.get(Message.SOURCE) + .filter(MessageSource.IS_NOT_NIL) + .map(identity -> identity.displayName().colorIfAbsent(YELLOW).append(text(": ", GRAY))) + .orElse(Component.empty()) + .append(((Message) msg).text().colorIfAbsent(GRAY)); + + @Setting("self_message_format") + private Format selfMessageFormat = (view, msg) -> + translatable("schat.chat.message.you").color(DARK_AQUA) + .append(text(": ", GRAY)) + .append(((Message) msg).text().colorIfAbsent(GRAY)); +} diff --git a/ui/src/main/java/net/silthus/schat/ui/placeholder/Replacements.java b/ui/src/main/java/net/silthus/schat/ui/placeholder/Replacements.java index 7801afed9..786403876 100644 --- a/ui/src/main/java/net/silthus/schat/ui/placeholder/Replacements.java +++ b/ui/src/main/java/net/silthus/schat/ui/placeholder/Replacements.java @@ -30,7 +30,7 @@ import net.silthus.schat.message.Message; import net.silthus.schat.ui.format.Format; import net.silthus.schat.ui.format.MiniMessageFormat; -import net.silthus.schat.ui.view.ViewConfig; +import net.silthus.schat.ui.views.tabbed.TabFormatConfig; public class Replacements { @@ -38,7 +38,7 @@ public class Replacements { @Subscribe public void onChannelMessage(SendChannelMessageEvent event) { - final Format format = event.channel().get(ViewConfig.FORMAT_CONFIG).messageFormat(); + final Format format = event.channel().get(TabFormatConfig.TAB_FORMAT_CONFIG).messageFormat(); if (format instanceof MiniMessageFormat miniMessageFormat) event.message().set(ReplacementProvider.REPLACED_MESSAGE_FORMAT, applyTo(event.message(), miniMessageFormat.format())); } diff --git a/ui/src/main/java/net/silthus/schat/ui/view/View.java b/ui/src/main/java/net/silthus/schat/ui/view/View.java index ba4643f79..fbd57b877 100644 --- a/ui/src/main/java/net/silthus/schat/ui/view/View.java +++ b/ui/src/main/java/net/silthus/schat/ui/view/View.java @@ -25,14 +25,9 @@ import net.kyori.adventure.text.Component; import net.silthus.schat.chatter.Chatter; -import net.silthus.schat.pointer.Setting; - -import static net.silthus.schat.pointer.Setting.setting; public interface View { - Setting VIEW_HEIGHT = setting(Integer.class, "height", 100); // minecraft chat box height in lines - static View empty() { return EmptyView.EMPTY; } @@ -42,6 +37,8 @@ static View empty() { Component render(); default void update() { - chatter().sendRawMessage(render()); + final Component render = render(); + if (!render.equals(Component.empty())) + chatter().sendRawMessage(render); } } diff --git a/ui/src/main/java/net/silthus/schat/ui/view/ViewConfig.java b/ui/src/main/java/net/silthus/schat/ui/view/ViewConfig.java index f1f5a82c5..f8eae4221 100644 --- a/ui/src/main/java/net/silthus/schat/ui/view/ViewConfig.java +++ b/ui/src/main/java/net/silthus/schat/ui/view/ViewConfig.java @@ -26,7 +26,6 @@ import lombok.Data; import lombok.experimental.Accessors; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.JoinConfiguration; import net.silthus.schat.message.Message; import net.silthus.schat.message.MessageSource; import net.silthus.schat.ui.format.Format; @@ -43,11 +42,6 @@ @ConfigSerializable public class ViewConfig { - /** - * Controls the formatting of a channel tab. - */ - public static final net.silthus.schat.pointer.Setting FORMAT_CONFIG = net.silthus.schat.pointer.Setting.dynamicSetting(TabFormatConfig.class, "format", TabFormatConfig::new); - @Setting("height") private int height = 100; @Setting("system_message_format") @@ -59,9 +53,4 @@ public class ViewConfig { .append(((Message) msg).text()); @Setting("private_chat_format") private TabFormatConfig privateChatFormat = new TabFormatConfig(); - private transient JoinConfiguration channelJoinConfig = JoinConfiguration.builder() - .prefix(text("| ")) - .separator(text(" | ")) - .suffix(text(" |")) - .build(); } diff --git a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java index 4a6a60bb4..a6c1a91c6 100644 --- a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java +++ b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java @@ -55,7 +55,7 @@ import static net.silthus.schat.channel.ChannelSettings.FORCED; import static net.silthus.schat.ui.util.ViewHelper.formatMessage; import static net.silthus.schat.ui.util.ViewHelper.subscriptOf; -import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG; +import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG; @SuppressWarnings("CheckStyle") @Getter @@ -93,7 +93,7 @@ protected ChannelTab(@NonNull TabbedChannelsView view, } private TabFormatConfig config() { - return channel().get(FORMAT_CONFIG); + return channel().get(TAB_FORMAT_CONFIG); } @Override diff --git a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabFormatConfig.java b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabFormatConfig.java index 307b84643..a531bd7fc 100644 --- a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabFormatConfig.java +++ b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabFormatConfig.java @@ -24,28 +24,27 @@ package net.silthus.schat.ui.views.tabbed; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; -import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; -import net.silthus.schat.message.Message; -import net.silthus.schat.message.MessageSource; +import net.silthus.schat.ui.format.ChannelFormatConfig; import net.silthus.schat.ui.format.Format; import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.meta.Setting; -import static net.kyori.adventure.text.Component.text; -import static net.kyori.adventure.text.Component.translatable; -import static net.kyori.adventure.text.format.NamedTextColor.DARK_AQUA; -import static net.kyori.adventure.text.format.NamedTextColor.GRAY; import static net.kyori.adventure.text.format.NamedTextColor.RED; -import static net.kyori.adventure.text.format.NamedTextColor.YELLOW; @Data @Accessors(fluent = true) @ConfigSerializable -public class TabFormatConfig { +@EqualsAndHashCode(callSuper = true) +public class TabFormatConfig extends ChannelFormatConfig { + /** + * Controls the formatting of a channel tab. + */ + public static final net.silthus.schat.pointer.Setting TAB_FORMAT_CONFIG = net.silthus.schat.pointer.Setting.dynamicSetting(TabFormatConfig.class, "format", TabFormatConfig::new); @Setting("active_color") private TextColor activeColor = NamedTextColor.GREEN; @@ -71,17 +70,15 @@ public class TabFormatConfig { @Setting("unread_count_decoration") private TextDecoration unreadCountDecoration = null; - @Setting("message_format") - private Format messageFormat = (view, msg) -> - msg.get(Message.SOURCE) - .filter(MessageSource.IS_NOT_NIL) - .map(identity -> identity.displayName().colorIfAbsent(YELLOW).append(text(": ", GRAY))) - .orElse(Component.empty()) - .append(((Message) msg).text().colorIfAbsent(GRAY)); + @Override + public TabFormatConfig messageFormat(Format messageFormat) { + super.messageFormat(messageFormat); + return this; + } - @Setting("self_message_format") - private Format selfMessageFormat = (view, msg) -> - translatable("schat.chat.message.you").color(DARK_AQUA) - .append(text(": ", GRAY)) - .append(((Message) msg).text().colorIfAbsent(GRAY)); + @Override + public TabFormatConfig selfMessageFormat(Format selfMessageFormat) { + super.selfMessageFormat(selfMessageFormat); + return this; + } } diff --git a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsView.java b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsView.java index 82c3166cc..a19d47d61 100644 --- a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsView.java +++ b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsView.java @@ -44,7 +44,6 @@ import net.silthus.schat.events.chatter.ChatterChangedActiveChannelEvent; import net.silthus.schat.events.chatter.ChatterReceivedMessageEvent; import net.silthus.schat.message.Message; -import net.silthus.schat.pointer.Setting; import net.silthus.schat.ui.util.ViewHelper; import net.silthus.schat.ui.view.View; import net.silthus.schat.ui.view.ViewConfig; @@ -56,17 +55,16 @@ import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.JoinConfiguration.newlines; import static net.silthus.schat.channel.ChannelSettings.PRIVATE; -import static net.silthus.schat.pointer.Setting.setting; @Getter @Accessors(fluent = true) public class TabbedChannelsView implements View { - public static final Setting CHANNEL_JOIN_CONFIG = setting(JoinConfiguration.class, "channel_join_config", JoinConfiguration.builder() + public static final JoinConfiguration CHANNEL_JOIN_CONFIG = JoinConfiguration.builder() .prefix(text("| ")) .separator(text(" | ")) .suffix(text(" |")) - .build()); + .build(); private final Chatter chatter; private final ViewConfig config; @@ -161,7 +159,7 @@ private Component joinTabs(List tabs) { if (tabs.isEmpty()) return Component.empty(); else - return join(config().channelJoinConfig(), tabs); + return join(CHANNEL_JOIN_CONFIG, tabs); } private void addTab(Channel channel) { diff --git a/ui/src/test/java/net/silthus/schat/ui/ViewModuleTest.java b/ui/src/test/java/net/silthus/schat/ui/ViewModuleTest.java index f889757e4..8b9e4d161 100644 --- a/ui/src/test/java/net/silthus/schat/ui/ViewModuleTest.java +++ b/ui/src/test/java/net/silthus/schat/ui/ViewModuleTest.java @@ -38,6 +38,7 @@ import static net.silthus.schat.eventbus.EventBusMock.eventBusMock; import static net.silthus.schat.message.Message.message; import static net.silthus.schat.ui.placeholder.ReplacementProvider.REPLACED_MESSAGE_FORMAT; +import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG; import static net.silthus.schat.util.gson.GsonProvider.gsonProvider; import static org.assertj.core.api.Assertions.assertThat; @@ -65,7 +66,7 @@ private Message sendMessage(Channel channel) { @Test void on_sendChannelMessage_event_message_is_rendered_and_set() { - final Message message = sendMessage(channelWith(ViewConfig.FORMAT_CONFIG, new TabFormatConfig().messageFormat(new MiniMessageFormat("%test%: ")))); + final Message message = sendMessage(channelWith(TAB_FORMAT_CONFIG, new TabFormatConfig().messageFormat(new MiniMessageFormat("%test%: ")))); assertThat(message.get(REPLACED_MESSAGE_FORMAT)).isEqualTo("success: "); } diff --git a/ui/src/test/java/net/silthus/schat/ui/view/ViewTest.java b/ui/src/test/java/net/silthus/schat/ui/view/ViewTest.java new file mode 100644 index 000000000..3aec06442 --- /dev/null +++ b/ui/src/test/java/net/silthus/schat/ui/view/ViewTest.java @@ -0,0 +1,29 @@ +package net.silthus.schat.ui.view; + +import net.kyori.adventure.text.Component; +import net.silthus.schat.chatter.ChatterMock; +import org.junit.jupiter.api.Test; + +class ViewTest { + + @Test + void empty_view_update_does_not_send_empty_raw_message() { + final EmptyView view = new EmptyView(); + view.update(); + view.chatter().assertReceivedNoRawMessage(); + } + + private static final class EmptyView implements View { + private final ChatterMock chatter = ChatterMock.randomChatter(); + + @Override + public ChatterMock chatter() { + return chatter; + } + + @Override + public Component render() { + return Component.empty(); + } + } +} diff --git a/ui/src/test/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsViewTests.java b/ui/src/test/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsViewTests.java index dd8584143..658d01300 100644 --- a/ui/src/test/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsViewTests.java +++ b/ui/src/test/java/net/silthus/schat/ui/views/tabbed/TabbedChannelsViewTests.java @@ -24,7 +24,6 @@ package net.silthus.schat.ui.views.tabbed; import lombok.SneakyThrows; -import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.flattener.ComponentFlattener; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; @@ -63,8 +62,8 @@ import static net.silthus.schat.message.MessageHelper.randomMessage; import static net.silthus.schat.message.MessageSource.of; import static net.silthus.schat.ui.placeholder.ReplacementProvider.REPLACED_MESSAGE_FORMAT; -import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG; import static net.silthus.schat.ui.views.Views.tabbedChannels; +import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -260,7 +259,7 @@ void underlines_channel() { class and_different_format_is_used { @BeforeEach void setUp() { - channel.set(FORMAT_CONFIG, new TabFormatConfig().activeColor(RED)); + channel.set(TAB_FORMAT_CONFIG, new TabFormatConfig().activeColor(RED)); } @Test @@ -441,13 +440,13 @@ void given_no_unread_messages_channel_one_has_no_unread_counter() { @Test void given_highlight_unread_is_false_then_unread_indicator_is_hidden() { - channelTwo.set(FORMAT_CONFIG, new TabFormatConfig().highlightUnread(false)); + channelTwo.set(TAB_FORMAT_CONFIG, new TabFormatConfig().highlightUnread(false)); assertColorOnlyViewContains("two"); } @Test void given_show_unread_counter_is_false_then_counter_is_hidden() { - channelTwo.set(FORMAT_CONFIG, new TabFormatConfig().showUnreadCount(false)); + channelTwo.set(TAB_FORMAT_CONFIG, new TabFormatConfig().showUnreadCount(false)); assertViewDoesNotContain(""); } } @@ -479,20 +478,6 @@ void renders_private_channel_last() { } } } - - @Nested - class with_custom_channel_join_config_format { - - @BeforeEach - void setUp() { - view.config().channelJoinConfig(JoinConfiguration.builder().separator(text(" - ")).build()); - } - - @Test - void uses_custom_format() { - assertTextRenders("❌one - ❌two"); - } - } } @Nested @@ -504,7 +489,7 @@ void setUp() { "zzz", set(PRIORITY, 10) ); - channel.set(FORMAT_CONFIG, new TabFormatConfig().messageFormat((v, message) -> message.get(Message.SOURCE) + channel.set(TAB_FORMAT_CONFIG, new TabFormatConfig().messageFormat((v, message) -> message.get(Message.SOURCE) .orElse(MessageSource.nil()) .displayName() .append(text(": ").append(message.getOrDefault(Message.TEXT, empty())))