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. 4 5 #include "ui/views/controls/table/table_utils.h" 6 7 #include "base/strings/string_number_conversions.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "ui/gfx/font.h" 10 #include "ui/views/controls/table/test_table_model.h" 11 12 using ui::TableColumn; 13 using ui::TableModel; 14 15 namespace views { 16 17 namespace { 18 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]); 25 } 26 return result; 27 } 28 29 ui::TableColumn CreateTableColumnWithWidth(int width) { 30 ui::TableColumn column; 31 column.width = width; 32 return column; 33 } 34 35 } // namespace 36 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::Font font; 44 std::vector<int> result( 45 CalculateTableColumnSizes(100, 0, font, font, 0, 0, columns, &model)); 46 EXPECT_EQ("20,30", IntVectorToString(result)); 47 48 // Same with some padding, it should be ignored. 49 result = CalculateTableColumnSizes(100, 0, font, font, 2, 0, columns, &model); 50 EXPECT_EQ("20,30", IntVectorToString(result)); 51 52 // Same with not enough space, it shouldn't matter. 53 result = CalculateTableColumnSizes(10, 0, font, font, 2, 0, columns, &model); 54 EXPECT_EQ("20,30", IntVectorToString(result)); 55 } 56 57 // Verifies if no size is specified the last column gets all the available 58 // space. 59 TEST(TableUtilsTest, LastColumnGetsAllSpace) { 60 TestTableModel model(4); 61 std::vector<TableColumn> columns; 62 columns.push_back(ui::TableColumn()); 63 columns.push_back(ui::TableColumn()); 64 gfx::Font font; 65 std::vector<int> result( 66 CalculateTableColumnSizes(500, 0, font, font, 0, 0, columns, &model)); 67 EXPECT_NE(0, result[0]); 68 EXPECT_GE(result[1], WidthForContent(font, font, 0, 0, columns[1], &model)); 69 EXPECT_EQ(500, result[0] + result[1]); 70 } 71 72 // Verifies a single column with a percent=1 is resized correctly. 73 TEST(TableUtilsTest, SingleResizableColumn) { 74 TestTableModel model(4); 75 std::vector<TableColumn> columns; 76 columns.push_back(ui::TableColumn()); 77 columns.push_back(ui::TableColumn()); 78 columns.push_back(ui::TableColumn()); 79 columns[2].percent = 1.0f; 80 gfx::Font font; 81 std::vector<int> result( 82 CalculateTableColumnSizes(500, 0, font, font, 0, 0, columns, &model)); 83 EXPECT_EQ(result[0], WidthForContent(font, font, 0, 0, columns[0], &model)); 84 EXPECT_EQ(result[1], WidthForContent(font, font, 0, 0, columns[1], &model)); 85 EXPECT_EQ(500 - result[0] - result[1], result[2]); 86 87 // The same with a slightly larger width passed in. 88 result = 89 CalculateTableColumnSizes(1000, 0, font, font, 0, 0, columns, &model); 90 EXPECT_EQ(result[0], WidthForContent(font, font, 0, 0, columns[0], &model)); 91 EXPECT_EQ(result[1], WidthForContent(font, font, 0, 0, columns[1], &model)); 92 EXPECT_EQ(1000 - result[0] - result[1], result[2]); 93 94 // Verify padding for the first column is honored. 95 result = 96 CalculateTableColumnSizes(1000, 10, font, font, 0, 0, columns, &model); 97 EXPECT_EQ(result[0], 98 WidthForContent(font, font, 0, 0, columns[0], &model) + 10); 99 EXPECT_EQ(result[1], WidthForContent(font, font, 0, 0, columns[1], &model)); 100 EXPECT_EQ(1000 - result[0] - result[1], result[2]); 101 102 // Just enough space to show the first two columns. Should force last column 103 // to min size. 104 result = 105 CalculateTableColumnSizes(1000, 0, font, font, 0, 0, columns, &model); 106 result = CalculateTableColumnSizes(result[0] + result[1], 0, font, font, 0, 0, 107 columns, &model); 108 EXPECT_EQ(result[0], WidthForContent(font, font, 0, 0, columns[0], &model)); 109 EXPECT_EQ(result[1], WidthForContent(font, font, 0, 0, columns[1], &model)); 110 EXPECT_EQ(kUnspecifiedColumnWidth, result[2]); 111 } 112 113 } // namespace views 114 115