1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/chromeos/login/ui/oobe_dialog_size_utils.h"
6 
7 #include <stddef.h>
8 
9 #include "base/macros.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/geometry/insets.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/size.h"
14 
15 namespace chromeos {
16 
17 namespace {
18 
19 constexpr int kShelfHeight = 56;
20 constexpr int kVirtualKeyboardHeight = 280;
21 constexpr int kDockedMagnifierHeight = 235;
22 
23 }  // namespace
24 
25 class OobeDialogSizeUtilsTest : public testing::Test {
26  public:
27   OobeDialogSizeUtilsTest() = default;
28 
29   ~OobeDialogSizeUtilsTest() override = default;
30 
ValidateDialog(const gfx::Rect & area,const gfx::Rect & dialog)31   void ValidateDialog(const gfx::Rect& area, const gfx::Rect& dialog) {
32     // Dialog should fully fit into the area.
33     EXPECT_TRUE(area.Contains(dialog));
34 
35     EXPECT_GE(dialog.x(), area.x());
36     EXPECT_LE(dialog.right(), area.right());
37     EXPECT_GE(dialog.y(), area.y());
38     EXPECT_LE(dialog.bottom(), area.bottom());
39 
40     // Dialog is centered in area.
41     EXPECT_EQ(area.CenterPoint(), dialog.CenterPoint());
42 
43     EXPECT_LE(dialog.width(), kMaxDialogSize.width());
44     EXPECT_LE(dialog.height(), kMaxDialogSize.height());
45     // If there is at least some space, we should have margins.
46     if (dialog.width() > kMinDialogSize.width()) {
47       EXPECT_GE(dialog.x(), kMinMargins.left());
48       EXPECT_GE(area.right() - dialog.right(), kMinMargins.right());
49     }
50     if (dialog.height() > kMinDialogSize.height()) {
51       EXPECT_TRUE(dialog.y() >= kMinMargins.top());
52       EXPECT_TRUE(area.bottom() - dialog.bottom() >= kMinMargins.bottom());
53     }
54     // If dialog size is lesser than minimum size, there should be no margins
55     if (dialog.width() < kMinDialogSize.width()) {
56       EXPECT_EQ(dialog.x(), area.x());
57       EXPECT_EQ(dialog.right(), area.right());
58     }
59     if (dialog.height() < kMinDialogSize.height()) {
60       EXPECT_EQ(dialog.y(), area.y());
61       EXPECT_EQ(dialog.bottom(), area.bottom());
62     }
63   }
64 
SizeWithoutShelf(const gfx::Rect & area) const65   gfx::Rect SizeWithoutShelf(const gfx::Rect& area) const {
66     return gfx::Rect(area.width(), area.height() - kShelfHeight);
67   }
68 
69  private:
70   DISALLOW_COPY_AND_ASSIGN(OobeDialogSizeUtilsTest);
71 };
72 
73 // We have plenty of space on the screen.
TEST_F(OobeDialogSizeUtilsTest,Chromebook)74 TEST_F(OobeDialogSizeUtilsTest, Chromebook) {
75   gfx::Rect usual_device(1200, 800);
76   gfx::Rect dialog;
77   OobeDialogPaddingMode padding;
78 
79   CalculateOobeDialogBounds(usual_device, kShelfHeight, &dialog, &padding);
80   ValidateDialog(SizeWithoutShelf(usual_device), dialog);
81   EXPECT_EQ(padding, OobeDialogPaddingMode::PADDING_WIDE);
82 }
83 
84 // We have plenty of space on the screen, but virtual keyboard takes some space.
TEST_F(OobeDialogSizeUtilsTest,ChromebookVirtualKeyboard)85 TEST_F(OobeDialogSizeUtilsTest, ChromebookVirtualKeyboard) {
86   gfx::Rect usual_device_with_keyboard(1200, 800 - kVirtualKeyboardHeight);
87   gfx::Rect dialog;
88   OobeDialogPaddingMode padding;
89 
90   CalculateOobeDialogBounds(usual_device_with_keyboard, 0, &dialog, &padding);
91   ValidateDialog(usual_device_with_keyboard, dialog);
92   EXPECT_EQ(padding, OobeDialogPaddingMode::PADDING_NARROW);
93 }
94 
95 // Tablet device can have smaller screen size.
TEST_F(OobeDialogSizeUtilsTest,TabletHorizontal)96 TEST_F(OobeDialogSizeUtilsTest, TabletHorizontal) {
97   gfx::Rect tablet_device(1080, 675);
98   gfx::Rect dialog;
99   OobeDialogPaddingMode padding;
100 
101   CalculateOobeDialogBounds(tablet_device, kShelfHeight, &dialog, &padding);
102   ValidateDialog(SizeWithoutShelf(tablet_device), dialog);
103   EXPECT_EQ(padding, OobeDialogPaddingMode::PADDING_NARROW);
104 }
105 
106 // Tablet device in horizontal mode with virtual keyboard have restricted
107 // vertical space.
TEST_F(OobeDialogSizeUtilsTest,TabletHorizontalVirtualKeyboard)108 TEST_F(OobeDialogSizeUtilsTest, TabletHorizontalVirtualKeyboard) {
109   gfx::Rect tablet_device(1080, 675 - kVirtualKeyboardHeight);
110   gfx::Rect dialog;
111   OobeDialogPaddingMode padding;
112 
113   CalculateOobeDialogBounds(tablet_device, 0, &dialog, &padding);
114   ValidateDialog(tablet_device, dialog);
115   EXPECT_EQ(padding, OobeDialogPaddingMode::PADDING_NARROW);
116 }
117 
118 // Tablet device in horizontal mode with docked magnifier have restricted
119 // vertical space.
TEST_F(OobeDialogSizeUtilsTest,TabletHorizontalDockedMagnifier)120 TEST_F(OobeDialogSizeUtilsTest, TabletHorizontalDockedMagnifier) {
121   gfx::Rect tablet_device(0, 0, 1080, 675 - kDockedMagnifierHeight);
122   gfx::Rect dialog;
123   OobeDialogPaddingMode padding;
124 
125   CalculateOobeDialogBounds(tablet_device, 0, &dialog, &padding);
126   ValidateDialog(tablet_device, dialog);
127   EXPECT_EQ(padding, OobeDialogPaddingMode::PADDING_NARROW);
128 }
129 
130 // Tablet device in horizontal mode with virtual keyboard and docked
131 // magnifier results in very few vertical space.
TEST_F(OobeDialogSizeUtilsTest,TabletHorizontalVirtualKeyboardMagnifier)132 TEST_F(OobeDialogSizeUtilsTest, TabletHorizontalVirtualKeyboardMagnifier) {
133   gfx::Rect tablet_device(
134       0, 0, 1080, 675 - kVirtualKeyboardHeight - kDockedMagnifierHeight);
135 
136   gfx::Rect dialog;
137   OobeDialogPaddingMode padding;
138 
139   CalculateOobeDialogBounds(tablet_device, 0, &dialog, &padding);
140   ValidateDialog(tablet_device, dialog);
141   EXPECT_EQ(padding, OobeDialogPaddingMode::PADDING_NARROW);
142 }
143 
144 // Tablet in vertical mode puts some strain on dialog width.
TEST_F(OobeDialogSizeUtilsTest,TabletVertical)145 TEST_F(OobeDialogSizeUtilsTest, TabletVertical) {
146   gfx::Rect tablet_device(675, 1080);
147   gfx::Rect dialog;
148   OobeDialogPaddingMode padding;
149 
150   CalculateOobeDialogBounds(tablet_device, kShelfHeight, &dialog, &padding);
151   ValidateDialog(SizeWithoutShelf(tablet_device), dialog);
152   EXPECT_EQ(padding, OobeDialogPaddingMode::PADDING_NARROW);
153 }
154 
155 }  // namespace chromeos
156