File: qabstractitemmodel.t

package info (click to toggle)
qt4-perl 4.8.4-1.3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 8,752 kB
  • ctags: 8,157
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (158 lines) | stat: -rw-r--r-- 4,236 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/perl

package TestModel;

use strict;
use warnings;
use blib;
use QtCore4;
use QtCore4::isa qw( Qt::AbstractItemModel );

sub NEW {
    my ($class, $parent) = @_;
    $class->SUPER::NEW($parent);

    this->{rootIndex} = Qt::ModelIndex();
    this->{data} = [
        [ qw( PerlQt is Awesome ) ],
        [ qw( The quick brown ) ],
        [ qw( jumped over the ) ],
        [ qw( lazy dog and ) ],
        [ qw( then ate lunch ) ]
    ];
}

1;

package main;

use strict;
use warnings;
no warnings 'once';
use blib;
use QtCore4;
use TestModel;
use Test::More tests=>15;

my $model;
my $unimplementedPureVirtualErr = 'Unimplemented pure virtual method called';

# Pure virutal methods
sub testColumnCount {
    my $columnCount = eval{ $model->columnCount() };
    like( $@, qr/$unimplementedPureVirtualErr/, 'columnCount() unimplemented' );
    *TestModel::columnCount = sub {
        my ($parent) = @_;
        return 5;
    };
    $columnCount = $model->columnCount();
    is( $columnCount, 5, 'columnCount()' );
}

sub testData {
    my $index = $model->index( 0, 0, Qt::ModelIndex() );
    my $data = eval{ $model->data( $index, Qt::DisplayRole() ) };
    like( $@, qr/$unimplementedPureVirtualErr/, 'data() unimplemented' );
    *TestModel::data = sub {
        my ( $index, $role ) = @_;

        if ( $role == Qt::DisplayRole() ) {
            my $data = Qt::this()->{data};
            return Qt::Variant( Qt::String( $data->[$index->row()]->[$index->column()] ) );
        }

        return Qt::Variant();
    };
    $data = $model->data( $index, Qt::DisplayRole() );
    is( $data->toString(), $model->{data}->[0]->[0], 'data()' );
}

sub testIndex {
    my $index = eval{ $model->index(0, 0, Qt::ModelIndex()) };
    like( $@, qr/$unimplementedPureVirtualErr/, 'index() unimplemented' );
    *TestModel::index = sub {
        my ( $row, $column, $parent ) = @_;

        if ( !$parent->isValid() ) {
            $parent = Qt::this()->{rootIndex};
        }
        return Qt::this()->createIndex( $row, $column, $parent );
    };
    $index = $model->index(0, 0, Qt::ModelIndex());
    ok( $index->parent() == $model->{rootIndex}, 'index()' );
}

sub testParent {
    my $parent = eval{ $model->parent(Qt::ModelIndex()) };
    like( $@, qr/$unimplementedPureVirtualErr/, 'parent() unimplemented' );
    *TestModel::parent = sub {
        my ( $index ) = @_;

        if ( !$index->isValid() ) {
            return Qt::this()->{rootIndex};
        }
        return $index->internalPointer();
    };
    $parent = $model->parent(Qt::ModelIndex());
    is( $parent, $model->{rootIndex}, 'rootIndex()' );
}

sub testRowCount {
    my $rowCount = eval{ $model->rowCount() };
    like( $@, qr/Unimplemented pure virtual method called/, 'rowCount() unimplemented' );
    *TestModel::rowCount = sub {
        my ($parent) = @_;
        if ($parent->isValid()) {
            return 0;
        }
        return 3;
    };
    $rowCount = $model->rowCount(Qt::ModelIndex());
    is( $rowCount, 3, 'rowCount()' );
    $rowCount = $model->rowCount($model->index(0, 0, Qt::ModelIndex));
    is( $rowCount, 0, 'rowCount()' );
}

#-------------------------------------------------------------------------------

sub testBuddy {
    my $index = $model->index( 0, 0, Qt::ModelIndex() );
    ok( $model->buddy($index) == $index, 'Inherited buddy()' );
    *TestModel::buddy = sub {
        package TestModel;
        my ($index) = @_;
        return Qt::this()->SUPER::buddy($index);
    };
    ok( $model->buddy($index) == $index, 'buddy()' );
}

sub testFlags {
    my $index = $model->index( 0, 0, Qt::ModelIndex() );
    my $defaultFlags = Qt::ItemIsEnabled() | Qt::ItemIsSelectable();
    ok( $model->flags($index) == $defaultFlags, 'Inherited flags()' );
    *TestModel::flags = sub {
        package TestModel;
        my ($index) = @_;
        return Qt::this()->SUPER::flags($index);
    };
    ok( $model->flags($index) == $defaultFlags, 'flags()' );
}

sub main {
    my $app = Qt::CoreApplication( \@ARGV );

    $model = TestModel();
    # Pure virtual methods
    testParent();
    testIndex();
    testColumnCount();
    testRowCount();
    testData();

    testBuddy();
    testFlags();

    return 0;
}

exit main();