summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/frame/browser_controls_test.cc
blob: 5bb535b1c29acea1561ed607b02a07d0f195cd1e (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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
/*
 * Copyright (C) 2015 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "third_party/blink/renderer/core/frame/browser_controls.h"

#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/input/web_coalesced_input_event.h"
#include "third_party/blink/public/platform/web_url_loader_mock_factory.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/frame/frame_test_helpers.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/visual_viewport.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/geometry/dom_rect.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/paint/compositing/composited_layer_mapping.h"
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
#include "third_party/blink/renderer/core/testing/scoped_mock_overlay_scrollbars.h"
#include "third_party/blink/renderer/core/testing/sim/sim_request.h"
#include "third_party/blink/renderer/core/testing/sim/sim_test.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"

namespace blink {

// These tests cover browser controls scrolling on main-thread.
// The animation for completing a partial show/hide is done in compositor so
// it is not covered here.
class BrowserControlsTest : public testing::Test,
                            public ScopedMockOverlayScrollbars {
 public:
  BrowserControlsTest() : base_url_("http://www.test.com/") {
    RegisterMockedHttpURLLoad("large-div.html");
    RegisterMockedHttpURLLoad("overflow-scrolling.html");
    RegisterMockedHttpURLLoad("iframe-scrolling.html");
    RegisterMockedHttpURLLoad("iframe-scrolling-inner.html");
    RegisterMockedHttpURLLoad("percent-height.html");
    RegisterMockedHttpURLLoad("vh-height.html");
    RegisterMockedHttpURLLoad("vh-height-width-800.html");
    RegisterMockedHttpURLLoad("95-vh.html");
    RegisterMockedHttpURLLoad("vh-height-width-800-extra-wide.html");
  }

  ~BrowserControlsTest() override {
    platform_->GetURLLoaderMockFactory()
        ->UnregisterAllURLsAndClearMemoryCache();
  }

  WebViewImpl* Initialize(const std::string& page_name = "large-div.html") {
    // Load a page with large body and set viewport size to 400x400 to ensure
    // main frame is scrollable.
    helper_.InitializeAndLoad(base_url_ + page_name, nullptr, nullptr, nullptr,
                              &ConfigureSettings);

    GetWebView()->MainFrameWidget()->Resize(IntSize(400, 400));
    return GetWebView();
  }

  static void ConfigureSettings(WebSettings* settings) {
    settings->SetJavaScriptEnabled(true);
    settings->SetPreferCompositingToLCDTextEnabled(true);
    // Android settings
    settings->SetViewportEnabled(true);
    settings->SetViewportMetaEnabled(true);
    settings->SetShrinksViewportContentToFit(true);
    settings->SetMainFrameResizesAreOrientationChanges(true);
  }

  void RegisterMockedHttpURLLoad(const std::string& file_name) {
    url_test_helpers::RegisterMockedURLLoadFromBase(
        WebString::FromUTF8(base_url_), test::CoreTestDataPath(),
        WebString::FromUTF8(file_name));
  }

  WebCoalescedInputEvent GenerateEvent(WebInputEvent::Type type,
                                       int delta_x = 0,
                                       int delta_y = 0) {
    WebGestureEvent event(type, WebInputEvent::kNoModifiers,
                          WebInputEvent::GetStaticTimeStampForTests(),
                          WebGestureDevice::kTouchscreen);
    event.SetPositionInWidget(FloatPoint(100, 100));
    if (type == WebInputEvent::Type::kGestureScrollUpdate) {
      event.data.scroll_update.delta_x = delta_x;
      event.data.scroll_update.delta_y = delta_y;
    } else if (type == WebInputEvent::Type::kGestureScrollBegin) {
      event.data.scroll_begin.delta_x_hint = delta_x;
      event.data.scroll_begin.delta_y_hint = delta_y;
    }
    return WebCoalescedInputEvent(event, ui::LatencyInfo());
  }

  void VerticalScroll(float delta_y) {
    GetWebView()->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, delta_y));
    GetWebView()->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, delta_y));
    GetWebView()->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  }

  Element* GetElementById(const WebString& id) {
    return static_cast<Element*>(
        GetWebView()->MainFrameImpl()->GetDocument().GetElementById(id));
  }

  WebViewImpl* GetWebView() const { return helper_.GetWebView(); }
  LocalFrame* GetFrame() const { return helper_.LocalMainFrame()->GetFrame(); }
  VisualViewport& GetVisualViewport() const {
    return helper_.GetWebView()->GetPage()->GetVisualViewport();
  }

  void UpdateAllLifecyclePhases() {
    GetWebView()->MainFrameWidget()->UpdateAllLifecyclePhases(
        DocumentUpdateReason::kTest);
  }

 private:
  ScopedTestingPlatformSupport<TestingPlatformSupport> platform_;
  std::string base_url_;
  frame_test_helpers::WebViewHelper helper_;
};

class BrowserControlsSimTest : public SimTest {
 public:
  BrowserControlsSimTest() {}

  void SetUp() override {
    SimTest::SetUp();

    // Use settings that resemble the Android configuration.
    WebView().GetSettings()->SetViewportEnabled(true);
    WebView().GetSettings()->SetPreferCompositingToLCDTextEnabled(true);
    WebView().GetSettings()->SetViewportMetaEnabled(true);
    WebView().GetSettings()->SetViewportEnabled(true);
    WebView().GetSettings()->SetMainFrameResizesAreOrientationChanges(true);
    WebView().GetSettings()->SetShrinksViewportContentToFit(true);
    WebView().SetDefaultPageScaleLimits(0.25f, 5);
    Compositor().layer_tree_host()->UpdateBrowserControlsState(
        cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown,
        false);
    WebView().ResizeWithBrowserControls(WebSize(412, 604), 56.f, 50.f, true);
  }

  WebCoalescedInputEvent GenerateEvent(WebInputEvent::Type type,
                                       int delta_x = 0,
                                       int delta_y = 0) {
    WebGestureEvent event(type, WebInputEvent::kNoModifiers,
                          WebInputEvent::GetStaticTimeStampForTests(),
                          WebGestureDevice::kTouchscreen);
    event.SetPositionInWidget(FloatPoint(100, 100));
    if (type == WebInputEvent::Type::kGestureScrollUpdate) {
      event.data.scroll_update.delta_x = delta_x;
      event.data.scroll_update.delta_y = delta_y;
    } else if (type == WebInputEvent::Type::kGestureScrollBegin) {
      event.data.scroll_begin.delta_x_hint = delta_x;
      event.data.scroll_begin.delta_y_hint = delta_y;
    }
    return WebCoalescedInputEvent(event, ui::LatencyInfo());
  }

  void VerticalScroll(float delta_y) {
    WebView().MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, delta_y));
    WebView().MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, delta_y));
    WebView().MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  }
};

// Disable these tests on Mac OSX until further investigation.
// Local build on Mac is OK but the bot fails. This is not an issue as
// Browser Controls are currently only used on Android.
#if defined(OS_MACOSX)
#define MAYBE(test) DISABLED_##test
#else
#define MAYBE(test) test
#endif

// Scrolling down should hide browser controls.
TEST_F(BrowserControlsTest, MAYBE(HideOnScrollDown)) {
  WebViewImpl* web_view = Initialize();
  // initialize browser controls to be shown.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());

  // Browser controls should be scrolled partially and page should not scroll.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -25.f));
  EXPECT_FLOAT_EQ(25.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 0),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Browser controls should consume 25px and become hidden. Excess scroll
  // should be
  // consumed by the page.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -40.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 15),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Only page should consume scroll
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -20.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 35),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Scrolling down should hide bottom browser controls.
TEST_F(BrowserControlsTest, MAYBE(HideBottomControlsOnScrollDown)) {
  WebViewImpl* web_view = Initialize();
  // initialize browser controls to be shown.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 0,
                                      50.f, true);
  web_view->GetBrowserControls().SetShownRatio(0.0, 1);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  // Bottom controls and page content should both scroll and there should be
  // no content offset.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -25.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_FLOAT_EQ(0.5f, web_view->GetBrowserControls().BottomShownRatio());
  EXPECT_EQ(ScrollOffset(0, 25.f),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Browser controls should become completely hidden.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -40.f));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().BottomShownRatio());
  EXPECT_EQ(ScrollOffset(0, 65.f),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Scrolling up should show browser controls.
TEST_F(BrowserControlsTest, MAYBE(ShowOnScrollUp)) {
  WebViewImpl* web_view = Initialize();
  // initialize browser controls to be hidden.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, false);
  web_view->GetBrowserControls().SetShownRatio(0, 0);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 10.f));
  EXPECT_FLOAT_EQ(10.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 0),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 50.f));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 0),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Scrolling up should show the bottom browser controls.
TEST_F(BrowserControlsTest, MAYBE(ShowBottomControlsOnScrollUp)) {
  WebViewImpl* web_view = Initialize();
  // initialize browser controls to be hidden.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 0,
                                      50.f, false);
  web_view->GetBrowserControls().SetShownRatio(0, 0);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  // Allow some space to scroll up.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -50.f));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 25.f));
  EXPECT_FLOAT_EQ(0.5f, web_view->GetBrowserControls().BottomShownRatio());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_FLOAT_EQ(1.f, web_view->GetBrowserControls().BottomShownRatio());
  EXPECT_EQ(ScrollOffset(0, 25),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Scrolling up after previous scroll downs should cause browser controls to be
// shown only after all previously scrolled down amount is compensated.
TEST_F(BrowserControlsTest, MAYBE(ScrollDownThenUp)) {
  WebViewImpl* web_view = Initialize();
  // initialize browser controls to be shown and position page at 100px.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  GetFrame()->View()->GetScrollableArea()->SetScrollOffset(
      ScrollOffset(0, 100), mojom::blink::ScrollType::kProgrammatic);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());

  // Scroll down to completely hide browser controls. Excess deltaY (100px)
  // should be consumed by the page.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -150.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 200),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Scroll up and ensure the browser controls does not move until we recover
  // 100px previously scrolled.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 40.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 160),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 60.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 100),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Now we have hit the threshold so further scroll up should be consumed by
  // browser controls.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 30.f));
  EXPECT_FLOAT_EQ(30.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 100),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Once top control is fully shown then page should consume any excess scroll.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 70.f));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Scrolling down should always cause visible browser controls to start hiding
// even if we have been scrolling up previously.
TEST_F(BrowserControlsTest, MAYBE(ScrollUpThenDown)) {
  WebViewImpl* web_view = Initialize();
  // initialize browser controls to be hidden and position page at 100px.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, false);
  web_view->GetBrowserControls().SetShownRatio(0, 0);
  GetFrame()->View()->GetScrollableArea()->SetScrollOffset(
      ScrollOffset(0, 100), mojom::blink::ScrollType::kProgrammatic);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  // Scroll up to completely show browser controls. Excess deltaY (50px) should
  // be consumed by the page.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 100.f));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Scroll down and ensure only browser controls is scrolled
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -40.f));
  EXPECT_FLOAT_EQ(10.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -60.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 100),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Browser controls should not consume horizontal scroll.
TEST_F(BrowserControlsTest, MAYBE(HorizontalScroll)) {
  WebViewImpl* web_view = Initialize();
  // initialize browser controls to be shown.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());

  // Browser controls should not consume horizontal scroll.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, -110.f, -100.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(110, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, -40.f, 0));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(150, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Page scale should not impact browser controls scrolling
TEST_F(BrowserControlsTest, MAYBE(PageScaleHasNoImpact)) {
  WebViewImpl* web_view = Initialize();
  GetWebView()->SetDefaultPageScaleLimits(0.25f, 5);
  web_view->SetPageScaleFactor(2.0);

  // Initialize browser controls to be shown.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());

  // Browser controls should be scrolled partially and page should not scroll.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -20.f));
  EXPECT_FLOAT_EQ(30.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 0),
            GetFrame()->View()->GetScrollableArea()->GetScrollOffset());

  // Browser controls should consume 30px and become hidden. Excess scroll
  // should be consumed by the page at 2x scale.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -70.f));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 20),
            GetFrame()->View()->GetScrollableArea()->GetScrollOffset());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));

  // Change page scale and test.
  web_view->SetPageScaleFactor(0.5);

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 20),
            GetFrame()->View()->GetScrollableArea()->GetScrollOffset());

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 50.f));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 20),
            GetFrame()->View()->GetScrollableArea()->GetScrollOffset());

  // At 0.5x scale scrolling 10px should take us to the top of the page.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 10.f));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 0),
            GetFrame()->View()->GetScrollableArea()->GetScrollOffset());
}

// Some scroll deltas result in a shownRatio that can't be realized in a
// floating-point number. Make sure that if the browser controls aren't fully
// scrolled, scrollBy doesn't return any excess delta. i.e. There should be no
// slippage between the content and browser controls.
TEST_F(BrowserControlsTest, MAYBE(FloatingPointSlippage)) {
  WebViewImpl* web_view = Initialize();
  GetWebView()->SetDefaultPageScaleLimits(0.25f, 5);
  web_view->SetPageScaleFactor(2.0);

  // Initialize browser controls to be shown.
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);

  web_view->GetBrowserControls().ScrollBegin();
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());

  // This will result in a 20px scroll to the browser controls so the show ratio
  // will be 30/50 == 0.6 which is not representible in a float. Make sure
  // that scroll still consumes the whole delta.
  FloatSize remaining_delta =
      web_view->GetBrowserControls().ScrollBy(FloatSize(0, 10));
  EXPECT_EQ(0, remaining_delta.Height());
}

// Scrollable subregions should scroll before browser controls
TEST_F(BrowserControlsTest, MAYBE(ScrollableSubregionScrollFirst)) {
  WebViewImpl* web_view = Initialize("overflow-scrolling.html");
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  GetFrame()->View()->GetScrollableArea()->SetScrollOffset(
      ScrollOffset(0, 50), mojom::blink::ScrollType::kProgrammatic);

  // Test scroll down
  // A full scroll down should scroll the overflow div first but browser
  // controls and main frame should not scroll.
  VerticalScroll(-800.f);
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Now scroll down should start hiding browser controls but main frame
  // should not scroll.
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, -40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -40.f));
  EXPECT_FLOAT_EQ(10.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Continued scroll down should scroll down the main frame
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 80),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Test scroll up
  // A full scroll up should scroll overflow div first
  VerticalScroll(800.f);
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 80),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Now scroll up should start showing browser controls but main frame
  // should not scroll.
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, 40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 40.f));
  EXPECT_FLOAT_EQ(40.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 80),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Continued scroll up scroll up the main frame
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Scrollable iframes should scroll before browser controls
TEST_F(BrowserControlsTest, MAYBE(ScrollableIframeScrollFirst)) {
  WebViewImpl* web_view = Initialize("iframe-scrolling.html");
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  GetFrame()->View()->GetScrollableArea()->SetScrollOffset(
      ScrollOffset(0, 50), mojom::blink::ScrollType::kProgrammatic);

  // Test scroll down
  // A full scroll down should scroll the iframe first but browser controls and
  // main frame should not scroll.
  VerticalScroll(-800.f);
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Now scroll down should start hiding browser controls but main frame
  // should not scroll.
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, -40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -40.f));
  EXPECT_FLOAT_EQ(10.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Continued scroll down should scroll down the main frame
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 80),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Test scroll up
  // A full scroll up should scroll iframe first
  VerticalScroll(800.f);
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 80),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Now scroll up should start showing browser controls but main frame
  // should not scroll.
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, 40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 40.f));
  EXPECT_FLOAT_EQ(40.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 80),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  // Continued scroll up scroll up the main frame
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 40.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 50),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());
}

// Browser controls visibility should remain consistent when height is changed.
TEST_F(BrowserControlsTest, MAYBE(HeightChangeMaintainsVisibility)) {
  WebViewImpl* web_view = Initialize();
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 20.f,
                                      0, false);
  web_view->GetBrowserControls().SetShownRatio(0, 0);

  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 20.f,
                                      0, false);
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 40.f,
                                      0, false);
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  // Scroll up to show browser controls.
  VerticalScroll(40.f);
  EXPECT_FLOAT_EQ(40.f, web_view->GetBrowserControls().ContentOffset());

  // Changing height of a fully shown browser controls should correctly adjust
  // content offset
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 30.f,
                                      0, false);
  EXPECT_FLOAT_EQ(30.f, web_view->GetBrowserControls().ContentOffset());
}

// Zero delta should not have any effect on browser controls.
TEST_F(BrowserControlsTest, MAYBE(ZeroHeightMeansNoEffect)) {
  WebViewImpl* web_view = Initialize();
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 0, 0,
                                      false);
  web_view->GetBrowserControls().SetShownRatio(0, 0);
  GetFrame()->View()->GetScrollableArea()->SetScrollOffset(
      ScrollOffset(0, 100), mojom::blink::ScrollType::kProgrammatic);

  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  VerticalScroll(20.f);
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 80),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  VerticalScroll(-30.f);
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 110),
            GetFrame()->View()->LayoutViewport()->GetScrollOffset());

  web_view->GetBrowserControls().SetShownRatio(1, 1);
  EXPECT_FLOAT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
}

// Browser controls should not hide when scrolling up past limit
TEST_F(BrowserControlsTest, MAYBE(ScrollUpPastLimitDoesNotHide)) {
  WebViewImpl* web_view = Initialize();
  // Initialize browser controls to be shown
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  // Use 2x scale so that both visual viewport and frameview are scrollable
  web_view->SetPageScaleFactor(2.0);

  // Fully scroll frameview but visualviewport remains scrollable
  web_view->MainFrameImpl()->SetScrollOffset(WebSize(0, 10000));
  GetVisualViewport().SetLocation(FloatPoint(0, 0));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, -10.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -10.f));
  EXPECT_FLOAT_EQ(40, web_view->GetBrowserControls().ContentOffset());

  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(50, web_view->GetBrowserControls().ContentOffset());

  web_view->GetBrowserControls().SetShownRatio(1, 1);
  // Fully scroll visual veiwport but frameview remains scrollable
  web_view->MainFrameImpl()->SetScrollOffset(WebSize(0, 0));
  GetVisualViewport().SetLocation(FloatPoint(0, 10000));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, -20.f));
  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -20.f));
  EXPECT_FLOAT_EQ(30, web_view->GetBrowserControls().ContentOffset());

  GetWebView()->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(50, web_view->GetBrowserControls().ContentOffset());

  web_view->GetBrowserControls().SetShownRatio(1, 1);
  // Fully scroll both frameview and visual viewport
  web_view->MainFrameImpl()->SetScrollOffset(WebSize(0, 10000));
  GetVisualViewport().SetLocation(FloatPoint(0, 10000));
  VerticalScroll(-30.f);
  // Browser controls should not move because neither frameview nor visual
  // viewport
  // are scrollable
  EXPECT_FLOAT_EQ(50.f, web_view->GetBrowserControls().ContentOffset());
}

// Browser controls should honor its constraints
TEST_F(BrowserControlsSimTest, MAYBE(StateConstraints)) {
  SimRequest request("https://example.com/test.html", "text/html");
  LoadURL("https://example.com/test.html");
  request.Complete(R"HTML(
        <!DOCTYPE html>
        <meta name="viewport" content="width=device-width">
        <style>
          body {
            margin: 0;
            height: 1000px;
          }
        </style>
      )HTML");
  Compositor().BeginFrame();

  WebView().ResizeWithBrowserControls(WebSize(400, 400), 50.f, 0, false);
  Compositor().BeginFrame();

  GetDocument().View()->GetScrollableArea()->SetScrollOffset(
      ScrollOffset(0, 100), mojom::blink::ScrollType::kProgrammatic);
  // Setting permitted state should change the content offset to match the
  // constraint.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kShown, cc::BrowserControlsState::kShown,
      false);
  Compositor().BeginFrame();
  EXPECT_FLOAT_EQ(50.f, WebView().GetBrowserControls().ContentOffset());

  WebView().ResizeWithBrowserControls(WebSize(400, 400), 50.f, 50.f, false);
  Compositor().BeginFrame();
  // Bottom controls shouldn't affect the content offset.
  EXPECT_FLOAT_EQ(50.f, WebView().GetBrowserControls().ContentOffset());

  // Only shown state is permitted so controls cannot hide.
  VerticalScroll(-20.f);
  EXPECT_FLOAT_EQ(50, WebView().GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 120),
            GetDocument().View()->LayoutViewport()->GetScrollOffset());

  // Setting permitted state should change content offset to match the
  // constraint.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kHidden,
      false);
  Compositor().BeginFrame();
  EXPECT_FLOAT_EQ(0, WebView().GetBrowserControls().ContentOffset());

  // Only hidden state is permitted so controls cannot show
  VerticalScroll(30.f);
  EXPECT_FLOAT_EQ(0, WebView().GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 90),
            GetDocument().View()->LayoutViewport()->GetScrollOffset());

  // Setting permitted state to "both" should not change content offset.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kBoth, false);
  Compositor().BeginFrame();
  EXPECT_FLOAT_EQ(0, WebView().GetBrowserControls().ContentOffset());

  // Both states are permitted so controls can either show or hide
  VerticalScroll(50.f);
  EXPECT_FLOAT_EQ(50, WebView().GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 90),
            GetDocument().View()->LayoutViewport()->GetScrollOffset());

  VerticalScroll(-50.f);
  EXPECT_FLOAT_EQ(0, WebView().GetBrowserControls().ContentOffset());
  EXPECT_EQ(ScrollOffset(0, 90),
            GetDocument().View()->LayoutViewport()->GetScrollOffset());

  // Setting permitted state to "both" should not change an in-flight offset.
  WebView().MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin, 0, 20.f));
  WebView().MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 20.f));
  EXPECT_FLOAT_EQ(20, WebView().GetBrowserControls().ContentOffset());

  WebView().MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_FLOAT_EQ(0, WebView().GetBrowserControls().ContentOffset());
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kBoth, false);
  Compositor().BeginFrame();
  EXPECT_FLOAT_EQ(0, WebView().GetBrowserControls().ContentOffset());

  // Setting just the constraint should affect the content offset.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kBoth,
      false);
  Compositor().BeginFrame();
  EXPECT_FLOAT_EQ(0, WebView().GetBrowserControls().ContentOffset());

  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kShown, cc::BrowserControlsState::kBoth, false);
  Compositor().BeginFrame();
  EXPECT_FLOAT_EQ(50, WebView().GetBrowserControls().ContentOffset());
}

// Ensure that browser controls do not affect the layout by showing and hiding
// except for position: fixed elements.
TEST_F(BrowserControlsTest, MAYBE(DontAffectLayoutHeight)) {
  // Initialize with the browser controls showing.
  WebViewImpl* web_view = Initialize("percent-height.html");
  web_view->ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(100.f, web_view->GetBrowserControls().ContentOffset());

  // When the browser controls are showing, there's 300px for the layout height
  // so
  // 50% should result in both the position:fixed and position: absolute divs
  // having 150px of height.
  Element* abs_pos = GetElementById(WebString::FromUTF8("abs"));
  Element* fixed_pos = GetElementById(WebString::FromUTF8("fixed"));
  EXPECT_FLOAT_EQ(150.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(150.f, fixed_pos->getBoundingClientRect()->height());

  // The layout size on the LocalFrameView should not include the browser
  // controls.
  EXPECT_EQ(300, GetFrame()->View()->GetLayoutSize().Height());

  // Hide the browser controls.
  VerticalScroll(-100.f);
  web_view->ResizeWithBrowserControls(WebSize(400, 400), 100.f, 0, false);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  // Hiding the browser controls shouldn't change the height of the initial
  // containing block for non-position: fixed. Position: fixed however should
  // use the entire height of the viewport however.
  EXPECT_FLOAT_EQ(150.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());

  // The layout size should not change as a result of browser controls hiding.
  EXPECT_EQ(300, GetFrame()->View()->GetLayoutSize().Height());
}

// Ensure that browser controls do not affect the layout by showing and hiding
// except for position: fixed elements.
TEST_F(BrowserControlsSimTest, MAYBE(AffectLayoutHeightWhenConstrained)) {
  SimRequest request("https://example.com/test.html", "text/html");
  LoadURL("https://example.com/test.html");
  request.Complete(R"HTML(
        <!DOCTYPE html>
          <style>
            #abs {
              position: absolute;
              left: 0px;
              top: 0px;
              width: 100px;
              height: 50%;
            }

            #fixed {
              position: fixed;
              right: 0px;
              top: 0px;
              width: 100px;
              height: 50%;
            }

            #spacer {
              height: 1000px;
            }
          </style>
        <div id="abs"></div>
        <div id="fixed"></div>
        <div id="spacer"></div>
      )HTML");
  Compositor().BeginFrame();

  WebView().ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown, false);
  Compositor().BeginFrame();

  Element* abs_pos = GetDocument().getElementById(WebString::FromUTF8("abs"));
  Element* fixed_pos =
      GetDocument().getElementById(WebString::FromUTF8("fixed"));

  ASSERT_EQ(100.f, WebView().GetBrowserControls().ContentOffset());

  // Hide the browser controls.
  VerticalScroll(-100.f);
  WebView().ResizeWithBrowserControls(WebSize(400, 400), 100.f, 0, false);
  Compositor().BeginFrame();
  ASSERT_EQ(300, GetDocument().GetFrame()->View()->GetLayoutSize().Height());

  // Now lock the controls in a hidden state. The layout and elements should
  // resize without a WebView::resize.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kBoth,
      false);
  Compositor().BeginFrame();

  EXPECT_FLOAT_EQ(200.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());

  EXPECT_EQ(400, GetDocument().GetFrame()->View()->GetLayoutSize().Height());

  // Unlock the controls, the sizes should change even though the controls are
  // still hidden.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kBoth, false);
  Compositor().BeginFrame();

  EXPECT_FLOAT_EQ(150.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());

  EXPECT_EQ(300, GetDocument().GetFrame()->View()->GetLayoutSize().Height());

  // Now lock the controls in a shown state.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kShown, cc::BrowserControlsState::kBoth, false);
  WebView().ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  Compositor().BeginFrame();

  EXPECT_FLOAT_EQ(150.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(150.f, fixed_pos->getBoundingClientRect()->height());

  EXPECT_EQ(300, GetDocument().GetFrame()->View()->GetLayoutSize().Height());

  // Shown -> Hidden
  WebView().ResizeWithBrowserControls(WebSize(400, 400), 100.f, 0, false);
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kBoth,
      false);
  Compositor().BeginFrame();

  EXPECT_FLOAT_EQ(200.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());

  EXPECT_EQ(400, GetDocument().GetFrame()->View()->GetLayoutSize().Height());

  // Go from Unlocked and showing, to locked and hidden but issue the resize
  // before the constraint update to check for race issues.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown, false);
  WebView().ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  Compositor().BeginFrame();
  ASSERT_EQ(300, GetDocument().GetFrame()->View()->GetLayoutSize().Height());

  WebView().ResizeWithBrowserControls(WebSize(400, 400), 100.f, 0, false);
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kHidden,
      false);
  Compositor().BeginFrame();

  EXPECT_FLOAT_EQ(200.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());

  EXPECT_EQ(400, GetDocument().GetFrame()->View()->GetLayoutSize().Height());
}

// Ensure that browser controls do not affect vh units.
TEST_F(BrowserControlsTest, MAYBE(DontAffectVHUnits)) {
  // Initialize with the browser controls showing.
  WebViewImpl* web_view = Initialize("vh-height.html");
  web_view->ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(100.f, web_view->GetBrowserControls().ContentOffset());

  // 'vh' units should be based on the viewport when the browser controls are
  // hidden.
  Element* abs_pos = GetElementById(WebString::FromUTF8("abs"));
  Element* fixed_pos = GetElementById(WebString::FromUTF8("fixed"));
  EXPECT_FLOAT_EQ(200.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());

  // The size used for viewport units should not be reduced by the top
  // controls.
  EXPECT_EQ(400, GetFrame()->View()->ViewportSizeForViewportUnits().Height());

  // Hide the browser controls.
  VerticalScroll(-100.f);
  web_view->ResizeWithBrowserControls(WebSize(400, 400), 100.f, 0, false);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  // vh units should be static with respect to the browser controls so neighter
  // <div> should change size are a result of the browser controls hiding.
  EXPECT_FLOAT_EQ(200.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());

  // The viewport size used for vh units should not change as a result of top
  // controls hiding.
  EXPECT_EQ(400, GetFrame()->View()->ViewportSizeForViewportUnits().Height());
}

// Ensure that on a legacy page (there's a non-1 minimum scale) 100vh units fill
// the viewport, with browser controls hidden, when the viewport encompasses the
// layout width.
TEST_F(BrowserControlsTest, MAYBE(DontAffectVHUnitsWithScale)) {
  // Initialize with the browser controls showing.
  WebViewImpl* web_view = Initialize("vh-height-width-800.html");
  web_view->ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(100.f, web_view->GetBrowserControls().ContentOffset());

  // Device viewport is 400px but the page is width=800 so minimum-scale
  // should be 0.5. This is also the scale at which the viewport fills the
  // layout width.
  ASSERT_EQ(0.5f, web_view->MinimumPageScaleFactor());

  // We should size vh units so that 100vh fills the viewport at min-scale so
  // we have to account for the minimum page scale factor. Since both boxes
  // are 50vh, and layout scale = 0.5, we have a vh viewport of 400 / 0.5 = 800
  // so we expect 50vh to be 400px.
  Element* abs_pos = GetElementById(WebString::FromUTF8("abs"));
  Element* fixed_pos = GetElementById(WebString::FromUTF8("fixed"));
  EXPECT_FLOAT_EQ(400.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(400.f, fixed_pos->getBoundingClientRect()->height());

  // The size used for viewport units should not be reduced by the top
  // controls.
  EXPECT_EQ(800, GetFrame()->View()->ViewportSizeForViewportUnits().Height());

  // Hide the browser controls.
  VerticalScroll(-100.f);
  web_view->ResizeWithBrowserControls(WebSize(400, 400), 100.f, 0, false);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  // vh units should be static with respect to the browser controls so neighter
  // <div> should change size are a result of the browser controls hiding.
  EXPECT_FLOAT_EQ(400.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(400.f, fixed_pos->getBoundingClientRect()->height());

  // The viewport size used for vh units should not change as a result of top
  // controls hiding.
  EXPECT_EQ(800, GetFrame()->View()->ViewportSizeForViewportUnits().Height());
}

// Ensure that on a legacy page (there's a non-1 minimum scale) whose viewport
// at minimum-scale is larger than the layout size, 100vh units fill the
// viewport, with browser controls hidden, when the viewport is scaled such that
// its width equals the layout width.
TEST_F(BrowserControlsTest, MAYBE(DontAffectVHUnitsUseLayoutSize)) {
  // Initialize with the browser controls showing.
  WebViewImpl* web_view = Initialize("vh-height-width-800-extra-wide.html");
  web_view->ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(100.f, web_view->GetBrowserControls().ContentOffset());

  // Device viewport is 400px and page is width=800 but there's an element
  // that's 1600px wide so the minimum scale is 0.25 to encompass that.
  ASSERT_EQ(0.25f, web_view->MinimumPageScaleFactor());

  // The viewport will match the layout width at scale=0.5 so the height used
  // for vh should be (300 / 0.5) for the layout height + (100 / 0.5) for top
  // controls = 800.
  EXPECT_EQ(800, GetFrame()->View()->ViewportSizeForViewportUnits().Height());
}

// Ensure that vh units are correctly calculated when a top controls min-height
// is set.
TEST_F(BrowserControlsTest, MAYBE(VHUnitsWithTopMinHeight)) {
  // Initialize with the browser controls showing.
  // Top controls height: 100, top controls min-height: 20.
  WebViewImpl* web_view = Initialize("vh-height.html");
  web_view->ResizeWithBrowserControls(WebSize(400, 300), WebSize(400, 300),
                                      {100, 20, 0, 0, false, true});
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  ASSERT_EQ(100.f, web_view->GetBrowserControls().ContentOffset());

  // 'vh' units should be based on the viewport when the browser controls are
  // hidden. However, the viewport height will be limited by the min-height
  // since the top controls can't completely hide.
  Element* abs_pos = GetElementById(WebString::FromUTF8("abs"));
  Element* fixed_pos = GetElementById(WebString::FromUTF8("fixed"));
  float div_height = 0.5f * (300 + (100 - 20));
  EXPECT_FLOAT_EQ(div_height, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(div_height, fixed_pos->getBoundingClientRect()->height());

  // The size used for viewport units should be reduced by the top controls
  // min-height.
  EXPECT_EQ(380, GetFrame()->View()->ViewportSizeForViewportUnits().Height());

  // Scroll the top controls to hide. They won't scroll past the min-height.
  VerticalScroll(-100.f);
  web_view->ResizeWithBrowserControls(WebSize(400, 380), WebSize(400, 380),
                                      {100, 20, 0, 0, false, false});
  UpdateAllLifecyclePhases();

  ASSERT_EQ(20.f, web_view->GetBrowserControls().ContentOffset());

  // vh units should be static with respect to the browser controls so neither
  // <div> should change size are a result of the browser controls hiding.
  EXPECT_FLOAT_EQ(190.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(190.f, fixed_pos->getBoundingClientRect()->height());

  // The viewport size used for vh units should not change as a result of top
  // controls hiding.
  ASSERT_EQ(380, GetFrame()->View()->ViewportSizeForViewportUnits().Height());
}

// Ensure that vh units are correctly calculated when a bottom controls
// min-height is set.
TEST_F(BrowserControlsTest, MAYBE(VHUnitsWithBottomMinHeight)) {
  // Initialize with the browser controls showing.
  // Top controls height: 100, top controls min-height: 20.
  // Bottom controls height: 50, bottom controls min-height: 10.
  WebViewImpl* web_view = Initialize("vh-height.html");
  web_view->ResizeWithBrowserControls(WebSize(400, 250), WebSize(400, 250),
                                      {100, 20, 50, 10, false, true});
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  EXPECT_FLOAT_EQ(100.f, web_view->GetBrowserControls().ContentOffset());

  // 'vh' units should be based on the viewport when the browser controls are
  // hidden. However, the viewport height will be limited by the min-height
  // since the top and bottom controls can't completely hide.
  Element* abs_pos = GetElementById(WebString::FromUTF8("abs"));
  Element* fixed_pos = GetElementById(WebString::FromUTF8("fixed"));
  float div_height = 0.5f * (250 + (100 - 20) + (50 - 10));
  EXPECT_FLOAT_EQ(div_height, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(div_height, fixed_pos->getBoundingClientRect()->height());

  // The size used for viewport units should be reduced by the top/bottom
  // controls min-height.
  EXPECT_EQ(370, GetFrame()->View()->ViewportSizeForViewportUnits().Height());

  // Scroll the controls to hide. They won't scroll past the min-height.
  VerticalScroll(-100.f);
  web_view->ResizeWithBrowserControls(WebSize(400, 370), WebSize(400, 370),
                                      {100, 20, 50, 10, false, false});
  UpdateAllLifecyclePhases();

  EXPECT_FLOAT_EQ(20.f, web_view->GetBrowserControls().ContentOffset());
  EXPECT_FLOAT_EQ(10.f, web_view->GetBrowserControls().BottomContentOffset());

  // vh units should be static with respect to the browser controls so neither
  // <div> should change size are a result of the browser controls hiding.
  EXPECT_FLOAT_EQ(185.f, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(185.f, fixed_pos->getBoundingClientRect()->height());

  // The viewport size used for vh units should not change as a result of the
  // controls hiding.
  ASSERT_EQ(370, GetFrame()->View()->ViewportSizeForViewportUnits().Height());
}

// Ensure that vh units are correctly calculated with changing min-heights.
TEST_F(BrowserControlsTest, MAYBE(VHUnitsWithMinHeightsChanging)) {
  // Initialize with the browser controls showing.
  // Top controls height: 100, top controls min-height: 20.
  // Bottom controls height: 50, bottom controls min-height: 10.
  WebViewImpl* web_view = Initialize("vh-height.html");
  web_view->ResizeWithBrowserControls(WebSize(400, 250), WebSize(400, 250),
                                      {100, 20, 50, 10, false, true});
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  EXPECT_FLOAT_EQ(100.f, web_view->GetBrowserControls().ContentOffset());

  // 'vh' units should be based on the viewport when the browser controls are
  // hidden. However, the viewport height will be limited by the min-height
  // since the top and bottom controls can't completely hide.
  Element* abs_pos = GetElementById(WebString::FromUTF8("abs"));
  Element* fixed_pos = GetElementById(WebString::FromUTF8("fixed"));
  float div_height = 0.5f * (250 + (100 - 20) + (50 - 10));
  EXPECT_FLOAT_EQ(div_height, abs_pos->getBoundingClientRect()->height());
  EXPECT_FLOAT_EQ(div_height, fixed_pos->getBoundingClientRect()->height());

  // The size used for viewport units should be reduced by the top/bottom
  // controls min-height.
  EXPECT_EQ(370, GetFrame()->View()->ViewportSizeForViewportUnits().Height());

  // Make the min-heights 0.
  web_view->ResizeWithBrowserControls(WebSize(400, 250), WebSize(400, 250),
                                      {100, 0, 50, 0, false, true});
  UpdateAllLifecyclePhases();

  // The viewport size used for vh units should be updated to reflect the change
  // to the min-heights.
  float height = 250 + (100 - 0) + (50 - 0);
  ASSERT_EQ(height,
            GetFrame()->View()->ViewportSizeForViewportUnits().Height());
}

// This tests that the viewport remains anchored when browser controls are
// brought in while the document is fully scrolled. This normally causes
// clamping of the visual viewport to keep it bounded by the layout viewport
// so we're testing that the viewport anchoring logic is working to keep the
// view unchanged.
TEST_F(BrowserControlsTest,
       MAYBE(AnchorViewportDuringbrowserControlsAdjustment)) {
  int content_height = 1016;
  int layout_viewport_height = 500;
  int visual_viewport_height = 500;
  int browser_controls_height = 100;
  int page_scale = 2;
  int min_scale = 1;

  // Initialize with the browser controls showing.
  WebViewImpl* web_view = Initialize("large-div.html");
  GetWebView()->SetDefaultPageScaleLimits(min_scale, 5);
  web_view->ResizeWithBrowserControls(WebSize(800, layout_viewport_height),
                                      browser_controls_height, 0, true);
  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  LocalFrameView* view = GetFrame()->View();
  ScrollableArea* root_viewport = GetFrame()->View()->GetScrollableArea();

  int expected_visual_offset =
      ((layout_viewport_height + browser_controls_height / min_scale) *
           page_scale -
       (visual_viewport_height + browser_controls_height)) /
      page_scale;
  int expected_layout_offset =
      content_height -
      (layout_viewport_height + browser_controls_height / min_scale);
  int expected_root_offset = expected_visual_offset + expected_layout_offset;

  // Zoom in to 2X and fully scroll both viewports.
  web_view->SetPageScaleFactor(page_scale);
  {
    web_view->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
    web_view->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -10000));

    ASSERT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

    EXPECT_EQ(expected_visual_offset,
              GetVisualViewport().GetScrollOffset().Height());
    EXPECT_EQ(expected_layout_offset,
              view->LayoutViewport()->GetScrollOffset().Height());
    EXPECT_EQ(expected_root_offset, root_viewport->GetScrollOffset().Height());

    web_view->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  }

  // Commit the browser controls resize so that the browser controls do not
  // shrink the layout size. This should not have moved any of the viewports.
  web_view->ResizeWithBrowserControls(
      WebSize(800, layout_viewport_height + browser_controls_height),
      browser_controls_height, 0, false);
  UpdateAllLifecyclePhases();
  ASSERT_EQ(expected_visual_offset,
            GetVisualViewport().GetScrollOffset().Height());
  ASSERT_EQ(expected_layout_offset,
            view->LayoutViewport()->GetScrollOffset().Height());
  ASSERT_EQ(expected_root_offset, root_viewport->GetScrollOffset().Height());

  // Now scroll back up just enough to show the browser controls. The browser
  // controls should shrink both viewports but the layout viewport by a greater
  // amount. This means the visual viewport's offset must be clamped to keep it
  // within the layout viewport. Make sure we adjust the scroll position to
  // account for this and keep the visual viewport at the same location relative
  // to the document (i.e. the user shouldn't see a movement).
  {
    web_view->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
    web_view->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 80));

    GetVisualViewport().ClampToBoundaries();
    view->LayoutViewport()->SetScrollOffset(
        view->LayoutViewport()->GetScrollOffset(),
        mojom::blink::ScrollType::kProgrammatic);

    ASSERT_EQ(80.f, web_view->GetBrowserControls().ContentOffset());
    EXPECT_EQ(expected_root_offset, root_viewport->GetScrollOffset().Height());

    web_view->MainFrameWidget()->HandleInputEvent(
        GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  }
}

// Ensure that vh units are correct when browser controls are in a locked
// state. That is, normally we need to add the browser controls height to vh
// units since 100vh includes the browser controls even if they're hidden while
// the ICB height does not. When the controls are locked hidden, the ICB size
// is the full viewport height so there's no need to add the browser controls
// height.  crbug.com/688738.
TEST_F(BrowserControlsSimTest, MAYBE(ViewportUnitsWhenControlsLocked)) {
  // Initialize with the browser controls showing.
  SimRequest request("https://example.com/test.html", "text/html");
  LoadURL("https://example.com/test.html");
  request.Complete(R"HTML(
          <!DOCTYPE html>
            <style>
              #abs {
                position: absolute;
                left: 0px;
                top: 0px;
                width: 100px;
                height: 50vh;
              }

              #fixed {
                position: fixed;
                right: 0px;
                top: 0px;
                width: 100px;
                height: 50vh;
              }

              #spacer {
                height: 1000px;
              }
            </style>
            <div id="abs"></div>
            <div id="fixed"></div>
            <div id="spacer"></div>
      )HTML");
  WebView().ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown, false);
  Compositor().BeginFrame();

  ASSERT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());
  ASSERT_EQ(100.f, WebView().GetBrowserControls().ContentOffset());
  ASSERT_EQ(300, GetDocument().View()->GetLayoutSize().Height());

  Element* abs_pos = GetDocument().getElementById("abs");
  Element* fixed_pos = GetDocument().getElementById("fixed");

  // Lock the browser controls to hidden.
  {
    Compositor().layer_tree_host()->UpdateBrowserControlsState(
        cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kHidden,
        false);
    WebView().ResizeWithBrowserControls(WebSize(400, 400), 100.f, 0, false);
    Compositor().BeginFrame();

    ASSERT_EQ(0.f, WebView().GetBrowserControls().ContentOffset());
    ASSERT_EQ(400, GetDocument().View()->GetLayoutSize().Height());

    // Make sure we're not adding the browser controls height to the vh units
    // as when they're locked to hidden, the ICB fills the entire viewport
    // already.
    EXPECT_FLOAT_EQ(200.f, abs_pos->getBoundingClientRect()->height());
    EXPECT_FLOAT_EQ(200.f, fixed_pos->getBoundingClientRect()->height());
    EXPECT_EQ(400,
              GetDocument().View()->ViewportSizeForViewportUnits().Height());
  }

  // Lock the browser controls to shown. This should cause the vh units to
  // behave as usual by including the browser controls region in 100vh.
  {
    Compositor().layer_tree_host()->UpdateBrowserControlsState(
        cc::BrowserControlsState::kShown, cc::BrowserControlsState::kShown,
        false);
    WebView().ResizeWithBrowserControls(WebSize(400, 300), 100.f, 0, true);
    Compositor().BeginFrame();

    ASSERT_EQ(100.f, WebView().GetBrowserControls().ContentOffset());
    ASSERT_EQ(300, GetDocument().View()->GetLayoutSize().Height());

    // Make sure we're not adding the browser controls height to the vh units as
    // when they're locked to shown, the ICB fills the entire viewport already.
    EXPECT_FLOAT_EQ(150.f, abs_pos->getBoundingClientRect()->height());
    EXPECT_FLOAT_EQ(150.f, fixed_pos->getBoundingClientRect()->height());
    EXPECT_EQ(400,
              GetDocument().View()->ViewportSizeForViewportUnits().Height());
  }
}

// Test the size adjustment sent to the viewport when top controls exist.
TEST_F(BrowserControlsTest, MAYBE(TopControlsSizeAdjustment)) {
  WebViewImpl* web_view = Initialize();
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      0, false);
  web_view->GetBrowserControls().SetShownRatio(1, 0.0);
  EXPECT_FLOAT_EQ(-50.f,
                  web_view->GetBrowserControls().UnreportedSizeAdjustment());

  web_view->GetBrowserControls().SetShownRatio(0.5, 0.0);
  EXPECT_FLOAT_EQ(-25.f,
                  web_view->GetBrowserControls().UnreportedSizeAdjustment());

  web_view->GetBrowserControls().SetShownRatio(0.0, 0.0);
  EXPECT_FLOAT_EQ(0.f,
                  web_view->GetBrowserControls().UnreportedSizeAdjustment());
}

// Test the size adjustment sent to the viewport when bottom controls exist.
// There should never be an adjustment since the bottom controls do not change
// the content offset.
TEST_F(BrowserControlsTest, MAYBE(BottomControlsSizeAdjustment)) {
  WebViewImpl* web_view = Initialize();
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 0,
                                      50.f, false);
  web_view->GetBrowserControls().SetShownRatio(0.0, 1);
  EXPECT_FLOAT_EQ(0.f,
                  web_view->GetBrowserControls().UnreportedSizeAdjustment());

  web_view->GetBrowserControls().SetShownRatio(0.0, 0.5);
  EXPECT_FLOAT_EQ(0.f,
                  web_view->GetBrowserControls().UnreportedSizeAdjustment());

  web_view->GetBrowserControls().SetShownRatio(0.0, 0.0);
  EXPECT_FLOAT_EQ(0.f,
                  web_view->GetBrowserControls().UnreportedSizeAdjustment());
}

TEST_F(BrowserControlsTest, MAYBE(GrowingHeightKeepsTopControlsHidden)) {
  WebViewImpl* web_view = Initialize();
  float bottom_height = web_view->GetBrowserControls().BottomHeight();
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 1.f,
                                      bottom_height, false);

  web_view->GetBrowserControls().UpdateConstraintsAndState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kHidden);

  // As we expand the top controls height while hidden, the content offset
  // shouldn't change.
  EXPECT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(), 50.f,
                                      bottom_height, false);
  EXPECT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());

  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(),
                                      100.f, bottom_height, false);
  EXPECT_EQ(0.f, web_view->GetBrowserControls().ContentOffset());
}

TEST_F(BrowserControlsTest,
       MAYBE(HidingBrowserControlsInvalidatesGraphicsLayer)) {
  // Initialize with the browser controls showing.
  WebViewImpl* web_view = Initialize("95-vh.html");
  web_view->ResizeWithBrowserControls(WebSize(412, 604), 56.f, 0, true);
  web_view->GetBrowserControls().SetShownRatio(1, 1);
  UpdateAllLifecyclePhases();

  GetFrame()->GetDocument()->View()->SetTracksRasterInvalidations(true);

  // Hide the browser controls.
  VerticalScroll(-100.f);
  web_view->ResizeWithBrowserControls(WebSize(412, 660), 56.f, 0, false);
  UpdateAllLifecyclePhases();

  // Ensure there is a raster invalidation of the bottom of the layer.
  const auto& raster_invalidations = GetFrame()
                                         ->ContentLayoutObject()
                                         ->Layer()
                                         ->GetCompositedLayerMapping()
                                         ->ScrollingContentsLayer()
                                         ->GetRasterInvalidationTracking()
                                         ->Invalidations();
  EXPECT_EQ(1u, raster_invalidations.size());
  EXPECT_EQ(IntRect(0, 643, 412, 17), raster_invalidations[0].rect);
  EXPECT_EQ(PaintInvalidationReason::kIncremental,
            raster_invalidations[0].reason);

  GetFrame()->GetDocument()->View()->SetTracksRasterInvalidations(false);
}

// Test that the browser controls have different shown ratios when scrolled with
// a minimum height set for only top controls.
TEST_F(BrowserControlsTest, MAYBE(ScrollWithMinHeightSetForTopControlsOnly)) {
  WebViewImpl* web_view = Initialize();
  float top_height = 56;
  float bottom_height = 50;
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(),
                                      top_height, bottom_height, false);
  web_view->GetBrowserControls().SetShownRatio(1.f, 1.f);
  web_view->GetBrowserControls().SetParams(
      {top_height, 20, bottom_height, 0, false, true});
  // Scroll down to hide the controls.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -100));

  // The bottom controls should be completely hidden while the top controls are
  // at the minimum height.
  EXPECT_EQ(0.f, web_view->GetBrowserControls().BottomShownRatio());
  EXPECT_GT(web_view->GetBrowserControls().TopShownRatio(), 0);
  EXPECT_EQ(20, web_view->GetBrowserControls().ContentOffset());

  // Scrolling back up should bring the browser controls shown ratios back to 1.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 100));
  EXPECT_EQ(1.f, web_view->GetBrowserControls().BottomShownRatio());
  EXPECT_EQ(1.f, web_view->GetBrowserControls().TopShownRatio());
  EXPECT_EQ(top_height, web_view->GetBrowserControls().ContentOffset());
}

// Test that the browser controls don't scroll off when a minimum height is set.
TEST_F(BrowserControlsTest, MAYBE(ScrollWithMinHeightSet)) {
  WebViewImpl* web_view = Initialize();
  float top_height = 56;
  float bottom_height = 50;
  web_view->ResizeWithBrowserControls(web_view->MainFrameWidget()->Size(),
                                      top_height, bottom_height, false);
  web_view->GetBrowserControls().SetShownRatio(1.f, 1.f);
  web_view->GetBrowserControls().SetParams(
      {top_height, 20, bottom_height, 10, false, true});

  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -100));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));

  // Browser controls don't scroll off completely, and stop scrolling at the min
  // height.
  EXPECT_EQ(20, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(10, web_view->GetBrowserControls().BottomContentOffset());

  // Ending the scroll then scrolling again shouldn't make any difference.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, -50));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollEnd));
  EXPECT_EQ(20, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(10, web_view->GetBrowserControls().BottomContentOffset());

  // Finally, scroll back up to show the controls completely.
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollBegin));
  web_view->MainFrameWidget()->HandleInputEvent(
      GenerateEvent(WebInputEvent::Type::kGestureScrollUpdate, 0, 100));
  EXPECT_EQ(top_height, web_view->GetBrowserControls().ContentOffset());
  EXPECT_EQ(bottom_height,
            web_view->GetBrowserControls().BottomContentOffset());
}

#undef MAYBE

// Test that sending both an animated and non-animated browser control update
// doesn't cause the animated one to squash the non-animated.
// https://crbug.com/861618.
TEST_F(BrowserControlsSimTest, MixAnimatedAndNonAnimatedUpdateState) {
  SimRequest request("https://example.com/test.html", "text/html");
  LoadURL("https://example.com/test.html");
  request.Complete(R"HTML(
          <!DOCTYPE html>
          <meta name="viewport" content="width=device-width">
          <style>
            body {
              height: 2000px;
            }
          </style>
      )HTML");
  Compositor().BeginFrame();

  ASSERT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());

  // Kick off a non-animated clamp to hide the top controls.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kBoth,
      false /* animated */);

  // Now kick off an animated one to do the same thing.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kBoth,
      true /* animated */);

  // Advance time. In https://crbug.com/861618, the animation didn't realize
  // yet we're already at 0, so it would play the compositor-side up to 80ms,
  // somewhere mid-way hidden. Later on in this BeginFrame the changes from the
  // main thread are committed so the top controls shown ratio will set to 0.
  Compositor().BeginFrame(0.080);

  EXPECT_EQ(0.f, WebView().GetBrowserControls().TopShownRatio());

  // Tick the animation again. The animation should have been stopped. In
  // https://crbug.com/861618, the animation would continue to play since it
  // was kicked off after the non-animated call as far as the compositor could
  // see. This means this animation tick would set the delta to some non-0 value
  // again. This value will be committed to the main thread causing the controls
  // to show.
  Compositor().BeginFrame();

  EXPECT_EQ(0.f, WebView().GetBrowserControls().TopShownRatio());
}

