diff --git a/js/EnabledComponentAPIMixin.js b/js/EnabledComponentAPIMixin.js new file mode 100644 index 00000000..f4f32aa2 --- /dev/null +++ b/js/EnabledComponentAPIMixin.js @@ -0,0 +1,39 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * PhET-iO API mixin EnabledComponent + * + * @author Michael Kauzmann (PhET Interactive Simulations) + */ + +import PropertyAPI from '../../axon/js/PropertyAPI.js'; +import PropertyIO from '../../axon/js/PropertyIO.js'; +import merge from '../../phet-core/js/merge.js'; +import BooleanIO from '../../tandem/js/types/BooleanIO.js'; +import sun from './sun.js'; + +const EnabledComponentAPIMixin = Type => { + return class extends Type { + + /** + * @param {Object} [options] + */ + constructor( options ) { + assert && assert( arguments.length === 0 || arguments.length === 1, '0 or 1 args expected' ); + + options = merge( { + enabledPropertyOptions: { + phetioFeatured: true, + phetioType: PropertyIO( BooleanIO ) + } + }, options ); + + super( options ); + + this.enabledProperty = new PropertyAPI( options.enabledPropertyOptions ); + } + }; +}; + +sun.register( 'EnabledComponentAPIMixin', EnabledComponentAPIMixin ); +export default EnabledComponentAPIMixin; \ No newline at end of file diff --git a/js/EnabledPhetioObject.js b/js/EnabledPhetioObject.js index 961da6a0..7c53f01f 100644 --- a/js/EnabledPhetioObject.js +++ b/js/EnabledPhetioObject.js @@ -49,11 +49,15 @@ const EnabledPhetioObject = { // This phet-io support only applies to instances of PhetioObject if ( !ownsEnabledProperty ) { - assert && Tandem.PHET_IO_ENABLED && Tandem.errorOnFailedValidation() && this.isPhetioInstrumented() && - assert( !!options.enabledProperty.phetioFeatured === !!this.phetioFeatured, - 'provided enabledProperty must be phetioFeatured if this checkbox is' ); + + if ( Tandem.PHET_IO_ENABLED && Tandem.errorOnFailedValidation() && this.isPhetioInstrumented() ) { + + assert && assert( options.enabledProperty.isPhetioInstrumented(), 'provided enabledProperty must be instrumented if this PhetioObject is.' ); + assert && this.phetioFeatured && assert( options.enabledProperty.phetioFeatured, 'provided enabledProperty must be phetioFeatured if this PhetioObject is.' ); + } // If enabledProperty was passed in, PhET-iO wrappers like Studio needs to know about that linkage + // This is supported in API.js types because in practice LinkedElementIO defer to their linked PhetioObject. this.enabledProperty.isPhetioInstrumented() && this.addLinkedElement( options.enabledProperty, { tandem: options.tandem.createTandem( EnabledComponent.ENABLED_PROPERTY_TANDEM_NAME ) } ); diff --git a/js/SliderAPI.js b/js/SliderAPI.js index 93948402..df016274 100644 --- a/js/SliderAPI.js +++ b/js/SliderAPI.js @@ -9,6 +9,7 @@ import merge from '../../phet-core/js/merge.js'; import DragListenerAPI from '../../scenery/js/listeners/DragListenerAPI.js'; import NodeAPI from '../../scenery/js/nodes/NodeAPI.js'; +import EnabledComponentAPIMixin from './EnabledComponentAPIMixin.js'; import SliderIO from './SliderIO.js'; import sun from './sun.js'; @@ -36,7 +37,7 @@ class TrackAPI extends NodeAPI { } } -class SliderAPI extends NodeAPI { +class SliderAPI extends EnabledComponentAPIMixin( NodeAPI ) { /** * @param {Object} [options] @@ -52,15 +53,7 @@ class SliderAPI extends NodeAPI { super( options ); - this.track = new TrackAPI( { - - // TODO: this is now part of the API, but how to we tell phetioAPITest to test this arbitrarily deep tandem override for Slider, https://github.com/phetsims/phet-io/issues/1657 - dragListenerOptions: { - pressActionOptions: { - phetioFeature: true - } - } - } ); + this.track = new TrackAPI(); } } diff --git a/js/SliderTests.js b/js/SliderTests.js index 21d48ea8..c157e71e 100644 --- a/js/SliderTests.js +++ b/js/SliderTests.js @@ -8,6 +8,7 @@ * @author Michael Kauzmann (PhET Interactive Simulations) */ +import BooleanProperty from '../../axon/js/BooleanProperty.js'; import Property from '../../axon/js/Property.js'; import Range from '../../dot/js/Range.js'; import phetioAPITest from '../../tandem/js/phetioAPITest.js'; @@ -17,5 +18,14 @@ import SliderAPI from './SliderAPI.js'; QUnit.module( 'Slider' ); QUnit.test( 'Slider PhET-iO API validation', assert => { - phetioAPITest( assert, new SliderAPI(), 'slider', tandem => new HSlider( new Property( 0 ), new Range( 0, 10 ), { tandem: tandem } ) ); + phetioAPITest( assert, new SliderAPI(), 'slider', + tandem => new HSlider( new Property( 0 ), new Range( 0, 10 ), { tandem: tandem } ) ); +} ); + +QUnit.test( 'Slider PhET-iO API validation, provided enabledProperty', assert => { + phetioAPITest( assert, new SliderAPI(), 'slider', + tandem => new HSlider( new Property( 0 ), new Range( 0, 10 ), { + tandem: tandem, + enabledProperty: new BooleanProperty( false, { tandem: tandem.createTandem( 'otherEnabledProperty' ), phetioFeatured: true } ) + } ) ); } ); \ No newline at end of file