@Test public void testViewCardinalities() { // SwitcherView should appear differently depending on number of views available // Multiple views -> Combo box // One view -> Label // No views -> Neither // Create the test object View switcherView = SwitcherView.VIEW_INFO.createView(mockComponent); // During normal initialization, should have two view infos. // A Combo Box should be found. Assert.assertNotNull(findComponent(switcherView, JComboBox.class)); // With only one view info, should have a label instead Mockito.when(mockComponent.getViewInfos(ViewType.OBJECT)) .thenReturn(Collections.singleton(mockViewInfo1)); switcherView = SwitcherView.VIEW_INFO.createView(mockComponent); Assert.assertNull(findComponent(switcherView, JComboBox.class)); Assert.assertNotNull(findComponent(switcherView, JLabel.class)); // With no views, there should be no label or combo box Mockito.when(mockComponent.getViewInfos(ViewType.OBJECT)) .thenReturn(Collections.<ViewInfo>emptySet()); switcherView = SwitcherView.VIEW_INFO.createView(mockComponent); Assert.assertNull(findComponent(switcherView, JComboBox.class)); Assert.assertNull(findComponent(switcherView, JLabel.class)); }
@Test(dataProvider = "generateSwitcherTests") public void testSwitcher(boolean addGUI, boolean doChange, boolean acceptChange) { // Create the test object View switcherView = SwitcherView.VIEW_INFO.createView(mockComponent); // Act as though the view provider has/hasn't accepted the view switch Mockito.when(mockViewProvider.setHousedViewManifestation(mockViewInfo2)) .thenReturn(acceptChange); // Attach view provider to gui, if this test iteration says we should if (addGUI) { switcherView.addMonitoredGUI(mockViewProvider); } // Find combo box so we can change selection @SuppressWarnings("rawtypes") JComboBox comboBox = findComponent(switcherView, JComboBox.class); Assert.assertNotNull(comboBox); // Change the item - again, only if we're supposed to if (doChange) { comboBox.setSelectedItem(mockViewInfo2); } // Verify that the change was reported to the GUI, // if a GUI was attached and the change was done Mockito.verify(mockViewProvider, Mockito.times(addGUI && doChange ? 1 : 0)) .setHousedViewManifestation(mockViewInfo2); // Verify that combo box state has/hasn't reset, as apparopriate // Specifically, only should if change was done, and an attached GUI didn't reject it boolean shouldChange = doChange && !(addGUI && !acceptChange); Assert.assertEquals(comboBox.getSelectedItem(), shouldChange ? mockViewInfo2 : mockViewInfo1); }
@Test public void testAppearance() { // Exercise custom ComboBoxUI // Mostly visual, so not much to verify // Create the test object View switcherView = SwitcherView.VIEW_INFO.createView(mockComponent); // Find combo box so we can test its UI object @SuppressWarnings("rawtypes") JComboBox comboBox = findComponent(switcherView, JComboBox.class); ComboBoxUI ui = comboBox.getUI(); // Verify that ui paints a rounded rect (per spec) Graphics mockGraphics = Mockito.mock(Graphics.class, Mockito.RETURNS_MOCKS); ui.paint(mockGraphics, comboBox); Mockito.verify(mockGraphics) .fillRoundRect( Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt()); }
@Test public void testBadInput() { // Make sure that view does not break for unexpected input // (verify that null checks are present.) // Create the test object View switcherView = SwitcherView.VIEW_INFO.createView(mockComponent); // Add monitored GUI can take a variety of object types // Make sure an unexpected object type does not trigger an exception switcherView.addMonitoredGUI(null); switcherView.addMonitoredGUI("hello"); Mockito.when(mockViewProvider.getHousedViewManifestation()).thenReturn(null); switcherView.addMonitoredGUI(mockViewProvider); }