flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mih...@apache.org
Subject [2/3] git commit: [flex-sdk] [refs/heads/develop] - FLEX-34885 Added a new unit test class, ListCollectionView_PropertyChangeEvent_Tests, that not only tests a few scenarios to make sure that ListCollectionView reacts as expected to PropertyChangeEvents,
Date Tue, 23 Feb 2016 11:15:02 GMT
FLEX-34885
Added a new unit test class, ListCollectionView_PropertyChangeEvent_Tests, that not only tests
a few scenarios to make sure that ListCollectionView reacts as expected to PropertyChangeEvents,
but also makes explicit 2 of the consequences of manually dispatching PropertyChangeEvents
through the VOs in the list without specifying a "property" and an "oldValue" in the PropertyChangeEvent:
1. null is assumed to have been replaced in the collection by that VO, so it will be removed
if it already exists. This may be unexpected to developers.
2. if there's a customCompareFunction in the Sort object, it will be called with null as one
of the arguments. This may again be unexpected to developers.

NOTES:
-also removed some whitespace and improved asdocs.


Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/860ab480
Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/860ab480
Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/860ab480

Branch: refs/heads/develop
Commit: 860ab480c4387eeaca0076e2f89f50784cee03c4
Parents: b870fbc
Author: Mihai Chira <mihaic@apache.org>
Authored: Tue Feb 23 11:53:20 2016 +0100
Committer: Mihai Chira <mihaic@apache.org>
Committed: Tue Feb 23 11:53:20 2016 +0100

----------------------------------------------------------------------
 .../framework/src/mx/collections/ArrayList.as   |  16 +-
 .../src/mx/collections/ListCollectionView.as    |  28 +-
 .../framework/src/mx/collections/Sort.as        |   4 +-
 ...tCollectionView_PropertyChangeEvent_Tests.as | 277 +++++++++++++++++++
 4 files changed, 294 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/860ab480/frameworks/projects/framework/src/mx/collections/ArrayList.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/collections/ArrayList.as b/frameworks/projects/framework/src/mx/collections/ArrayList.as
index 276afc5..dbfbb42 100644
--- a/frameworks/projects/framework/src/mx/collections/ArrayList.as
+++ b/frameworks/projects/framework/src/mx/collections/ArrayList.as
@@ -748,8 +748,7 @@ public class ArrayList extends EventDispatcher
         {
             if (hasEventListener(CollectionEvent.COLLECTION_CHANGE))
             {
-                var event:CollectionEvent =
-                    new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
+                var event:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
                 event.kind = kind;
 				if(kind != CollectionEventKind.RESET && kind != CollectionEventKind.REFRESH)
 				    event.items.push(item);
@@ -761,8 +760,7 @@ public class ArrayList extends EventDispatcher
             if (hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE) && 
                (kind == CollectionEventKind.ADD || kind == CollectionEventKind.REMOVE))
             {
-                var objEvent:PropertyChangeEvent =
-                    new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
+                var objEvent:PropertyChangeEvent = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
                 objEvent.property = location;
                 if (kind == CollectionEventKind.ADD)
                     objEvent.newValue = item;
@@ -814,9 +812,7 @@ public class ArrayList extends EventDispatcher
     {
         if (item && (item is IEventDispatcher))
         {
-            IEventDispatcher(item).addEventListener(
-                                        PropertyChangeEvent.PROPERTY_CHANGE, 
-                                        itemUpdateHandler, false, 0, true);
+            IEventDispatcher(item).addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,
itemUpdateHandler, false, 0, true);
         }
     }
     
@@ -837,12 +833,8 @@ public class ArrayList extends EventDispatcher
     {
         if (item && item is IEventDispatcher)
         {
-            IEventDispatcher(item).removeEventListener(
-                                        PropertyChangeEvent.PROPERTY_CHANGE, 
-                                        itemUpdateHandler);    
+            IEventDispatcher(item).removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,
itemUpdateHandler);
         }
     }
-    
 }
-
 }

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/860ab480/frameworks/projects/framework/src/mx/collections/ListCollectionView.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/collections/ListCollectionView.as b/frameworks/projects/framework/src/mx/collections/ListCollectionView.as
index 173a2d9..a002f1d 100644
--- a/frameworks/projects/framework/src/mx/collections/ListCollectionView.as
+++ b/frameworks/projects/framework/src/mx/collections/ListCollectionView.as
@@ -89,8 +89,7 @@ package mx.collections
  *  @playerversion AIR 1.1
  *  @productversion Flex 3
  */
