@@ -10,15 +10,36 @@ var getSVGElemScreenBBox = require(
1010var testMock = require ( '../../image/mocks/domain_refs_editable.json' ) ;
1111var delay = require ( '../assets/delay' ) ;
1212var mouseEvent = require ( '../assets/mouse_event' ) ;
13+ // we have to use drag to move annotations for some reason
1314var drag = require ( '../assets/drag' ) ;
15+ var SVGTools = require ( '../assets/svg_tools' ) ;
1416
1517// color of the rectangles
1618var rectColor1 = 'rgb(10, 20, 30)' ;
1719var rectColor2 = 'rgb(10, 20, 31)' ;
1820var rectColor3 = 'rgb(100, 200, 232)' ;
1921var rectColor4 = 'rgb(200, 200, 232)' ;
22+ var arrowColor1 = 'rgb(231, 200, 100)' ;
23+ var arrowColor2 = 'rgb(231, 200, 200)' ;
2024
21- var DELAY_TIME = 0 ;
25+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 1000000 ;
26+
27+ var DELAY_TIME = 1000 ;
28+
29+ function svgRectToJSON ( svgrect ) {
30+ return JSON . stringify ( SVGTools . svgRectToObj ( svgrect ) ) ;
31+ }
32+
33+ function checkBBox ( bboxBefore , bboxAfter , moveX , moveY ) {
34+ // We print out the objects for sanity, because sometimes Jasmine says a
35+ // test passed when it actually did nothing!
36+ console . log ( 'bboxBefore' , svgRectToJSON ( bboxBefore ) ) ;
37+ console . log ( 'bboxAfter' , svgRectToJSON ( bboxAfter ) ) ;
38+ console . log ( 'moveX' , moveX ) ;
39+ console . log ( 'moveY' , moveY ) ;
40+ expect ( bboxAfter . x ) . toBeCloseTo ( bboxBefore . x + moveX , 2 ) ;
41+ expect ( bboxAfter . y ) . toBeCloseTo ( bboxBefore . y + moveY , 2 ) ;
42+ }
2243
2344function testObjectMove ( objectColor , moveX , moveY , type ) {
2445 var bboxBefore = getSVGElemScreenBBox (
@@ -37,27 +58,83 @@ function testObjectMove(objectColor, moveX, moveY, type) {
3758 var bboxAfter = getSVGElemScreenBBox (
3859 domainRefComponents . findAROByColor ( objectColor , undefined , type )
3960 ) ;
40- expect ( bboxAfter . x ) . toBeCloseTo ( bboxBefore . x + moveX , 2 ) ;
41- expect ( bboxAfter . y ) . toBeCloseTo ( bboxBefore . y + moveY , 2 ) ;
61+ checkBBox ( bboxBefore , bboxAfter , moveX , moveY ) ;
62+ }
63+
64+ function dragPos0 ( bbox , corner ) {
65+ if ( corner === 'bl' ) {
66+ return [ bbox . x + bbox . width * 0.5 ,
67+ bbox . y + bbox . height * 0.5 - 10 ] ;
68+ } else if ( corner === 'tr' ) {
69+ return [ bbox . x + bbox . width * 0.5 ,
70+ bbox . y + bbox . height * 0.5 + 10 ] ;
71+ } else {
72+ return [ bbox . x + bbox . width * 0.5 ,
73+ bbox . y + bbox . height * 0.5 ] ;
74+ }
4275}
4376
44- function testAnnotationMove ( objectColor , moveX , moveY , type ) {
77+ // Tests moving the annotation label
78+ function testAnnotationMoveLabel ( objectColor , moveX , moveY ) {
79+ var bboxAfter ;
80+ // Get where the text box (label) is before dragging it
4581 var bboxBefore = getSVGElemScreenBBox (
46- domainRefComponents . findAROByColor ( objectColor , undefined , type )
82+ domainRefComponents . findAROByColor ( objectColor , undefined , 'rect' )
4783 ) ;
48- var opt = {
49- pos0 : [ bboxBefore . x + bboxBefore . width * 0.5 ,
50- bboxBefore . y + bboxBefore . height * 0.5 ] ,
84+ // we have to use drag to move annotations for some reason
85+ var optLabelDrag = {
86+ pos0 : dragPos0 ( bboxBefore )
5187 } ;
52- opt . dpos = [ moveX , moveY ] ;
53- return ( new Promise ( function ( ) { drag ( opt ) ; } ) )
88+ optLabelDrag . dpos = [ moveX , moveY ] ;
89+ console . log ( 'optLabelDrag' , optLabelDrag ) ;
90+ // drag the label, this will make the arrow rotate around the arrowhead
91+ return ( new Promise ( function ( resolve ) {
92+ drag ( optLabelDrag ) ; resolve ( ) ;
93+ } ) )
94+ . then ( delay ( DELAY_TIME ) )
5495 . then ( function ( ) {
55- var bboxAfter = getSVGElemScreenBBox (
56- domainRefComponents . findAROByColor ( objectColor , undefined , type )
96+ // then check it's position
97+ bboxAfter = getSVGElemScreenBBox (
98+ domainRefComponents . findAROByColor ( objectColor , undefined , 'rect' )
5799 ) ;
58- expect ( bboxAfter . x ) . toBeCloseTo ( bboxBefore . x + moveX , 2 ) ;
59- expect ( bboxAfter . y ) . toBeCloseTo ( bboxBefore . y + moveY , 2 ) ;
60- } ) ;
100+ checkBBox ( bboxBefore , bboxAfter , moveX , moveY ) ;
101+ } )
102+ . then ( delay ( DELAY_TIME ) ) ;
103+ }
104+
105+ // Tests moving the whole annotation
106+ function testAnnotationMoveWhole ( objectColor , arrowColor , moveX , moveY , corner ) {
107+ var bboxAfter ;
108+ var arrowBBoxAfter ;
109+ // Get where the text box (label) is before dragging it
110+ var bboxBefore = getSVGElemScreenBBox (
111+ domainRefComponents . findAROByColor ( objectColor , undefined , 'rect' )
112+ ) ;
113+ var arrowBBoxBefore = getSVGElemScreenBBox (
114+ domainRefComponents . findAROByColor ( arrowColor , undefined , 'path' , 'fill' )
115+ ) ;
116+ var optArrowDrag = {
117+ pos0 : dragPos0 ( arrowBBoxBefore , corner )
118+ } ;
119+ optArrowDrag . dpos = [ moveX , moveY ] ;
120+ console . log ( 'optArrowDrag' , optArrowDrag ) ;
121+ // drag the whole annotation
122+ ( new Promise ( function ( resolve ) {
123+ drag ( optArrowDrag ) ; resolve ( ) ;
124+ } ) )
125+ . then ( delay ( DELAY_TIME ) )
126+ . then ( function ( ) {
127+ // check the new position of the arrow and label
128+ arrowBBoxAfter = getSVGElemScreenBBox (
129+ domainRefComponents . findAROByColor ( arrowColor , undefined , 'path' , 'fill' )
130+ ) ;
131+ bboxAfter = getSVGElemScreenBBox (
132+ domainRefComponents . findAROByColor ( objectColor , undefined , 'rect' )
133+ ) ;
134+ checkBBox ( arrowBBoxBefore , arrowBBoxAfter , moveX , moveY ) ;
135+ checkBBox ( bboxBefore , bboxAfter , moveX , moveY ) ;
136+ } )
137+ . then ( delay ( DELAY_TIME ) ) ;
61138}
62139
63140describe ( 'Shapes referencing domain' , function ( ) {
@@ -82,11 +159,21 @@ describe('Shapes referencing domain', function() {
82159 . then ( done ) ;
83160 } ;
84161 }
85- function testAnnotationMoveItFun ( color , x , y , type ) {
162+ function testAnnotationMoveLabelItFun ( color , x , y ) {
163+ return function ( done ) {
164+ Plotly . newPlot ( gd , Lib . extendDeep ( { } , testMock ) )
165+ . then ( delay ( DELAY_TIME ) )
166+ . then ( testAnnotationMoveLabel ( color , x , y ) )
167+ . then ( delay ( DELAY_TIME ) )
168+ . catch ( failTest )
169+ . then ( done ) ;
170+ } ;
171+ }
172+ function testAnnotationMoveWholeItFun ( color , arrowColor , x , y , corner ) {
86173 return function ( done ) {
87174 Plotly . newPlot ( gd , Lib . extendDeep ( { } , testMock ) )
88175 . then ( delay ( DELAY_TIME ) )
89- . then ( testAnnotationMove ( color , x , y , type ) )
176+ . then ( testAnnotationMoveWhole ( color , arrowColor , x , y , corner ) )
90177 . then ( delay ( DELAY_TIME ) )
91178 . catch ( failTest )
92179 . then ( done ) ;
@@ -96,8 +183,12 @@ describe('Shapes referencing domain', function() {
96183 testObjectMoveItFun ( rectColor1 , 100 , - 300 , 'path' ) ) ;
97184 it ( 'should move box on log x axis and linear y to the proper position' ,
98185 testObjectMoveItFun ( rectColor2 , - 400 , - 200 , 'path' ) ) ;
99- it ( 'should move annotation box on linear x axis and log y to the proper position' ,
100- testAnnotationMoveItFun ( rectColor3 , 50 , - 100 , 'rect' ) ) ;
101- it ( 'should move annotation box on log x axis and linear y to the proper position' ,
102- testAnnotationMoveItFun ( rectColor4 , - 75 , - 150 , 'rect' ) ) ;
186+ it ( 'should move annotation label on linear x axis and log y to the proper position' ,
187+ testAnnotationMoveLabelItFun ( rectColor3 , 50 , - 100 , 'rect' ) ) ;
188+ it ( 'should move annotation label on log x axis and linear y to the proper position' ,
189+ testAnnotationMoveLabelItFun ( rectColor4 , - 75 , - 150 , 'rect' ) ) ;
190+ it ( 'should move whole annotation on linear x axis and log y to the proper position' ,
191+ testAnnotationMoveWholeItFun ( rectColor3 , arrowColor1 , 50 , - 100 , 'bl' ) ) ;
192+ it ( 'should move whole annotation on log x axis and linear y to the proper position' ,
193+ testAnnotationMoveWholeItFun ( rectColor4 , arrowColor2 , - 75 , - 150 , 'tr' ) ) ;
103194} ) ;
0 commit comments