Browse Source

Added back function to MenuManager.

feature-menu
Nick Stolbov 3 years ago
parent
commit
87459dab50
  1. 7
      src/main/java/Application/MenuManager.java
  2. 26
      src/test/java/Application/MenuManagerTest.java

7
src/main/java/Application/MenuManager.java

@ -14,6 +14,13 @@ public class MenuManager {
this.currentMenu = currentMenu.getMenu(i); this.currentMenu = currentMenu.getMenu(i);
} }
public void back() throws Exception {
if(!this.inRootMenu())
this.currentMenu = this.currentMenu.getPreviousMenu();
else
throw new Exception("Menu is a root menu, a previous menu doesn't exist");
}
public Menu getCurrentMenu(){ public Menu getCurrentMenu(){
return this.currentMenu; return this.currentMenu;
} }

26
src/test/java/Application/MenuManagerTest.java

@ -1,6 +1,7 @@
package Application; package Application;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -42,4 +43,29 @@ class MenuManagerTest {
mm.select(0); mm.select(0);
assertFalse(mm.inRootMenu()); assertFalse(mm.inRootMenu());
} }
@Test
void testBackFunction() {
try {
mm.back();
Assertions.fail("Test failed: shouldn't be able to go back in rootMenu");
} catch (Exception e) {
//Should throw Exception because currently in rootMenu
}
mm.select(0);
try {
mm.back();
assertEquals(mm.getCurrentMenu(), rootMenu);
} catch (Exception e) {
Assertions.fail("Test failed: " + e.getMessage());
}
mm.select(0);
mm.select(0);
try {
mm.back();
assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0));
} catch (Exception e) {
Assertions.fail("Test failed: " + e.getMessage());
}
}
} }
Loading…
Cancel
Save