runelite-client: use guice for dependency injection

This commit is contained in:
Adam
2017-11-13 18:05:16 -05:00
parent f0ed5ee34f
commit fda56fb235
101 changed files with 1422 additions and 868 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
* 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;
import com.google.inject.Guice;
import org.junit.Test;
public class RuneliteModuleTest
{
@Test
public void testConfigure()
{
Guice.createInjector(new RuneliteModule());
}
}

View File

@@ -25,16 +25,42 @@
package net.runelite.client.config;
import com.google.common.eventbus.EventBus;
import com.google.inject.Guice;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import java.io.IOException;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import javax.inject.Inject;
import net.runelite.client.account.AccountSession;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class ConfigManagerTest
{
@Mock
@Bind
EventBus eventBus;
@Mock
@Bind
ScheduledExecutorService executor;
@Inject
ConfigManager manager;
@Before
public void before()
{
Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this);
}
@Test
public void testGetConfig() throws IOException
{
@@ -43,7 +69,6 @@ public class ConfigManagerTest
accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
ConfigManager manager = new ConfigManager(mock(EventBus.class));
manager.setConfiguration("test", "key", "moo");
TestConfig conf = manager.getConfig(TestConfig.class);
@@ -58,8 +83,6 @@ public class ConfigManagerTest
accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
ConfigManager manager = new ConfigManager(mock(EventBus.class));
TestConfig conf = manager.getConfig(TestConfig.class);
Assert.assertEquals("default", conf.key());
}
@@ -72,8 +95,6 @@ public class ConfigManagerTest
accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
ConfigManager manager = new ConfigManager(mock(EventBus.class));
TestConfig conf = manager.getConfig(TestConfig.class);
conf.key("new value");
@@ -88,8 +109,6 @@ public class ConfigManagerTest
accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
ConfigManager manager = new ConfigManager(mock(EventBus.class));
TestConfig conf = manager.getConfig(TestConfig.class);
ConfigDescriptor descriptor = manager.getConfigDescriptor(conf);
Assert.assertEquals(1, descriptor.getItems().size());

View File

@@ -0,0 +1,105 @@
/*
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
* 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;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.grapher.graphviz.GraphvizGrapher;
import com.google.inject.grapher.graphviz.GraphvizModule;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import joptsimple.OptionSet;
import net.runelite.client.RuneLite;
import net.runelite.client.RuneliteModule;
import net.runelite.client.ui.ClientUI;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PluginManagerTest
{
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Mock
ClientUI clientUi;
@Before
public void before()
{
RuneLite.setOptions(mock(OptionSet.class));
Injector injector = Guice.createInjector(new RuneliteModule(),
BoundFieldModule.of(this));
RuneLite.setInjector(injector);
// test with no client bound
RuneLite runelite = injector.getInstance(RuneLite.class);
runelite.setGui(clientUi);
}
@Test
public void testLoadPlugins() throws Exception
{
PluginManager pluginManager = new PluginManager();
pluginManager.loadPlugins();
}
@Test
public void dumpGraph() throws Exception
{
List<Module> modules = new ArrayList<>();
modules.add(new GraphvizModule());
modules.add(new RuneliteModule());
PluginManager pluginManager = new PluginManager();
pluginManager.loadPlugins();
for (Plugin p : pluginManager.getAllPlugins())
{
modules.add(p);
}
File file = folder.newFile();
try (PrintWriter out = new PrintWriter(file, "UTF-8"))
{
Injector injector = Guice.createInjector(modules);
GraphvizGrapher grapher = injector.getInstance(GraphvizGrapher.class);
grapher.setOut(out);
grapher.setRankdir("TB");
grapher.graph(injector);
}
}
}

View File

@@ -24,16 +24,14 @@
*/
package net.runelite.client.plugins.hiscore;
import net.runelite.client.RuneLite;
import org.junit.Test;
import static org.mockito.Mockito.mock;
public class HiscorePanelTest
{
@Test
public void testConstructor()
{
new HiscorePanel(mock(RuneLite.class));
new HiscorePanel();
}
}

View File

@@ -24,15 +24,19 @@
*/
package net.runelite.client.plugins.slayer;
import com.google.inject.Guice;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import javax.inject.Inject;
import static net.runelite.api.ChatMessageType.SERVER;
import net.runelite.client.RuneLite;
import net.runelite.api.Client;
import net.runelite.client.events.ChatMessage;
import net.runelite.client.game.ItemManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import org.mockito.Matchers;
import org.mockito.Mock;
import static org.mockito.Mockito.when;
import org.mockito.runners.MockitoJUnitRunner;
@@ -47,22 +51,34 @@ public class SlayerTest
private static final String TASK_COMPLETE = "You need something new to hunt.";
private static final String TASK_CANCELED = "Your task has been cancelled.";
@Mock(answer = RETURNS_DEEP_STUBS)
private RuneLite runeLite;
@Mock
@Bind
Client client;
@Mock
private SlayerConfig slayerConfig;
@Bind
SlayerConfig slayerConfig;
private Slayer slayerPlugin;
@Mock
@Bind
SlayerOverlay overlay;
@Mock
@Bind
InfoBoxManager infoBoxManager;
@Mock
@Bind
ItemManager itemManager;
@Inject
Slayer slayerPlugin;
@Before
public void before()
{
RuneLite.setRunelite(runeLite);
when(runeLite.getConfigManager().getConfig(Matchers.any(Class.class))).thenReturn(slayerConfig);
Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this);
when(slayerConfig.enabled()).thenReturn(true);
slayerPlugin = new Slayer();
}
@Test