1 // Copyright (c) 2012 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.
5 #include "ui/views/controls/table/table_utils.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/font_list.h"
10 #include "ui/views/controls/table/test_table_model.h"
12 using ui::TableColumn
;
19 std::string
IntVectorToString(const std::vector
<int>& values
) {
21 for (size_t i
= 0; i
< values
.size(); ++i
) {
24 result
+= base::IntToString(values
[i
]);
29 ui::TableColumn
CreateTableColumnWithWidth(int width
) {
30 ui::TableColumn column
;
37 // Verifies columns with a specified width is honored.
38 TEST(TableUtilsTest
, SetWidthHonored
) {
39 TestTableModel
model(4);
40 std::vector
<TableColumn
> columns
;
41 columns
.push_back(CreateTableColumnWithWidth(20));
42 columns
.push_back(CreateTableColumnWithWidth(30));
43 gfx::FontList font_list
;
44 std::vector
<int> result(CalculateTableColumnSizes(
45 100, 0, font_list
, font_list
, 0, 0, columns
, &model
));
46 EXPECT_EQ("20,30", IntVectorToString(result
));
48 // Same with some padding, it should be ignored.
49 result
= CalculateTableColumnSizes(
50 100, 0, font_list
, font_list
, 2, 0, columns
, &model
);
51 EXPECT_EQ("20,30", IntVectorToString(result
));
53 // Same with not enough space, it shouldn't matter.
54 result
= CalculateTableColumnSizes(
55 10, 0, font_list
, font_list
, 2, 0, columns
, &model
);
56 EXPECT_EQ("20,30", IntVectorToString(result
));
59 // Verifies if no size is specified the last column gets all the available
61 TEST(TableUtilsTest
, LastColumnGetsAllSpace
) {
62 TestTableModel
model(4);
63 std::vector
<TableColumn
> columns
;
64 columns
.push_back(ui::TableColumn());
65 columns
.push_back(ui::TableColumn());
66 gfx::FontList font_list
;
67 std::vector
<int> result(CalculateTableColumnSizes(
68 500, 0, font_list
, font_list
, 0, 0, columns
, &model
));
69 EXPECT_NE(0, result
[0]);
71 WidthForContent(font_list
, font_list
, 0, 0, columns
[1], &model
));
72 EXPECT_EQ(500, result
[0] + result
[1]);
75 // Verifies a single column with a percent=1 is resized correctly.
76 TEST(TableUtilsTest
, SingleResizableColumn
) {
77 TestTableModel
model(4);
78 std::vector
<TableColumn
> columns
;
79 columns
.push_back(ui::TableColumn());
80 columns
.push_back(ui::TableColumn());
81 columns
.push_back(ui::TableColumn());
82 columns
[2].percent
= 1.0f
;
83 gfx::FontList font_list
;
84 std::vector
<int> result(CalculateTableColumnSizes(
85 500, 0, font_list
, font_list
, 0, 0, columns
, &model
));
87 WidthForContent(font_list
, font_list
, 0, 0, columns
[0], &model
));
89 WidthForContent(font_list
, font_list
, 0, 0, columns
[1], &model
));
90 EXPECT_EQ(500 - result
[0] - result
[1], result
[2]);
92 // The same with a slightly larger width passed in.
93 result
= CalculateTableColumnSizes(
94 1000, 0, font_list
, font_list
, 0, 0, columns
, &model
);
96 WidthForContent(font_list
, font_list
, 0, 0, columns
[0], &model
));
98 WidthForContent(font_list
, font_list
, 0, 0, columns
[1], &model
));
99 EXPECT_EQ(1000 - result
[0] - result
[1], result
[2]);
101 // Verify padding for the first column is honored.
102 result
= CalculateTableColumnSizes(
103 1000, 10, font_list
, font_list
, 0, 0, columns
, &model
);
105 WidthForContent(font_list
, font_list
, 0, 0, columns
[0], &model
)
108 WidthForContent(font_list
, font_list
, 0, 0, columns
[1], &model
));
109 EXPECT_EQ(1000 - result
[0] - result
[1], result
[2]);
111 // Just enough space to show the first two columns. Should force last column
113 result
= CalculateTableColumnSizes(
114 1000, 0, font_list
, font_list
, 0, 0, columns
, &model
);
115 result
= CalculateTableColumnSizes(
116 result
[0] + result
[1], 0, font_list
, font_list
, 0, 0, columns
, &model
);
118 WidthForContent(font_list
, font_list
, 0, 0, columns
[0], &model
));
120 WidthForContent(font_list
, font_list
, 0, 0, columns
[1], &model
));
121 EXPECT_EQ(kUnspecifiedColumnWidth
, result
[2]);