/** helper to set preference to an invalid format and verify. */ void setInvalidPreferenceAndVerify() { fViewerListener.reset(); fViewerListener.addUpdates( TreePath.EMPTY, ((TestElementVMContext) fViewer.getInput()).getElement(), -1, ALL_UPDATES_COMPLETE | PROPERTY_UPDATES); fVMListener.reset(); fVMListener.addUpdates( TreePath.EMPTY, fModel.getRootElement(), -1, ALL_UPDATES_COMPLETE | PROPERTY_UPDATES); fViewer .getPresentationContext() .setProperty(PROP_FORMATTED_VALUE_FORMAT_PREFERENCE, "invalid format"); while (!fViewerListener.isFinished(ALL_UPDATES_COMPLETE | PROPERTY_UPDATES) || !fVMListener.isFinished(CONTENT_UPDATES | PROPERTY_UPDATES)) { if (!fDisplay.readAndDispatch()) { fDisplay.sleep(); } } validateModel( IFormattedValues.HEX_FORMAT, " (" + FormattedValueVMUtil.getFormatLabel(IFormattedValues.HEX_FORMAT) + ")", DummyFormattedValueService.DUMMY_FORMAT, " (" + DummyFormattedValueService.DUMMY_FORMAT + ")"); }
/** * Test that when the preference format is invalid, each element can still change to a format. * Also, each element can restore to the invalid preference format such that the element uses * first available format from service. */ public void testChangeElementFormatWithInvalidPreference() { recreateViewModel(); String preferenceFormat = IFormattedValues.NATURAL_FORMAT; setInput(preferenceFormat); // set preference format to an invalid format and verify setInvalidPreferenceAndVerify(); // set each element to a format, and verify HashMap<String, ElementFormatSetting> map = new HashMap<String, ElementFormatSetting>(); String[] format = { IFormattedValues.HEX_FORMAT, IFormattedValues.DECIMAL_FORMAT, IFormattedValues.OCTAL_FORMAT, IFormattedValues.BINARY_FORMAT, IFormattedValues.NATURAL_FORMAT }; makeElementFormatSetting(fViewer, TreePath.EMPTY, format, -1, 0, map); ArrayList<ElementFormatSetting> elementFormats = new ArrayList<ElementFormatSetting>(map.values()); setFormatAndValidate("invalid format", elementFormats, elementFormats, false, false, false); // Restore each element to preference format which is an invalid format for (ElementFormatSetting e : elementFormats) { e.formatId = null; } fViewerListener.reset(); fViewerListener.addUpdates( TreePath.EMPTY, ((TestElementVMContext) fViewer.getInput()).getElement(), -1, ALL_UPDATES_COMPLETE | PROPERTY_UPDATES); fVMListener.reset(); fVMListener.addUpdates( TreePath.EMPTY, fModel.getRootElement(), -1, ALL_UPDATES_COMPLETE | PROPERTY_UPDATES); if (fVMProvider instanceof IElementFormatProvider) { IElementFormatProvider ep = ((IElementFormatProvider) fVMProvider); for (ElementFormatSetting es : elementFormats) { ep.setActiveFormat( fViewer.getPresentationContext(), es.nodes.toArray(new IVMNode[es.nodes.size()]), fViewer.getInput(), es.elementPaths.toArray(new TreePath[es.elementPaths.size()]), es.formatId); } } while (!fViewerListener.isFinished(ALL_UPDATES_COMPLETE | PROPERTY_UPDATES) || !fVMListener.isFinished(CONTENT_UPDATES | PROPERTY_UPDATES)) { if (!fDisplay.readAndDispatch()) { fDisplay.sleep(); } } // verify service's first available format is used validateModel( IFormattedValues.HEX_FORMAT, " (" + FormattedValueVMUtil.getFormatLabel(IFormattedValues.HEX_FORMAT) + ")", DummyFormattedValueService.DUMMY_FORMAT, " (" + DummyFormattedValueService.DUMMY_FORMAT + ")"); }
/** * Test that when an element is set to to an invalid format and the preference format is invalid, * the element uses first available format from service. */ public void testInvalidElementFormatWithInvalidPreference() { recreateViewModel(); String preferenceFormat = IFormattedValues.NATURAL_FORMAT; setInput(preferenceFormat); // set preference format to an invalid format and verify setInvalidPreferenceAndVerify(); // set each element to an invalid format setElementInvalidFormat(); // verify service's first available format is used when element format and preference format are // invalid validateModel( IFormattedValues.HEX_FORMAT, " (" + FormattedValueVMUtil.getFormatLabel(IFormattedValues.HEX_FORMAT) + ")", DummyFormattedValueService.DUMMY_FORMAT, " (" + DummyFormattedValueService.DUMMY_FORMAT + ")"); }
private void setFormatAndValidate( String formatId, ArrayList<ElementFormatSetting> setElementFormats, ArrayList<ElementFormatSetting> expectElementFormats, boolean expectContentCached, boolean expectFormattedValuesCached, boolean expectCacheMissError) { fViewerListener.reset(); fViewerListener.addUpdates( TreePath.EMPTY, ((TestElementVMContext) fViewer.getInput()).getElement(), -1, ALL_UPDATES_COMPLETE | PROPERTY_UPDATES); fVMListener.reset(); int vmUpdateFlags = PROPERTY_UPDATES; if (!expectContentCached) { vmUpdateFlags |= ALL_UPDATES_COMPLETE; } fVMListener.addUpdates(TreePath.EMPTY, fModel.getRootElement(), vmListenerLevel, vmUpdateFlags); fFormattedValuesListener.reset(); if (expectFormattedValuesCached && !expectCacheMissError) { fFormattedValuesListener.setCachedFormats(new String[] {formatId}); } if (fVMProvider instanceof IElementFormatProvider && setElementFormats != null) { IElementFormatProvider ep = ((IElementFormatProvider) fVMProvider); for (ElementFormatSetting es : setElementFormats) { ep.setActiveFormat( fViewer.getPresentationContext(), es.nodes.toArray(new IVMNode[es.nodes.size()]), fViewer.getInput(), es.elementPaths.toArray(new TreePath[es.elementPaths.size()]), es.formatId); } } else { // Set the new number format to the viewer. fViewer .getPresentationContext() .setProperty(PROP_FORMATTED_VALUE_FORMAT_PREFERENCE, formatId); } while (!fViewerListener.isFinished(ALL_UPDATES_COMPLETE | PROPERTY_UPDATES) || !fVMListener.isFinished(CONTENT_UPDATES | PROPERTY_UPDATES)) if (!fDisplay.readAndDispatch()) fDisplay.sleep(); if (expectCacheMissError) { try { validateModel(expectElementFormats, formatId, "", formatId, ""); throw new RuntimeException("Expected validateModel to fail"); } catch (AssertionFailedError e) { // expected } } else { validateModel(expectElementFormats, formatId, "", formatId, ""); } if (expectCacheMissError) { String formatProperty = FormattedValueVMUtil.getPropertyForFormatId(formatId); assertTrue(fFormattedValuesListener.getFormattedValuesCompleted().isEmpty()); assertFalse(fFormattedValuesListener.getPropertiesUpdates().isEmpty()); for (IPropertiesUpdate update : fFormattedValuesListener.getPropertiesUpdates()) { PropertiesUpdateStatus status = (PropertiesUpdateStatus) update.getStatus(); assertEquals(IDsfStatusConstants.INVALID_STATE, status.getCode()); ElementFormatSetting elementFormat = null; if (expectElementFormats != null) { TreePath viewerPath = update.getElementPath(); for (ElementFormatSetting es : expectElementFormats) { if (es.elementPaths.indexOf(viewerPath) >= 0) { elementFormat = es; break; } } } if (elementFormat != null) { assertEquals( "Cache contains stale data. Refresh view.", status .getStatus(FormattedValueVMUtil.getPropertyForFormatId(elementFormat.formatId)) .getMessage()); } else { assertEquals( "Cache contains stale data. Refresh view.", status.getStatus(formatProperty).getMessage()); } assertEquals( "Cache contains stale data. Refresh view.", status.getStatus(PROP_FORMATTED_VALUE_ACTIVE_FORMAT_VALUE).getMessage()); assertEquals(1, status.getChildren().length); } } else { assertTrue(fFormattedValuesListener.isFinished()); } }