summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/frame/document_loading_rendering_test.cc
blob: 43fcb6b1c69ec7398fe41bc22dd12044e60ca45e (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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/style_change_reason.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/frame_request_callback_collection.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/geometry/dom_rect.h"
#include "third_party/blink/renderer/core/html/html_iframe_element.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/testing/sim/sim_compositor.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/unit_test_helpers.h"

namespace blink {

class DocumentLoadingRenderingTest : public SimTest {};

TEST_F(DocumentLoadingRenderingTest,
       ShouldResumeCommitsAfterBodyParsedWithoutSheets) {
  SimRequest main_resource("https://example.com/test.html", "text/html");

  LoadURL("https://example.com/test.html");

  // Still in the head, should not resume commits.
  main_resource.Write("<!DOCTYPE html>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());
  main_resource.Write("<title>Test</title><style>div { color red; }</style>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Implicitly inserts the body. Since there's no loading stylesheets we
  // should resume commits.
  main_resource.Write("<p>Hello World</p>");
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());

  // Finish the load, should stay resumed.
  main_resource.Finish();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());
}

TEST_F(DocumentLoadingRenderingTest,
       ShouldResumeCommitsAfterBodyIfSheetsLoaded) {
  SimRequest main_resource("https://example.com/test.html", "text/html");
  SimSubresourceRequest css_resource("https://example.com/test.css",
                                     "text/css");

  LoadURL("https://example.com/test.html");

  // Still in the head, should not resume commits.
  main_resource.Write("<!DOCTYPE html><link rel=stylesheet href=test.css>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Sheet is streaming in, but not ready yet.
  css_resource.Start();
  css_resource.Write("a { color: red; }");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Sheet finished, but no body yet, so don't resume.
  css_resource.Finish();
  test::RunPendingTasks();
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Body inserted and sheet is loaded so resume commits.
  main_resource.Write("<body>");
  test::RunPendingTasks();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());

  // Finish the load, should stay resumed.
  main_resource.Finish();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());
}

TEST_F(DocumentLoadingRenderingTest, ShouldResumeCommitsAfterSheetsLoaded) {
  SimRequest main_resource("https://example.com/test.html", "text/html");
  SimSubresourceRequest css_resource("https://example.com/test.css",
                                     "text/css");

  LoadURL("https://example.com/test.html");

  // Still in the head, should not resume commits.
  main_resource.Write("<!DOCTYPE html><link rel=stylesheet href=test.css>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Sheet is streaming in, but not ready yet.
  css_resource.Start();
  css_resource.Write("a { color: red; }");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Body inserted, but sheet is still loading so don't resume.
  main_resource.Write("<body>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Sheet finished and there's a body so resume.
  css_resource.Finish();
  test::RunPendingTasks();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());

  // Finish the load, should stay resumed.
  main_resource.Finish();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());
}

TEST_F(DocumentLoadingRenderingTest,
       ShouldResumeCommitsAfterDocumentElementWithNoSheets) {
  SimRequest main_resource("https://example.com/test.svg", "image/svg+xml");
  SimSubresourceRequest css_resource("https://example.com/test.css",
                                     "text/css");

  LoadURL("https://example.com/test.svg");

  // Sheet loading and no documentElement, so don't resume.
  main_resource.Write("<?xml-stylesheet type='text/css' href='test.css'?>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Sheet finishes loading, but no documentElement yet so don't resume.
  css_resource.Complete("a { color: red; }");
  test::RunPendingTasks();
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Root inserted so resume.
  main_resource.Write("<svg xmlns='http://www.w3.org/2000/svg'></svg>");
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());

  // Finish the load, should stay resumed.
  main_resource.Finish();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());
}

TEST_F(DocumentLoadingRenderingTest, ShouldResumeCommitsAfterSheetsLoadForXml) {
  SimRequest main_resource("https://example.com/test.svg", "image/svg+xml");
  SimSubresourceRequest css_resource("https://example.com/test.css",
                                     "text/css");

  LoadURL("https://example.com/test.svg");

  // Not done parsing.
  main_resource.Write("<?xml-stylesheet type='text/css' href='test.css'?>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Sheet is streaming in, but not ready yet.
  css_resource.Start();
  css_resource.Write("a { color: red; }");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Root inserted, but sheet is still loading so don't resume.
  main_resource.Write("<svg xmlns='http://www.w3.org/2000/svg'></svg>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Finish the load, but sheets still loading so don't resume.
  main_resource.Finish();
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Sheet finished, so resume commits.
  css_resource.Finish();
  test::RunPendingTasks();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());
}

TEST_F(DocumentLoadingRenderingTest, ShouldResumeCommitsAfterFinishParsingXml) {
  SimRequest main_resource("https://example.com/test.svg", "image/svg+xml");

  LoadURL("https://example.com/test.svg");

  // Finish parsing, no sheets loading so resume.
  main_resource.Finish();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());
}

TEST_F(DocumentLoadingRenderingTest, ShouldResumeImmediatelyForImageDocuments) {
  SimRequest main_resource("https://example.com/test.png", "image/png");

  LoadURL("https://example.com/test.png");

  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  // Not really a valid image but enough for the test. ImageDocuments should
  // resume painting as soon as the first bytes arrive.
  main_resource.Write("image data");
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());

  main_resource.Finish();
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());
}

TEST_F(DocumentLoadingRenderingTest, ShouldScheduleFrameAfterSheetsLoaded) {
  SimRequest main_resource("https://example.com/test.html", "text/html");
  SimSubresourceRequest first_css_resource("https://example.com/first.css",
                                           "text/css");
  SimSubresourceRequest second_css_resource("https://example.com/second.css",
                                            "text/css");

  LoadURL("https://example.com/test.html");

  // Load a stylesheet.
  main_resource.Write(
      "<!DOCTYPE html><link id=link rel=stylesheet href=first.css>");
  EXPECT_TRUE(Compositor().DeferMainFrameUpdate());

  first_css_resource.Start();
  first_css_resource.Write("body { color: red; }");
  main_resource.Write("<body>");
  first_css_resource.Finish();
  test::RunPendingTasks();

  // Sheet finished and there's a body so resume.
  EXPECT_FALSE(Compositor().DeferMainFrameUpdate());

  main_resource.Finish();
  Compositor().BeginFrame();

  // Replace the stylesheet by changing href.
  auto* element = GetDocument().getElementById("link");
  EXPECT_NE(nullptr, element);
  element->setAttribute(html_names::kHrefAttr, "second.css");
  EXPECT_FALSE(Compositor().NeedsBeginFrame());

  second_css_resource.Complete("body { color: red; }");
  EXPECT_TRUE(Compositor().NeedsBeginFrame());
}

TEST_F(DocumentLoadingRenderingTest,
       ShouldNotPaintIframeContentWithPendingSheets) {
  SimRequest main_resource("https://example.com/test.html", "text/html");
  SimRequest frame_resource("https://example.com/frame.html", "text/html");
  SimSubresourceRequest css_resource("https://example.com/test.css",
                                     "text/css");

  LoadURL("https://example.com/test.html");

  WebView().MainFrameWidget()->Resize(WebSize(800, 600));

  main_resource.Complete(R"HTML(
    <!DOCTYPE html>
    <body style='background: white'>
    <iframe id=frame src=frame.html style='border: none'></iframe>
    <p style='transform: translateZ(0)'>Hello World</p>
  )HTML");

  // Main page is ready to begin painting as there's no pending sheets.
  // The frame is not yet loaded, so we only paint the main frame.
  auto frame1 = Compositor().BeginFrame();
  EXPECT_EQ(2u, frame1.DrawCount());
  EXPECT_TRUE(frame1.Contains(SimCanvas::kText, "black"));
  EXPECT_TRUE(frame1.Contains(SimCanvas::kRect, "white"));

  frame_resource.Complete(R"HTML(
    <!DOCTYPE html>
    <style>html { background: pink; color: gray; }</style>
    <link rel=stylesheet href=test.css>
    <p style='background: yellow;'>Hello World</p>
    <div style='transform: translateZ(0); background: green;'>
        <p style='background: blue;'>Hello Layer</p>
        <div style='position: relative; background: red;'>Hello World</div>
    </div>
  )HTML");

  // Trigger a layout with a blocking sheet. For example, a parent frame
  // executing a script that reads offsetTop in the child frame could do this.
  auto* child_frame =
      To<HTMLIFrameElement>(GetDocument().getElementById("frame"));
  child_frame->contentDocument()->UpdateStyleAndLayout(
      DocumentUpdateReason::kTest);

  auto frame2 = Compositor().BeginFrame();

  // The child frame still has a sheet blocking in head, so nothing is painted.
  // Still only paint the main frame.
  EXPECT_EQ(2u, frame2.DrawCount());
  EXPECT_TRUE(frame2.Contains(SimCanvas::kText, "black"));
  EXPECT_TRUE(frame2.Contains(SimCanvas::kRect, "white"));

  // Finish loading the sheets in the child frame. After it we should continue
  // parsing and paint the frame contents.
  css_resource.Complete();
  test::RunPendingTasks();

  // First frame where all frames are loaded, should paint the text in the
  // child frame.
  auto frame3 = Compositor().BeginFrame();
  EXPECT_EQ(10u, frame3.DrawCount());
  // Paint commands for the main frame.
  EXPECT_TRUE(frame3.Contains(SimCanvas::kText, "black"));
  EXPECT_TRUE(frame3.Contains(SimCanvas::kRect, "white"));
  // Paint commands for the child frame.
  EXPECT_EQ(3u, frame3.DrawCount(SimCanvas::kText, "gray"));
  EXPECT_TRUE(frame3.Contains(SimCanvas::kRect, "pink"));
  EXPECT_TRUE(frame3.Contains(SimCanvas::kRect, "yellow"));
  EXPECT_TRUE(frame3.Contains(SimCanvas::kRect, "green"));
  EXPECT_TRUE(frame3.Contains(SimCanvas::kRect, "blue"));
  EXPECT_TRUE(frame3.Contains(SimCanvas::kRect, "red"));
}

