[NUI] add enum validation for Grid,FlexLayout (#2008)
authorYeongJong Lee <cleanlyj@naver.com>
Wed, 23 Sep 2020 04:16:00 +0000 (13:16 +0900)
committerGitHub <noreply@github.com>
Wed, 23 Sep 2020 04:16:00 +0000 (13:16 +0900)
This validation check is added by TCT reviewer request.

Co-authored-by: TizenAPI-Bot <37820187+TizenAPI-Bot@users.noreply.github.com>
Co-authored-by: dongsug-song <35130733+dongsug-song@users.noreply.github.com>
src/Tizen.NUI/src/public/Layouting/FlexLayout.cs
src/Tizen.NUI/src/public/Layouting/GridLayout.cs
src/Tizen.NUI/src/public/Layouting/LayoutGroup.cs

index 73e1c12..bd5ea21 100755 (executable)
@@ -41,13 +41,13 @@ namespace Tizen.NUI
         /// FlexAlignmentSelfProperty
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly BindableProperty FlexAlignmentSelfProperty = BindableProperty.CreateAttached("FlexAlignmentSelf", typeof(AlignmentType), typeof(FlexLayout), AlignmentType.Auto, propertyChanged: OnChildPropertyChanged);
+        public static readonly BindableProperty FlexAlignmentSelfProperty = BindableProperty.CreateAttached("FlexAlignmentSelf", typeof(AlignmentType), typeof(FlexLayout), AlignmentType.Auto, validateValue: ValidateEnum((int)AlignmentType.Auto, (int)AlignmentType.Stretch), propertyChanged: OnChildPropertyChanged);
 
         /// <summary>
         /// FlexPositionTypeProperty
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly BindableProperty FlexPositionTypeProperty = BindableProperty.CreateAttached("FlexPositionType", typeof(PositionType), typeof(FlexLayout), PositionType.Relative, propertyChanged: OnChildPropertyChanged);
+        public static readonly BindableProperty FlexPositionTypeProperty = BindableProperty.CreateAttached("FlexPositionType", typeof(PositionType), typeof(FlexLayout), PositionType.Relative, validateValue: ValidateEnum((int)PositionType.Relative, (int)PositionType.Absolute), propertyChanged: OnChildPropertyChanged);
 
         /// <summary>
         /// AspectRatioProperty
@@ -333,12 +333,16 @@ namespace Tizen.NUI
         /// Gets/Sets the flex direction in the layout.
         /// The direction of the main-axis which determines the direction that flex items are laid out.
         /// </summary>
+        /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
         /// <since_tizen> 6 </since_tizen>
         public FlexDirection Direction
         {
             get => (FlexDirection)Interop.FlexLayout.FlexLayout_GetFlexDirection(swigCPtr);
             set
             {
+                if (value < FlexDirection.Column || value > FlexDirection.RowReverse)
+                    throw new InvalidEnumArgumentException(nameof(Direction));
+
                 Interop.FlexLayout.FlexLayout_SetFlexDirection(swigCPtr, (int)value);
                 RequestLayout();
             }
@@ -350,12 +354,16 @@ namespace Tizen.NUI
         /// For example, you can use this property to center a child horizontally within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Row"/>
         /// or vertically within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Column"/>.
         /// </summary>
+        /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
         /// <since_tizen> 6 </since_tizen>
         public FlexJustification Justification
         {
             get => (FlexJustification)Interop.FlexLayout.FlexLayout_GetFlexJustification(swigCPtr);
             set
             {
+                if (value < FlexJustification.FlexStart || value > FlexJustification.SpaceAround)
+                    throw new InvalidEnumArgumentException(nameof(Justification));
+
                 Interop.FlexLayout.FlexLayout_SetFlexJustification(swigCPtr, (int)value);
                 RequestLayout();
             }
@@ -368,14 +376,19 @@ namespace Tizen.NUI
         /// If wrapping is allowed items are wrapped into multiple lines along the main axis if needed. wrap reverse behaves the same, but the order of the lines is reversed.<br/>
         /// When wrapping lines <see cref="Alignment"/> can be used to specify how the lines are placed in the container.
         /// </summary>
+        /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
         /// <since_tizen> 6 </since_tizen>
         public FlexWrapType WrapType
         {
             get => (FlexWrapType)Interop.FlexLayout.FlexLayout_GetFlexWrap(swigCPtr);
             set
             {
+                if (value != FlexWrapType.NoWrap || value != FlexWrapType.Wrap)
+                    throw new InvalidEnumArgumentException(nameof(WrapType));
+
                 Interop.FlexLayout.FlexLayout_SetFlexWrap(swigCPtr, (int)value);
                 RequestLayout();
+
             }
         }
 
@@ -384,12 +397,16 @@ namespace Tizen.NUI
         /// Alignment defines the distribution of lines along the cross-axis.<br/>
         /// This only has effect when items are wrapped to multiple lines using flex wrap.
         /// </summary>
+        /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
         /// <since_tizen> 6 </since_tizen>
         public AlignmentType Alignment
         {
             get => GetFlexAlignment();
             set
             {
+                if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
+                    throw new InvalidEnumArgumentException(nameof(Alignment));
+
                 Interop.FlexLayout.FlexLayout_SetFlexAlignment(swigCPtr, (int)value);
                 RequestLayout();
             }
@@ -400,12 +417,16 @@ namespace Tizen.NUI
         /// Items alignment describes how to align children along the cross axis of their container.<br/>
         /// Align items is very similar to <see cref="Justification"/> but instead of applying to the main axis, align items applies to the cross axis.
         /// </summary>
+        /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
         /// <since_tizen> 6 </since_tizen>
         public AlignmentType ItemsAlignment
         {
             get => GetFlexItemsAlignment();
             set
             {
+                if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
+                    throw new InvalidEnumArgumentException(nameof(ItemsAlignment));
+
                 Interop.FlexLayout.FlexLayout_SetFlexItemsAlignment(swigCPtr, (int)value);
                 RequestLayout();
             }
index 77e8d2e..fc10d60 100755 (executable)
@@ -54,25 +54,25 @@ namespace Tizen.NUI
         /// HorizontalStretchProperty
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly BindableProperty HorizontalStretchProperty = BindableProperty.CreateAttached("HorizontalStretch", typeof(StretchFlags), typeof(GridLayout), default(StretchFlags), propertyChanged: OnChildPropertyChanged);
+        public static readonly BindableProperty HorizontalStretchProperty = BindableProperty.CreateAttached("HorizontalStretch", typeof(StretchFlags), typeof(GridLayout), default(StretchFlags), validateValue: ValidateEnum((int)StretchFlags.None, (int)StretchFlags.ExpandAndFill), propertyChanged: OnChildPropertyChanged);
 
         /// <summary>
         /// VerticalStretchProperty
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly BindableProperty VerticalStretchProperty = BindableProperty.CreateAttached("VerticalStretch", typeof(StretchFlags), typeof(GridLayout), default(StretchFlags), propertyChanged: OnChildPropertyChanged);
+        public static readonly BindableProperty VerticalStretchProperty = BindableProperty.CreateAttached("VerticalStretch", typeof(StretchFlags), typeof(GridLayout), default(StretchFlags), validateValue: ValidateEnum((int)StretchFlags.None, (int)StretchFlags.ExpandAndFill), propertyChanged: OnChildPropertyChanged);
 
         /// <summary>
         /// HorizontalAlignmentProperty
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly BindableProperty HorizontalAlignmentProperty = BindableProperty.CreateAttached("HorizontalAlignment", typeof(Alignment), typeof(GridLayout), Alignment.Start, propertyChanged: OnChildPropertyChanged);
+        public static readonly BindableProperty HorizontalAlignmentProperty = BindableProperty.CreateAttached("HorizontalAlignment", typeof(Alignment), typeof(GridLayout), Alignment.Start, validateValue: ValidateEnum((int)Alignment.Start, (int)Alignment.End), propertyChanged: OnChildPropertyChanged);
 
         /// <summary>
         /// VerticalAlignmentProperty
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly BindableProperty VerticalAlignmentProperty = BindableProperty.CreateAttached("VerticalAlignment", typeof(Alignment), typeof(GridLayout), Alignment.Start, propertyChanged: OnChildPropertyChanged);
+        public static readonly BindableProperty VerticalAlignmentProperty = BindableProperty.CreateAttached("VerticalAlignment", typeof(Alignment), typeof(GridLayout), Alignment.Start, validateValue: ValidateEnum((int)Alignment.Start, (int)Alignment.End), propertyChanged: OnChildPropertyChanged);
 
         private const int CellUndefined = int.MinValue;
         private Orientation gridOrientation = Orientation.Horizontal;
@@ -228,6 +228,7 @@ namespace Tizen.NUI
         /// <summary>
         /// Get/Set the orientation in the layout
         /// </summary>
+        /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
         /// <since_tizen> 8 </since_tizen>
         public Orientation GridOrientation
         {
@@ -235,6 +236,9 @@ namespace Tizen.NUI
             set
             {
                 if (gridOrientation == value) return;
+                if (value != Orientation.Horizontal || value != Orientation.Vertical)
+                    throw new InvalidEnumArgumentException(nameof(GridOrientation));
+
                 gridOrientation = value;
                 RequestLayout();
             }
index 71df49e..854909d 100755 (executable)
@@ -588,5 +588,15 @@ namespace Tizen.NUI
             View view = bindable as View;
             view?.Layout?.RequestLayout();
         }
+
+        internal static Binding.BindableProperty.ValidateValueDelegate ValidateEnum(int enumMin, int enumMax)
+        {
+
+            return (Binding.BindableObject bindable, object value) =>
+            {
+                int @enum = (int)value;
+                return enumMin <= @enum && @enum <= enumMax;
+            };
+        }
     }
-}
\ No newline at end of file
+}