timers plugin: add fight cave and inferno timers

Co-authored-by: winterdaze <35933468+winterdaze-rs@users.noreply.github.com>
Co-authored-by: Jordan Atwood <jordan.atwood423@gmail.com>
This commit is contained in:
Adam
2020-08-20 14:10:44 -04:00
parent ad940f51ee
commit be1e1bbdbb
5 changed files with 369 additions and 3 deletions

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2020, Jordan <nightfirecat@protonmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.timers;
import java.time.Instant;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class ElapsedTimerTest
{
@Test
public void testGetText()
{
final Instant now = Instant.now();
final Instant fiveSecondsAgo = now.minusSeconds(5);
final Instant fiveMinutesAgo = now.minusSeconds(5 * 60);
final Instant oneHourAgo = now.minusSeconds(60 * 60);
final Instant fiveHoursAgo = now.minusSeconds(5 * 60 * 60);
assertEquals("00:00", timerText(now, now));
assertEquals("00:00", timerText(now, null));
assertEquals("00:05", timerText(fiveSecondsAgo, now));
assertEquals("00:05", timerText(fiveSecondsAgo, null));
assertEquals("04:55", timerText(fiveMinutesAgo, fiveSecondsAgo));
assertEquals("05:00", timerText(fiveMinutesAgo, now));
assertEquals("05:00", timerText(fiveMinutesAgo, null));
assertEquals("55:00", timerText(oneHourAgo, fiveMinutesAgo));
assertEquals("59:55", timerText(oneHourAgo, fiveSecondsAgo));
assertEquals("01:00", timerText(oneHourAgo, now));
assertEquals("01:00", timerText(oneHourAgo, null));
assertEquals("04:00", timerText(fiveHoursAgo, oneHourAgo));
assertEquals("04:55", timerText(fiveHoursAgo, fiveMinutesAgo));
assertEquals("04:59", timerText(fiveHoursAgo, fiveSecondsAgo));
assertEquals("05:00", timerText(fiveHoursAgo, now));
assertEquals("05:00", timerText(fiveHoursAgo, null));
}
private static String timerText(final Instant startTime, final Instant lastTime)
{
return new ElapsedTimer(null, null, startTime, lastTime).getText();
}
}

View File

@@ -29,6 +29,7 @@ import com.google.inject.Inject;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import java.time.Duration;
import java.time.Instant;
import java.util.EnumSet;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
@@ -42,18 +43,24 @@ import net.runelite.client.game.SpriteManager;
import net.runelite.client.ui.overlay.infobox.InfoBox;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.nullable;
import org.mockito.Mock;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
@RunWith(MockitoJUnitRunner.class)
public class TimersPluginTest
@@ -63,6 +70,7 @@ public class TimersPluginTest
private static final String HALF_TELEBLOCK_MESSAGE = "<col=4f006f>A Tele Block spell has been cast on you by Runelite. It will expire in 2 minutes, 30 seconds.</col>";
private static final String TRANSPARENT_CHATBOX_FULL_TELEBLOCK_MESSAGE = "<col=c356ef>A Tele Block spell has been cast on you by Alexsuperfly. It will expire in 5 minutes.</col>";
private static final String TRANSPARENT_CHATBOX_TELEBLOCK_REMOVED_MESSAGE = "<col=c356ef>Your Tele Block has been removed because you killed Alexsuperfly.</col>";
private static final int FIGHT_CAVES_REGION_ID = 9551;
@Inject
private TimersPlugin timersPlugin;
@@ -233,4 +241,66 @@ public class TimersPluginTest
int mins = (int) infoBox.getDuration().toMinutes();
assertTrue(mins == 1 || mins == 2);
}
@Test
public void testTzhaarTimer()
{
when(timersConfig.showTzhaarTimers()).thenReturn(true);
when(client.getMapRegions()).thenReturn(new int[]{FIGHT_CAVES_REGION_ID});
class InstantRef
{
Instant i;
}
InstantRef startTime = new InstantRef();
when(timersConfig.tzhaarStartTime()).then(a -> startTime.i);
doAnswer((Answer<Void>) invocationOnMock ->
{
Object argument = invocationOnMock.getArguments()[0];
startTime.i = (Instant) argument;
return null;
}).when(timersConfig).tzhaarStartTime(nullable(Instant.class));
InstantRef lastTime = new InstantRef();
when(timersConfig.tzhaarLastTime()).then(a -> lastTime.i);
doAnswer((Answer<Void>) invocationOnMock ->
{
Object argument = invocationOnMock.getArguments()[0];
lastTime.i = (Instant) argument;
return null;
}).when(timersConfig).tzhaarLastTime(nullable(Instant.class));
// test timer creation: verify the infobox was added and that it is an ElapsedTimer
ChatMessage chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", "<col=ef1020>Wave: 1</col>", "", 0);
timersPlugin.onChatMessage(chatMessage);
ArgumentCaptor<InfoBox> captor = ArgumentCaptor.forClass(InfoBox.class);
verify(infoBoxManager, times(1)).addInfoBox(captor.capture());
assertTrue(captor.getValue() instanceof ElapsedTimer);
// test timer pause: verify the added ElapsedTimer has a non-null lastTime
chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", "<col=ef1020>The Inferno has been paused. You may now log out.", "", 0);
timersPlugin.onChatMessage(chatMessage);
verify(infoBoxManager, times(1)).removeInfoBox(captor.capture());
verify(infoBoxManager, times(2)).addInfoBox(captor.capture());
assertTrue(captor.getValue() instanceof ElapsedTimer);
ElapsedTimer timer = (ElapsedTimer) captor.getValue();
assertNotEquals(timer.getLastTime(), null);
Instant oldTime = ((ElapsedTimer) captor.getValue()).getStartTime();
// test timer unpause: verify the last time is null after being unpaused
chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", "<col=ef1020>Wave: 2</col>", "", 0);
timersPlugin.onChatMessage(chatMessage);
verify(infoBoxManager, times(2)).removeInfoBox(captor.capture());
verify(infoBoxManager, times(3)).addInfoBox(captor.capture());
assertTrue(captor.getValue() instanceof ElapsedTimer);
timer = (ElapsedTimer) captor.getValue();
assertNull(timer.getLastTime());
// test timer remove: verify the infobox was removed (and no more were added)
chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", "You have been defeated!", "", 0);
timersPlugin.onChatMessage(chatMessage);
verify(infoBoxManager, times(3)).removeInfoBox(captor.capture());
verify(infoBoxManager, times(3)).addInfoBox(captor.capture());
}
}