Add ICU message format support
[chromium-blink-merge.git] / ui / views / controls / table / table_utils_unittest.cc
blob98b1fd2f78697b2d75adb0fdc75b3f4a53d7cc8f
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;
13 using ui::TableModel;
15 namespace views {
17 namespace {
19 std::string IntVectorToString(const std::vector<int>& values) {
20 std::string result;
21 for (size_t i = 0; i < values.size(); ++i) {
22 if (i != 0)
23 result += ",";
24 result += base::IntToString(values[i]);
26 return result;
29 ui::TableColumn CreateTableColumnWithWidth(int width) {
30 ui::TableColumn column;
31 column.width = width;
32 return column;
35 } // namespace
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
60 // space.
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]);
70 EXPECT_GE(result[1],
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));
86 EXPECT_EQ(result[0],
87 WidthForContent(font_list, font_list, 0, 0, columns[0], &model));
88 EXPECT_EQ(result[1],
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);
95 EXPECT_EQ(result[0],
96 WidthForContent(font_list, font_list, 0, 0, columns[0], &model));
97 EXPECT_EQ(result[1],
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);
104 EXPECT_EQ(result[0],
105 WidthForContent(font_list, font_list, 0, 0, columns[0], &model)
106 + 10);
107 EXPECT_EQ(result[1],
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
112 // to min size.
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);
117 EXPECT_EQ(result[0],
118 WidthForContent(font_list, font_list, 0, 0, columns[0], &model));
119 EXPECT_EQ(result[1],
120 WidthForContent(font_list, font_list, 0, 0, columns[1], &model));
121 EXPECT_EQ(kUnspecifiedColumnWidth, result[2]);
124 } // namespace views