+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.util.dialogrunner;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-import org.testng.annotations.Test;
-
-import junit.extensions.PA;
-
public class DialogRunnerTest
{
- public class MockDialog implements DialogRunnerI<MockDialog>
- {
- DialogRunnerI runner = new DialogRunner();
-
- @Override
- public DialogRunnerI addResponse(Object response, RunResponse action)
- {
- return runner.addResponse(response, action);
- }
-
- public List<RunResponse> doDialog(String resp)
- {
- return runner.handleResponse(resp);
- }
-
- @Override
- public List<RunResponse> handleResponse(Object response)
- {
- return null;
- }
- }
-
- MockDialog dialog = new MockDialog();
-
- @Test
- public void testDialogRunner()
- {
- RunResponse ok, cancel, help, ineed;
- final String ooh = "OOOOoooOOOOH!";
- final String r_ok = "OK";
- final String r_cancel = "CANCEL";
- final String r_done = "DONE";
- final String r_help = "HELP";
- final String r_ddoit = "DIDNT DOIT";
- final String r_needsb = "I NEED SOMEBODY";
-
- ok = new RunResponse("OK")
- {
- @Override
- public void run()
- {
- returnValue = "DONE";
- }
- };
- final RunResponse befok = new RunResponse("OK")
- {
- @Override
- public void run()
- {
- returnValue = "OK";
- }
- };
-
- cancel = new RunResponse("CANCEL")
- {
- @Override
- public void run()
- {
- returnValue = r_ddoit;
- }
- };
- help = new RunResponse("HELP")
- {
- @Override
- public void run()
- {
- returnValue = r_needsb;
- }
- };
- ineed = new RunResponse(r_needsb)
- {
- @Override
- public void run()
- {
- returnValue = ooh;
- }
- };
-
- assertFalse(isRegistered(dialog.runner, ok));
-
- dialog.addResponse("OK", ok).addResponse("CANCEL", cancel).
- addResponse("HELP", help).addResponse(r_needsb, ineed);
-
- assertTrue(isRegistered(dialog.runner, ok));
-
-// Assert.assertFalse(dialog.runner.firstRunWasCalled);
- List<RunResponse> actions = dialog.doDialog("OK");
- // OK called, nothing else.
- // Assert.assertTrue(dialog.runner.firstRunWasCalled);
- assertTrue(actions.contains(ok));//ok.wasRun);
- assertEquals(ok.returnValue, r_done);
- assertFalse(actions.contains(cancel));//cancel.wasRun);
- assertEquals(actions/*dialog.runner.responses*/.size(), 2);
-
- // do it again - check it doesn't trigger again
- //ok.wasRun = false;
- actions = dialog.doDialog("OK");
- assertFalse(actions.contains(ok));
-
- // reset - everything false/null
-// dialog.runner.resetResponses();
-// Assert.assertFalse(dialog.runner.firstRunWasCalled);
-// Assert.assertFalse(ok.wasRun);
-// Assert.assertNull(ok.returned);
-// Assert.assertEquals(dialog.runner.responses.size(), 0);
-
- // cancel called ..
- actions = dialog.doDialog("HELP");
-// Assert.assertTrue(dialog.runner.firstRunWasCalled);
- assertFalse(actions.contains(ok));//ok.wasRun);
- assertEquals(ineed.returnValue, ooh);
- assertEquals(actions/*dialog.runner.responses*/.size(), 3);
-
- // TODO: test prepend and chained execution of tasks for a response.
-// Assert.assertFalse(dialog.runner.isRegistered(befok));
- dialog.runner.addResponse("OK", befok); //setFirstResponse(befok);
-
- assertTrue(isRegistered(dialog.runner, befok));
- assertTrue(isRegistered(dialog.runner, ok));
-
-// dialog.runner.resetResponses();
-
- actions = dialog.doDialog("OK");
- assertTrue(actions.contains(befok));//befok.wasRun);
- assertTrue(actions.contains(ok)); //ok.wasRun);
- assertEquals(actions/*dialog.runner.responses*/.size(), 3);
- }
-
- private boolean isRegistered(DialogRunnerI runner, RunResponse action)
- {
- Map<Object, Collection<Object>> actions = (Map<Object, Collection<Object>>) PA.getValue(runner, "callbacks");
- Collection<Object> registered = actions.get(action.getTrigger());
- return registered != null && registered.contains(action);
- }
}