forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpane-container-element-spec.coffee
266 lines (208 loc) · 9.12 KB
/
pane-container-element-spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
PaneContainer = require '../src/pane-container'
PaneAxisElement = require '../src/pane-axis-element'
PaneAxis = require '../src/pane-axis'
params =
location: 'center'
config: atom.config
confirm: atom.confirm.bind(atom)
viewRegistry: atom.views
applicationDelegate: atom.applicationDelegate
describe "PaneContainerElement", ->
describe "when panes are added or removed", ->
it "inserts or removes resize elements", ->
childTagNames = ->
child.nodeName.toLowerCase() for child in paneAxisElement.children
paneAxis = new PaneAxis({}, atom.views)
paneAxisElement = paneAxis.getElement()
expect(childTagNames()).toEqual []
paneAxis.addChild(new PaneAxis({}, atom.views))
expect(childTagNames()).toEqual [
'atom-pane-axis'
]
paneAxis.addChild(new PaneAxis({}, atom.views))
expect(childTagNames()).toEqual [
'atom-pane-axis'
'atom-pane-resize-handle'
'atom-pane-axis'
]
paneAxis.addChild(new PaneAxis({}, atom.views))
expect(childTagNames()).toEqual [
'atom-pane-axis'
'atom-pane-resize-handle'
'atom-pane-axis'
'atom-pane-resize-handle'
'atom-pane-axis'
]
paneAxis.removeChild(paneAxis.getChildren()[2])
expect(childTagNames()).toEqual [
'atom-pane-axis'
'atom-pane-resize-handle'
'atom-pane-axis'
]
it "transfers focus to the next pane if a focused pane is removed", ->
container = new PaneContainer(params)
containerElement = container.getElement()
leftPane = container.getActivePane()
leftPaneElement = leftPane.getElement()
rightPane = leftPane.splitRight()
rightPaneElement = rightPane.getElement()
jasmine.attachToDOM(containerElement)
rightPaneElement.focus()
expect(document.activeElement).toBe rightPaneElement
rightPane.destroy()
expect(document.activeElement).toBe leftPaneElement
describe "when a pane is split", ->
it "builds appropriately-oriented atom-pane-axis elements", ->
container = new PaneContainer(params)
containerElement = container.getElement()
pane1 = container.getActivePane()
pane2 = pane1.splitRight()
pane3 = pane2.splitDown()
horizontalPanes = containerElement.querySelectorAll('atom-pane-container > atom-pane-axis.horizontal > atom-pane')
expect(horizontalPanes.length).toBe 1
expect(horizontalPanes[0]).toBe pane1.getElement()
verticalPanes = containerElement.querySelectorAll('atom-pane-container > atom-pane-axis.horizontal > atom-pane-axis.vertical > atom-pane')
expect(verticalPanes.length).toBe 2
expect(verticalPanes[0]).toBe pane2.getElement()
expect(verticalPanes[1]).toBe pane3.getElement()
pane1.destroy()
verticalPanes = containerElement.querySelectorAll('atom-pane-container > atom-pane-axis.vertical > atom-pane')
expect(verticalPanes.length).toBe 2
expect(verticalPanes[0]).toBe pane2.getElement()
expect(verticalPanes[1]).toBe pane3.getElement()
describe "when the resize element is dragged ", ->
[container, containerElement] = []
beforeEach ->
container = new PaneContainer(params)
containerElement = container.getElement()
document.querySelector('#jasmine-content').appendChild(containerElement)
dragElementToPosition = (element, clientX) ->
element.dispatchEvent(new MouseEvent('mousedown',
view: window
bubbles: true
button: 0
))
element.dispatchEvent(new MouseEvent 'mousemove',
view: window
bubbles: true
clientX: clientX
)
element.dispatchEvent(new MouseEvent 'mouseup',
iew: window
bubbles: true
button: 0
)
getElementWidth = (element) ->
element.getBoundingClientRect().width
expectPaneScale = (pairs...) ->
for [pane, expectedFlexScale] in pairs
expect(pane.getFlexScale()).toBeCloseTo(expectedFlexScale, 0.1)
getResizeElement = (i) ->
containerElement.querySelectorAll('atom-pane-resize-handle')[i]
getPaneElement = (i) ->
containerElement.querySelectorAll('atom-pane')[i]
it "adds and removes panes in the direction that the pane is being dragged", ->
leftPane = container.getActivePane()
expectPaneScale [leftPane, 1]
middlePane = leftPane.splitRight()
expectPaneScale [leftPane, 1], [middlePane, 1]
dragElementToPosition(
getResizeElement(0),
getElementWidth(getPaneElement(0)) / 2
)
expectPaneScale [leftPane, 0.5], [middlePane, 1.5]
rightPane = middlePane.splitRight()
expectPaneScale [leftPane, 0.5], [middlePane, 1.5], [rightPane, 1]
dragElementToPosition(
getResizeElement(1),
getElementWidth(getPaneElement(0)) + getElementWidth(getPaneElement(1)) / 2
)
expectPaneScale [leftPane, 0.5], [middlePane, 0.75], [rightPane, 1.75]
waitsForPromise -> middlePane.close()
runs -> expectPaneScale [leftPane, 0.44], [rightPane, 1.55]
waitsForPromise -> leftPane.close()
runs -> expectPaneScale [rightPane, 1]
it "splits or closes panes in orthogonal direction that the pane is being dragged", ->
leftPane = container.getActivePane()
expectPaneScale [leftPane, 1]
rightPane = leftPane.splitRight()
expectPaneScale [leftPane, 1], [rightPane, 1]
dragElementToPosition(
getResizeElement(0),
getElementWidth(getPaneElement(0)) / 2
)
expectPaneScale [leftPane, 0.5], [rightPane, 1.5]
# dynamically split pane, pane's flexScale will become to 1
lowerPane = leftPane.splitDown()
expectPaneScale [lowerPane, 1], [leftPane, 1], [leftPane.getParent(), 0.5]
# dynamically close pane, the pane's flexscale will recover to origin value
waitsForPromise -> lowerPane.close()
runs -> expectPaneScale [leftPane, 0.5], [rightPane, 1.5]
it "unsubscribes from mouse events when the pane is detached", ->
container.getActivePane().splitRight()
element = getResizeElement(0)
spyOn(document, 'addEventListener').andCallThrough()
spyOn(document, 'removeEventListener').andCallThrough()
spyOn(element, 'resizeStopped').andCallThrough()
element.dispatchEvent(new MouseEvent('mousedown',
view: window
bubbles: true
button: 0
))
waitsFor ->
document.addEventListener.callCount is 2
runs ->
expect(element.resizeStopped.callCount).toBe 0
container.destroy()
expect(element.resizeStopped.callCount).toBe 1
expect(document.removeEventListener.callCount).toBe 2
it "does not throw an error when resized to fit content in a detached state", ->
container.getActivePane().splitRight()
element = getResizeElement(0)
element.remove()
expect(-> element.resizeToFitContent()).not.toThrow()
describe "pane resizing", ->
[leftPane, rightPane] = []
beforeEach ->
container = new PaneContainer(params)
leftPane = container.getActivePane()
rightPane = leftPane.splitRight()
describe "when pane:increase-size is triggered", ->
it "increases the size of the pane", ->
expect(leftPane.getFlexScale()).toBe 1
expect(rightPane.getFlexScale()).toBe 1
atom.commands.dispatch(leftPane.getElement(), 'pane:increase-size')
expect(leftPane.getFlexScale()).toBe 1.1
expect(rightPane.getFlexScale()).toBe 1
atom.commands.dispatch(rightPane.getElement(), 'pane:increase-size')
expect(leftPane.getFlexScale()).toBe 1.1
expect(rightPane.getFlexScale()).toBe 1.1
describe "when pane:decrease-size is triggered", ->
it "decreases the size of the pane", ->
expect(leftPane.getFlexScale()).toBe 1
expect(rightPane.getFlexScale()).toBe 1
atom.commands.dispatch(leftPane.getElement(), 'pane:decrease-size')
expect(leftPane.getFlexScale()).toBe 1/1.1
expect(rightPane.getFlexScale()).toBe 1
atom.commands.dispatch(rightPane.getElement(), 'pane:decrease-size')
expect(leftPane.getFlexScale()).toBe 1/1.1
expect(rightPane.getFlexScale()).toBe 1/1.1
describe "when only a single pane is present", ->
[singlePane] = []
beforeEach ->
container = new PaneContainer(params)
singlePane = container.getActivePane()
describe "when pane:increase-size is triggered", ->
it "does not increases the size of the pane", ->
expect(singlePane.getFlexScale()).toBe 1
atom.commands.dispatch(singlePane.getElement(), 'pane:increase-size')
expect(singlePane.getFlexScale()).toBe 1
atom.commands.dispatch(singlePane.getElement(), 'pane:increase-size')
expect(singlePane.getFlexScale()).toBe 1
describe "when pane:decrease-size is triggered", ->
it "does not decreases the size of the pane", ->
expect(singlePane.getFlexScale()).toBe 1
atom.commands.dispatch(singlePane.getElement(), 'pane:decrease-size')
expect(singlePane.getFlexScale()).toBe 1
atom.commands.dispatch(singlePane.getElement(), 'pane:decrease-size')
expect(singlePane.getFlexScale()).toBe 1