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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
/*
This file is part of KDDockWidgets.
SPDX-FileCopyrightText: 2019 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Sérgio Martins <sergio.martins@kdab.com>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "utils.h"
#include "Config.h"
#include "core/LayoutSaver_p.h"
#include "core/Position_p.h"
#include "core/WindowBeingDragged_p.h"
#include "core/layouting/Item_p.h"
#include "core/ViewFactory.h"
#include "core/DropArea.h"
#include "core/MainWindow.h"
#include "core/DockWidget.h"
#include "core/DockWidget_p.h"
#include "core/Separator.h"
#include "core/TabBar.h"
#include "core/Stack.h"
#include "core/SideBar.h"
#include "core/Platform.h"
#include <QtTest>
using namespace KDDockWidgets;
using namespace KDDockWidgets::Core;
using namespace KDDockWidgets::Tests;
class TestDocks : public QObject
{
Q_OBJECT
private Q_SLOTS:
void tst_dragByTabBar();
void tst_negativeAnchorPosition();
void tst_negativeAnchorPosition2();
void tst_negativeAnchorPosition3();
void tst_negativeAnchorPosition4();
void tst_negativeAnchorPosition5();
void tst_negativeAnchorPosition6();
void tst_negativeAnchorPosition7();
void tst_crash2();
void tst_keepLast();
};
void TestDocks::tst_dragByTabBar()
{
auto func = [](bool documentMode, bool tabsAlwaysVisible) {
EnsureTopLevelsDeleted e;
auto flags = KDDockWidgets::Config::self().flags()
| KDDockWidgets::Config::Flag_HideTitleBarWhenTabsVisible;
if (tabsAlwaysVisible)
flags |= KDDockWidgets::Config::Flag_AlwaysShowTabs;
KDDockWidgets::Config::self().setFlags(flags);
auto m = createMainWindow();
auto dropArea = m->dropArea();
auto dock1 = createDockWidget(
"dock1", Platform::instance()->tests_createView({ true, {}, Size(400, 400) }));
auto dock2 = createDockWidget(
"dock2", Platform::instance()->tests_createView({ true, {}, Size(400, 400) }));
auto dock3 = createDockWidget(
"dock3", Platform::instance()->tests_createView({ true, {}, Size(400, 400) }));
m->addDockWidgetAsTab(dock1);
m->view()->resize(Size(osWindowMinWidth(), 200));
dock2->addDockWidgetAsTab(dock3);
if (documentMode)
dock2->dptr()->group()->stack()->setDocumentMode(true);
auto fw = dock2->floatingWindow();
fw->view()->move(m->pos() + Point(500, 500));
QVERIFY(fw->isVisible());
QVERIFY(!fw->titleBar()->isVisible());
dragFloatingWindowTo(fw, dropArea, DropLocation_Right);
};
func(false, false);
func(true, false);
func(false, true);
func(true, true);
}
void TestDocks::tst_negativeAnchorPosition()
{
// Tests that we don't hit:
// void KDDockWidgets::Anchor::setPosition(int, KDDockWidgets::Anchor::SetPositionOptions)
// Negative position
EnsureTopLevelsDeleted e;
auto m = createMainWindow(Size(1002, 806));
auto w1 = Platform::instance()->tests_createView({ true, {}, Size(104, 104) });
w1->resize(994, 718);
auto w2 = Platform::instance()->tests_createView({ true, {}, Size(133, 343) });
w2->resize(392, 362);
auto w3 = Platform::instance()->tests_createView({ true, {}, Size(133, 343) });
w3->resize(392, 362);
Core::DropArea *layout = m->multiSplitter();
auto d1 = createDockWidget("1", w1);
auto d2 = createDockWidget("2", w2);
auto d3 = createDockWidget("3", w3);
m->addDockWidgetAsTab(d1);
m->addDockWidget(d2, Location_OnTop);
m->addDockWidget(d3, Location_OnTop);
d2->close();
WAIT_FOR_RESIZE(d3->view());
d2->open(); // Should not result in negative anchor positions (Test will fail due to a spdlog warning)
WAIT_FOR_RESIZE(d3->view());
layout->checkSanity();
d2->close();
WAIT_FOR_RESIZE(d3->view());
layout->checkSanity();
// Now resize the Window, after removing middle one
const int availableToShrink =
layout->layoutSize().height() - layout->view()->minSize().height();
const Size newSize = { layout->layoutWidth(), layout->layoutHeight() - availableToShrink };
QVERIFY(layout->layoutMinimumSize().expandedTo(newSize) == newSize);
layout->setLayoutSize(newSize);
d2->destroyLater();
WAIT_FOR_DELETED(d2);
layout->checkSanity();
}
void TestDocks::tst_negativeAnchorPosition2()
{
// Tests that the "Out of bounds position" warning doesn't appear. Test will abort if yes.
EnsureTopLevelsDeleted e;
auto m = createMainWindow(Size(800, 500), MainWindowOption_None);
auto dropArea = m->dropArea();
Core::DropArea *layout = dropArea;
auto dock1 = createDockWidget("1", Platform::instance()->tests_createView({ true }), {}, {},
/*show=*/false);
auto dock2 = createDockWidget("2", Platform::instance()->tests_createView({ true }), {}, {},
/*show=*/false);
auto dock3 = createDockWidget("3", Platform::instance()->tests_createView({ true }), {}, {},
/*show=*/false);
m->addDockWidget(dock1, Location_OnLeft);
m->addDockWidget(dock2, Location_OnRight, nullptr, InitialVisibilityOption::StartHidden);
m->addDockWidget(dock3, Location_OnRight);
QCOMPARE(layout->placeholderCount(), 1);
QCOMPARE(layout->count(), 3);
dock1->setFloating(true);
dock1->setFloating(false);
dock2->destroyLater();
layout->checkSanity();
QVERIFY(Platform::instance()->tests_waitForDeleted(dock2));
}
void TestDocks::tst_negativeAnchorPosition3()
{
// 1. Another case, when floating a dock:
EnsureTopLevelsDeleted e;
std::vector<DockDescriptor> docks = {
{ Location_OnLeft, -1, nullptr, InitialVisibilityOption::StartVisible },
{ Location_OnRight, -1, nullptr, InitialVisibilityOption::StartVisible },
{ Location_OnLeft, -1, nullptr, InitialVisibilityOption::StartVisible },
{ Location_OnBottom, -1, nullptr, InitialVisibilityOption::StartHidden },
{ Location_OnRight, -1, nullptr, InitialVisibilityOption::StartVisible }
};
auto m = createMainWindow(docks);
auto dropArea = m->dropArea();
Core::DropArea *layout = dropArea;
layout->checkSanity();
auto dock1 = docks.at(1).createdDock;
auto dock3 = docks.at(3).createdDock;
dock1->setFloating(true);
layout->checkSanity();
}
void TestDocks::tst_negativeAnchorPosition4()
{
// 1. Tests that we don't get a warning
// Out of bounds position= -5 ; oldPosition= 0 KDDockWidgets::Anchor(0x55e726be9090, name =
// "left") KDDockWidgets::MainWindow(0x55e726beb8d0)
EnsureTopLevelsDeleted e;
std::vector<DockDescriptor> docks = {
{ Location_OnLeft, -1, nullptr, InitialVisibilityOption::StartHidden },
{ Location_OnTop, -1, nullptr, InitialVisibilityOption::StartVisible },
{ Location_OnRight, -1, nullptr, InitialVisibilityOption::StartVisible },
{ Location_OnLeft, -1, nullptr, InitialVisibilityOption::StartVisible },
{ Location_OnRight, -1, nullptr, InitialVisibilityOption::StartVisible }
};
auto m = createMainWindow(docks);
auto dropArea = m->dropArea();
Core::DropArea *layout = dropArea;
layout->checkSanity();
auto dock1 = docks.at(1).createdDock;
auto dock2 = docks.at(2).createdDock;
dock2->setFloating(true);
auto fw2 = dock2->floatingWindow();
dropArea->addWidget(fw2->dropArea()->view(), Location_OnLeft, dock1->dptr()->group()->layoutItem());
dock2->setFloating(true);
fw2 = dock2->floatingWindow();
dropArea->addWidget(fw2->dropArea()->view(), Location_OnRight, dock1->dptr()->group()->layoutItem());
layout->checkSanity();
docks.at(0).createdDock->destroyLater();
docks.at(4).createdDock->destroyLater();
WAIT_FOR_DELETED(docks.at(4).createdDock);
}
void TestDocks::tst_negativeAnchorPosition5()
{
EnsureTopLevelsDeleted e;
std::vector<DockDescriptor> docks = {
{ Location_OnBottom, -1, nullptr, InitialVisibilityOption::StartHidden },
{ Location_OnBottom, -1, nullptr, InitialVisibilityOption::StartHidden },
{ Location_OnBottom, -1, nullptr, InitialVisibilityOption::StartHidden },
};
auto m = createMainWindow(docks);
auto dropArea = m->dropArea();
Core::DropArea *layout = dropArea;
layout->checkSanity();
auto dock0 = docks.at(0).createdDock;
auto dock1 = docks.at(1).createdDock;
dock1->open();
QVERIFY(layout->checkSanity());
dock0->open();
QVERIFY(layout->checkSanity());
// Cleanup
for (auto dock : DockRegistry::self()->dockwidgets())
dock->destroyLater();
QVERIFY(Platform::instance()->tests_waitForDeleted(dock0));
}
void TestDocks::tst_negativeAnchorPosition6()
{
// Tests a case when we add a widget to left/right but the layout doesn't have enough height (or
// vice-versa)
EnsureTopLevelsDeleted e;
auto m = createMainWindow(Size(501, 500), MainWindowOption_None);
m->view()->resize(Size(100, 100));
m->show();
auto layout = m->multiSplitter();
auto w1 = Platform::instance()->tests_createView({ true, {}, Size(400, 100) });
auto w2 = Platform::instance()->tests_createView({ true, {}, Size(400, 100) });
auto w3 = Platform::instance()->tests_createView({ true, {}, Size(400, 100) });
auto w4 = Platform::instance()->tests_createView({ true, {}, Size(400, 900) });
auto d1 = createDockWidget("1", w1);
auto d2 = createDockWidget("2", w2);
auto d3 = createDockWidget("3", w3);
auto d4 = createDockWidget("4", w4);
m->addDockWidget(d1, Location_OnBottom);
m->addDockWidget(d2, Location_OnBottom);
m->addDockWidget(d3, Location_OnBottom);
QCOMPARE(layout->count(), 3);
QCOMPARE(layout->placeholderCount(), 0);
m->addDockWidget(d4, Location_OnRight, d3);
layout->checkSanity();
Item *centralItem = m->dropArea()->centralFrame();
layout->rectForDrop(nullptr, Location_OnTop, centralItem);
layout->checkSanity();
}
void TestDocks::tst_negativeAnchorPosition7()
{
EnsureTopLevelsDeleted e;
auto m = createMainWindow(Size(501, 500), MainWindowOption_None);
m->show();
auto w1 = Platform::instance()->tests_createView({ true, {}, Size(400, 400) });
auto w2 = Platform::instance()->tests_createView({ true, {}, Size(400, 400) });
auto d1 = newDockWidget("1");
d1->setGuestView(w1->asWrapper());
auto d2 = newDockWidget("2");
d2->setGuestView(w2->asWrapper());
auto w3 = Platform::instance()->tests_createView({ true, {}, Size(100, 100) });
auto d3 = newDockWidget("3");
d3->setGuestView(w3->asWrapper());
// Stack 1, 2
m->addDockWidget(d2, Location_OnTop);
m->addDockWidget(d1, Location_OnTop);
// add a small one to the middle
// Stack: 1, 3, 2
m->addDockWidget(d3, Location_OnTop, d2);
m->layout()->checkSanity();
}
void TestDocks::tst_crash2()
{
auto func = [](bool show) {
{
EnsureTopLevelsDeleted e;
auto m = createMainWindow(Size(501, 500), MainWindowOption_None);
auto layout = m->multiSplitter();
m->setVisible(show);
Core::DockWidget::List docks;
const int num = 4;
for (int i = 0; i < num; ++i)
docks.push_back(newDockWidget(QString::number(i)));
std::vector<KDDockWidgets::Location> locations = { Location_OnLeft, Location_OnRight,
Location_OnRight, Location_OnRight };
std::vector<KDDockWidgets::InitialVisibilityOption> options = {
InitialVisibilityOption::StartHidden, InitialVisibilityOption::StartHidden,
InitialVisibilityOption::StartVisible, InitialVisibilityOption::StartHidden
};
std::vector<bool> floatings = { true, false, false, false };
for (int i = 0; i < num; ++i) {
m->addDockWidget(docks[i], locations[i], nullptr, options[i]);
layout->checkSanity();
docks[i]->setFloating(floatings[i]);
}
qDeleteAll(docks);
qDeleteAll(DockRegistry::self()->groups());
}
{
EnsureTopLevelsDeleted e;
auto m = createMainWindow(Size(501, 500), MainWindowOption_None);
auto layout = m->multiSplitter();
m->show();
const int num = 3;
Core::DockWidget::List docks;
for (int i = 0; i < num; ++i)
docks.push_back(newDockWidget(QString::number(i)));
std::vector<KDDockWidgets::Location> locations = { Location_OnLeft, Location_OnLeft,
Location_OnRight };
std::vector<KDDockWidgets::InitialVisibilityOption> options = {
InitialVisibilityOption::StartVisible, InitialVisibilityOption::StartVisible,
InitialVisibilityOption::StartHidden
};
std::vector<bool> floatings = { true, false, false };
for (int i = 0; i < num; ++i) {
m->addDockWidget(docks[i], locations[i], nullptr, options[i]);
layout->checkSanity();
if (i == 2) {
// Wait for the resizes. This used to make the app crash.
QTest::qWait(1000);
}
docks[i]->setFloating(floatings[i]);
}
layout->checkSanity();
qDeleteAll(docks);
qDeleteAll(DockRegistry::self()->groups());
}
};
func(true);
func(false);
}
void TestDocks::tst_keepLast()
{
// 1 event loop for DelayedDelete. Avoids LSAN warnings.
QTest::qWait(1);
}
#define KDDW_TEST_NAME TestDocks
#include "test_main_qt.h"
#include "tst_docks_slow7.moc"
|