[Bf-blender-cvs] [4e3cfaeb16] uiTable: Unit tests for uiTable API (and fix bugs found using it :)

Julian Eisel noreply at git.blender.org
Tue Feb 7 01:03:36 CET 2017


Commit: 4e3cfaeb163315cc5675281a5a703d8c0f1793c4
Author: Julian Eisel
Date:   Tue Feb 7 00:45:33 2017 +0100
Branches: uiTable
https://developer.blender.org/rB4e3cfaeb163315cc5675281a5a703d8c0f1793c4

Unit tests for uiTable API (and fix bugs found using it :)

===================================================================

M	source/blender/editors/include/UI_table.h
M	source/blender/editors/interface/table.c
M	tests/gtests/CMakeLists.txt
A	tests/gtests/interface/CMakeLists.txt
A	tests/gtests/interface/UI_table_test.cc

===================================================================

diff --git a/source/blender/editors/include/UI_table.h b/source/blender/editors/include/UI_table.h
index ca1220198d..9524616aa3 100644
--- a/source/blender/editors/include/UI_table.h
+++ b/source/blender/editors/include/UI_table.h
@@ -41,6 +41,11 @@ enum uiTableColumnAlignemt {
 	TABLE_COLUMN_ALIGN_RIGHT,
 };
 
+enum uiTableUnit {
+	TABLE_UNIT_PX,
+	TABLE_UNIT_PERCENT,
+};
+
 
 uiTable *UI_table_horizontal_flow_create(void) ATTR_WARN_UNUSED_RESULT;
 uiTable *UI_table_vertical_flow_create(void) ATTR_WARN_UNUSED_RESULT;
@@ -55,14 +60,13 @@ uiTableColumn *UI_table_column_add(uiTable *table, const char *idname, const cha
                                    uiTableCellDrawFunc cell_draw) ATTR_NONNULL(1, 2);
 void UI_table_column_remove(uiTable *table, uiTableColumn *column) ATTR_NONNULL();
 uiTableColumn *UI_table_column_lookup(uiTable *table, const char *idname) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
-void UI_table_column_width_set(uiTableColumn *col, const uiTableSize width, const int min_width_px) ATTR_NONNULL();
+void UI_table_column_width_set(uiTableColumn *column, const unsigned int width, enum uiTableUnit unit,
+                               const int min_width_px) ATTR_NONNULL();
 void UI_table_column_alignment_set(uiTableColumn *column, enum uiTableColumnAlignemt alignment) ATTR_NONNULL();
 /* *** Rows *** */
 uiTableRow *UI_table_row_add(uiTable *table, void *rowdata) ATTR_NONNULL(1);
 void UI_table_row_height_set(uiTable *table, uiTableRow *row, unsigned int height) ATTR_NONNULL();
 
-uiTableSize UI_table_size_px(const int value);
-uiTableSize UI_table_size_percentage(const int value);
 unsigned int UI_table_get_rowcount(const uiTable *table);
 
 #endif /* __UI_TABLE_H__ */
diff --git a/source/blender/editors/interface/table.c b/source/blender/editors/interface/table.c
index d2a977677d..94420613ea 100644
--- a/source/blender/editors/interface/table.c
+++ b/source/blender/editors/interface/table.c
@@ -69,10 +69,7 @@ typedef struct TableHorizontalFlow {
  * for size properties (only column widths right now).
  */
 typedef struct uiTableSize {
-	enum {
-		TABLE_UNIT_PX,
-		TABLE_UNIT_PERCENT,
-	} unit;
+	enum uiTableUnit unit;
 	int value;
 } uiTableSize;
 
@@ -154,15 +151,24 @@ static void table_row_height_set(uiTable *table, uiTableRow *row, unsigned int h
 	}
 }
 