// Test that requesting an animated hide on the top controls actually
// animates rather than happening instantly.
TEST_F(BrowserControlsSimTest, HideAnimated) {
  SimRequest request("https://example.com/test.html", "text/html");
  LoadURL("https://example.com/test.html");
  request.Complete(R"HTML(
          <!DOCTYPE html>
          <meta name="viewport" content="width=device-width">
          <style>
            body {
              height: 2000px;
            }
          </style>
      )HTML");
  Compositor().BeginFrame();

  ASSERT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());
  ASSERT_EQ(1.f, WebView().GetBrowserControls().BottomShownRatio());

  // Kick off an animated hide.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kHidden,
      true /* animated */);

  Compositor().BeginFrame();

  ASSERT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());
  ASSERT_EQ(1.f, WebView().GetBrowserControls().BottomShownRatio());

  // Advance time.
  Compositor().BeginFrame(0.080);

  EXPECT_NE(0.f, WebView().GetBrowserControls().TopShownRatio());
  EXPECT_NE(1.f, WebView().GetBrowserControls().TopShownRatio());
  EXPECT_EQ(WebView().GetBrowserControls().TopShownRatio(),
            WebView().GetBrowserControls().BottomShownRatio());
}

// Test that requesting an animated show on the top controls actually
// animates rather than happening instantly.
TEST_F(BrowserControlsSimTest, ShowAnimated) {
  SimRequest request("https://example.com/test.html", "text/html");
  LoadURL("https://example.com/test.html");
  request.Complete(R"HTML(
          <!DOCTYPE html>
          <meta name="viewport" content="width=device-width">
          <style>
            body {
              height: 2000px;
            }
          </style>
      )HTML");
  Compositor().BeginFrame();

  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kHidden,
      false);

  Compositor().BeginFrame();

  ASSERT_EQ(0.f, WebView().GetBrowserControls().TopShownRatio());
  ASSERT_EQ(0.f, WebView().GetBrowserControls().BottomShownRatio());

  // Kick off an animated show.
  Compositor().layer_tree_host()->UpdateBrowserControlsState(
      cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown,
      true /* animated */);

  Compositor().BeginFrame();

  ASSERT_EQ(0.f, WebView().GetBrowserControls().TopShownRatio());
  ASSERT_EQ(0.f, WebView().GetBrowserControls().BottomShownRatio());

  // Advance time.
  Compositor().BeginFrame(0.080);

  EXPECT_NE(0.f, WebView().GetBrowserControls().TopShownRatio());
  EXPECT_NE(1.f, WebView().GetBrowserControls().TopShownRatio());

  // The bottom controls shown ratio should follow the top controls.
  EXPECT_EQ(WebView().GetBrowserControls().TopShownRatio(),
            WebView().GetBrowserControls().BottomShownRatio());
}

