summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.html
blob: d1bcd60c67fcbd06cf7c5264c041fece71f55eef (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<!DOCTYPE html>
<!--
Copyright (C) 2015 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-view</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<script src="../../../bower_components/page/page.js"></script>
<script src="../../../scripts/util.js"></script>

<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="gr-diff-view.html">

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

<test-fixture id="basic">
  <template>
    <gr-diff-view></gr-diff-view>
  </template>
</test-fixture>

<test-fixture id="blank">
  <template>
    <div></div>
  </template>
</test-fixture>

<script>
  suite('gr-diff-view tests', function() {
    var element;
    var sandbox;

    setup(function() {
      sandbox = sinon.sandbox.create();

      stub('gr-rest-api-interface', {
        getLoggedIn: function() { return Promise.resolve(false); },
        getProjectConfig: function() { return Promise.resolve({}); },
        getDiffChangeDetail: function() { return Promise.resolve(null); },
        getChangeFiles: function() { return Promise.resolve({}); },
        saveFileReviewed: function() { return Promise.resolve(); },
      });
      element = fixture('basic');
    });

    teardown(function() {
      sandbox.restore();
    });

    test('toggle left diff with a hotkey', function() {
      var toggleLeftDiffStub = sandbox.stub(element.$.diff, 'toggleLeftDiff');
      MockInteractions.pressAndReleaseKeyOn(element, 65, 'shift', 'a');
      assert.isTrue(toggleLeftDiffStub.calledOnce);
    });

    test('keyboard shortcuts', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: 'PARENT',
        patchNum: '10',
      };
      element._change = {
        revisions: {
          a: {_number: 10},
        },
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';
      element.changeViewState.selectedFileIndex = 1;

      var showStub = sandbox.stub(page, 'show');
      MockInteractions.pressAndReleaseKeyOn(element, 85, null, 'u');
      assert(showStub.lastCall.calledWithExactly('/c/42/'),
          'Should navigate to /c/42/');

      MockInteractions.pressAndReleaseKeyOn(element, 221, null, ']');
      assert(showStub.lastCall.calledWithExactly('/c/42/10/wheatley.md'),
          'Should navigate to /c/42/10/wheatley.md');
      element._path = 'wheatley.md';
      assert.equal(element.changeViewState.selectedFileIndex, 2);

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/10/glados.txt'),
          'Should navigate to /c/42/10/glados.txt');
      element._path = 'glados.txt';
      assert.equal(element.changeViewState.selectedFileIndex, 1);

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/10/chell.go'),
          'Should navigate to /c/42/10/chell.go');
      element._path = 'chell.go';
      assert.equal(element.changeViewState.selectedFileIndex, 0);

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/'),
          'Should navigate to /c/42/');
      assert.equal(element.changeViewState.selectedFileIndex, 0);

      var showPrefsStub = sandbox.stub(element.$.prefsOverlay, 'open',
          function() { return Promise.resolve({}); });

      MockInteractions.pressAndReleaseKeyOn(element, 188, null, ',');
      assert(showPrefsStub.calledOnce);

      var scrollStub = sandbox.stub(element.$.cursor, 'moveToNextChunk');
      MockInteractions.pressAndReleaseKeyOn(element, 78, null, 'n');
      assert(scrollStub.calledOnce);

      scrollStub = sandbox.stub(element.$.cursor, 'moveToPreviousChunk');
      MockInteractions.pressAndReleaseKeyOn(element, 80, null, 'p');
      assert(scrollStub.calledOnce);

      scrollStub = sandbox.stub(element.$.cursor, 'moveToNextCommentThread');
      MockInteractions.pressAndReleaseKeyOn(element, 78, 'shift', 'n');
      assert(scrollStub.calledOnce);

      scrollStub = sandbox.stub(element.$.cursor,
          'moveToPreviousCommentThread');
      MockInteractions.pressAndReleaseKeyOn(element, 80, 'shift', 'p');
      assert(scrollStub.calledOnce);

      var computeContainerClassStub = sandbox.stub(element.$.diff,
          '_computeContainerClass');
      MockInteractions.pressAndReleaseKeyOn(element, 74, null, 'j');
      assert(computeContainerClassStub.lastCall.calledWithExactly(
          false, 'SIDE_BY_SIDE', true));

      MockInteractions.pressAndReleaseKeyOn(element, 27, null, 'esc');
      assert(computeContainerClassStub.lastCall.calledWithExactly(
          false, 'SIDE_BY_SIDE', false));
    });

    test('saving diff preferences', function() {
      var savePrefs = sandbox.stub(element, '_handlePrefsSave');
      var cancelPrefs = sandbox.stub(element, '_handlePrefsCancel');
      element.$.diffPreferences._handleSave();
      assert(savePrefs.calledOnce);
      assert(cancelPrefs.notCalled);
    });

    test('cancelling diff preferences', function() {
      var savePrefs = sandbox.stub(element, '_handlePrefsSave');
      var cancelPrefs = sandbox.stub(element, '_handlePrefsCancel');
      element.$.diffPreferences._handleCancel();
      assert(cancelPrefs.calledOnce);
      assert(savePrefs.notCalled);
    });

    test('keyboard shortcuts with patch range', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: '5',
        patchNum: '10',
      };
      element._change = {
        revisions: {
          a: {_number: 10},
        },
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';

      var showStub = sandbox.stub(page, 'show');

      MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
      assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' +
          'only work when the user is logged in.');
      assert.isNull(window.sessionStorage.getItem(
          'changeView.showReplyDialog'));

      element._loggedIn = true;
      MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
      assert.isTrue(element.changeViewState.showReplyDialog);

      assert(showStub.lastCall.calledWithExactly('/c/42/5..10'),
          'Should navigate to /c/42/5..10');

      MockInteractions.pressAndReleaseKeyOn(element, 85, null, 'u');
      assert(showStub.lastCall.calledWithExactly('/c/42/5..10'),
          'Should navigate to /c/42/5..10');

      MockInteractions.pressAndReleaseKeyOn(element, 221, null, ']');
      assert(showStub.lastCall.calledWithExactly('/c/42/5..10/wheatley.md'),
          'Should navigate to /c/42/5..10/wheatley.md');
      element._path = 'wheatley.md';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/5..10/glados.txt'),
          'Should navigate to /c/42/5..10/glados.txt');
      element._path = 'glados.txt';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/5..10/chell.go'),
          'Should navigate to /c/42/5..10/chell.go');
      element._path = 'chell.go';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/5..10'),
          'Should navigate to /c/42/5..10');
    });

    test('keyboard shortcuts with old patch number', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: 'PARENT',
        patchNum: '1',
      };
      element._change = {
        revisions: {
          a: {_number: 1},
          b: {_number: 2},
        },
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';

      var showStub = sandbox.stub(page, 'show');

      MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
      assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' +
          'only work when the user is logged in.');
      assert.isNull(window.sessionStorage.getItem(
          'changeView.showReplyDialog'));

      element._loggedIn = true;
      MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
      assert.isTrue(element.changeViewState.showReplyDialog);

      assert(showStub.lastCall.calledWithExactly('/c/42/1'),
          'Should navigate to /c/42/1');

      MockInteractions.pressAndReleaseKeyOn(element, 85, null, 'u');
      assert(showStub.lastCall.calledWithExactly('/c/42/1'),
          'Should navigate to /c/42/1');

      MockInteractions.pressAndReleaseKeyOn(element, 221, null, ']');
      assert(showStub.lastCall.calledWithExactly('/c/42/1/wheatley.md'),
          'Should navigate to /c/42/1/wheatley.md');
      element._path = 'wheatley.md';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/1/glados.txt'),
          'Should navigate to /c/42/1/glados.txt');
      element._path = 'glados.txt';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/1/chell.go'),
          'Should navigate to /c/42/1/chell.go');
      element._path = 'chell.go';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/1'),
          'Should navigate to /c/42/1');
    });

    test('go up to change via kb without change loaded', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: 'PARENT',
        patchNum: '1',
      };

      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';

      var showStub = sandbox.stub(page, 'show');

      MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
      assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' +
          'only work when the user is logged in.');
      assert.isNull(window.sessionStorage.getItem(
          'changeView.showReplyDialog'));

      element._loggedIn = true;
      MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
      assert.isTrue(element.changeViewState.showReplyDialog);

      assert(showStub.lastCall.calledWithExactly('/c/42/1'),
          'Should navigate to /c/42/1');

      MockInteractions.pressAndReleaseKeyOn(element, 85, null, 'u');
      assert(showStub.lastCall.calledWithExactly('/c/42/1'),
          'Should navigate to /c/42/1');

      MockInteractions.pressAndReleaseKeyOn(element, 221, null, ']');
      assert(showStub.lastCall.calledWithExactly('/c/42/1/wheatley.md'),
          'Should navigate to /c/42/1/wheatley.md');
      element._path = 'wheatley.md';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/1/glados.txt'),
          'Should navigate to /c/42/1/glados.txt');
      element._path = 'glados.txt';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/1/chell.go'),
          'Should navigate to /c/42/1/chell.go');
      element._path = 'chell.go';

      MockInteractions.pressAndReleaseKeyOn(element, 219, null, '[');
      assert(showStub.lastCall.calledWithExactly('/c/42/1'),
          'Should navigate to /c/42/1');
    });

    test('jump to file dropdown', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: 'PARENT',
        patchNum: '10',
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';
      flushAsynchronousOperations();
      var linkEls =
          Polymer.dom(element.root).querySelectorAll('.dropdown-content > a');
      assert.equal(linkEls.length, 3);
      assert.isFalse(linkEls[0].hasAttribute('selected'));
      assert.isTrue(linkEls[1].hasAttribute('selected'));
      assert.isFalse(linkEls[2].hasAttribute('selected'));
      assert.equal(linkEls[0].getAttribute('data-key-nav'), '[');
      assert.equal(linkEls[1].getAttribute('data-key-nav'), '');
      assert.equal(linkEls[2].getAttribute('data-key-nav'), ']');
      assert.equal(linkEls[0].getAttribute('href'), '/c/42/10/chell.go');
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/10/glados.txt');
      assert.equal(linkEls[2].getAttribute('href'), '/c/42/10/wheatley.md');

      assert.equal(element._computeFileDisplayName('/foo/bar/baz'),
          '/foo/bar/baz');
      assert.equal(element._computeFileDisplayName('/COMMIT_MSG'),
          'Commit message');
      assert.equal(element._computeFileDisplayName('/MERGE_LIST'),
          'Merge list');
    });

    test('jump to file dropdown with patch range', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: '5',
        patchNum: '10',
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';
      flushAsynchronousOperations();
      var linkEls =
          Polymer.dom(element.root).querySelectorAll('.dropdown-content > a');
      assert.equal(linkEls.length, 3);
      assert.isFalse(linkEls[0].hasAttribute('selected'));
      assert.isTrue(linkEls[1].hasAttribute('selected'));
      assert.isFalse(linkEls[2].hasAttribute('selected'));
      assert.equal(linkEls[0].getAttribute('data-key-nav'), '[');
      assert.equal(linkEls[1].getAttribute('data-key-nav'), '');
      assert.equal(linkEls[2].getAttribute('data-key-nav'), ']');
      assert.equal(linkEls[0].getAttribute('href'), '/c/42/5..10/chell.go');
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/5..10/glados.txt');
      assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/wheatley.md');
    });

    test('prev/up/next links', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: 'PARENT',
        patchNum: '10',
      };
      element._change = {
        revisions: {
          a: {_number: 10},
        },
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';
      flushAsynchronousOperations();
      var linkEls = Polymer.dom(element.root).querySelectorAll('.navLink');
      assert.equal(linkEls.length, 3);
      assert.equal(linkEls[0].getAttribute('href'), '/c/42/10/chell.go');
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/');
      assert.equal(linkEls[2].getAttribute('href'), '/c/42/10/wheatley.md');
      element._path = 'wheatley.md';
      flushAsynchronousOperations();
      assert.equal(linkEls[0].getAttribute('href'), '/c/42/10/glados.txt');
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/');
      assert.isFalse(linkEls[2].hasAttribute('href'));
      element._path = 'chell.go';
      flushAsynchronousOperations();
      assert.isFalse(linkEls[0].hasAttribute('href'));
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/');
      assert.equal(linkEls[2].getAttribute('href'), '/c/42/10/glados.txt');
      element._path = 'not_a_real_file';
      flushAsynchronousOperations();
      assert.equal(linkEls[0].getAttribute('href'), '/c/42/10/wheatley.md');
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/');
      assert.equal(linkEls[2].getAttribute('href'), '/c/42/10/chell.go');
    });

    test('download link', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: 'PARENT',
        patchNum: '10',
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';
      flushAsynchronousOperations();
      assert.equal(element.$$('.downloadLink').getAttribute('href'),
          '/changes/42/revisions/10/patch?zip&path=glados.txt');
    });

    test('prev/up/next links with patch range', function() {
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: '5',
        patchNum: '10',
      };
      element._change = {
        revisions: {
          a: {_number: 5},
          b: {_number: 10},
        },
      };
      element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
      element._path = 'glados.txt';
      flushAsynchronousOperations();
      var linkEls = Polymer.dom(element.root).querySelectorAll('.navLink');
      assert.equal(linkEls.length, 3);
      assert.equal(linkEls[0].getAttribute('href'), '/c/42/5..10/chell.go');
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/5..10');
      assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/wheatley.md');
      element._path = 'wheatley.md';
      flushAsynchronousOperations();
      assert.equal(linkEls[0].getAttribute('href'), '/c/42/5..10/glados.txt');
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/5..10');
      assert.isFalse(linkEls[2].hasAttribute('href'));
      element._path = 'chell.go';
      flushAsynchronousOperations();
      assert.isFalse(linkEls[0].hasAttribute('href'));
      assert.equal(linkEls[1].getAttribute('href'), '/c/42/5..10');
      assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/glados.txt');
    });

    test('file review status', function(done) {
      element._loggedIn = true;
      element._changeNum = '42';
      element._patchRange = {
        basePatchNum: '1',
        patchNum: '2',
      };
      element._fileList = ['/COMMIT_MSG'];
      element._path = '/COMMIT_MSG';
      var saveReviewedStub = sandbox.stub(element, '_saveReviewedState',
          function() { return Promise.resolve(); });

      flush(function() {
        var commitMsg = Polymer.dom(element.root).querySelector(
            'input[type="checkbox"]');

        assert.isTrue(commitMsg.checked);
        MockInteractions.tap(commitMsg);
        assert.isFalse(commitMsg.checked);
        assert.isTrue(saveReviewedStub.lastCall.calledWithExactly(false));

        MockInteractions.tap(commitMsg);
        assert.isTrue(commitMsg.checked);
        assert.isTrue(saveReviewedStub.lastCall.calledWithExactly(true));

        done();
      });
    });

    test('diff mode selector correctly toggles the diff', function() {
      var select = element.$.modeSelect;
      var diffDisplay = element.$.diff;
      element._userPrefs = {default_diff_view: 'SIDE_BY_SIDE'};

      // The mode selected in the view state reflects the selected option.
      assert.equal(element._getDiffViewMode(), select.value);

      // The mode selected in the view state reflects the view rednered in the
      // diff.
      assert.equal(select.value, diffDisplay.viewMode);

      // We will simulate a user change of the selected mode.
      var newMode = 'UNIFIED_DIFF';
      // Set the actual value of the select, and simulate the change event.
      select.value = newMode;
      element.fire('change', {}, {node: select});

      // Make sure the handler was called and the state is still coherent.
      assert.equal(element._getDiffViewMode(), newMode);
      assert.equal(element._getDiffViewMode(), select.value);
      assert.equal(element._getDiffViewMode(), diffDisplay.viewMode);
    });

    test('diff mode selector initializes from preferences', function() {
      var resolvePrefs;
      var prefsPromise = new Promise(function(resolve) {
        resolvePrefs = resolve;
      });
      var getPreferencesStub = sandbox.stub(element.$.restAPI, 'getPreferences',
          function() { return prefsPromise; });

      // Attach a new gr-diff-view so we can intercept the preferences fetch.
      var view = document.createElement('gr-diff-view');
      var select = view.$.modeSelect;
      fixture('blank').appendChild(view);
      flushAsynchronousOperations();

      // At this point the diff mode doesn't yet have the user's preference.
      assert.equal(select.value, 'SIDE_BY_SIDE');

      // Receive the overriding preference.
      resolvePrefs({default_diff_view: 'UNIFIED'});
      flushAsynchronousOperations();
      assert.equal(select.value, 'SIDE_BY_SIDE');
    });

    test('_loadHash', function() {
      assert.isNotOk(element.$.cursor.initialLineNumber);

      // Ignores invalid hashes:
      element._loadHash('not valid');
      assert.isNotOk(element.$.cursor.initialLineNumber);

      // Revision hash:
      element._loadHash('234');
      assert.equal(element.$.cursor.initialLineNumber, 234);
      assert.equal(element.$.cursor.side, 'right');

      // Base hash:
      element._loadHash('b345');
      assert.equal(element.$.cursor.initialLineNumber, 345);
      assert.equal(element.$.cursor.side, 'left');

      // GWT-style base hash:
      element._loadHash('a123');
      assert.equal(element.$.cursor.initialLineNumber, 123);
      assert.equal(element.$.cursor.side, 'left');
    });

    test('_shortenPath with long path should add ellipsis', function() {
      var path =
          'level1/level2/level3/level4/file.js';
      var shortenedPath = util.truncatePath(path);
      // The expected path is truncated with an ellipsis.
      var expectedPath = '\u2026/file.js';
      assert.equal(shortenedPath, expectedPath);

      var path = 'level2/file.js';
      var shortenedPath = util.truncatePath(path);
      assert.equal(shortenedPath, expectedPath);
    });

    test('_shortenPath with short path should not add ellipsis', function() {
      var path = 'file.js';
      var expectedPath = 'file.js';
      var shortenedPath = util.truncatePath(path);
      assert.equal(shortenedPath, expectedPath);
    });

    test('_onLineSelected', function() {
      var replaceStateStub = sandbox.stub(history, 'replaceState');
      var moveStub = sandbox.stub(element.$.cursor, 'moveToLineNumber');

      var e = {};
      var detail = {number: 123, side: 'right'};

      element._onLineSelected(e, detail);

      assert.isTrue(moveStub.called);
      assert.equal(moveStub.lastCall.args[0], detail.number);
      assert.equal(moveStub.lastCall.args[1], detail.side);

      assert.isTrue(replaceStateStub.called);
    });

    test('_getDiffURL encodes special characters', function() {
      var changeNum = 123;
      var patchRange = {basePatchNum: 123, patchNum: 456};
      var path = 'c++/cpp.cpp';
      assert.equal(element._getDiffURL(changeNum, patchRange, path),
          '/c/123/123..456/c%252B%252B/cpp.cpp');
    });

    test('_getDiffViewMode', function() {
      // No user prefs or change view state set.
      assert.equal(element._getDiffViewMode(), 'SIDE_BY_SIDE');

      // User prefs but no change view state set.
      element._userPrefs = {default_diff_view: 'UNIFIED_DIFF'};
      assert.equal(element._getDiffViewMode(), 'UNIFIED_DIFF');

      // User prefs and change view state set.
      element.changeViewState = {diffMode: 'SIDE_BY_SIDE'};
      assert.equal(element._getDiffViewMode(), 'SIDE_BY_SIDE');
    });

    suite('_loadCommentMap', function() {
      test('empty', function(done) {
        stub('gr-rest-api-interface', {
          getDiffRobotComments: function() { return Promise.resolve({}); },
          getDiffComments: function() { return Promise.resolve({}); },
        });
        element._loadCommentMap().then(function(map) {
          assert.equal(Object.keys(map).length, 0);
          done();
        });
      });

      test('paths in patch range', function(done) {
        stub('gr-rest-api-interface', {
          getDiffRobotComments: function() { return Promise.resolve({}); },
          getDiffComments: function() {
            return Promise.resolve({
              'path/to/file/one.cpp': [{patch_set: 3, message: 'lorem'}],
              'path-to/file/two.py': [{patch_set: 5, message: 'ipsum'}],
            });
          },
        });
        element._changeNum = '42';
        element._patchRange = {
          basePatchNum: '3',
          patchNum: '5',
        };
        element._loadCommentMap().then(function(map) {
          assert.deepEqual(Object.keys(map),
              ['path/to/file/one.cpp', 'path-to/file/two.py']);
          done();
        });
      });

      test('empty for paths outside patch range', function(done) {
        stub('gr-rest-api-interface', {
          getDiffRobotComments: function() { return Promise.resolve({}); },
          getDiffComments: function() {
            return Promise.resolve({
              'path/to/file/one.cpp': [{patch_set: 'PARENT', message: 'lorem'}],
              'path-to/file/two.py': [{patch_set: 2, message: 'ipsum'}],
            });
          },
        });
        element._changeNum = '42';
        element._patchRange = {
          basePatchNum: '3',
          patchNum: '5',
        };
        element._loadCommentMap().then(function(map) {
          assert.equal(Object.keys(map).length, 0);
          done();
        });
      });
    });

    suite('_computeCommentSkips', function() {
      test('empty file list', function() {
        var commentMap = {
          'path/one.jpg': true,
          'path/three.wav': true,
        };
        var path = 'path/two.m4v';
        var fileList = [];
        var result = element._computeCommentSkips(commentMap, fileList, path);
        assert.isNull(result.previous);
        assert.isNull(result.next);
      });

      test('finds skips', function() {
        var fileList = ['path/one.jpg', 'path/two.m4v', 'path/three.wav'];
        var path = fileList[1];
        var commentMap = {};
        commentMap[fileList[0]] = true;
        commentMap[fileList[1]] = false;
        commentMap[fileList[2]] = true;

        var result = element._computeCommentSkips(commentMap, fileList, path);
        assert.equal(result.previous, fileList[0]);
        assert.equal(result.next, fileList[2]);

        commentMap[fileList[1]] = true;

        result = element._computeCommentSkips(commentMap, fileList, path);
        assert.equal(result.previous, fileList[0]);
        assert.equal(result.next, fileList[2]);

        path = fileList[0];

        result = element._computeCommentSkips(commentMap, fileList, path);
        assert.isNull(result.previous);
        assert.equal(result.next, fileList[1]);

        path = fileList[2];

        result = element._computeCommentSkips(commentMap, fileList, path);
        assert.equal(result.previous, fileList[1]);
        assert.isNull(result.next);
      });
    });
  });
</script>