summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/diff/gr-diff-cursor/gr-diff-cursor_test.html
blob: 9668a54d48ef162eab53f25936d568bb6215d55f (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<!--
@license
Copyright (C) 2016 The Android Open Source Project

Licensed 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.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-diff-cursor</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<script src="../../../scripts/util.js"></script>

<link rel="import" href="../gr-diff/gr-diff.html">
<link rel="import" href="./gr-diff-cursor.html">
<link rel="import" href="../../shared/gr-rest-api-interface/mock-diff-response_test.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <mock-diff-response></mock-diff-response>
    <gr-diff></gr-diff>
    <gr-diff-cursor></gr-diff-cursor>
    <gr-rest-api-interface></gr-rest-api-interface>
  </template>
</test-fixture>

<script>
  suite('gr-diff-cursor tests', () => {
    let sandbox;
    let cursorElement;
    let diffElement;
    let mockDiffResponse;

    setup(done => {
      sandbox = sinon.sandbox.create();

      const fixtureElems = fixture('basic');
      mockDiffResponse = fixtureElems[0];
      diffElement = fixtureElems[1];
      cursorElement = fixtureElems[2];
      const restAPI = fixtureElems[3];

      // Register the diff with the cursor.
      cursorElement.push('diffs', diffElement);

      diffElement.loggedIn = false;
      diffElement.patchRange = {basePatchNum: 1, patchNum: 2};
      diffElement.comments = {left: [], right: []};
      const setupDone = () => {
        cursorElement._updateStops();
        cursorElement.moveToFirstChunk();
        diffElement.removeEventListener('render', setupDone);
        done();
      };
      diffElement.addEventListener('render', setupDone);

      restAPI.getDiffPreferences().then(prefs => {
        diffElement.prefs = prefs;
        diffElement.diff = mockDiffResponse.diffResponse;
      });
    });

    teardown(() => sandbox.restore());

    test('diff cursor functionality (side-by-side)', () => {
      // The cursor has been initialized to the first delta.
      assert.isOk(cursorElement.diffRow);

      const firstDeltaRow = diffElement.$$('.section.delta .diff-row');
      assert.equal(cursorElement.diffRow, firstDeltaRow);

      cursorElement.moveDown();

      assert.notEqual(cursorElement.diffRow, firstDeltaRow);
      assert.equal(cursorElement.diffRow, firstDeltaRow.nextSibling);

      cursorElement.moveUp();

      assert.notEqual(cursorElement.diffRow, firstDeltaRow.nextSibling);
      assert.equal(cursorElement.diffRow, firstDeltaRow);
    });

    test('cursor scroll behavior', () => {
      cursorElement._handleDiffRenderStart();
      assert.equal(cursorElement._scrollBehavior, 'keep-visible');
      assert.isTrue(cursorElement._focusOnMove);

      cursorElement._handleWindowScroll();
      assert.equal(cursorElement._scrollBehavior, 'never');
      assert.isFalse(cursorElement._focusOnMove);

      cursorElement.handleDiffUpdate();
      assert.equal(cursorElement._scrollBehavior, 'keep-visible');
      assert.isTrue(cursorElement._focusOnMove);
    });

    suite('unified diff', () => {
      setup(done => {
        // We must allow the diff to re-render after setting the viewMode.
        const renderHandler = function() {
          diffElement.removeEventListener('render', renderHandler);
          cursorElement.reInitCursor();
          done();
        };
        diffElement.addEventListener('render', renderHandler);
        diffElement.viewMode = 'UNIFIED_DIFF';
      });

      test('diff cursor functionality (unified)', () => {
        // The cursor has been initialized to the first delta.
        assert.isOk(cursorElement.diffRow);

        let firstDeltaRow = diffElement.$$('.section.delta .diff-row');
        assert.equal(cursorElement.diffRow, firstDeltaRow);

        firstDeltaRow = diffElement.$$('.section.delta .diff-row');
        assert.equal(cursorElement.diffRow, firstDeltaRow);

        cursorElement.moveDown();

        assert.notEqual(cursorElement.diffRow, firstDeltaRow);
        assert.equal(cursorElement.diffRow, firstDeltaRow.nextSibling);

        cursorElement.moveUp();

        assert.notEqual(cursorElement.diffRow, firstDeltaRow.nextSibling);
        assert.equal(cursorElement.diffRow, firstDeltaRow);
      });
    });

    test('cursor side functionality', () => {
      // The side only applies to side-by-side mode, which should be the default
      // mode.
      assert.equal(diffElement.viewMode, 'SIDE_BY_SIDE');

      const firstDeltaSection = diffElement.$$('.section.delta');
      const firstDeltaRow = firstDeltaSection.querySelector('.diff-row');

      // Because the first delta in this diff is on the right, it should be set
      // to the right side.
      assert.equal(cursorElement.side, 'right');
      assert.equal(cursorElement.diffRow, firstDeltaRow);
      const firstIndex = cursorElement.$.cursorManager.index;

      // Move the side to the left. Because this delta only has a right side, we
      // should be moved up to the previous line where there is content on the
      // right. The previous row is part of the previous section.
      cursorElement.moveLeft();

      assert.equal(cursorElement.side, 'left');
      assert.notEqual(cursorElement.diffRow, firstDeltaRow);
      assert.equal(cursorElement.$.cursorManager.index, firstIndex - 1);
      assert.equal(cursorElement.diffRow.parentElement,
          firstDeltaSection.previousSibling);

      // If we move down, we should skip everything in the first delta because
      // we are on the left side and the first delta has no content on the left.
      cursorElement.moveDown();

      assert.equal(cursorElement.side, 'left');
      assert.notEqual(cursorElement.diffRow, firstDeltaRow);
      assert.isTrue(cursorElement.$.cursorManager.index > firstIndex);
      assert.equal(cursorElement.diffRow.parentElement,
          firstDeltaSection.nextSibling);
    });

    test('chunk skip functionality', () => {
      const chunks = Polymer.dom(diffElement.root).querySelectorAll(
          '.section.delta');
      const indexOfChunk = function(chunk) {
        return Array.prototype.indexOf.call(chunks, chunk);
      };

      // We should be initialized to the first chunk. Since this chunk only has
      // content on the right side, our side should be right.
      let currentIndex = indexOfChunk(cursorElement.diffRow.parentElement);
      assert.equal(currentIndex, 0);
      assert.equal(cursorElement.side, 'right');

      // Move to the next chunk.
      cursorElement.moveToNextChunk();

      // Since this chunk only has content on the left side. we should have been
      // automatically mvoed over.
      const previousIndex = currentIndex;
      currentIndex = indexOfChunk(cursorElement.diffRow.parentElement);
      assert.equal(currentIndex, previousIndex + 1);
      assert.equal(cursorElement.side, 'left');
    });

    test('initialLineNumber disabled', done => {
      const moveToNumStub = sandbox.stub(cursorElement, 'moveToLineNumber');
      const moveToChunkStub = sandbox.stub(cursorElement, 'moveToFirstChunk');

      function renderHandler() {
        diffElement.removeEventListener('render', renderHandler);
        assert.isFalse(moveToNumStub.called);
        assert.isTrue(moveToChunkStub.called);
        done();
      }
      diffElement.addEventListener('render', renderHandler);
      diffElement._diffChanged(mockDiffResponse.diffResponse);
    });

    test('initialLineNumber enabled', done => {
      const moveToNumStub = sandbox.stub(cursorElement, 'moveToLineNumber');
      const moveToChunkStub = sandbox.stub(cursorElement, 'moveToFirstChunk');

      function renderHandler() {
        diffElement.removeEventListener('render', renderHandler);
        assert.isFalse(moveToChunkStub.called);
        assert.isTrue(moveToNumStub.called);
        assert.equal(moveToNumStub.lastCall.args[0], 10);
        assert.equal(moveToNumStub.lastCall.args[1], 'right');
        done();
      }
      diffElement.addEventListener('render', renderHandler);

      cursorElement.initialLineNumber = 10;
      cursorElement.side = 'right';

      diffElement._diffChanged(mockDiffResponse.diffResponse);
    });

    test('getAddress', () => {
      // It should initialize to the first chunk: line 5 of the revision.
      assert.deepEqual(cursorElement.getAddress(),
          {leftSide: false, number: 5});

      // Revision line 4 is up.
      cursorElement.moveUp();
      assert.deepEqual(cursorElement.getAddress(),
          {leftSide: false, number: 4});

      // Base line 4 is left.
      cursorElement.moveLeft();
      assert.deepEqual(cursorElement.getAddress(), {leftSide: true, number: 4});

      // Moving to the next chunk takes it back to the start.
      cursorElement.moveToNextChunk();
      assert.deepEqual(cursorElement.getAddress(),
          {leftSide: false, number: 5});

      // The following chunk is a removal starting on line 10 of the base.
      cursorElement.moveToNextChunk();
      assert.deepEqual(cursorElement.getAddress(),
          {leftSide: true, number: 10});

      // Should be null if there is no selection.
      cursorElement.$.cursorManager.unsetCursor();
      assert.isNotOk(cursorElement.getAddress());
    });

    test('_findRowByNumberAndFile', () => {
      // Get the first ab row after the first chunk.
      const row = Polymer.dom(diffElement.root).querySelectorAll('tr')[8];

      // It should be line 8 on the right, but line 5 on the left.
      assert.equal(cursorElement._findRowByNumberAndFile(8, 'right'), row);
      assert.equal(cursorElement._findRowByNumberAndFile(5, 'left'), row);
    });

    test('expand context updates stops', done => {
      sandbox.spy(cursorElement, 'handleDiffUpdate');
      MockInteractions.tap(diffElement.$$('.showContext'));
      flush(() => {
        assert.isTrue(cursorElement.handleDiffUpdate.called);
        done();
      });
    });
  });
</script>