Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
b02531a9
Commit
b02531a9
authored
Sep 04, 2015
by
Hans Muller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Dismissable unit tests
parent
4e01c054
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
210 additions
and
73 deletions
+210
-73
card_collection.dart
examples/widgets/card_collection.dart
+3
-3
dismissable_test.dart
packages/unit/test/widget/dismissable_test.dart
+191
-60
widget_tester.dart
packages/unit/test/widget/widget_tester.dart
+16
-10
No files found.
examples/widgets/card_collection.dart
View file @
b02531a9
...
...
@@ -64,7 +64,7 @@ class CardCollectionApp extends App {
});
}
String
dismissDirectionText
(
DismissDirection
direction
)
{
String
_
dismissDirectionText
(
DismissDirection
direction
)
{
String
s
=
direction
.
toString
();
return
"dismiss
${s.substring(s.indexOf('.') + 1)}
"
;
}
...
...
@@ -85,7 +85,7 @@ class CardCollectionApp extends App {
icon:
icon
,
onPressed:
()
{
changeDismissDirection
(
direction
);
},
child:
new
Row
([
new
Flexible
(
child:
new
Text
(
dismissDirectionText
(
direction
))),
new
Flexible
(
child:
new
Text
(
_
dismissDirectionText
(
direction
))),
new
Radio
(
value:
direction
,
onChanged:
changeDismissDirection
,
...
...
@@ -116,7 +116,7 @@ class CardCollectionApp extends App {
left:
new
IconButton
(
icon:
"navigation/menu"
,
onPressed:
_handleOpenDrawer
),
center:
new
Text
(
'Swipe Away'
),
right:
[
new
Text
(
dismissDirectionText
(
_dismissDirection
))
new
Text
(
_
dismissDirectionText
(
_dismissDirection
))
]
);
}
...
...
packages/unit/test/widget/dismissable_test.dart
View file @
b02531a9
...
...
@@ -5,70 +5,201 @@ import 'package:test/test.dart';
import
'../engine/mock_events.dart'
;
import
'widget_tester.dart'
;
const
double
itemExtent
=
100.0
;
ScrollDirection
scrollDirection
=
ScrollDirection
.
vertical
;
DismissDirection
dismissDirection
=
DismissDirection
.
horizontal
;
List
<
int
>
dismissedItems
=
[];
void
handleOnResized
(
item
)
{
expect
(
dismissedItems
.
contains
(
item
),
isFalse
);
}
void
handleOnDismissed
(
item
)
{
expect
(
dismissedItems
.
contains
(
item
),
isFalse
);
dismissedItems
.
add
(
item
);
}
Widget
buildDismissableItem
(
int
item
)
{
return
new
Dismissable
(
key:
new
ValueKey
<
int
>(
item
),
direction:
dismissDirection
,
onDismissed:
()
{
handleOnDismissed
(
item
);
},
onResized:
()
{
handleOnResized
(
item
);
},
child:
new
Container
(
width:
itemExtent
,
height:
itemExtent
,
child:
new
Text
(
item
.
toString
())
)
);
}
Widget
widgetBuilder
(
)
{
return
new
Container
(
padding:
const
EdgeDims
.
all
(
10.0
),
child:
new
ScrollableList
<
int
>(
items:
[
0
,
1
,
2
,
3
,
4
],
itemBuilder:
buildDismissableItem
,
scrollDirection:
scrollDirection
,
itemExtent:
itemExtent
)
);
}
void
dismissItem
(
WidgetTester
tester
,
int
item
,
{
DismissDirection
gestureDirection
})
{
assert
(
gestureDirection
!=
DismissDirection
.
horizontal
);
assert
(
gestureDirection
!=
DismissDirection
.
vertical
);
Widget
itemWidget
=
tester
.
findText
(
item
.
toString
());
expect
(
itemWidget
,
isNotNull
);
Point
downLocation
;
Point
upLocation
;
switch
(
gestureDirection
)
{
case
DismissDirection
.
left
:
// Note: getTopRight() returns a point that's just beyond
// itemWidget's right edge and outside the Dismissable event
// listener's bounds.
downLocation
=
tester
.
getTopRight
(
itemWidget
)
+
const
Offset
(-
0.1
,
0.0
);
upLocation
=
tester
.
getTopLeft
(
itemWidget
);
break
;
case
DismissDirection
.
right
:
downLocation
=
tester
.
getTopLeft
(
itemWidget
);
upLocation
=
tester
.
getTopRight
(
itemWidget
);
break
;
case
DismissDirection
.
up
:
// Note: getBottomLeft() returns a point that's just below
// itemWidget's bottom edge and outside the Dismissable event
// listener's bounds.
downLocation
=
tester
.
getBottomLeft
(
itemWidget
)
+
const
Offset
(
0.0
,
-
0.1
);
upLocation
=
tester
.
getTopLeft
(
itemWidget
);
break
;
case
DismissDirection
.
down
:
downLocation
=
tester
.
getTopLeft
(
itemWidget
);
upLocation
=
tester
.
getBottomLeft
(
itemWidget
);
break
;
}
TestPointer
pointer
=
new
TestPointer
(
5
);
tester
.
dispatchEvent
(
pointer
.
down
(
downLocation
),
downLocation
);
tester
.
dispatchEvent
(
pointer
.
move
(
upLocation
),
downLocation
);
tester
.
dispatchEvent
(
pointer
.
up
(),
downLocation
);
double
t0
=
0.0
;
new
FakeAsync
().
run
((
async
)
{
tester
.
pumpFrame
(
widgetBuilder
,
t0
);
// start the resize animation
tester
.
pumpFrame
(
widgetBuilder
,
t0
+
1000.0
);
// finish the resize animation
async
.
elapse
(
new
Duration
(
seconds:
1
));
tester
.
pumpFrame
(
widgetBuilder
,
t0
+
2000.0
);
// dismiss
async
.
elapse
(
new
Duration
(
seconds:
1
));
});
}
void
main
(
)
{
test
(
'Horizontal drag triggers dismiss'
,
()
{
test
(
'Horizontal drag triggers dismiss scrollDirection=vertical'
,
()
{
WidgetTester
tester
=
new
WidgetTester
();
scrollDirection
=
ScrollDirection
.
vertical
;
dismissDirection
=
DismissDirection
.
horizontal
;
dismissedItems
=
[];
tester
.
pumpFrame
(
widgetBuilder
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
right
);
expect
(
tester
.
findText
(
'0'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
]));
dismissItem
(
tester
,
1
,
gestureDirection:
DismissDirection
.
left
);
expect
(
tester
.
findText
(
'1'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
,
1
]));
});
test
(
'Vertical drag triggers dismiss scrollDirection=horizontal'
,
()
{
WidgetTester
tester
=
new
WidgetTester
();
scrollDirection
=
ScrollDirection
.
horizontal
;
dismissDirection
=
DismissDirection
.
vertical
;
dismissedItems
=
[];
tester
.
pumpFrame
(
widgetBuilder
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
up
);
expect
(
tester
.
findText
(
'0'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
]));
dismissItem
(
tester
,
1
,
gestureDirection:
DismissDirection
.
down
);
expect
(
tester
.
findText
(
'1'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
,
1
]));
});
test
(
'drag-left with DismissDirection.left triggers dismiss'
,
()
{
WidgetTester
tester
=
new
WidgetTester
();
scrollDirection
=
ScrollDirection
.
vertical
;
dismissDirection
=
DismissDirection
.
left
;
dismissedItems
=
[];
tester
.
pumpFrame
(
widgetBuilder
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
right
);
expect
(
tester
.
findText
(
'0'
),
isNotNull
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
left
);
expect
(
tester
.
findText
(
'0'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
]));
});
test
(
'drag-right with DismissDirection.right triggers dismiss'
,
()
{
WidgetTester
tester
=
new
WidgetTester
();
scrollDirection
=
ScrollDirection
.
vertical
;
dismissDirection
=
DismissDirection
.
right
;
dismissedItems
=
[];
tester
.
pumpFrame
(
widgetBuilder
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
left
);
expect
(
tester
.
findText
(
'0'
),
isNotNull
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
right
);
expect
(
tester
.
findText
(
'0'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
]));
});
test
(
'drag-up with DismissDirection.up triggers dismiss'
,
()
{
WidgetTester
tester
=
new
WidgetTester
();
scrollDirection
=
ScrollDirection
.
horizontal
;
dismissDirection
=
DismissDirection
.
up
;
dismissedItems
=
[];
tester
.
pumpFrame
(
widgetBuilder
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
down
);
expect
(
tester
.
findText
(
'0'
),
isNotNull
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
up
);
expect
(
tester
.
findText
(
'0'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
]));
});
test
(
'drag-down with DismissDirection.down triggers dismiss'
,
()
{
WidgetTester
tester
=
new
WidgetTester
();
TestPointer
pointer
=
new
TestPointer
(
5
);
const
double
itemHeight
=
50.0
;
List
<
int
>
dismissedItems
=
[];
void
handleOnResized
(
item
)
{
expect
(
dismissedItems
.
contains
(
item
),
isFalse
);
}
void
handleOnDismissed
(
item
)
{
expect
(
dismissedItems
.
contains
(
item
),
isFalse
);
dismissedItems
.
add
(
item
);
}
Widget
buildDismissableItem
(
int
item
)
{
return
new
Dismissable
(
key:
new
ValueKey
<
int
>(
item
),
onDismissed:
()
{
handleOnDismissed
(
item
);
},
onResized:
()
{
handleOnResized
(
item
);
},
child:
new
Container
(
height:
itemHeight
,
child:
new
Text
(
item
.
toString
())
)
);
}
Widget
builder
()
{
return
new
Container
(
padding:
const
EdgeDims
.
all
(
10.0
),
child:
new
ScrollableList
<
int
>(
items:
[
0
,
1
,
2
,
3
,
4
,
5
],
itemBuilder:
buildDismissableItem
,
scrollDirection:
ScrollDirection
.
vertical
,
itemExtent:
itemHeight
)
);
}
tester
.
pumpFrame
(
builder
);
Widget
item3
=
tester
.
findText
(
"3"
);
expect
(
item3
,
isNotNull
);
scrollDirection
=
ScrollDirection
.
horizontal
;
dismissDirection
=
DismissDirection
.
down
;
dismissedItems
=
[];
tester
.
pumpFrame
(
widgetBuilder
);
expect
(
dismissedItems
,
isEmpty
);
// Gesture: press-drag-release from the Dismissable's top-left corner
// to its top-right corner. Triggers the resize animation which concludes
// by calling onDismissed().
Point
downLocation
=
tester
.
getTopLeft
(
item3
);
Point
upLocation
=
tester
.
getTopRight
(
item3
);
tester
.
dispatchEvent
(
pointer
.
down
(
downLocation
),
downLocation
);
tester
.
dispatchEvent
(
pointer
.
move
(
upLocation
),
upLocation
);
tester
.
dispatchEvent
(
pointer
.
up
(),
upLocation
);
new
FakeAsync
().
run
((
async
)
{
tester
.
pumpFrame
(
builder
);
// start the resize animation
tester
.
pumpFrame
(
builder
,
1000.0
);
// finish the resize animation
async
.
elapse
(
new
Duration
(
seconds:
1
));
tester
.
pumpFrame
(
builder
,
2000.0
);
// dismiss
async
.
elapse
(
new
Duration
(
seconds:
1
));
expect
(
dismissedItems
,
equals
([
3
]));
expect
(
tester
.
findText
(
"3"
),
isNull
);
});
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
up
);
expect
(
tester
.
findText
(
'0'
),
isNotNull
);
expect
(
dismissedItems
,
isEmpty
);
dismissItem
(
tester
,
0
,
gestureDirection:
DismissDirection
.
down
);
expect
(
tester
.
findText
(
'0'
),
isNull
);
expect
(
dismissedItems
,
equals
([
0
]));
});
}
packages/unit/test/widget/widget_tester.dart
View file @
b02531a9
...
...
@@ -76,25 +76,31 @@ class WidgetTester {
});
}
Point
getCenter
(
Widget
widge
t
)
{
Point
_getWidgetPoint
(
Widget
widget
,
Function
sizeToPoin
t
)
{
assert
(
widget
!=
null
);
RenderBox
box
=
widget
.
renderObject
as
RenderBox
;
assert
(
box
!=
null
);
return
box
.
localToGlobal
(
box
.
size
.
center
(
Point
.
origin
));
return
box
.
localToGlobal
(
sizeToPoint
(
box
.
size
));
}
Point
getCenter
(
Widget
widget
)
{
return
_getWidgetPoint
(
widget
,
(
Size
size
)
=>
size
.
center
(
Point
.
origin
));
}
Point
getTopLeft
(
Widget
widget
)
{
assert
(
widget
!=
null
);
RenderBox
box
=
widget
.
renderObject
as
RenderBox
;
assert
(
box
!=
null
);
return
box
.
localToGlobal
(
Point
.
origin
);
return
_getWidgetPoint
(
widget
,
(
_
)
=>
Point
.
origin
);
}
Point
getTopRight
(
Widget
widget
)
{
assert
(
widget
!=
null
);
RenderBox
box
=
widget
.
renderObject
as
RenderBox
;
assert
(
box
!=
null
);
return
box
.
localToGlobal
(
box
.
size
.
topRight
(
Point
.
origin
));
return
_getWidgetPoint
(
widget
,
(
Size
size
)
=>
size
.
topRight
(
Point
.
origin
));
}
Point
getBottomLeft
(
Widget
widget
)
{
return
_getWidgetPoint
(
widget
,
(
Size
size
)
=>
size
.
bottomLeft
(
Point
.
origin
));
}
Point
getBottomRight
(
Widget
widget
)
{
return
_getWidgetPoint
(
widget
,
(
Size
size
)
=>
size
.
bottomRight
(
Point
.
origin
));
}
HitTestResult
_hitTest
(
Point
location
)
=>
SkyBinding
.
instance
.
hitTest
(
location
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment