summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/ng_base_layout_algorithm_test.cc
blob: b6a1bd173f18f6b4f8e2c348184f6278af58ba41 (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
// Copyright 2017 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 "third_party/blink/renderer/core/layout/ng/ng_base_layout_algorithm_test.h"

#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/layout/ng/layout_ng_block_flow.h"
#include "third_party/blink/renderer/core/layout/ng/ng_block_layout_algorithm.h"
#include "third_party/blink/renderer/core/layout/ng/ng_constraint_space_builder.h"
#include "third_party/blink/renderer/core/layout/ng/ng_fieldset_layout_algorithm.h"
#include "third_party/blink/renderer/core/layout/ng/ng_layout_result.h"
#include "third_party/blink/renderer/core/layout/ng/ng_length_utils.h"
#include "third_party/blink/renderer/core/layout/ng/ng_physical_box_fragment.h"
#include "third_party/blink/renderer/core/layout/ng/ng_physical_fragment.h"

namespace blink {

void NGBaseLayoutAlgorithmTest::SetUp() {
  EnableCompositing();
  NGLayoutTest::SetUp();
}

void NGBaseLayoutAlgorithmTest::AdvanceToLayoutPhase() {
  if (GetDocument().Lifecycle().GetState() ==
      DocumentLifecycle::kInPerformLayout)
    return;
  GetDocument().Lifecycle().AdvanceTo(DocumentLifecycle::kInStyleRecalc);
  GetDocument().Lifecycle().AdvanceTo(DocumentLifecycle::kStyleClean);
  GetDocument().Lifecycle().AdvanceTo(DocumentLifecycle::kInPerformLayout);
}

scoped_refptr<const NGPhysicalBoxFragment>
NGBaseLayoutAlgorithmTest::RunBlockLayoutAlgorithm(
    NGBlockNode node,
    const NGConstraintSpace& space,
    const NGBreakToken* break_token) {
  NGFragmentGeometry fragment_geometry =
      CalculateInitialFragmentGeometry(space, node);

  scoped_refptr<const NGLayoutResult> result =
      NGBlockLayoutAlgorithm(
          {node, fragment_geometry, space, To<NGBlockBreakToken>(break_token)})
          .Layout();

  return To<NGPhysicalBoxFragment>(&result->PhysicalFragment());
}

std::pair<scoped_refptr<const NGPhysicalBoxFragment>, NGConstraintSpace>
NGBaseLayoutAlgorithmTest::RunBlockLayoutAlgorithmForElement(Element* element) {
  auto* block_flow = To<LayoutBlockFlow>(element->GetLayoutObject());
  NGBlockNode node(block_flow);
  NGConstraintSpace space =
      NGConstraintSpace::CreateFromLayoutObject(*block_flow);
  NGFragmentGeometry fragment_geometry =
      CalculateInitialFragmentGeometry(space, node);

  scoped_refptr<const NGLayoutResult> result =
      NGBlockLayoutAlgorithm({node, fragment_geometry, space}).Layout();
  return std::make_pair(To<NGPhysicalBoxFragment>(&result->PhysicalFragment()),
                        std::move(space));
}

scoped_refptr<const NGPhysicalBoxFragment>
NGBaseLayoutAlgorithmTest::RunFieldsetLayoutAlgorithm(
    NGBlockNode node,
    const NGConstraintSpace& space,
    const NGBreakToken* break_token) {
  NGFragmentGeometry fragment_geometry =
      CalculateInitialFragmentGeometry(space, node);

  scoped_refptr<const NGLayoutResult> result =
      NGFieldsetLayoutAlgorithm(
          {node, fragment_geometry, space, To<NGBlockBreakToken>(break_token)})
          .Layout();

  return To<NGPhysicalBoxFragment>(&result->PhysicalFragment());
}

scoped_refptr<const NGPhysicalBoxFragment>
NGBaseLayoutAlgorithmTest::GetBoxFragmentByElementId(const char* id) {
  LayoutObject* layout_object = GetLayoutObjectByElementId(id);
  CHECK(layout_object && layout_object->IsLayoutNGMixin());
  scoped_refptr<const NGPhysicalBoxFragment> fragment =
      To<LayoutBlockFlow>(layout_object)->GetPhysicalFragment(0);
  CHECK(fragment);
  return fragment;
}

const NGPhysicalBoxFragment* NGBaseLayoutAlgorithmTest::CurrentFragmentFor(
    const LayoutNGBlockFlow* block_flow) {
  return block_flow->GetPhysicalFragment(0);
}

const NGPhysicalBoxFragment* FragmentChildIterator::NextChild(
    PhysicalOffset* fragment_offset) {
  if (!parent_)
    return nullptr;
  if (index_ >= parent_->Children().size())
    return nullptr;
  while (parent_->Children()[index_]->Type() !=
         NGPhysicalFragment::kFragmentBox) {
    ++index_;
    if (index_ >= parent_->Children().size())
      return nullptr;
  }
  auto& child = parent_->Children()[index_++];
  if (fragment_offset)
    *fragment_offset = child.Offset();
  return To<NGPhysicalBoxFragment>(child.get());
}

NGConstraintSpace ConstructBlockLayoutTestConstraintSpace(
    WritingDirectionMode writing_direction,
    LogicalSize size,
    bool stretch_inline_size_if_auto,
    bool is_new_formatting_context,
    LayoutUnit fragmentainer_space_available) {
  NGFragmentationType block_fragmentation =
      fragmentainer_space_available != kIndefiniteSize
          ? NGFragmentationType::kFragmentColumn
          : NGFragmentationType::kFragmentNone;

  NGConstraintSpaceBuilder builder(writing_direction.GetWritingMode(),
                                   writing_direction,
                                   is_new_formatting_context);
  builder.SetAvailableSize(size);
  builder.SetPercentageResolutionSize(size);
  builder.SetStretchInlineSizeIfAuto(stretch_inline_size_if_auto);
  builder.SetFragmentainerBlockSize(fragmentainer_space_available);
  builder.SetFragmentationType(block_fragmentation);
  return builder.ToConstraintSpace();
}

}  // namespace blink