1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "renderer/table_layout.h"
31 #include "testing/base/public/gunit.h"
32 
33 namespace mozc {
34 namespace renderer {
35 
36 #define EXPECT_SIZE_EQ(                                                 \
37     expect_width, expect_height, actual_size)                           \
38   do {                                                                  \
39      EXPECT_EQ((expect_width), (actual_size).width);                    \
40      EXPECT_EQ((expect_height), (actual_size).height);                  \
41   } while (false)
42 
43 #define EXPECT_RECT_EQ(                                                 \
44     expect_left, expect_top, expect_width, expect_height, actual_rect)  \
45   do {                                                                  \
46      EXPECT_EQ((expect_left), (actual_rect).origin.x);                  \
47      EXPECT_EQ((expect_top), (actual_rect).origin.y);                   \
48      EXPECT_EQ((expect_width), (actual_rect).Width());                  \
49      EXPECT_EQ((expect_height), (actual_rect).Height());                \
50   } while (false)
51 
52 enum COLUMN_TYPE {
53   COLUMN_SHORTCUT = 0,
54   COLUMN_GAP1,
55   COLUMN_CANDIDATE,
56   COLUMN_DESCRIPTION,
57   NUMBER_OF_COLUMNS,
58 };
59 
TEST(TableLayoutTest,AllElement)60 TEST(TableLayoutTest, AllElement) {
61   const int kWindowBorder = 1;
62   const int kNumberOfRow = 10;
63   const int kHeaderHeight = 9;
64   const int kFooterHeight = 13;
65   const int kVSCrollBarWidth = 11;
66   const int kRowRectPadding = 2;
67 
68   TableLayout layout;
69   layout.Initialize(kNumberOfRow, NUMBER_OF_COLUMNS);
70   layout.SetVScrollBar(kVSCrollBarWidth);
71   layout.SetRowRectPadding(kRowRectPadding);
72   layout.SetWindowBorder(kWindowBorder);
73 
74   const Size kGap1(5, 0);
75 
76   layout.EnsureHeaderSize(Size(0, kHeaderHeight));
77   layout.EnsureFooterSize(Size(0, kFooterHeight));
78 
79   layout.EnsureCellSize(COLUMN_GAP1, kGap1);
80   for (size_t row = 0; row < kNumberOfRow; ++row) {
81     const Size candidate(row + 1, 10);
82     const Size description(15, 5);
83     layout.EnsureCellSize(COLUMN_CANDIDATE, candidate);
84     layout.EnsureCellSize(COLUMN_DESCRIPTION, description);
85   }
86 
87   layout.FreezeLayout();
88 
89   EXPECT_SIZE_EQ(47, 164, layout.GetTotalSize());
90   EXPECT_RECT_EQ(1, 1, 45, 9, layout.GetHeaderRect());
91   EXPECT_RECT_EQ(1, 150, 45, 13, layout.GetFooterRect());
92   EXPECT_RECT_EQ(35, 10, 11, 140, layout.GetVScrollBarRect());
93   EXPECT_RECT_EQ(1, 24, 34, 14, layout.GetRowRect(1));
94   EXPECT_RECT_EQ(8, 10, 10, 140, layout.GetColumnRect(COLUMN_CANDIDATE));
95   EXPECT_RECT_EQ(3, 26, 0, 10, layout.GetCellRect(1, COLUMN_SHORTCUT));
96   // Although the specified cell with of column 1 is 2, the actual layout
97   // width of this cell is 10 because of the width of column 9.
98   EXPECT_RECT_EQ(8, 26, 10, 10, layout.GetCellRect(1, COLUMN_CANDIDATE));
99   EXPECT_RECT_EQ(18, 26, 15, 10, layout.GetCellRect(1, COLUMN_DESCRIPTION));
100 }
101 
TEST(TableLayoutTest,AllElementWithMinimumFooterWidth)102 TEST(TableLayoutTest, AllElementWithMinimumFooterWidth) {
103   const int kWindowBorder = 1;
104   const int kNumberOfRow = 10;
105   const int kHeaderHeight = 9;
106   const int kFooterHeight = 13;
107   const int kFooterWidth = 100;
108   const int kVSCrollBarWidth = 11;
109   const int kRowRectPadding = 2;
110 
111   TableLayout layout;
112   layout.Initialize(kNumberOfRow, NUMBER_OF_COLUMNS);
113   layout.SetVScrollBar(kVSCrollBarWidth);
114   layout.SetRowRectPadding(kRowRectPadding);
115   layout.SetWindowBorder(kWindowBorder);
116 
117   const Size kGap1(5, 0);
118 
119   layout.EnsureHeaderSize(Size(0, kHeaderHeight));
120   layout.EnsureFooterSize(Size(kFooterWidth, kFooterHeight));
121 
122   layout.EnsureCellSize(COLUMN_GAP1, kGap1);
123   for (size_t row = 0; row < kNumberOfRow; ++row) {
124     const Size candidate(row + 1, 10);
125     const Size description(15, 5);
126     layout.EnsureCellSize(COLUMN_CANDIDATE, candidate);
127     layout.EnsureCellSize(COLUMN_DESCRIPTION, description);
128   }
129 
130   layout.FreezeLayout();
131 
132   // Although the maximum width of cells are 10 + 15 = 25,
133   // the expected window width is 102 because of footer width.
134   EXPECT_SIZE_EQ(102, 164, layout.GetTotalSize());
135   EXPECT_RECT_EQ(1, 1, 100, 9, layout.GetHeaderRect());
136   EXPECT_RECT_EQ(1, 150, 100, 13, layout.GetFooterRect());
137   EXPECT_RECT_EQ(90, 10, 11, 140, layout.GetVScrollBarRect());
138   EXPECT_RECT_EQ(1, 24, 89, 14, layout.GetRowRect(1));
139   EXPECT_RECT_EQ(8, 10, 10, 140, layout.GetColumnRect(COLUMN_CANDIDATE));
140   EXPECT_RECT_EQ(3, 26, 0, 10, layout.GetCellRect(1, COLUMN_SHORTCUT));
141   // Although the specified cell with of column 1 is 2, the actual layout
142   // width of this cell is 10 because of the width of column 9.
143   EXPECT_RECT_EQ(8, 26, 10, 10, layout.GetCellRect(1, COLUMN_CANDIDATE));
144   EXPECT_RECT_EQ(18, 26, 15, 10, layout.GetCellRect(1, COLUMN_DESCRIPTION));
145 }
146 
TEST(TableLayoutTest,EnsureCellsWidth)147 TEST(TableLayoutTest, EnsureCellsWidth) {
148   TableLayout layout;
149   layout.Initialize(1, 4);
150   for (size_t i = 0; i < 4; ++i) {
151     layout.EnsureCellSize(i, Size(10, 10));
152   }
153   layout.EnsureColumnsWidth(1, 2, 100);
154   layout.FreezeLayout();
155 
156   EXPECT_SIZE_EQ(120, 10, layout.GetTotalSize());
157   EXPECT_RECT_EQ(0, 0, 10, 10, layout.GetColumnRect(0));
158   EXPECT_RECT_EQ(10, 0, 10, 10, layout.GetColumnRect(1));
159   EXPECT_RECT_EQ(20, 0, 90, 10, layout.GetColumnRect(2));
160   EXPECT_RECT_EQ(110, 0, 10, 10, layout.GetColumnRect(3));
161 }
162 
TEST(TableLayoutTest,EnsureCellsWidthCallTwice)163 TEST(TableLayoutTest, EnsureCellsWidthCallTwice) {
164   TableLayout layout;
165   layout.Initialize(1, 4);
166   for (size_t i = 0; i < 4; ++i) {
167     layout.EnsureCellSize(i, Size(10, 10));
168   }
169   layout.EnsureColumnsWidth(1, 2, 100);
170   layout.EnsureColumnsWidth(0, 1, 100);
171   layout.FreezeLayout();
172 
173   EXPECT_SIZE_EQ(120, 10, layout.GetTotalSize());
174   EXPECT_RECT_EQ(0, 0, 10, 10, layout.GetColumnRect(0));
175   EXPECT_RECT_EQ(10, 0, 90, 10, layout.GetColumnRect(1));
176   EXPECT_RECT_EQ(100, 0, 10, 10, layout.GetColumnRect(2));
177   EXPECT_RECT_EQ(110, 0, 10, 10, layout.GetColumnRect(3));
178 }
179 
TEST(TableLayoutTest,VScrollIndicatorPositions)180 TEST(TableLayoutTest, VScrollIndicatorPositions) {
181   TableLayout layout;
182   // Set the size to 100.
183   layout.Initialize(1, 1);
184   layout.EnsureCellSize(0, Size(1, 100));
185   layout.SetVScrollBar(10);
186   layout.FreezeLayout();
187 
188   const int kCandidatesTotal = 15;
189   const Rect vscrollBarRect = layout.GetVScrollBarRect();
190   EXPECT_RECT_EQ(1, 0, 10, 100, layout.GetVScrollBarRect());
191 
192   Rect indicatorRect = layout.GetVScrollIndicatorRect(0, 5, kCandidatesTotal);
193   EXPECT_EQ(vscrollBarRect.Left(), indicatorRect.Left());
194   EXPECT_EQ(vscrollBarRect.Right(), indicatorRect.Right());
195   EXPECT_EQ(0, indicatorRect.Top());
196   EXPECT_EQ(static_cast<int>(100.0 * 6 / 15 + 0.5), indicatorRect.Bottom());
197 
198   indicatorRect = layout.GetVScrollIndicatorRect(5, 10, kCandidatesTotal);
199   EXPECT_EQ(vscrollBarRect.Left(), indicatorRect.Left());
200   EXPECT_EQ(vscrollBarRect.Right(), indicatorRect.Right());
201   EXPECT_EQ(static_cast<int>(100.0 * 5 / 15 + 0.5), indicatorRect.Top());
202   EXPECT_EQ(static_cast<int>(100.0 * 11 / 15 + 0.5), indicatorRect.Bottom());
203 
204   indicatorRect = layout.GetVScrollIndicatorRect(10, 14, kCandidatesTotal);
205   EXPECT_EQ(vscrollBarRect.Left(), indicatorRect.Left());
206   EXPECT_EQ(vscrollBarRect.Right(), indicatorRect.Right());
207   EXPECT_EQ(static_cast<int>(100.0 * 10 / 15 + 0.5), indicatorRect.Top());
208   EXPECT_EQ(100, indicatorRect.Bottom());
209 }
210 
TEST(TableLayoutTest,VScrollVerySmallIndicator)211 TEST(TableLayoutTest, VScrollVerySmallIndicator) {
212   TableLayout layout;
213   layout.Initialize(1, 1);
214   layout.EnsureCellSize(0, Size(1, 100));
215   layout.SetVScrollBar(10);
216   layout.FreezeLayout();
217 
218   const int kCandidatesTotal = 200;
219   EXPECT_RECT_EQ(1, 0, 10, 100, layout.GetVScrollBarRect());
220   EXPECT_RECT_EQ(1, 0, 10, 1,
221                  layout.GetVScrollIndicatorRect(0, 1, kCandidatesTotal));
222   EXPECT_RECT_EQ(1, 99, 10, 1,
223                  layout.GetVScrollIndicatorRect(199, 199, kCandidatesTotal));
224 }
225 
TEST(TableLayoutTest,LayoutFreeze)226 TEST(TableLayoutTest, LayoutFreeze) {
227   TableLayout layout;
228   layout.Initialize(1, 1);
229 
230   EXPECT_FALSE(layout.IsLayoutFrozen());
231 
232   layout.FreezeLayout();
233 
234   EXPECT_TRUE(layout.IsLayoutFrozen());
235 
236   layout.Initialize(1, 1);
237 
238   EXPECT_FALSE(layout.IsLayoutFrozen());
239 }
240 
241 }  // namespace renderer
242 }  // namespace mozc
243