-static unsigned int table_get_tot_width_unfixed_columns(const uiTable *table)
+static unsigned int table_column_width_clamp(const uiTableColumn *column, const unsigned int maxwidth,
+                                             const unsigned int unclamped_width)
+{
+	unsigned int width = unclamped_width;
+	CLAMP(width, column->min_width, maxwidth);
+	return width;
+}
+
+static unsigned int table_calc_tot_width_unfixed_columns(const uiTable *table)
 {
 	unsigned int nonfixed_width = table->max_width;
 
 	TABLE_COLUMNS_ITER_BEGIN(table, column)
 	{
 		if (column->width.unit == TABLE_UNIT_PX) {
-			BLI_assert(nonfixed_width - column->width.value > 0);
-			nonfixed_width -= column->width.value;
+			unsigned int width = table_column_width_clamp(column, table->max_width, column->width.value);
+			BLI_assert(nonfixed_width >= width);
+			nonfixed_width -= width;
 		}
 	}
 	TABLE_COLUMNS_ITER_END;
@@ -174,22 +180,23 @@ static struct TableColumnDrawInfo table_column_drawinfo_init(const uiTable *tabl
 {
 	struct TableColumnDrawInfo drawinfo = {};
 
-	drawinfo.totwidth_nonfixed = table_get_tot_width_unfixed_columns(table);
+	drawinfo.totwidth_nonfixed = table_calc_tot_width_unfixed_columns(table);
 	BLI_assert(drawinfo.totwidth_nonfixed <= table->max_width);
 
 	return drawinfo;
 }
 
-static unsigned int table_column_calc_width(const uiTableColumn *column, const struct TableColumnDrawInfo *drawinfo)
+static unsigned int table_column_calc_width(const uiTableColumn *column, const struct TableColumnDrawInfo *drawinfo,
+                                            const unsigned int maxwidth)
 {
 	unsigned int width = column->width.value;
 
 	if (column->width.unit == TABLE_UNIT_PERCENT) {
+		CLAMP_MAX(width, 100); /* more than 100% doesn't make sense */
 		width = iroundf(width * 0.01f * drawinfo->totwidth_nonfixed);
 	}
-	width = MAX2(column->min_width, width);
 
-	return width;
+	return table_column_width_clamp(column, maxwidth, width);
 }
 
 /**
@@ -200,7 +207,7 @@ static void table_column_calc_x_coords(const uiTableColumn *column, const float
                                        struct TableColumnDrawInfo *io_drawinfo,
                                        int *r_xmin, int *r_xmax)
 {
-	const unsigned int width = table_column_calc_width(column, io_drawinfo);
+	const unsigned int width = table_column_calc_width(column, io_drawinfo, max_width);
 
 	if (column->alignment == TABLE_COLUMN_ALIGN_LEFT) {
 		*r_xmin = io_drawinfo->totwidth_left;
@@ -217,11 +224,18 @@ static void table_column_calc_x_coords(const uiTableColumn *column, const float
 	}
 }
 
-static void table_row_calc_y_coords(uiTableRow *row, const unsigned int ofs_y, int *r_ymin, int *r_ymax)
+static void table_row_calc_y_coords(uiTable *table, uiTableRow *row, const unsigned int ofs_y, int *r_ymin, int *r_ymax)
 {
+	unsigned int height = row->height;
+
+	if (table->flow_direction == TABLE_FLOW_HORIZONTAL) {
+		TableHorizontalFlow *horizontal_table = (TableHorizontalFlow *)table;
+		CLAMP_MAX(height, horizontal_table->max_height);
+	}
+
 	/* Assuming inverted direction, from top to bottom. */
 	*r_ymax = -ofs_y;
-	*r_ymin = *r_ymax - row->height;
+	*r_ymin = *r_ymax - height;
 }
 
 
@@ -297,7 +311,7 @@ uiTableColumn *UI_table_column_add(uiTable *table, const char *idname, const cha
 	col->drawname = drawname;
 	col->cell_draw = cell_draw;
 	col->alignment = TABLE_COLUMN_ALIGN_LEFT;
-	UI_table_column_width_set(col, UI_table_size_percentage(100), 0);
+	UI_table_column_width_set(col, 100, TABLE_UNIT_PERCENT, 0);
 
 	BLI_addtail(&table->columns, col);
 
@@ -326,11 +340,13 @@ uiTableColumn *UI_table_column_lookup(uiTable *table, const char *idname)
 /**
  * Set the size info for \a col.
  * \param width: The width in either pixels (#UI_table_size_px), or percentage (#UI_table_size_percentage).
+ * \param min_width_px: Minimum width for the column (always in px).
  */
-void UI_table_column_width_set(uiTableColumn *column, const uiTableSize width, const int min_width_px)
+void UI_table_column_width_set(uiTableColumn *column, const unsigned int width, enum uiTableUnit unit,
+                               const int min_width_px)
 {
-	BLI_assert(width.value >= 0);
-	column->width = width;
+	column->width.unit = unit;
+	column->width.value = width;
 	column->min_width = min_width_px;
 }
 
@@ -371,11 +387,10 @@ void UI_table_draw(uiTable *table)
 	} flow_table = {table};
 
 	struct TableColumnDrawInfo column_drawinfo = table_column_drawinfo_init(table);
-	unsigned int xofs = 0, yofs = 0;
 	BLI_mempool_iter iter;
+	unsigned int xofs = 0, yofs = 0;
 
 
-	BLI_mempool_iternew(table->row_pool, &iter);
 	TABLE_COLUMNS_ITER_BEGIN(table, column)
 	{
 		int prev_row_height = -1; /* to check if rows have consistent height */
@@ -384,6 +399,7 @@ void UI_table_draw(uiTable *table)
 
 		table_column_calc_x_coords(column, table->max_width, &column_drawinfo, &drawrect.xmin, &drawrect.xmax);
 
+		BLI_mempool_iternew(table->row_pool, &iter);
 		for (uiTableRow *row = BLI_mempool_iterstep(&iter); row; row = BLI_mempool_iterstep(&iter)) {
 			/* check for consistent row height */
 			if ((prev_row_height >= 0) && (row->height != prev_row_height)) {
@@ -395,7 +411,7 @@ void UI_table_draw(uiTable *table)
 				yofs = 0;
 			}
 
-			table_row_calc_y_coords(row, yofs, &drawrect.ymin, &drawrect.ymax);
+			table_row_calc_y_coords(table, row, yofs, &drawrect.ymin, &drawrect.ymax);
 			column->cell_draw(row->rowdata, drawrect);
 
 			yofs += row->height;
@@ -415,18 +431,6 @@ void UI_table_draw(uiTable *table)
 	TABLE_COLUMNS_ITER_END;
 }
 
-uiTableSize UI_table_size_px(const int value)
-{
-	uiTableSize size = {.unit = TABLE_UNIT_PX, .value = value};
-	return size;
-}
-
-uiTableSize UI_table_size_percentage(const int value)
-{
-	uiTableSize size = {.unit = TABLE_UNIT_PERCENT, .value = value};
-	return size;
-}
-
 unsigned int UI_table_get_rowcount(const uiTable *table)
 {
 	return BLI_mempool_count(table->row_pool);
diff --git a/tests/gtests/CMakeLists.txt b/tests/gtests/CMakeLists.txt
index a3860ce3e6..ae3129fc23 100644
--- a/tests/gtests/CMakeLists.txt
+++ b/tests/gtests/CMakeLists.txt
@@ -10,6 +10,7 @@ if(WITH_GTESTS)
 	add_subdirectory(testing)
 	add_subdirectory(blenlib)
 	add_subdirectory(guardedalloc)
+	add_subdirectory(interface)
 	add_subdirectory(bmesh)
 endif()
 
diff --git a/tests/gtests/interface/CMakeLists.txt b/tests/gtests/interface/CMakeLists.txt
new file mode 100644
index 0000000000..d279f1675a
--- /dev/null
+++ b/tests/gtests/interface/CMakeLists.txt
@@ -0,0 +1,44 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+	.
+	..
+	../../../source/blender/blenlib
+	../../../source/blender/editors/include
+	../../../source/blender/makesdna
+)
+
+include_directories(${INC})
+
+setup_libdirs()
+get_property(BLENDER_SORTED_LIBS GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP)
+
+# Current BLENDER_SORTED_LIBS works with starting list of symbols in creator, but not
+# for this test. Doubling the list does let all the symbols be resolved, but link time is a bit painful.
+set(BLENDER_SORTED_LIBS ${BLENDER_SORTED_LIBS} ${BLENDER_SORTED_LIBS})
+
+if(WITH_BUILDINFO)
+	set(_buildinfo_src "$<TARGET_OBJECTS:buildinfoobj>")

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list