1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <qcoreapplication.h>
33#include <qdebug.h>
34#include <qfocusframe.h>
35#include <qtableview.h>
36#include <qstandarditemmodel.h>
37
38class tst_QFocusFrame : public QObject
39{
40Q_OBJECT
41
42public:
43 tst_QFocusFrame();
44 virtual ~tst_QFocusFrame();
45
46private slots:
47 void getSetCheck();
48 void focusFrameInsideScrollview();
49};
50
51tst_QFocusFrame::tst_QFocusFrame()
52{
53}
54
55tst_QFocusFrame::~tst_QFocusFrame()
56{
57}
58
59// Testing get/set functions
60void tst_QFocusFrame::getSetCheck()
61{
62 QFocusFrame *obj1 = new QFocusFrame();
63 // QWidget * QFocusFrame::widget()
64 // void QFocusFrame::setWidget(QWidget *)
65 QWidget var1;
66 QWidget *var2 = new QWidget(&var1);
67 obj1->setWidget(var2);
68 QCOMPARE(var2, obj1->widget());
69 obj1->setWidget((QWidget *)0);
70 QCOMPARE((QWidget *)0, obj1->widget());
71 delete obj1;
72}
73
74void tst_QFocusFrame::focusFrameInsideScrollview()
75{
76 // Make sure that the focus frame follows the widget, even
77 // if the widget is inside a QAbstractItemView. A QAbstractItemView will scroll
78 // all the children, including the focus frame, when it scrolls, which
79 // is why special considerations are taken inside the focus frame to
80 // prevent the frame to scroll away from the widget it tracks.
81
82 if (qApp->style()->objectName() != QLatin1String("macintosh"))
83 QSKIP("This test is only valid when using a style that has a focus frame");
84
85 QWidget window;
86 window.setGeometry(ax: 100, ay: 100, aw: 500, ah: 500);
87
88 QTableView tableView(&window);
89 tableView.resize(window.size());
90 QStandardItemModel *itemModel = new QStandardItemModel();
91 for (int i = 0; i < 50; ++i)
92 itemModel->appendRow(aitem: new QStandardItem("Value"));
93 tableView.setModel(itemModel);
94 tableView.edit(index: itemModel->index(row: 8, column: 0));
95
96 window.show();
97 QFocusFrame *focusFrame = nullptr;
98 QTRY_VERIFY(focusFrame = window.findChild<QFocusFrame *>());
99 const QPoint initialOffset = focusFrame->widget()->mapToGlobal(QPoint()) - focusFrame->mapToGlobal(QPoint());
100
101 tableView.scrollTo(index: itemModel->index(row: 40, column: 0));
102 QPoint offsetAfterScroll = focusFrame->widget()->mapToGlobal(QPoint()) - focusFrame->mapToGlobal(QPoint());
103 QCOMPARE(offsetAfterScroll, initialOffset);
104
105 tableView.scrollTo(index: itemModel->index(row: 0, column: 0));
106 offsetAfterScroll = focusFrame->widget()->mapToGlobal(QPoint()) - focusFrame->mapToGlobal(QPoint());
107 QCOMPARE(offsetAfterScroll, initialOffset);
108}
109
110QTEST_MAIN(tst_QFocusFrame)
111#include "tst_qfocusframe.moc"
112

source code of qtbase/tests/auto/widgets/widgets/qfocusframe/tst_qfocusframe.cpp