namespace {

class CheckRafCallback final
    : public FrameRequestCallbackCollection::FrameCallback {
 public:
  void Invoke(double high_res_time_ms) override { was_called_ = true; }
  bool WasCalled() const { return was_called_; }

 private:
  bool was_called_ = false;
};

}  // namespace

TEST_F(DocumentLoadingRenderingTest,
       ShouldThrottleIframeLifecycleUntilPendingSheetsLoaded) {
  SimRequest main_resource("https://example.com/main.html", "text/html");
  SimRequest frame_resource("https://example.com/frame.html", "text/html");
  SimSubresourceRequest css_resource("https://example.com/frame.css",
                                     "text/css");

  LoadURL("https://example.com/main.html");

  WebView().MainFrameWidget()->Resize(WebSize(800, 600));

  main_resource.Complete(R"HTML(
    <!DOCTYPE html>
    <body style='background: red'>
    <iframe id=frame src=frame.html></iframe>
  )HTML");

  frame_resource.Complete(R"HTML(
    <!DOCTYPE html>
    <link rel=stylesheet href=frame.css>
    <body style='background: blue'>
  )HTML");

  auto* child_frame =
      To<HTMLIFrameElement>(GetDocument().getElementById("frame"));

  // Frame while the child frame still has pending sheets.
  auto* frame1_callback = MakeGarbageCollected<CheckRafCallback>();
  child_frame->contentDocument()->RequestAnimationFrame(frame1_callback);
  auto frame1 = Compositor().BeginFrame();
  EXPECT_FALSE(frame1_callback->WasCalled());
  EXPECT_TRUE(frame1.Contains(SimCanvas::kRect, "red"));
  EXPECT_FALSE(frame1.Contains(SimCanvas::kRect, "blue"));

  // Finish loading the sheets in the child frame. Should enable lifecycle
  // updates and raf callbacks.
  css_resource.Complete();
  test::RunPendingTasks();

  // Frame with all lifecycle updates enabled.
  auto* frame2_callback = MakeGarbageCollected<CheckRafCallback>();
  child_frame->contentDocument()->RequestAnimationFrame(frame2_callback);
  auto frame2 = Compositor().BeginFrame();
  EXPECT_TRUE(frame1_callback->WasCalled());
  EXPECT_TRUE(frame2_callback->WasCalled());
  EXPECT_TRUE(frame2.Contains(SimCanvas::kRect, "red"));
  EXPECT_TRUE(frame2.Contains(SimCanvas::kRect, "blue"));
}