-public class ListCollectionView extends Proxy
-       implements ICollectionView, IList, IMXMLObject
+public class ListCollectionView extends Proxy implements ICollectionView, IList, IMXMLObject
 {
     include "../core/Version.as";
 
@@ -290,8 +289,7 @@ public class ListCollectionView extends Proxy
             var newHasItems:Boolean;
             if (_list)
             {
-                _list.removeEventListener(CollectionEvent.COLLECTION_CHANGE,
-                                          listChangeHandler);
+                _list.removeEventListener(CollectionEvent.COLLECTION_CHANGE, listChangeHandler);
                 oldHasItems = _list.length > 0;
             }
 
@@ -300,8 +298,7 @@ public class ListCollectionView extends Proxy
             if (_list)
             {
                 // weak listeners to collections and dataproviders
-                _list.addEventListener(CollectionEvent.COLLECTION_CHANGE,
-                                       listChangeHandler, false, 0, true);
+                _list.addEventListener(CollectionEvent.COLLECTION_CHANGE, listChangeHandler,
false, 0, true);
                 newHasItems = _list.length > 0;
             }
 
@@ -1448,7 +1445,7 @@ public class ListCollectionView extends Proxy
                 {
                     item = updateInfo.target;
                     //if the target != source that means the update
-                    //happened to some subprop of the item in the collection
+                    //happened to some sub-property of the item in the collection.
                     //if we have a custom comparator this will affect
                     //the sort so for now say we should move but
                     //maybe we could optimize further
@@ -1495,13 +1492,10 @@ public class ListCollectionView extends Proxy
                     updatedItems.push(updateEntry);
                 }
 
-                //if we've already set replace don't unset it
-                //if there's a filterFunction need to go through replace
-                //if there's no property specified for the sort we'll need
-                //to assume we have to replace
-                //if there is a property see if it affects the sort
-                updateEntry.move =
-                    updateEntry.move
+                //if we've already set move don't unset it
+                //if there's a filterFunction need to go through move
+                //if the property affects the sort, we'll need to move
+                updateEntry.move = updateEntry.move
                     || filterFunction != null
                     || updateEntry.entireObjectChanged
                     || (sort && sort.propertyAffectsSort(String(updateInfo.property)));
@@ -1529,7 +1523,7 @@ public class ListCollectionView extends Proxy
 
 			// now go through the updated items and add all events for all 
 			// properties that changed in that item (except for those items
-			// we moved
+			// we moved)
             var temp:Array = [];
             for (var ctr:int = 0; ctr < eventItems.length; ctr++)
                 for (var ctr1:int = 0; ctr1 < updatedItems.length; ctr1++)
@@ -1542,9 +1536,7 @@ public class ListCollectionView extends Proxy
 
         if (eventItems.length > 0)
         {
-            
-            var updateEvent:CollectionEvent =
-                new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
+            var updateEvent:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
             updateEvent.kind = CollectionEventKind.UPDATE;
             updateEvent.items = eventItems;
             dispatchEvent(updateEvent);

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/860ab480/frameworks/projects/framework/src/mx/collections/Sort.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/collections/Sort.as b/frameworks/projects/framework/src/mx/collections/Sort.as
index 2859f48..7e2e82c 100644
--- a/frameworks/projects/framework/src/mx/collections/Sort.as
+++ b/frameworks/projects/framework/src/mx/collections/Sort.as
@@ -568,7 +568,9 @@ public class Sort extends EventDispatcher implements ISort
      */
     public function propertyAffectsSort(property:String):Boolean
     {
-        if (usingCustomCompareFunction || !fields) return true;
+        if (usingCustomCompareFunction || !fields)
+            return true;
+
         for (var i:int = 0; i < fields.length; i++)
         {
             var field:ISortField = fields[i];

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/860ab480/frameworks/projects/framework/tests/mx/collections/ListCollectionView_PropertyChangeEvent_Tests.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/tests/mx/collections/ListCollectionView_PropertyChangeEvent_Tests.as
b/frameworks/projects/framework/tests/mx/collections/ListCollectionView_PropertyChangeEvent_Tests.as
new file mode 100644
index 0000000..cf7e7dc
--- /dev/null
+++ b/frameworks/projects/framework/tests/mx/collections/ListCollectionView_PropertyChangeEvent_Tests.as
@@ -0,0 +1,277 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package mx.collections {
+    import mx.events.PropertyChangeEvent;
+    import mx.events.PropertyChangeEventKind;
+    import mx.utils.ObjectUtil;
+
+    import org.flexunit.asserts.assertEquals;
+    import org.flexunit.asserts.assertTrue;
+
+    public class ListCollectionView_PropertyChangeEvent_Tests
+    {
+        private static var _sut:ListCollectionView;
+        private static var _firstWorkout:WorkoutVO;
+
+        private static var PROPERTY_CHANGE_EVENT:PropertyChangeEvent;
+        private static var PROPERTY_CHANGE_EVENT_UPDATE:PropertyChangeEvent;
+        private static var PROPERTY_CHANGE_EVENT_UPDATE_CONVENIENCE:PropertyChangeEvent;
+        private static var _noTimesFilterFunctionCalled:int;
+        private static var _lastFilteredObject:Object;
+
+        [Before]
+        public function setUp():void
+        {
+            PROPERTY_CHANGE_EVENT = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
+            PROPERTY_CHANGE_EVENT_UPDATE = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE,
false, false, PropertyChangeEventKind.UPDATE);
+            PROPERTY_CHANGE_EVENT_UPDATE_CONVENIENCE = PropertyChangeEvent.createUpdateEvent(null,
null, null, null);
+
+            _noTimesFilterFunctionCalled = 0;
+
+            InspectableSort.setUp();
+
+            _sut = new ListCollectionView(new ArrayList());
+            _sut.addAll(createWorkouts());
+        }
+
+        [After]
+        public function tearDown():void
+        {
+            _sut = null;
+            _lastFilteredObject = null;
+
+            PROPERTY_CHANGE_EVENT = null;
+            PROPERTY_CHANGE_EVENT_UPDATE = null;
+            PROPERTY_CHANGE_EVENT_UPDATE_CONVENIENCE = null;
+
+            InspectableSort.tearDown();
+        }
+
+        [Test]
+        public function test_PropertyChangeEvents_equivalent():void
+        {
+            //when
+            var eventComparison:int = ObjectUtil.compare(PROPERTY_CHANGE_EVENT_UPDATE, PROPERTY_CHANGE_EVENT_UPDATE_CONVENIENCE);
+
+            //then
+            assertEquals(0, eventComparison);
+        }
+
+        [Test]
+        public function test_when_collection_item_dispatches_PropertyChangeEvent_item_is_run_through_filter_and_there_is_no_fatal():void
+        {
+            //given
+            _sut.filterFunction = allowAll;
+            _sut.refresh();
+
+            _noTimesFilterFunctionCalled = 0;
+            _lastFilteredObject = null;
+
+            //when
+            _firstWorkout.dispatchEvent(PROPERTY_CHANGE_EVENT);
+
+            //then - no fatal, and object has been filtered
+            assertEquals(1, _noTimesFilterFunctionCalled);
+            assertEquals(_firstWorkout, _lastFilteredObject);
+        }
+
+        [Test]
+        public function test_when_collection_item_dispatches_PropertyChangeEvent_null_is_removed_from_list():void
+        {
+            //given
+            _sut.addItem(null);
+            _sut.filterFunction = allowAll;
+            _sut.refresh();
+            var positionOfNull:int = _sut.getItemIndex(null);
+
+            //when
+            _firstWorkout.dispatchEvent(PROPERTY_CHANGE_EVENT);
+
+            //then
+            assertTrue(positionOfNull != -1);
+            assertEquals(-1, _sut.getItemIndex(null));
+        }
+
+        [Test]
+        public function test_when_collection_item_dispatches_PropertyChangeEvent_with_UPDATE_null_is_removed_from_list():void
+        {
+            //given
+            _sut.addItem(null);
+            _sut.filterFunction = allowAll;
+            _sut.refresh();
+            var positionOfNull:int = _sut.getItemIndex(null);
+
+            //when
+            _firstWorkout.dispatchEvent(PROPERTY_CHANGE_EVENT_UPDATE);
+
+            //then
+            assertTrue(positionOfNull != -1);
+            assertEquals(-1, _sut.getItemIndex(null));
+        }
+
+        [Test]
+        public function test_when_collection_item_dispatches_PropertyChangeEvent_with_correct_property_and_oldValue_null_stays_in_list():void
+        {
+            //given
+            _sut.addItem(null);
+            var sort:InspectableSort = new InspectableSort([new SortField("name")]);
+            _sut.sort = sort;
+            _sut.refresh();
+            const positionOfNull:int = _sut.getItemIndex(null);
+            const positionOfFirstWorkout:int = _sut.getItemIndex(_firstWorkout);
+
+            //when
+            _firstWorkout.dispatchEvent(PropertyChangeEvent.createUpdateEvent(_firstWorkout,
"name", _firstWorkout.name, "zzz"));
+
+            //then
+            assertTrue(positionOfNull != -1);
+            assertEquals(positionOfNull, _sut.getItemIndex(null));
+            assertEquals(positionOfFirstWorkout, _sut.getItemIndex(_firstWorkout));
+        }
+
+        [Test]
+        public function test_when_collection_item_dispatches_PropertyChangeEvent_with_correct_property_and_null_oldValue_null_stays_in_list():void
+        {
+            //given
+            _sut.addItem(null);
+            var sort:InspectableSort = new InspectableSort([new SortField("name")]);
+            _sut.sort = sort;
+            _sut.refresh();
+            const positionOfNull:int = _sut.getItemIndex(null);
+            const positionOfFirstWorkout:int = _sut.getItemIndex(_firstWorkout);
+
+            //when
+            _firstWorkout.dispatchEvent(PropertyChangeEvent.createUpdateEvent(_firstWorkout,
"name", null, null));
+
+            //then
+            assertTrue(positionOfNull != -1);
+            assertEquals(positionOfNull, _sut.getItemIndex(null));
+            assertEquals(positionOfFirstWorkout, _sut.getItemIndex(_firstWorkout));
+        }
+
+        [Test]
+        public function test_when_collection_item_dispatches_PropertyChangeEvent_item_is_added_in_correct_place_based_on_sort_and_there_is_no_fatal():void
+        {
+            //given
+            var sort:InspectableSort = new InspectableSort([new SortField("name")]);
+            _sut.sort = sort;
+            _sut.refresh();
+            const positionOfFirstWorkout:int = _sut.getItemIndex(_firstWorkout);
+
+            //when
+            _firstWorkout.dispatchEvent(PROPERTY_CHANGE_EVENT);
+
+            //then - no fatal, and:
+            //object's correct position has been inspected with Sort
+            assertEquals(_firstWorkout, InspectableSort.lastItemSearchedFor);
+            //and null (PropertyChangeEvent.oldValue) has been sought for when trying to
remove it
+            assertTrue(InspectableSort.itemsSearchedFor.indexOf(null) != -1);
+            //and it's in the same position
+            assertEquals(positionOfFirstWorkout, _sut.getItemIndex(_firstWorkout));
+        }
+
+        [Test(expects="Error")]
+        public function test_when_collection_item_dispatches_PropertyChangeEvent_sort_compare_function_called_with_null_and_fatals_if_no_null_check():void
+        {
+            function compareWorkouts(a:Object, b:Object):int
+            {
+                if(a.duration > b.duration)
+                    return 1;
+                if(a.duration < b.duration)
+                    return -1;
+
+                return 0;
+            }
+            //given
+            var sort:InspectableSort = new InspectableSort([], compareWorkouts);
+            _sut.sort = sort;
+            _sut.refresh();
+
+            //when
+            _firstWorkout.dispatchEvent(PROPERTY_CHANGE_EVENT);
+
+            //then - fatal because compareWorkouts was called with null, which it didn't
expect - but should have
+        }
+
+        private static function allowAll(object:Object):Boolean
+        {
+            _lastFilteredObject = object;
+            _noTimesFilterFunctionCalled++;
+            return true;
+        }
+
+        private static function createWorkouts():IList
+        {
+            var result:ArrayList = new ArrayList();
+            for (var i:int = 0; i < 10; i++)
+            {
+                result.addItem(new WorkoutVO("Workout" + i, i));
+            }
+
+            _firstWorkout = result.getItemAt(0) as WorkoutVO;
+
+            return result;
+        }
+    }
+}
+
+import spark.collections.Sort;
+
+[Bindable]
+class WorkoutVO
+{
+    public var duration:int;
+    public var name:String;
+
+    public function WorkoutVO(name:String, duration:int)
+    {
+        this.name = name;
+        this.duration = duration;
+    }
+}
+
+class InspectableSort extends Sort
+{
+    public static var lastItemSearchedFor:Object;
+    public static var itemsSearchedFor:Array;
+
+    public function InspectableSort(fields:Array = null, customCompareFunction:Function =
null, unique:Boolean = false)
+    {
+        super(fields, customCompareFunction, unique);
+    }
+
+    override public function findItem(items:Array, values:Object, mode:String, returnInsertionIndex:Boolean
= false, compareFunction:Function = null):int
+    {
+        lastItemSearchedFor = values;
+        itemsSearchedFor.push(values);
+        return super.findItem(items, values, mode, returnInsertionIndex, compareFunction);
+    }
+
+    public static function setUp():void
+    {
+        itemsSearchedFor = [];
+    }
+
+    public static function tearDown():void
+    {
+        lastItemSearchedFor = null;
+        itemsSearchedFor = null;
+    }
+}
\ No newline at end of file


Mime
View raw message