require File.dirname(__FILE__) + '/test_helper.rb' require 'qtext/object_table_model.rb' require 'Qt4' class TestObjectTableModel < Test::Unit::TestCase Thing = Struct.new( :name, :value, :location, :price, :ignored ) do include TreeViewable end def setup @data = [ Thing.new( "Screwdriver", 'high', 'toolbox', 10.96, 'not' ), Thing.new( "Thermometer", '15 degrees', 'bathroom', 0.01, 'here' ), Thing.new( "Bed", 'large', 'bedroom' ), Thing.new( "Approximation", 'useful', 'maths', 'none', 'all' ) ] @data[3].children = @data @model = ObjectTableModel.new( :data => @data, :headers => [ :name, :value, :location, Header.new( :attribute => :price, :alignment => Qt::AlignRight ) ] ) @main_window = Qt::MainWindow.new #~ @view = Qt::TableView.new( @main_window ) { |tv| tv.model = @model } @view = Qt::TreeView.new( @main_window ) { |tv| tv.model = @model } @main_window.central_widget = @view end should 'have children for data[3]' do assert_equal 4, @data[3].children.size end should 'have a 4 x 4 size' do assert_equal 4, @model.rowCount assert_equal 4, @model.row_count assert_equal 4, @model.columnCount assert_equal 4, @model.column_count end should 'have 0x0 size for children models at a given index' do index = @model.create_index(1,1) assert_equal 0, @model.row_count( index ) assert_equal 0, @model.column_count( index ) end should 'return correct data' do @data.each_with_index do |item,i| Thing.members.each_with_index do |member,j| next if j >= 4 model_index = @model.create_index(i,j) assert_equal item[member], @model.data( model_index ).value end end end should 'return nil for invalid index' do assert_equal Qt::Variant.invalid, @model.data( Qt::ModelIndex.invalid ) end should 'have price aligned right' do assert_equal Qt::AlignLeft.to_i, @model.data( @model.create_index(0,0), Qt::TextAlignmentRole ).to_i & Qt::AlignLeft.to_i assert_equal Qt::AlignLeft.to_i, @model.data( @model.create_index(0,1), Qt::TextAlignmentRole ).to_i & Qt::AlignLeft.to_i assert_equal Qt::AlignRight.to_i, @model.data( @model.create_index(0,3), Qt::TextAlignmentRole ).to_i & Qt::AlignRight.to_i end should 'have a nil parent' do assert_nil @model.parent end should 'have parent == @model' do model = ObjectTableModel.new( :parent => @model, :data => @data, :headers => [ :name, :value, :location, Header.new( :attribute => :price, :alignment => Qt::AlignRight ) ] ) assert_equal @model, model.parent end context 'tree view' do should 'have children' do model_index = @model.create_index(3,0) assert_equal 4, @model.row_count( model_index ) end end def dont_test_show_window @main_window.window_title = 'Test ObjectTableModel' @main_window.move( 150, 0 ) @main_window.show #~ $app.exec end end