-
Notifications
You must be signed in to change notification settings - Fork 0
fix: open the mail picker before skin lookups finish #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9aa8fa5
fix: open the mail picker before skin lookups finish
ryanbarlow97 1d95880
fix: close only the letter GUI that scheduled the close
ryanbarlow97 9848cfb
fix: redraw the picker when every recipient disappears
ryanbarlow97 c401fff
test: check an emptied picker is redrawn
ryanbarlow97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
src/test/java/net/tfminecraft/birdmessenger/PickerOpenTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package net.tfminecraft.birdmessenger; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.configuration.file.YamlConfiguration; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.inventory.Inventory; | ||
| import org.bukkit.inventory.InventoryHolder; | ||
| import org.bukkit.inventory.InventoryView; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.plugin.PluginManager; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.MockedConstruction; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| import net.tfminecraft.birdmessenger.gui.CharacterPickerGui; | ||
| import net.tfminecraft.birdmessenger.session.SendSession; | ||
| import net.tfminecraft.birdmessenger.session.SendSessionManager; | ||
| import net.tfminecraft.rpcharacters.RPCharacters; | ||
| import net.tfminecraft.rpcharacters.api.CharacterSkull; | ||
| import net.tfminecraft.rpcharacters.mail.CharacterMailTarget; | ||
|
|
||
| class PickerOpenTest { | ||
| private final UUID self = UUID.randomUUID(); | ||
| private final UUID other = UUID.randomUUID(); | ||
| private final List<Runnable> refreshCallbacks = new ArrayList<>(); | ||
| private final List<CharacterMailTarget> directory = new ArrayList<>(); | ||
| private BirdMessenger plugin; | ||
| private SendSessionManager sessions; | ||
| private Player player; | ||
| private InventoryHolder openHolder; | ||
| private int opens; | ||
|
|
||
| @BeforeEach void setup() throws Exception { | ||
| refreshCallbacks.clear(); | ||
| directory.clear(); | ||
| opens = 0; | ||
| openHolder = null; | ||
| plugin = mock(BirdMessenger.class, CALLS_REAL_METHODS); | ||
| doReturn(new YamlConfiguration()).when(plugin).getConfig(); | ||
| sessions = new SendSessionManager(plugin); | ||
| setField("config", new BirdConfig(plugin)); | ||
| setField("sessions", sessions); | ||
| player = mock(Player.class); | ||
| when(player.getUniqueId()).thenReturn(self); | ||
| when(player.isOnline()).thenReturn(true); | ||
| InventoryView view = mock(InventoryView.class); | ||
| Inventory top = mock(Inventory.class); | ||
| when(player.getOpenInventory()).thenReturn(view); | ||
| when(view.getTopInventory()).thenReturn(top); | ||
| when(top.getHolder()).thenAnswer(call -> openHolder); | ||
| when(player.openInventory(any(Inventory.class))).thenAnswer(call -> { | ||
| opens++; | ||
| openHolder = ((Inventory) call.getArgument(0)).getHolder(); | ||
| return view; | ||
| }); | ||
| } | ||
|
|
||
| @Test void pickerOpensBeforeSkinRefreshFinishesAndUpdatesAfterwards() { | ||
| directory.add(target(other, "a", "Alice")); | ||
| pendingLetter(); | ||
| try (var mocks = new Mocks()) { | ||
| plugin.openPicker(player); | ||
| assertEquals(1, opens, "picker must not wait for skin lookups"); | ||
| CharacterPickerGui picker = assertInstanceOf(CharacterPickerGui.class, openHolder); | ||
| assertEquals(1, picker.targets().size()); | ||
| assertEquals(1, refreshCallbacks.size()); | ||
|
|
||
| directory.add(target(other, "b", "Bob")); | ||
| refreshCallbacks.getFirst().run(); | ||
| assertEquals(1, opens, "refresh re-renders the open picker instead of reopening it"); | ||
| assertEquals(2, picker.targets().size()); | ||
| } | ||
| } | ||
|
|
||
| @Test void refreshDropsASelectedRecipientWhoIsNoLongerListed() { | ||
| CharacterMailTarget alice = target(other, "a", "Alice"); | ||
| CharacterMailTarget bob = target(other, "b", "Bob"); | ||
| directory.add(alice); | ||
| directory.add(bob); | ||
| pendingLetter(); | ||
| try (var mocks = new Mocks()) { | ||
| plugin.openPicker(player); | ||
| CharacterPickerGui picker = (CharacterPickerGui) openHolder; | ||
| SendSession session = sessions.get(self); | ||
| session.setSelected(picker.targets().get(1)); | ||
| refreshCallbacks.getFirst().run(); | ||
| assertEquals("b", session.getSelected().getCharacterId(), "a listed selection is kept"); | ||
|
|
||
| directory.remove(bob); | ||
| refreshCallbacks.getFirst().run(); | ||
| assertNull(session.getSelected()); | ||
|
|
||
| session.setSelected(picker.targets().get(0)); | ||
| directory.clear(); | ||
| clearInvocations(picker.getInventory()); | ||
| refreshCallbacks.getFirst().run(); | ||
| assertTrue(picker.targets().isEmpty(), "an emptied list is redrawn, not left stale"); | ||
| verify(picker.getInventory()).clear(); | ||
| verify(picker.getInventory()).setItem(eq(CharacterPickerGui.SLOT_CANCEL), any()); | ||
| verify(picker.getInventory()).setItem(eq(CharacterPickerGui.SLOT_CONFIRM), any()); | ||
| assertNull(session.getSelected()); | ||
| } | ||
| } | ||
|
|
||
| @Test void refreshIgnoresPickerThePlayerHasClosed() { | ||
| directory.add(target(other, "a", "Alice")); | ||
| pendingLetter(); | ||
| try (var mocks = new Mocks()) { | ||
| plugin.openPicker(player); | ||
| CharacterPickerGui picker = (CharacterPickerGui) openHolder; | ||
| openHolder = null; | ||
| directory.add(target(other, "b", "Bob")); | ||
| refreshCallbacks.getFirst().run(); | ||
| assertEquals(1, picker.targets().size()); | ||
| } | ||
| } | ||
|
|
||
| @Test void doesNotOpenWithoutPendingLetterOrOverAnOpenPicker() { | ||
| directory.add(target(other, "a", "Alice")); | ||
| try (var mocks = new Mocks()) { | ||
| plugin.openPicker(player); | ||
| assertEquals(0, opens); | ||
|
|
||
| pendingLetter(); | ||
| plugin.openPicker(player); | ||
| plugin.openPicker(player); | ||
| assertEquals(1, opens); | ||
| } | ||
| } | ||
|
|
||
| private void pendingLetter() { | ||
| SendSession session = sessions.getOrCreate(self); | ||
| session.setLetter(mock(ItemStack.class)); | ||
| } | ||
|
|
||
| private CharacterMailTarget target(UUID owner, String id, String name) { | ||
| return new CharacterMailTarget(owner, id, name, name, null, null, null, null); | ||
| } | ||
|
|
||
| private void setField(String name, Object value) throws Exception { | ||
| Field field = BirdMessenger.class.getDeclaredField(name); | ||
| field.setAccessible(true); | ||
| field.set(plugin, value); | ||
| } | ||
|
|
||
| private final class Mocks implements AutoCloseable { | ||
| private final MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class); | ||
| private final MockedStatic<RPCharacters> rpc = mockStatic(RPCharacters.class); | ||
| private final MockedStatic<CharacterSkull> skulls = mockStatic(CharacterSkull.class); | ||
| private final MockedConstruction<ItemStack> items = mockConstruction(ItemStack.class); | ||
|
|
||
| Mocks() { | ||
| PluginManager manager = mock(PluginManager.class); | ||
| when(manager.isPluginEnabled("RPCharacters")).thenReturn(true); | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(manager); | ||
| bukkit.when(() -> Bukkit.createInventory(any(InventoryHolder.class), anyInt(), anyString())) | ||
| .thenAnswer(call -> { | ||
| Inventory inventory = mock(Inventory.class); | ||
| InventoryHolder holder = call.getArgument(0); | ||
| when(inventory.getHolder()).thenReturn(holder); | ||
| return inventory; | ||
| }); | ||
| rpc.when(RPCharacters::listMailTargets).thenAnswer(call -> new ArrayList<>(directory)); | ||
| rpc.when(() -> RPCharacters.refreshMailTargetTexturesAsync(any())) | ||
| .thenAnswer(call -> refreshCallbacks.add(call.getArgument(0))); | ||
| skulls.when(() -> CharacterSkull.fromTextures(any(), any())) | ||
| .thenAnswer(call -> mock(ItemStack.class)); | ||
| } | ||
|
|
||
| @Override public void close() { | ||
| items.close(); | ||
| skulls.close(); | ||
| rpc.close(); | ||
| bukkit.close(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.