1 // Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "include/views/cef_box_layout.h"
6 #include "include/views/cef_fill_layout.h"
7 #include "include/views/cef_layout.h"
8 #include "include/views/cef_panel.h"
9 #include "include/views/cef_panel_delegate.h"
10 #include "include/views/cef_window.h"
11 #include "tests/ceftests/thread_helper.h"
12 #include "tests/gtest/include/gtest/gtest.h"
13
14 #define PANEL_TEST(name) UI_THREAD_TEST(ViewsPanelTest, name)
15
16 namespace {
17
18 class EmptyPanelDelegate : public CefPanelDelegate {
19 public:
EmptyPanelDelegate()20 EmptyPanelDelegate() {}
21
22 private:
23 IMPLEMENT_REFCOUNTING(EmptyPanelDelegate);
24 DISALLOW_COPY_AND_ASSIGN(EmptyPanelDelegate);
25 };
26
CreatePanel(CefRefPtr<CefPanelDelegate> delegate)27 void CreatePanel(CefRefPtr<CefPanelDelegate> delegate) {
28 CefRefPtr<CefPanel> panel = CefPanel::CreatePanel(delegate);
29 EXPECT_TRUE(panel.get());
30
31 // Verify the derived View relationship.
32 EXPECT_TRUE(panel->AsPanel().get());
33 EXPECT_FALSE(panel->AsWindow().get());
34 EXPECT_TRUE(panel->IsSame(panel));
35
36 // Verify default View state.
37 EXPECT_STREQ("Panel", panel->GetTypeString().ToString().c_str());
38 EXPECT_TRUE(panel->IsValid());
39 EXPECT_FALSE(panel->IsAttached());
40 if (delegate)
41 EXPECT_EQ(delegate.get(), panel->GetDelegate().get());
42 else
43 EXPECT_FALSE(panel->GetDelegate().get());
44 EXPECT_EQ(0, panel->GetID());
45 EXPECT_FALSE(panel->GetParentView().get());
46 EXPECT_EQ(CefRect(0, 0, 0, 0), panel->GetBounds());
47 EXPECT_EQ(CefRect(0, 0, 0, 0), panel->GetBoundsInScreen());
48 EXPECT_EQ(CefSize(0, 0), panel->GetPreferredSize());
49 EXPECT_EQ(CefSize(0, 0), panel->GetMinimumSize());
50 EXPECT_EQ(CefSize(0, 0), panel->GetMaximumSize());
51 EXPECT_EQ(CefSize(0, 0), panel->GetMaximumSize());
52 EXPECT_EQ(0, panel->GetHeightForWidth(100));
53 EXPECT_TRUE(panel->IsVisible());
54 EXPECT_FALSE(panel->IsDrawn());
55 EXPECT_TRUE(panel->IsEnabled());
56 EXPECT_FALSE(panel->IsFocusable());
57 EXPECT_FALSE(panel->IsAccessibilityFocusable());
58 EXPECT_EQ(CefColorSetARGB(255, 255, 255, 255), panel->GetBackgroundColor());
59
60 // Verify default Panel state.
61 EXPECT_TRUE(panel->GetLayout().get());
62 EXPECT_EQ(0U, panel->GetChildViewCount());
63
64 // Destroy the Panel.
65 panel = nullptr;
66
67 if (delegate) {
68 // Verify that nothing is keeping a reference to the delegate.
69 EXPECT_TRUE(delegate->HasOneRef());
70 }
71 }
72
CreatePanelNoDelegateImpl()73 void CreatePanelNoDelegateImpl() {
74 CreatePanel(nullptr);
75 }
76
CreatePanelWithDelegateImpl()77 void CreatePanelWithDelegateImpl() {
78 CreatePanel(new EmptyPanelDelegate);
79 }
80
81 } // namespace
82
83 // Test creation.
84 PANEL_TEST(CreatePanelNoDelegate)
85 PANEL_TEST(CreatePanelWithDelegate)
86
87 namespace {
88
89 class ParentPanelDelegate : public CefPanelDelegate {
90 public:
ParentPanelDelegate()91 ParentPanelDelegate() {}
92
OnParentViewChanged(CefRefPtr<CefView> view,bool added,CefRefPtr<CefView> parent)93 void OnParentViewChanged(CefRefPtr<CefView> view,
94 bool added,
95 CefRefPtr<CefView> parent) override {
96 EXPECT_FALSE(true); // Not reached.
97 }
98
OnChildViewChanged(CefRefPtr<CefView> view,bool added,CefRefPtr<CefView> child)99 void OnChildViewChanged(CefRefPtr<CefView> view,
100 bool added,
101 CefRefPtr<CefView> child) override {
102 Changed changed;
103 changed.view_ = view;
104 changed.added_ = added;
105 changed.child_ = child;
106 changed_.push_back(changed);
107 }
108
Verify(int callback_index,CefRefPtr<CefView> view,bool added,CefRefPtr<CefView> child)109 void Verify(int callback_index,
110 CefRefPtr<CefView> view,
111 bool added,
112 CefRefPtr<CefView> child) {
113 EXPECT_LT(callback_index, static_cast<int>(changed_.size()));
114 EXPECT_TRUE(view->IsSame(changed_[callback_index].view_))
115 << "callback_index " << callback_index;
116 EXPECT_EQ(added, changed_[callback_index].added_)
117 << "callback_index " << callback_index;
118 EXPECT_TRUE(child->IsSame(changed_[callback_index].child_))
119 << "callback_index " << callback_index;
120 }
121
Reset()122 void Reset() { changed_.clear(); }
123
IsReset() const124 bool IsReset() const { return changed_.empty(); }
125
126 struct Changed {
127 CefRefPtr<CefView> view_;
128 bool added_ = false;
129 CefRefPtr<CefView> child_;
130 };
131 std::vector<Changed> changed_;
132
133 private:
134 IMPLEMENT_REFCOUNTING(ParentPanelDelegate);
135 DISALLOW_COPY_AND_ASSIGN(ParentPanelDelegate);
136 };
137
138 class ChildPanelDelegate : public CefPanelDelegate {
139 public:
ChildPanelDelegate()140 ChildPanelDelegate() {}
141
OnParentViewChanged(CefRefPtr<CefView> view,bool added,CefRefPtr<CefView> parent)142 void OnParentViewChanged(CefRefPtr<CefView> view,
143 bool added,
144 CefRefPtr<CefView> parent) override {
145 EXPECT_FALSE(on_parent_view_changed_);
146 on_parent_view_changed_ = true;
147 view_ = view;
148 added_ = added;
149 parent_ = parent;
150 }
151
OnChildViewChanged(CefRefPtr<CefView> view,bool added,CefRefPtr<CefView> child)152 void OnChildViewChanged(CefRefPtr<CefView> view,
153 bool added,
154 CefRefPtr<CefView> child) override {
155 EXPECT_FALSE(true); // Not reached.
156 }
157
Verify(CefRefPtr<CefView> view,bool added,CefRefPtr<CefView> parent)158 void Verify(CefRefPtr<CefView> view, bool added, CefRefPtr<CefView> parent) {
159 EXPECT_TRUE(on_parent_view_changed_);
160 EXPECT_TRUE(view->IsSame(view_));
161 EXPECT_EQ(added, added_);
162 EXPECT_TRUE(parent->IsSame(parent_));
163 }
164
Reset()165 void Reset() {
166 on_parent_view_changed_ = false;
167 view_ = nullptr;
168 added_ = false;
169 parent_ = nullptr;
170 }
171
IsReset() const172 bool IsReset() const { return !on_parent_view_changed_; }
173
174 bool on_parent_view_changed_ = false;
175 CefRefPtr<CefView> view_;
176 bool added_ = false;
177 CefRefPtr<CefView> parent_;
178
179 private:
180 IMPLEMENT_REFCOUNTING(ChildPanelDelegate);
181 DISALLOW_COPY_AND_ASSIGN(ChildPanelDelegate);
182 };
183
ChildVerifyRemovedState(CefRefPtr<ParentPanelDelegate> parent_delegate,CefRefPtr<CefPanel> parent_panel,CefRefPtr<ChildPanelDelegate> child_delegate,CefRefPtr<CefPanel> child_panel)184 void ChildVerifyRemovedState(CefRefPtr<ParentPanelDelegate> parent_delegate,
185 CefRefPtr<CefPanel> parent_panel,
186 CefRefPtr<ChildPanelDelegate> child_delegate,
187 CefRefPtr<CefPanel> child_panel) {
188 EXPECT_FALSE(parent_panel->IsSame(child_panel));
189 EXPECT_FALSE(child_panel->IsSame(parent_panel));
190 EXPECT_FALSE(parent_panel->IsAttached());
191 EXPECT_FALSE(child_panel->IsAttached());
192 EXPECT_FALSE(parent_panel->GetParentView().get());
193 EXPECT_FALSE(child_panel->GetParentView().get());
194 }
195
ChildVerifyAddedState(CefRefPtr<ParentPanelDelegate> parent_delegate,CefRefPtr<CefPanel> parent_panel,CefRefPtr<ChildPanelDelegate> child_delegate,CefRefPtr<CefPanel> child_panel,int expected_child_index)196 void ChildVerifyAddedState(CefRefPtr<ParentPanelDelegate> parent_delegate,
197 CefRefPtr<CefPanel> parent_panel,
198 CefRefPtr<ChildPanelDelegate> child_delegate,
199 CefRefPtr<CefPanel> child_panel,
200 int expected_child_index) {
201 EXPECT_FALSE(parent_panel->IsSame(child_panel));
202 EXPECT_FALSE(child_panel->IsSame(parent_panel));
203 EXPECT_FALSE(parent_panel->IsAttached());
204 EXPECT_TRUE(child_panel->IsAttached());
205 EXPECT_TRUE(
206 child_panel->IsSame(parent_panel->GetChildViewAt(expected_child_index)));
207 EXPECT_TRUE(child_panel->GetParentView()->IsSame(parent_panel));
208 }
209
ChildVerifyFinalCallbackState(CefRefPtr<ParentPanelDelegate> parent_delegate,CefRefPtr<CefPanel> parent_panel,CefRefPtr<ChildPanelDelegate> child_delegate,CefRefPtr<CefPanel> child_panel,int expected_parent_callback_index,bool added)210 void ChildVerifyFinalCallbackState(
211 CefRefPtr<ParentPanelDelegate> parent_delegate,
212 CefRefPtr<CefPanel> parent_panel,
213 CefRefPtr<ChildPanelDelegate> child_delegate,
214 CefRefPtr<CefPanel> child_panel,
215 int expected_parent_callback_index,
216 bool added) {
217 parent_delegate->Verify(expected_parent_callback_index, parent_panel, added,
218 child_panel);
219 child_delegate->Verify(child_panel, added, parent_panel);
220 }
221
ChildAdd(CefRefPtr<ParentPanelDelegate> parent_delegate,CefRefPtr<CefPanel> parent_panel,CefRefPtr<ChildPanelDelegate> child_delegate,CefRefPtr<CefPanel> child_panel,int expected_child_index=0,int expected_parent_callback_index=0)222 void ChildAdd(CefRefPtr<ParentPanelDelegate> parent_delegate,
223 CefRefPtr<CefPanel> parent_panel,
224 CefRefPtr<ChildPanelDelegate> child_delegate,
225 CefRefPtr<CefPanel> child_panel,
226 int expected_child_index = 0,
227 int expected_parent_callback_index = 0) {
228 // Verify initial parent/child state.
229 ChildVerifyRemovedState(parent_delegate, parent_panel, child_delegate,
230 child_panel);
231
232 // Verify initial child callback state.
233 EXPECT_TRUE(child_delegate->IsReset());
234
235 // Add the child view.
236 parent_panel->AddChildView(child_panel);
237
238 // Verify final callback state.
239 ChildVerifyFinalCallbackState(parent_delegate, parent_panel, child_delegate,
240 child_panel, expected_parent_callback_index,
241 true);
242
243 // Reset child callback state.
244 child_delegate->Reset();
245
246 // Verify final parent/child state.
247 ChildVerifyAddedState(parent_delegate, parent_panel, child_delegate,
248 child_panel, expected_child_index);
249 }
250
ChildAddAt(CefRefPtr<ParentPanelDelegate> parent_delegate,CefRefPtr<CefPanel> parent_panel,CefRefPtr<ChildPanelDelegate> child_delegate,CefRefPtr<CefPanel> child_panel,int child_index,int expected_parent_callback_index)251 void ChildAddAt(CefRefPtr<ParentPanelDelegate> parent_delegate,
252 CefRefPtr<CefPanel> parent_panel,
253 CefRefPtr<ChildPanelDelegate> child_delegate,
254 CefRefPtr<CefPanel> child_panel,
255 int child_index,
256 int expected_parent_callback_index) {
257 // Verify initial parent/child state.
258 ChildVerifyRemovedState(parent_delegate, parent_panel, child_delegate,
259 child_panel);
260
261 // Verify initial child callback state.
262 EXPECT_TRUE(child_delegate->IsReset());
263
264 // Add the child view.
265 parent_panel->AddChildViewAt(child_panel, child_index);
266
267 // Verify final callback state.
268 ChildVerifyFinalCallbackState(parent_delegate, parent_panel, child_delegate,
269 child_panel, expected_parent_callback_index,
270 true);
271
272 // Reset child callback state.
273 child_delegate->Reset();
274
275 // Verify final parent/child state.
276 ChildVerifyAddedState(parent_delegate, parent_panel, child_delegate,
277 child_panel, child_index);
278 }
279
ChildRemove(CefRefPtr<ParentPanelDelegate> parent_delegate,CefRefPtr<CefPanel> parent_panel,CefRefPtr<ChildPanelDelegate> child_delegate,CefRefPtr<CefPanel> child_panel,bool remove_all,int expected_child_index=0,int expected_parent_callback_index=0)280 void ChildRemove(CefRefPtr<ParentPanelDelegate> parent_delegate,
281 CefRefPtr<CefPanel> parent_panel,
282 CefRefPtr<ChildPanelDelegate> child_delegate,
283 CefRefPtr<CefPanel> child_panel,
284 bool remove_all,
285 int expected_child_index = 0,
286 int expected_parent_callback_index = 0) {
287 // Verify initial parent/child state.
288 ChildVerifyAddedState(parent_delegate, parent_panel, child_delegate,
289 child_panel, expected_child_index);
290
291 // Verify initial child callback state.
292 EXPECT_TRUE(child_delegate->IsReset());
293
294 // Remove the child view.
295 if (remove_all)
296 parent_panel->RemoveAllChildViews();
297 else
298 parent_panel->RemoveChildView(child_panel);
299
300 // Verify final callback state.
301 ChildVerifyFinalCallbackState(parent_delegate, parent_panel, child_delegate,
302 child_panel, expected_parent_callback_index,
303 false);
304
305 // Reset child callback state.
306 child_delegate->Reset();
307
308 // Verify final parent/child state.
309 ChildVerifyRemovedState(parent_delegate, parent_panel, child_delegate,
310 child_panel);
311 }
312
ChildAddRemoveSingleImpl()313 void ChildAddRemoveSingleImpl() {
314 CefRefPtr<ParentPanelDelegate> parent_delegate = new ParentPanelDelegate();
315 CefRefPtr<CefPanel> parent_panel = CefPanel::CreatePanel(parent_delegate);
316
317 CefRefPtr<ChildPanelDelegate> child_delegate = new ChildPanelDelegate();
318 CefRefPtr<CefPanel> child_panel = CefPanel::CreatePanel(child_delegate);
319
320 // Add and explicitly remove the child view.
321 EXPECT_TRUE(parent_delegate->IsReset());
322 ChildAdd(parent_delegate, parent_panel, child_delegate, child_panel);
323 parent_delegate->Reset();
324
325 ChildRemove(parent_delegate, parent_panel, child_delegate, child_panel,
326 false);
327 parent_delegate->Reset();
328
329 // Add and implicitly remove the child view.
330 ChildAdd(parent_delegate, parent_panel, child_delegate, child_panel);
331 parent_delegate->Reset();
332
333 ChildRemove(parent_delegate, parent_panel, child_delegate, child_panel,
334 false);
335 parent_delegate->Reset();
336 }
337
ChildAddRemoveMultipleImpl()338 void ChildAddRemoveMultipleImpl() {
339 CefRefPtr<ParentPanelDelegate> parent_delegate = new ParentPanelDelegate();
340 CefRefPtr<CefPanel> parent_panel = CefPanel::CreatePanel(parent_delegate);
341
342 CefRefPtr<ChildPanelDelegate> child_delegate1 = new ChildPanelDelegate();
343 CefRefPtr<CefPanel> child_panel1 = CefPanel::CreatePanel(child_delegate1);
344
345 CefRefPtr<ChildPanelDelegate> child_delegate2 = new ChildPanelDelegate();
346 CefRefPtr<CefPanel> child_panel2 = CefPanel::CreatePanel(child_delegate2);
347
348 // Add multiple child views.
349 EXPECT_TRUE(parent_delegate->IsReset());
350 ChildAdd(parent_delegate, parent_panel, child_delegate1, child_panel1, 0, 0);
351 EXPECT_TRUE(child_delegate2->IsReset()); // child2 not called.
352 ChildAdd(parent_delegate, parent_panel, child_delegate2, child_panel2, 1, 1);
353 EXPECT_TRUE(child_delegate1->IsReset()); // child1 not called.
354 parent_delegate->Reset();
355
356 EXPECT_EQ(2U, parent_panel->GetChildViewCount());
357
358 // Explicitly remove specific child views.
359 ChildRemove(parent_delegate, parent_panel, child_delegate1, child_panel1,
360 false, 0, 0);
361 EXPECT_TRUE(child_delegate2->IsReset()); // child2 not called.
362 ChildRemove(parent_delegate, parent_panel, child_delegate2, child_panel2,
363 false, 0, 1);
364 EXPECT_TRUE(child_delegate1->IsReset()); // child1 not called.
365 parent_delegate->Reset();
366
367 EXPECT_EQ(0U, parent_panel->GetChildViewCount());
368
369 // Add multiple child views.
370 ChildAdd(parent_delegate, parent_panel, child_delegate1, child_panel1, 0, 0);
371 EXPECT_TRUE(child_delegate2->IsReset()); // child2 not called.
372 ChildAdd(parent_delegate, parent_panel, child_delegate2, child_panel2, 1, 1);
373 EXPECT_TRUE(child_delegate1->IsReset()); // child1 not called.
374 parent_delegate->Reset();
375
376 EXPECT_EQ(2U, parent_panel->GetChildViewCount());
377
378 EXPECT_TRUE(child_delegate1->IsReset());
379 EXPECT_TRUE(child_delegate2->IsReset());
380
381 // Implicitly remove all child views.
382 parent_panel->RemoveAllChildViews();
383
384 // Verify final callback state.
385 ChildVerifyFinalCallbackState(parent_delegate, parent_panel, child_delegate1,
386 child_panel1, 0, false);
387 ChildVerifyFinalCallbackState(parent_delegate, parent_panel, child_delegate2,
388 child_panel2, 1, false);
389
390 EXPECT_EQ(0U, parent_panel->GetChildViewCount());
391
392 // Reset callback state.
393 parent_delegate->Reset();
394 child_delegate1->Reset();
395 child_delegate2->Reset();
396
397 // Verify final parent/child state.
398 ChildVerifyRemovedState(parent_delegate, parent_panel, child_delegate1,
399 child_panel1);
400 ChildVerifyRemovedState(parent_delegate, parent_panel, child_delegate2,
401 child_panel2);
402 }
403
ChildOrderImpl()404 void ChildOrderImpl() {
405 CefRefPtr<ParentPanelDelegate> parent_delegate = new ParentPanelDelegate();
406 CefRefPtr<CefPanel> parent_panel = CefPanel::CreatePanel(parent_delegate);
407
408 CefRefPtr<ChildPanelDelegate> child_delegate1 = new ChildPanelDelegate();
409 CefRefPtr<CefPanel> child_panel1 = CefPanel::CreatePanel(child_delegate1);
410
411 CefRefPtr<ChildPanelDelegate> child_delegate2 = new ChildPanelDelegate();
412 CefRefPtr<CefPanel> child_panel2 = CefPanel::CreatePanel(child_delegate2);
413
414 CefRefPtr<ChildPanelDelegate> child_delegate3 = new ChildPanelDelegate();
415 CefRefPtr<CefPanel> child_panel3 = CefPanel::CreatePanel(child_delegate3);
416
417 // Add child views at specific indexes.
418 ChildAddAt(parent_delegate, parent_panel, child_delegate2, child_panel2, 0,
419 0);
420 ChildAddAt(parent_delegate, parent_panel, child_delegate3, child_panel3, 0,
421 1);
422 ChildAddAt(parent_delegate, parent_panel, child_delegate1, child_panel1, 1,
423 2);
424 parent_delegate->Reset();
425
426 EXPECT_EQ(3U, parent_panel->GetChildViewCount());
427
428 // ChildAddAt() will verify these results but let's check again just to make
429 // sure.
430 EXPECT_TRUE(child_panel3->IsSame(parent_panel->GetChildViewAt(0)));
431 EXPECT_TRUE(child_panel1->IsSame(parent_panel->GetChildViewAt(1)));
432 EXPECT_TRUE(child_panel2->IsSame(parent_panel->GetChildViewAt(2)));
433
434 // Move panel2 to the front.
435 parent_panel->ReorderChildView(child_panel2, 0);
436
437 EXPECT_TRUE(child_panel2->IsSame(parent_panel->GetChildViewAt(0)));
438 EXPECT_TRUE(child_panel3->IsSame(parent_panel->GetChildViewAt(1)));
439 EXPECT_TRUE(child_panel1->IsSame(parent_panel->GetChildViewAt(2)));
440
441 // Move panel3 to the end.
442 parent_panel->ReorderChildView(child_panel3, -1);
443
444 EXPECT_TRUE(child_panel2->IsSame(parent_panel->GetChildViewAt(0)));
445 EXPECT_TRUE(child_panel1->IsSame(parent_panel->GetChildViewAt(1)));
446 EXPECT_TRUE(child_panel3->IsSame(parent_panel->GetChildViewAt(2)));
447 }
448
ChildVisibleImpl()449 void ChildVisibleImpl() {
450 CefRefPtr<CefPanel> parent_panel = CefPanel::CreatePanel(nullptr);
451 CefRefPtr<CefPanel> child_panel1 = CefPanel::CreatePanel(nullptr);
452 CefRefPtr<CefPanel> child_panel2 = CefPanel::CreatePanel(nullptr);
453
454 // Nothing drawn by default.
455 EXPECT_FALSE(parent_panel->IsDrawn());
456 EXPECT_FALSE(child_panel1->IsDrawn());
457 EXPECT_FALSE(child_panel2->IsDrawn());
458
459 // Everything visible by default.
460 EXPECT_TRUE(parent_panel->IsVisible());
461 EXPECT_TRUE(child_panel1->IsVisible());
462 EXPECT_TRUE(child_panel2->IsVisible());
463
464 parent_panel->AddChildView(child_panel1);
465 parent_panel->AddChildView(child_panel2);
466
467 // Still the same.
468 EXPECT_FALSE(parent_panel->IsDrawn());
469 EXPECT_FALSE(child_panel1->IsDrawn());
470 EXPECT_FALSE(child_panel2->IsDrawn());
471 EXPECT_TRUE(parent_panel->IsVisible());
472 EXPECT_TRUE(child_panel1->IsVisible());
473 EXPECT_TRUE(child_panel2->IsVisible());
474
475 child_panel1->SetVisible(false);
476
477 // Child1 not visible.
478 EXPECT_TRUE(parent_panel->IsVisible());
479 EXPECT_FALSE(child_panel1->IsVisible());
480 EXPECT_TRUE(child_panel2->IsVisible());
481
482 child_panel1->SetVisible(true);
483
484 // Everything visible.
485 EXPECT_TRUE(parent_panel->IsVisible());
486 EXPECT_TRUE(child_panel1->IsVisible());
487 EXPECT_TRUE(child_panel2->IsVisible());
488
489 parent_panel->SetVisible(false);
490
491 // Children visible.
492 EXPECT_FALSE(parent_panel->IsVisible());
493 EXPECT_TRUE(child_panel1->IsVisible());
494 EXPECT_TRUE(child_panel2->IsVisible());
495
496 parent_panel->SetVisible(true);
497
498 // Everything visible.
499 EXPECT_TRUE(parent_panel->IsVisible());
500 EXPECT_TRUE(child_panel1->IsVisible());
501 EXPECT_TRUE(child_panel2->IsVisible());
502 }
503
ChildDrawnImpl()504 void ChildDrawnImpl() {
505 CefRefPtr<CefPanel> parent_panel = CefPanel::CreatePanel(nullptr);
506 CefRefPtr<CefPanel> child_panel1 = CefPanel::CreatePanel(nullptr);
507 CefRefPtr<CefPanel> child_panel2 = CefPanel::CreatePanel(nullptr);
508
509 // Nothing drawn by default.
510 EXPECT_FALSE(parent_panel->IsDrawn());
511 EXPECT_FALSE(child_panel1->IsDrawn());
512 EXPECT_FALSE(child_panel2->IsDrawn());
513
514 // Everything visible by default.
515 EXPECT_TRUE(parent_panel->IsVisible());
516 EXPECT_TRUE(child_panel1->IsVisible());
517 EXPECT_TRUE(child_panel2->IsVisible());
518
519 parent_panel->AddChildView(child_panel1);
520 parent_panel->AddChildView(child_panel2);
521
522 // Create and show a Window.
523 CefRefPtr<CefWindow> window = CefWindow::CreateTopLevelWindow(nullptr);
524 window->AddChildView(parent_panel);
525 window->CenterWindow(CefSize(400, 400));
526 window->Show();
527
528 // Everything visible and drawn now.
529 EXPECT_TRUE(parent_panel->IsVisible());
530 EXPECT_TRUE(parent_panel->IsDrawn());
531 EXPECT_TRUE(child_panel1->IsVisible());
532 EXPECT_TRUE(child_panel1->IsDrawn());
533 EXPECT_TRUE(child_panel2->IsVisible());
534 EXPECT_TRUE(child_panel2->IsDrawn());
535
536 child_panel1->SetVisible(false);
537
538 // Child1 not visible or drawn.
539 EXPECT_TRUE(parent_panel->IsVisible());
540 EXPECT_TRUE(parent_panel->IsDrawn());
541 EXPECT_FALSE(child_panel1->IsVisible());
542 EXPECT_FALSE(child_panel1->IsDrawn());
543 EXPECT_TRUE(child_panel2->IsVisible());
544 EXPECT_TRUE(child_panel2->IsDrawn());
545
546 child_panel1->SetVisible(true);
547
548 // Everything visible and drawn.
549 EXPECT_TRUE(parent_panel->IsVisible());
550 EXPECT_TRUE(parent_panel->IsDrawn());
551 EXPECT_TRUE(child_panel1->IsVisible());
552 EXPECT_TRUE(child_panel1->IsDrawn());
553 EXPECT_TRUE(child_panel2->IsVisible());
554 EXPECT_TRUE(child_panel2->IsDrawn());
555
556 parent_panel->SetVisible(false);
557
558 // Children visible, but nothing drawn.
559 EXPECT_FALSE(parent_panel->IsVisible());
560 EXPECT_FALSE(parent_panel->IsDrawn());
561 EXPECT_TRUE(child_panel1->IsVisible());
562 EXPECT_FALSE(child_panel1->IsDrawn());
563 EXPECT_TRUE(child_panel2->IsVisible());
564 EXPECT_FALSE(child_panel2->IsDrawn());
565
566 parent_panel->SetVisible(true);
567
568 // Everything visible and drawn.
569 EXPECT_TRUE(parent_panel->IsVisible());
570 EXPECT_TRUE(parent_panel->IsDrawn());
571 EXPECT_TRUE(child_panel1->IsVisible());
572 EXPECT_TRUE(child_panel1->IsDrawn());
573 EXPECT_TRUE(child_panel2->IsVisible());
574 EXPECT_TRUE(child_panel2->IsDrawn());
575
576 // Close the window.
577 window->Close();
578 }
579
580 } // namespace
581
582 // Test child behaviors.
583 PANEL_TEST(ChildAddRemoveSingle)
584 PANEL_TEST(ChildAddRemoveMultiple)
585 PANEL_TEST(ChildOrder)
586 PANEL_TEST(ChildVisible)
587 PANEL_TEST(ChildDrawn)
588
589 namespace {
590
591 class SizingPanelDelegate : public CefPanelDelegate {
592 public:
SizingPanelDelegate()593 SizingPanelDelegate() {}
594
GetPreferredSize(CefRefPtr<CefView> view)595 CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
596 got_get_preferred_size_ = true;
597 view_ = view;
598 return preferred_size_;
599 }
600
GetMinimumSize(CefRefPtr<CefView> view)601 CefSize GetMinimumSize(CefRefPtr<CefView> view) override {
602 got_get_minimum_size_ = true;
603 view_ = view;
604 return minimum_size_;
605 }
606
GetMaximumSize(CefRefPtr<CefView> view)607 CefSize GetMaximumSize(CefRefPtr<CefView> view) override {
608 got_get_maximum_size_ = true;
609 view_ = view;
610 return maximum_size_;
611 }
612
GetHeightForWidth(CefRefPtr<CefView> view,int width)613 int GetHeightForWidth(CefRefPtr<CefView> view, int width) override {
614 got_get_height_for_width_ = true;
615 view_ = view;
616 width_ = width;
617 return height_for_width_;
618 }
619
Reset()620 void Reset() {
621 preferred_size_ = CefSize(0, 0);
622 minimum_size_ = CefSize(0, 0);
623 maximum_size_ = CefSize(0, 0);
624 height_for_width_ = 0;
625 got_get_preferred_size_ = false;
626 got_get_minimum_size_ = false;
627 got_get_maximum_size_ = false;
628 got_get_height_for_width_ = false;
629 view_ = nullptr;
630 width_ = 0;
631 }
632
IsReset() const633 bool IsReset() const {
634 return !got_get_preferred_size_ && !got_get_minimum_size_ &&
635 !got_get_maximum_size_ && !got_get_height_for_width_;
636 }
637
638 CefSize preferred_size_;
639 CefSize minimum_size_;
640 CefSize maximum_size_;
641 int height_for_width_ = 0;
642
643 bool got_get_preferred_size_ = false;
644 bool got_get_minimum_size_ = false;
645 bool got_get_maximum_size_ = false;
646 bool got_get_height_for_width_ = false;
647
648 CefRefPtr<CefView> view_;
649 int width_ = 0;
650
651 private:
652 IMPLEMENT_REFCOUNTING(SizingPanelDelegate);
653 DISALLOW_COPY_AND_ASSIGN(SizingPanelDelegate);
654 };
655
SizeNoDelegateImpl()656 void SizeNoDelegateImpl() {
657 CefRefPtr<SizingPanelDelegate> delegate = new SizingPanelDelegate();
658 CefRefPtr<CefPanel> panel = CefPanel::CreatePanel(delegate);
659
660 // Default bounds are empty.
661 EXPECT_EQ(CefRect(0, 0, 0, 0), panel->GetBounds());
662
663 // Set and get the bounds.
664 panel->SetBounds(CefRect(100, 100, 200, 200));
665 EXPECT_EQ(CefRect(100, 100, 200, 200), panel->GetBounds());
666 EXPECT_EQ(CefSize(200, 200), panel->GetSize());
667 EXPECT_EQ(CefPoint(100, 100), panel->GetPosition());
668
669 // GetBoundsInScreen() drops the position because there is no Window.
670 EXPECT_EQ(CefRect(0, 0, 200, 200), panel->GetBoundsInScreen());
671
672 // Adjust the position but keep the size the same.
673 panel->SetPosition(CefPoint(50, 50));
674 EXPECT_EQ(CefRect(50, 50, 200, 200), panel->GetBounds());
675 EXPECT_EQ(CefSize(200, 200), panel->GetSize());
676 EXPECT_EQ(CefPoint(50, 50), panel->GetPosition());
677
678 // Adjust the size but keep the position the same.
679 panel->SetSize(CefSize(400, 400));
680 EXPECT_EQ(CefRect(50, 50, 400, 400), panel->GetBounds());
681 EXPECT_EQ(CefSize(400, 400), panel->GetSize());
682 EXPECT_EQ(CefPoint(50, 50), panel->GetPosition());
683
684 // No delegate methods were called during this test.
685 EXPECT_TRUE(delegate->IsReset());
686 }
687
SizeWithDelegateImpl()688 void SizeWithDelegateImpl() {
689 CefRefPtr<SizingPanelDelegate> delegate = new SizingPanelDelegate();
690 CefRefPtr<CefPanel> panel = CefPanel::CreatePanel(delegate);
691
692 // Default bounds are empty.
693 EXPECT_EQ(CefRect(0, 0, 0, 0), panel->GetBounds());
694
695 CefSize expected_size(100, 100);
696
697 // Test GetPreferredSize().
698 delegate->preferred_size_ = expected_size;
699 EXPECT_EQ(expected_size, panel->GetPreferredSize());
700 EXPECT_TRUE(delegate->got_get_preferred_size_);
701 EXPECT_FALSE(delegate->got_get_minimum_size_);
702 EXPECT_FALSE(delegate->got_get_maximum_size_);
703 EXPECT_FALSE(delegate->got_get_height_for_width_);
704 EXPECT_TRUE(panel->IsSame(delegate->view_));
705 delegate->Reset();
706
707 // Test GetMinimumSize().
708 delegate->minimum_size_ = expected_size;
709 EXPECT_EQ(expected_size, panel->GetMinimumSize());
710 EXPECT_FALSE(delegate->got_get_preferred_size_);
711 EXPECT_TRUE(delegate->got_get_minimum_size_);
712 EXPECT_FALSE(delegate->got_get_maximum_size_);
713 EXPECT_FALSE(delegate->got_get_height_for_width_);
714 EXPECT_TRUE(panel->IsSame(delegate->view_));
715 delegate->Reset();
716
717 // Test GetMaximumSize().
718 delegate->maximum_size_ = expected_size;
719 EXPECT_EQ(expected_size, panel->GetMaximumSize());
720 EXPECT_FALSE(delegate->got_get_preferred_size_);
721 EXPECT_FALSE(delegate->got_get_minimum_size_);
722 EXPECT_TRUE(delegate->got_get_maximum_size_);
723 EXPECT_FALSE(delegate->got_get_height_for_width_);
724 EXPECT_TRUE(panel->IsSame(delegate->view_));
725 delegate->Reset();
726
727 int expected_width = 200;
728 int expected_height = 100;
729
730 // Test GetHeightForWidth().
731 delegate->height_for_width_ = expected_height;
732 EXPECT_EQ(expected_height, panel->GetHeightForWidth(expected_width));
733 EXPECT_FALSE(delegate->got_get_preferred_size_);
734 EXPECT_FALSE(delegate->got_get_minimum_size_);
735 EXPECT_FALSE(delegate->got_get_maximum_size_);
736 EXPECT_TRUE(delegate->got_get_height_for_width_);
737 EXPECT_EQ(expected_width, delegate->width_);
738 EXPECT_TRUE(panel->IsSame(delegate->view_));
739 delegate->Reset();
740 }
741
742 } // namespace
743
744 // Test sizing.
745 PANEL_TEST(SizeNoDelegate)
746 PANEL_TEST(SizeWithDelegate)
747
748 namespace {
749
FillLayoutCreateImpl()750 void FillLayoutCreateImpl() {
751 CefRefPtr<CefPanel> panel = CefPanel::CreatePanel(nullptr);
752
753 // Explicitly set to FillLayout.
754 panel->SetToFillLayout();
755
756 CefRefPtr<CefLayout> layout = panel->GetLayout();
757 EXPECT_TRUE(layout.get());
758 EXPECT_TRUE(layout->AsFillLayout().get());
759 }
760
FillLayoutSizeToPreferredSizeImpl()761 void FillLayoutSizeToPreferredSizeImpl() {
762 CefRefPtr<SizingPanelDelegate> delegate = new SizingPanelDelegate();
763 CefRefPtr<CefPanel> panel = CefPanel::CreatePanel(delegate);
764
765 // Default Layout is FillLayout.
766 CefRefPtr<CefLayout> layout = panel->GetLayout();
767 EXPECT_TRUE(layout.get());
768 EXPECT_TRUE(layout->AsFillLayout().get());
769
770 // Default bounds are empty.
771 EXPECT_EQ(CefRect(0, 0, 0, 0), panel->GetBounds());
772
773 CefSize expected_size(100, 100);
774
775 delegate->preferred_size_ = expected_size;
776
777 // Trigger use of the preferred size.
778 panel->Layout();
779
780 EXPECT_TRUE(delegate->got_get_preferred_size_);
781 EXPECT_FALSE(delegate->got_get_minimum_size_);
782 EXPECT_FALSE(delegate->got_get_maximum_size_);
783 EXPECT_FALSE(delegate->got_get_height_for_width_);
784 EXPECT_TRUE(panel->IsSame(delegate->view_));
785 delegate->Reset();
786
787 // Size is now the preferred size.
788 EXPECT_EQ(expected_size, panel->GetSize());
789
790 // No additional delegate methods were called.
791 EXPECT_TRUE(delegate->IsReset());
792 }
793
FillLayoutSizeHierarchyImpl()794 void FillLayoutSizeHierarchyImpl() {
795 CefRefPtr<CefPanel> panel_parent = CefPanel::CreatePanel(nullptr);
796 CefRefPtr<CefPanel> panel_child = CefPanel::CreatePanel(nullptr);
797
798 CefSize expected_size(100, 100);
799
800 // Default Layout is FillLayout.
801 CefRefPtr<CefLayout> layout1 = panel_parent->GetLayout();
802 EXPECT_TRUE(layout1.get());
803 EXPECT_TRUE(layout1->AsFillLayout().get());
804
805 // Default bounds are empty.
806 EXPECT_EQ(CefRect(0, 0, 0, 0), panel_parent->GetBounds());
807 EXPECT_EQ(CefRect(0, 0, 0, 0), panel_child->GetBounds());
808
809 // Without delegates the size must be set on the parent.
810 panel_parent->SetSize(expected_size);
811
812 // FillLayout is the default Layout. Both panels should end up with the same
813 // size.
814 panel_parent->AddChildView(panel_child);
815
816 // Force layout.
817 panel_parent->Layout();
818
819 // Panels are now the same size.
820 EXPECT_EQ(expected_size, panel_parent->GetSize());
821 EXPECT_EQ(expected_size, panel_child->GetSize());
822
823 // Resize the parent panel to a larger size.
824 CefSize expected_size2(200, 200);
825 panel_parent->SetSize(expected_size2);
826
827 // Force layout.
828 panel_parent->Layout();
829
830 // Panels are now the larger size.
831 EXPECT_EQ(expected_size2, panel_parent->GetSize());
832 EXPECT_EQ(expected_size2, panel_child->GetSize());
833 }
834
FillLayoutSizeHierarchyWithDelegate(bool size_from_parent)835 void FillLayoutSizeHierarchyWithDelegate(bool size_from_parent) {
836 CefRefPtr<SizingPanelDelegate> delegate_parent = new SizingPanelDelegate();
837 CefRefPtr<CefPanel> panel_parent = CefPanel::CreatePanel(delegate_parent);
838 CefRefPtr<SizingPanelDelegate> delegate_child = new SizingPanelDelegate();
839 CefRefPtr<CefPanel> panel_child = CefPanel::CreatePanel(delegate_child);
840
841 CefSize expected_size(100, 100);
842
843 // The default layout is FillLayout, but explicitly set it anyways just for
844 // some testing variety.
845 panel_parent->SetToFillLayout();
846 panel_child->SetToFillLayout();
847
848 // Default bounds are empty.
849 EXPECT_EQ(CefRect(0, 0, 0, 0), panel_parent->GetBounds());
850 EXPECT_EQ(CefRect(0, 0, 0, 0), panel_child->GetBounds());
851
852 // With delegates the size can come from either the parent or child.
853 if (size_from_parent)
854 delegate_parent->preferred_size_ = expected_size;
855 else
856 delegate_child->preferred_size_ = expected_size;
857
858 // FillLayout is the default Layout. Both panels should end up with the same
859 // size.
860 panel_parent->AddChildView(panel_child);
861
862 // No delegate methods were called yet.
863 EXPECT_TRUE(delegate_parent->IsReset());
864 EXPECT_TRUE(delegate_child->IsReset());
865
866 // Force layout.
867 panel_parent->Layout();
868
869 // delegate_parent will be called to get the preferred size for panel_parent.
870 EXPECT_TRUE(delegate_parent->got_get_preferred_size_);
871 EXPECT_FALSE(delegate_parent->got_get_minimum_size_);
872 EXPECT_FALSE(delegate_parent->got_get_maximum_size_);
873 EXPECT_FALSE(delegate_parent->got_get_height_for_width_);
874 EXPECT_TRUE(panel_parent->IsSame(delegate_parent->view_));
875 delegate_parent->Reset();
876
877 // delegate_child will be called to get the preferred size for panel_child.
878 EXPECT_TRUE(delegate_child->got_get_preferred_size_);
879 EXPECT_FALSE(delegate_child->got_get_minimum_size_);
880 EXPECT_FALSE(delegate_child->got_get_maximum_size_);
881 EXPECT_FALSE(delegate_child->got_get_height_for_width_);
882 EXPECT_TRUE(panel_child->IsSame(delegate_child->view_));
883 delegate_child->Reset();
884
885 // Panels are now the same size.
886 EXPECT_EQ(expected_size, panel_parent->GetSize());
887 EXPECT_EQ(expected_size, panel_child->GetSize());
888
889 // Resize the parent panel to a larger size.
890 CefSize expected_size2(200, 200);
891 panel_parent->SetSize(expected_size2);
892
893 // Force layout.
894 panel_parent->Layout();
895
896 // Panels are now the larger size.
897 EXPECT_EQ(expected_size2, panel_parent->GetSize());
898 EXPECT_EQ(expected_size2, panel_child->GetSize());
899
900 // No additional delegate methods were called.
901 EXPECT_TRUE(delegate_parent->IsReset());
902 EXPECT_TRUE(delegate_child->IsReset());
903 }
904
FillLayoutSizeHierarchyFromParentWithDelegateImpl()905 void FillLayoutSizeHierarchyFromParentWithDelegateImpl() {
906 FillLayoutSizeHierarchyWithDelegate(true);
907 }
908
FillLayoutSizeHierarchyFromChildWithDelegateImpl()909 void FillLayoutSizeHierarchyFromChildWithDelegateImpl() {
910 FillLayoutSizeHierarchyWithDelegate(false);
911 }
912
913 } // namespace
914
915 // Test FillLayout.
916 PANEL_TEST(FillLayoutCreate)
917 PANEL_TEST(FillLayoutSizeToPreferredSize)
918 PANEL_TEST(FillLayoutSizeHierarchy)
919 PANEL_TEST(FillLayoutSizeHierarchyFromParentWithDelegate)
920 PANEL_TEST(FillLayoutSizeHierarchyFromChildWithDelegate)
921
922 namespace {
923
BoxLayoutCreateImpl()924 void BoxLayoutCreateImpl() {
925 CefRefPtr<CefPanel> panel = CefPanel::CreatePanel(nullptr);
926
927 CefBoxLayoutSettings settings;
928
929 // Explicitly set to BoxLayout.
930 panel->SetToBoxLayout(settings);
931
932 CefRefPtr<CefLayout> layout = panel->GetLayout();
933 EXPECT_TRUE(layout.get());
934 EXPECT_TRUE(layout->AsBoxLayout().get());
935 }
936
937 const int kBLParentSize = 100;
938 const int kBLChildSize = 10;
939
BoxLayoutSizeHierarchy(bool with_delegate,const CefBoxLayoutSettings & settings,const CefRect & expected_child1_bounds,const CefRect & expected_child2_bounds,int child1_flex=0,int child2_flex=0)940 void BoxLayoutSizeHierarchy(bool with_delegate,
941 const CefBoxLayoutSettings& settings,
942 const CefRect& expected_child1_bounds,
943 const CefRect& expected_child2_bounds,
944 int child1_flex = 0,
945 int child2_flex = 0) {
946 CefRefPtr<SizingPanelDelegate> delegate_parent;
947 if (with_delegate)
948 delegate_parent = new SizingPanelDelegate();
949 CefRefPtr<CefPanel> panel_parent = CefPanel::CreatePanel(delegate_parent);
950
951 CefRefPtr<SizingPanelDelegate> delegate_child1, delegate_child2;
952 if (with_delegate) {
953 delegate_child1 = new SizingPanelDelegate();
954 delegate_child2 = new SizingPanelDelegate();
955 }
956 CefRefPtr<CefPanel> panel_child1 = CefPanel::CreatePanel(delegate_child1);
957 CefRefPtr<CefPanel> panel_child2 = CefPanel::CreatePanel(delegate_child2);
958
959 // Default bounds are empty.
960 EXPECT_EQ(CefRect(0, 0, 0, 0), panel_parent->GetBounds());
961 EXPECT_EQ(CefRect(0, 0, 0, 0), panel_child1->GetBounds());
962 EXPECT_EQ(CefRect(0, 0, 0, 0), panel_child2->GetBounds());
963
964 // Give the parent a size.
965 CefSize initial_parent_size(kBLParentSize, kBLParentSize);
966 if (with_delegate)
967 delegate_parent->preferred_size_ = initial_parent_size;
968 else
969 panel_parent->SetSize(initial_parent_size);
970
971 // Give the children a size smaller than the parent.
972 CefSize initial_child_size(kBLChildSize, kBLChildSize);
973 if (with_delegate) {
974 delegate_child1->preferred_size_ = initial_child_size;
975 delegate_child2->preferred_size_ = initial_child_size;
976 } else {
977 panel_child1->SetSize(initial_child_size);
978 panel_child2->SetSize(initial_child_size);
979 }
980
981 // Set to BoxLayout with |settings|.
982 panel_parent->SetToBoxLayout(settings);
983
984 panel_parent->AddChildView(panel_child1);
985 panel_parent->AddChildView(panel_child2);
986
987 if (child1_flex > 0 || child2_flex > 0) {
988 // Flex will apply relative stretch in the main axis direction.
989 CefRefPtr<CefBoxLayout> layout = panel_parent->GetLayout()->AsBoxLayout();
990 if (child1_flex > 0)
991 layout->SetFlexForView(panel_child1, child1_flex);
992 if (child2_flex > 0)
993 layout->SetFlexForView(panel_child2, child2_flex);
994 }
995
996 if (with_delegate) {
997 // No delegate methods were called yet.
998 EXPECT_TRUE(delegate_parent->IsReset());
999 EXPECT_TRUE(delegate_child1->IsReset());
1000 EXPECT_TRUE(delegate_child2->IsReset());
1001 }
1002
1003 // Force layout.
1004 panel_parent->Layout();
1005
1006 if (with_delegate) {
1007 // delegate_parent will be called to get the preferred size for
1008 // panel_parent.
1009 EXPECT_TRUE(delegate_parent->got_get_preferred_size_);
1010 EXPECT_FALSE(delegate_parent->got_get_minimum_size_);
1011 EXPECT_FALSE(delegate_parent->got_get_maximum_size_);
1012 EXPECT_FALSE(delegate_parent->got_get_height_for_width_);
1013 EXPECT_TRUE(panel_parent->IsSame(delegate_parent->view_));
1014 delegate_parent->Reset();
1015
1016 // delegate_child1 will be called to get the preferred size for
1017 // panel_child1.
1018 // GetHeightForWidth may also be called depending on the settings.
1019 EXPECT_TRUE(delegate_child1->got_get_preferred_size_);
1020 EXPECT_FALSE(delegate_child1->got_get_minimum_size_);
1021 EXPECT_FALSE(delegate_child1->got_get_maximum_size_);
1022 EXPECT_TRUE(panel_child1->IsSame(delegate_child1->view_));
1023 delegate_child1->Reset();
1024
1025 // delegate_child2 will be called to get the preferred size for
1026 // panel_child2.
1027 // GetHeightForWidth may also be called depending on the settings.
1028 EXPECT_TRUE(delegate_child2->got_get_preferred_size_);
1029 EXPECT_FALSE(delegate_child2->got_get_minimum_size_);
1030 EXPECT_FALSE(delegate_child2->got_get_maximum_size_);
1031 EXPECT_TRUE(panel_child2->IsSame(delegate_child2->view_));
1032 delegate_child2->Reset();
1033 }
1034
1035 // The parent should be the same size.
1036 EXPECT_EQ(initial_parent_size, panel_parent->GetSize());
1037
1038 // Children should have the expected bounds.
1039 EXPECT_EQ(expected_child1_bounds, panel_child1->GetBounds());
1040 EXPECT_EQ(expected_child2_bounds, panel_child2->GetBounds());
1041
1042 if (with_delegate) {
1043 // No additional delegate methods were called.
1044 EXPECT_TRUE(delegate_parent->IsReset());
1045 EXPECT_TRUE(delegate_child1->IsReset());
1046 EXPECT_TRUE(delegate_child2->IsReset());
1047 }
1048 }
1049
BoxLayoutSizeHierarchyVerticalStretch(bool with_delegate)1050 void BoxLayoutSizeHierarchyVerticalStretch(bool with_delegate) {
1051 // Vertical layout with children stretched along the horizontal axis.
1052 //
1053 // -----------
1054 // |111111111|
1055 // |222222222|
1056 // | |
1057 // | |
1058 // | |
1059 // -----------
1060 //
1061 CefBoxLayoutSettings settings;
1062
1063 CefRect expected_child1_bounds(0, 0, kBLParentSize, kBLChildSize);
1064 CefRect expected_child2_bounds(0, kBLChildSize, kBLParentSize, kBLChildSize);
1065
1066 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1067 expected_child2_bounds);
1068 }
1069
BoxLayoutSizeHierarchyVerticalStretchImpl()1070 void BoxLayoutSizeHierarchyVerticalStretchImpl() {
1071 BoxLayoutSizeHierarchyVerticalStretch(false);
1072 }
1073
BoxLayoutSizeHierarchyVerticalStretchWithDelegateImpl()1074 void BoxLayoutSizeHierarchyVerticalStretchWithDelegateImpl() {
1075 BoxLayoutSizeHierarchyVerticalStretch(true);
1076 }
1077
BoxLayoutSizeHierarchyHorizontalStretch(bool with_delegate)1078 void BoxLayoutSizeHierarchyHorizontalStretch(bool with_delegate) {
1079 // Horizontal layout with children stretched along the vertical axis.
1080 //
1081 // -----------
1082 // |12 |
1083 // |12 |
1084 // |12 |
1085 // |12 |
1086 // |12 |
1087 // -----------
1088 //
1089 CefBoxLayoutSettings settings;
1090 settings.horizontal = true;
1091
1092 CefRect expected_child1_bounds(0, 0, kBLChildSize, kBLParentSize);
1093 CefRect expected_child2_bounds(kBLChildSize, 0, kBLChildSize, kBLParentSize);
1094
1095 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1096 expected_child2_bounds);
1097 }
1098
BoxLayoutSizeHierarchyHorizontalStretchImpl()1099 void BoxLayoutSizeHierarchyHorizontalStretchImpl() {
1100 BoxLayoutSizeHierarchyHorizontalStretch(false);
1101 }
1102
BoxLayoutSizeHierarchyHorizontalStretchWithDelegateImpl()1103 void BoxLayoutSizeHierarchyHorizontalStretchWithDelegateImpl() {
1104 BoxLayoutSizeHierarchyHorizontalStretch(true);
1105 }
1106
BoxLayoutSizeHierarchyVerticalCenter(bool with_delegate)1107 void BoxLayoutSizeHierarchyVerticalCenter(bool with_delegate) {
1108 // Vertical layout with children centered along the horizontal axis.
1109 //
1110 // -----------
1111 // | 1 |
1112 // | 2 |
1113 // | |
1114 // | |
1115 // | |
1116 // -----------
1117 //
1118 CefBoxLayoutSettings settings;
1119 settings.cross_axis_alignment = CEF_CROSS_AXIS_ALIGNMENT_CENTER;
1120
1121 int xoffset = (kBLParentSize - kBLChildSize) / 2;
1122 CefRect expected_child1_bounds(xoffset, 0, kBLChildSize, kBLChildSize);
1123 CefRect expected_child2_bounds(xoffset, kBLChildSize, kBLChildSize,
1124 kBLChildSize);
1125
1126 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1127 expected_child2_bounds);
1128 }
1129
BoxLayoutSizeHierarchyVerticalCenterImpl()1130 void BoxLayoutSizeHierarchyVerticalCenterImpl() {
1131 BoxLayoutSizeHierarchyVerticalCenter(false);
1132 }
1133
BoxLayoutSizeHierarchyVerticalCenterWithDelegateImpl()1134 void BoxLayoutSizeHierarchyVerticalCenterWithDelegateImpl() {
1135 BoxLayoutSizeHierarchyVerticalCenter(true);
1136 }
1137
BoxLayoutSizeHierarchyHorizontalCenter(bool with_delegate)1138 void BoxLayoutSizeHierarchyHorizontalCenter(bool with_delegate) {
1139 // Horizontal layout with children centered along the vertical axis.
1140 //
1141 // -----------
1142 // | |
1143 // | |
1144 // |12 |
1145 // | |
1146 // | |
1147 // -----------
1148 //
1149 CefBoxLayoutSettings settings;
1150 settings.horizontal = true;
1151 settings.cross_axis_alignment = CEF_CROSS_AXIS_ALIGNMENT_CENTER;
1152
1153 int yoffset = (kBLParentSize - kBLChildSize) / 2;
1154 CefRect expected_child1_bounds(0, yoffset, kBLChildSize, kBLChildSize);
1155 CefRect expected_child2_bounds(kBLChildSize, yoffset, kBLChildSize,
1156 kBLChildSize);
1157
1158 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1159 expected_child2_bounds);
1160 }
1161
BoxLayoutSizeHierarchyHorizontalCenterImpl()1162 void BoxLayoutSizeHierarchyHorizontalCenterImpl() {
1163 BoxLayoutSizeHierarchyHorizontalCenter(false);
1164 }
1165
BoxLayoutSizeHierarchyHorizontalCenterWithDelegateImpl()1166 void BoxLayoutSizeHierarchyHorizontalCenterWithDelegateImpl() {
1167 BoxLayoutSizeHierarchyHorizontalCenter(true);
1168 }
1169
BoxLayoutSizeHierarchyVerticalCenterCenter(bool with_delegate)1170 void BoxLayoutSizeHierarchyVerticalCenterCenter(bool with_delegate) {
1171 // Vertical layout with children centered along the horizontal and vertical
1172 // axis.
1173 //
1174 // -----------
1175 // | |
1176 // | 1 |
1177 // | 2 |
1178 // | |
1179 // -----------
1180 //
1181 CefBoxLayoutSettings settings;
1182 settings.main_axis_alignment = CEF_MAIN_AXIS_ALIGNMENT_CENTER;
1183 settings.cross_axis_alignment = CEF_CROSS_AXIS_ALIGNMENT_CENTER;
1184
1185 int xoffset = (kBLParentSize - kBLChildSize) / 2;
1186 int yoffset = (kBLParentSize - (kBLChildSize * 2)) / 2;
1187 CefRect expected_child1_bounds(xoffset, yoffset, kBLChildSize, kBLChildSize);
1188 CefRect expected_child2_bounds(xoffset, yoffset + kBLChildSize, kBLChildSize,
1189 kBLChildSize);
1190
1191 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1192 expected_child2_bounds);
1193 }
1194
BoxLayoutSizeHierarchyVerticalCenterCenterImpl()1195 void BoxLayoutSizeHierarchyVerticalCenterCenterImpl() {
1196 BoxLayoutSizeHierarchyVerticalCenterCenter(false);
1197 }
1198
BoxLayoutSizeHierarchyVerticalCenterCenterWithDelegateImpl()1199 void BoxLayoutSizeHierarchyVerticalCenterCenterWithDelegateImpl() {
1200 BoxLayoutSizeHierarchyVerticalCenterCenter(true);
1201 }
1202
BoxLayoutSizeHierarchyHorizontalCenterCenter(bool with_delegate)1203 void BoxLayoutSizeHierarchyHorizontalCenterCenter(bool with_delegate) {
1204 // Horizontal layout with children centered along the vertical and horizontal
1205 // axis.
1206 //
1207 // -----------
1208 // | |
1209 // | |
1210 // | 12 |
1211 // | |
1212 // | |
1213 // -----------
1214 //
1215 CefBoxLayoutSettings settings;
1216 settings.horizontal = true;
1217 settings.main_axis_alignment = CEF_MAIN_AXIS_ALIGNMENT_CENTER;
1218 settings.cross_axis_alignment = CEF_CROSS_AXIS_ALIGNMENT_CENTER;
1219
1220 int xoffset = (kBLParentSize - (kBLChildSize * 2)) / 2;
1221 int yoffset = (kBLParentSize - kBLChildSize) / 2;
1222 CefRect expected_child1_bounds(xoffset, yoffset, kBLChildSize, kBLChildSize);
1223 CefRect expected_child2_bounds(xoffset + kBLChildSize, yoffset, kBLChildSize,
1224 kBLChildSize);
1225
1226 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1227 expected_child2_bounds);
1228 }
1229
BoxLayoutSizeHierarchyHorizontalCenterCenterImpl()1230 void BoxLayoutSizeHierarchyHorizontalCenterCenterImpl() {
1231 BoxLayoutSizeHierarchyHorizontalCenterCenter(false);
1232 }
1233
BoxLayoutSizeHierarchyHorizontalCenterCenterWithDelegateImpl()1234 void BoxLayoutSizeHierarchyHorizontalCenterCenterWithDelegateImpl() {
1235 BoxLayoutSizeHierarchyHorizontalCenterCenter(true);
1236 }
1237
BoxLayoutSizeHierarchyVerticalStretchFlexOne(bool with_delegate)1238 void BoxLayoutSizeHierarchyVerticalStretchFlexOne(bool with_delegate) {
1239 // Vertical layout with child1 stretched along the horizontal and vertical
1240 // axis and child2 stretched along the horizontal axis only (unequal flex).
1241 //
1242 // -----------
1243 // |111111111|
1244 // |111111111|
1245 // |111111111|
1246 // |111111111|
1247 // |222222222|
1248 // -----------
1249 //
1250 CefBoxLayoutSettings settings;
1251
1252 CefRect expected_child1_bounds(0, 0, kBLParentSize,
1253 kBLParentSize - kBLChildSize);
1254 CefRect expected_child2_bounds(0, kBLParentSize - kBLChildSize, kBLParentSize,
1255 kBLChildSize);
1256
1257 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1258 expected_child2_bounds, 1, 0);
1259 }
1260
BoxLayoutSizeHierarchyVerticalStretchFlexOneImpl()1261 void BoxLayoutSizeHierarchyVerticalStretchFlexOneImpl() {
1262 BoxLayoutSizeHierarchyVerticalStretchFlexOne(false);
1263 }
1264
BoxLayoutSizeHierarchyVerticalStretchFlexOneWithDelegateImpl()1265 void BoxLayoutSizeHierarchyVerticalStretchFlexOneWithDelegateImpl() {
1266 BoxLayoutSizeHierarchyVerticalStretchFlexOne(true);
1267 }
1268
BoxLayoutSizeHierarchyHorizontalStretchFlexOne(bool with_delegate)1269 void BoxLayoutSizeHierarchyHorizontalStretchFlexOne(bool with_delegate) {
1270 // Horizontal layout with child1 stretched along the vertical and horizontal
1271 // axis and child2 stretched along the vertical axis only (unequal flex).
1272 //
1273 // -----------
1274 // |111111112|
1275 // |111111112|
1276 // |111111112|
1277 // |111111112|
1278 // |111111112|
1279 // -----------
1280 //
1281 CefBoxLayoutSettings settings;
1282 settings.horizontal = true;
1283
1284 CefRect expected_child1_bounds(0, 0, kBLParentSize - kBLChildSize,
1285 kBLParentSize);
1286 CefRect expected_child2_bounds(kBLParentSize - kBLChildSize, 0, kBLChildSize,
1287 kBLParentSize);
1288
1289 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1290 expected_child2_bounds, 1, 0);
1291 }
1292
BoxLayoutSizeHierarchyHorizontalStretchFlexOneImpl()1293 void BoxLayoutSizeHierarchyHorizontalStretchFlexOneImpl() {
1294 BoxLayoutSizeHierarchyHorizontalStretchFlexOne(false);
1295 }
1296
BoxLayoutSizeHierarchyHorizontalStretchFlexOneWithDelegateImpl()1297 void BoxLayoutSizeHierarchyHorizontalStretchFlexOneWithDelegateImpl() {
1298 BoxLayoutSizeHierarchyHorizontalStretchFlexOne(true);
1299 }
1300
BoxLayoutSizeHierarchyVerticalStretchFlexBoth(bool with_delegate)1301 void BoxLayoutSizeHierarchyVerticalStretchFlexBoth(bool with_delegate) {
1302 // Vertical layout with children stretched along the horizontal and vertical
1303 // axis (equal flex).
1304 //
1305 // -----------
1306 // |111111111|
1307 // |111111111|
1308 // |111111111|
1309 // |222222222|
1310 // |222222222|
1311 // |222222222|
1312 // -----------
1313 //
1314 CefBoxLayoutSettings settings;
1315
1316 CefRect expected_child1_bounds(0, 0, kBLParentSize, kBLParentSize / 2);
1317 CefRect expected_child2_bounds(0, kBLParentSize / 2, kBLParentSize,
1318 kBLParentSize / 2);
1319
1320 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1321 expected_child2_bounds, 1, 1);
1322 }
1323
BoxLayoutSizeHierarchyVerticalStretchFlexBothImpl()1324 void BoxLayoutSizeHierarchyVerticalStretchFlexBothImpl() {
1325 BoxLayoutSizeHierarchyVerticalStretchFlexBoth(false);
1326 }
1327
BoxLayoutSizeHierarchyVerticalStretchFlexBothWithDelegateImpl()1328 void BoxLayoutSizeHierarchyVerticalStretchFlexBothWithDelegateImpl() {
1329 BoxLayoutSizeHierarchyVerticalStretchFlexBoth(true);
1330 }
1331
BoxLayoutSizeHierarchyHorizontalStretchFlexBoth(bool with_delegate)1332 void BoxLayoutSizeHierarchyHorizontalStretchFlexBoth(bool with_delegate) {
1333 // Horizontal layout with children stretched along the vertical and horizontal
1334 // axis (equal flex).
1335 //
1336 // -----------
1337 // |111122222|
1338 // |111122222|
1339 // |111122222|
1340 // |111122222|
1341 // |111122222|
1342 // -----------
1343 //
1344 CefBoxLayoutSettings settings;
1345 settings.horizontal = true;
1346
1347 CefRect expected_child1_bounds(0, 0, kBLParentSize / 2, kBLParentSize);
1348 CefRect expected_child2_bounds(kBLParentSize / 2, 0, kBLParentSize / 2,
1349 kBLParentSize);
1350
1351 BoxLayoutSizeHierarchy(with_delegate, settings, expected_child1_bounds,
1352 expected_child2_bounds, 1, 1);
1353 }
1354
BoxLayoutSizeHierarchyHorizontalStretchFlexBothImpl()1355 void BoxLayoutSizeHierarchyHorizontalStretchFlexBothImpl() {
1356 BoxLayoutSizeHierarchyHorizontalStretchFlexBoth(false);
1357 }
1358
BoxLayoutSizeHierarchyHorizontalStretchFlexBothWithDelegateImpl()1359 void BoxLayoutSizeHierarchyHorizontalStretchFlexBothWithDelegateImpl() {
1360 BoxLayoutSizeHierarchyHorizontalStretchFlexBoth(true);
1361 }
1362
1363 } // namespace
1364
1365 // Test BoxLayout. The BoxLayoutSizeHierarchy* tests are representative but not
1366 // comprehensive (e.g. not all possible configurations are tested).
1367 PANEL_TEST(BoxLayoutCreate)
1368 PANEL_TEST(BoxLayoutSizeHierarchyVerticalStretch)
1369 PANEL_TEST(BoxLayoutSizeHierarchyVerticalStretchWithDelegate)
1370 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalStretch)
1371 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalStretchWithDelegate)
1372 PANEL_TEST(BoxLayoutSizeHierarchyVerticalCenter)
1373 PANEL_TEST(BoxLayoutSizeHierarchyVerticalCenterWithDelegate)
1374 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalCenter)
1375 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalCenterWithDelegate)
1376 PANEL_TEST(BoxLayoutSizeHierarchyVerticalCenterCenter)
1377 PANEL_TEST(BoxLayoutSizeHierarchyVerticalCenterCenterWithDelegate)
1378 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalCenterCenter)
1379 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalCenterCenterWithDelegate)
1380 PANEL_TEST(BoxLayoutSizeHierarchyVerticalStretchFlexOne)
1381 PANEL_TEST(BoxLayoutSizeHierarchyVerticalStretchFlexOneWithDelegate)
1382 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalStretchFlexOne)
1383 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalStretchFlexOneWithDelegate)
1384 PANEL_TEST(BoxLayoutSizeHierarchyVerticalStretchFlexBoth)
1385 PANEL_TEST(BoxLayoutSizeHierarchyVerticalStretchFlexBothWithDelegate)
1386 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalStretchFlexBoth)
1387 PANEL_TEST(BoxLayoutSizeHierarchyHorizontalStretchFlexBothWithDelegate)
1388