// Test that setting a constraint inside Blink doesn't clamp the ratio to the
// constraint. This is required since the CC-side will set the ratio correctly.
// If we did clamp the ratio, an animation running in CC would get clobbered
// when we commit.
TEST_F(BrowserControlsSimTest, ConstraintDoesntClampRatioInBlink) {
  SimRequest request("https://example.com/test.html", "text/html");
  LoadURL("https://example.com/test.html");
  request.Complete(R"HTML(
          <!DOCTYPE html>
          <meta name="viewport" content="width=device-width">
          <style>
            body {
              height: 2000px;
            }
          </style>
      )HTML");
  Compositor().BeginFrame();

  ASSERT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());
  ASSERT_EQ(1.f, WebView().GetBrowserControls().BottomShownRatio());

  {
    // Pass a hidden constraint to Blink (without going through CC). Make sure
    // the shown ratio doesn't change since CC is responsible for updating the
    // ratio.
    WebView().GetBrowserControls().UpdateConstraintsAndState(
        cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kBoth);
    EXPECT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());
    EXPECT_EQ(1.f, WebView().GetBrowserControls().BottomShownRatio());
    WebView().GetBrowserControls().UpdateConstraintsAndState(
        cc::BrowserControlsState::kHidden, cc::BrowserControlsState::kBoth);
    EXPECT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());
    EXPECT_EQ(1.f, WebView().GetBrowserControls().BottomShownRatio());

    // Constrain the controls to hidden from the compositor. This should
    // actually cause the controls to hide when we commit.
    Compositor().layer_tree_host()->UpdateBrowserControlsState(
        cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kHidden,
        false /* animated */);
    Compositor().BeginFrame();

    EXPECT_EQ(0.f, WebView().GetBrowserControls().TopShownRatio());
    EXPECT_EQ(0.f, WebView().GetBrowserControls().BottomShownRatio());
  }

  {
    // Pass a shown constraint to Blink (without going through CC). Make sure
    // the shown ratio doesn't change.
    WebView().GetBrowserControls().UpdateConstraintsAndState(
        cc::BrowserControlsState::kShown, cc::BrowserControlsState::kBoth);
    EXPECT_EQ(0.f, WebView().GetBrowserControls().TopShownRatio());
    EXPECT_EQ(0.f, WebView().GetBrowserControls().BottomShownRatio());
    WebView().GetBrowserControls().UpdateConstraintsAndState(
        cc::BrowserControlsState::kShown, cc::BrowserControlsState::kBoth);
    EXPECT_EQ(0.f, WebView().GetBrowserControls().TopShownRatio());
    EXPECT_EQ(0.f, WebView().GetBrowserControls().BottomShownRatio());

    // Constrain the controls to hidden from the compositor. This should
    // actually cause the controls to hide when we commit.
    Compositor().layer_tree_host()->UpdateBrowserControlsState(
        cc::BrowserControlsState::kBoth, cc::BrowserControlsState::kShown,
        false /* animated */);
    Compositor().BeginFrame();

    EXPECT_EQ(1.f, WebView().GetBrowserControls().TopShownRatio());
    EXPECT_EQ(1.f, WebView().GetBrowserControls().BottomShownRatio());
  }
}

}  // namespace blink