TEST_F(DocumentLoadingRenderingTest,
       ShouldContinuePaintingWhenSheetsStartedAfterBody) {
  // HaveRenderBlockingResourcesLoaded being tested here is always true with
  // ScopedBlockHTMLParserOnStyleSheets enabled. Remove this test when the flag
  // is removed.
  ScopedBlockHTMLParserOnStyleSheetsForTest scoped_feature(false);

  SimRequest main_resource("https://example.com/test.html", "text/html");
  SimSubresourceRequest css_head_resource("https://example.com/testHead.css",
                                          "text/css");
  SimSubresourceRequest css_body_resource("https://example.com/testBody.css",
                                          "text/css");

  LoadURL("https://example.com/test.html");

  // Still in the head, should not paint.
  main_resource.Write("<!DOCTYPE html><link rel=stylesheet href=testHead.css>");
  EXPECT_FALSE(GetDocument().HaveRenderBlockingResourcesLoaded());

  // Sheet is streaming in, but not ready yet.
  css_head_resource.Start();
  css_head_resource.Write("a { color: red; }");
  EXPECT_FALSE(GetDocument().HaveRenderBlockingResourcesLoaded());

  // Body inserted but sheet is still pending so don't paint.
  main_resource.Write("<body>");
  EXPECT_FALSE(GetDocument().HaveRenderBlockingResourcesLoaded());

  // Sheet finished and body inserted, ok to paint.
  css_head_resource.Finish();
  EXPECT_TRUE(GetDocument().HaveRenderBlockingResourcesLoaded());

  // In the body, should not stop painting.
  main_resource.Write("<link rel=stylesheet href=testBody.css>");
  EXPECT_TRUE(GetDocument().HaveRenderBlockingResourcesLoaded());

  // Finish loading the CSS resource (no change to painting).
  css_body_resource.Complete("a { color: red; }");
  EXPECT_TRUE(GetDocument().HaveRenderBlockingResourcesLoaded());

  // Finish the load, painting should stay enabled.
  main_resource.Finish();
  EXPECT_TRUE(GetDocument().HaveRenderBlockingResourcesLoaded());
}

TEST_F(DocumentLoadingRenderingTest,
       returnBoundingClientRectCorrectlyWhileLoadingImport) {
  SimRequest main_resource("https://example.com/test.html", "text/html");
  SimSubresourceRequest import_resource("https://example.com/import.css",
                                        "text/css");

  LoadURL("https://example.com/test.html");

  WebView().MainFrameWidget()->Resize(WebSize(800, 600));

  main_resource.Write(R"HTML(
    <html><body>
      <div id='test' style='font-size: 16px'>test</div>
      <script>
        var link = document.createElement('link');
        link.rel = 'import';
        link.href = 'import.css';
        document.head.appendChild(link);
      </script>
  )HTML");
  import_resource.Start();

  // Import loader isn't finish, shoudn't paint.
  EXPECT_FALSE(GetDocument().HaveRenderBlockingResourcesLoaded());

  // Pending imports should not block layout
  Element* element = GetDocument().getElementById("test");
  DOMRect* rect = element->getBoundingClientRect();
  EXPECT_TRUE(rect->width() > 0.f);
  EXPECT_TRUE(rect->height() > 0.f);
  EXPECT_FALSE(GetDocument().HaveRenderBlockingResourcesLoaded());

  import_resource.Write("div { color: red; }");
  import_resource.Finish();
  main_resource.Finish();
}

TEST_F(DocumentLoadingRenderingTest, StableSVGStopStylingWhileLoadingImport) {
  SimRequest main_resource("https://example.com/test.html", "text/html");
  SimSubresourceRequest import_resource("https://example.com/import.css",
                                        "text/css");

  LoadURL("https://example.com/test.html");

  main_resource.Write(R"HTML(
    <html><body>
      <svg>
        <linearGradient>
          <stop id='test' stop-color='green' stop-opacity='0.5' />
        </linearGradient>
      </svg>
  )HTML");

  // Verify that SVG <stop> styling is stable/accurate when recalculated
  // during import loading.
  const auto recalc_and_check = [this]() {
    GetDocument().GetStyleEngine().MarkAllElementsForStyleRecalc(
        StyleChangeReasonForTracing::Create("test reason"));
    GetDocument().UpdateStyleAndLayout(DocumentUpdateReason::kTest);

    Element* element = GetDocument().getElementById("test");
    ASSERT_NE(nullptr, element);
    const SVGComputedStyle& svg_style = element->ComputedStyleRef().SvgStyle();
    EXPECT_EQ(0xff008000, svg_style.StopColor().GetColor());
    EXPECT_EQ(.5f, svg_style.StopOpacity());
  };

  EXPECT_TRUE(GetDocument().HaveRenderBlockingResourcesLoaded());
  recalc_and_check();

  main_resource.Write(
      "<script>"
      "var link = document.createElement('link');"
      "link.rel = 'import';"
      "link.href = 'import.css';"
      "document.head.appendChild(link);"
      "</script>");

  EXPECT_FALSE(GetDocument().HaveRenderBlockingResourcesLoaded());
  recalc_and_check();

  import_resource.Complete();
  main_resource.Finish();

  recalc_and_check();
}

}  // namespace blink