diff options
Diffstat (limited to 'tests/auto/corelib/platform/android/tst_android.cpp')
-rw-r--r-- | tests/auto/corelib/platform/android/tst_android.cpp | 239 |
1 files changed, 211 insertions, 28 deletions
diff --git a/tests/auto/corelib/platform/android/tst_android.cpp b/tests/auto/corelib/platform/android/tst_android.cpp index 710e5b2ecf..76811a31ad 100644 --- a/tests/auto/corelib/platform/android/tst_android.cpp +++ b/tests/auto/corelib/platform/android/tst_android.cpp @@ -1,36 +1,21 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <jni.h> #include <QTest> +#include <QGuiApplication> #include <QtCore/qnativeinterface.h> #include <QtCore/qjniobject.h> +#include <QtCore/qdiriterator.h> +#include <QScreen> +#include <qpa/qplatformscreen.h> +#include <qpa/qplatformnativeinterface.h> +#include <QtCore/qdiriterator.h> +#include <QWidget> +#include <QSignalSpy> + +using namespace Qt::StringLiterals; class tst_Android : public QObject { @@ -38,9 +23,13 @@ Q_OBJECT private slots: void assetsRead(); void assetsNotWritable(); + void assetsIterating(); void testAndroidSdkVersion(); void testAndroidActivity(); void testRunOnAndroidMainThread(); + void testFullScreenDimensions(); + void orientationChange_data(); + void orientationChange(); }; void tst_Android::assetsRead() @@ -66,6 +55,31 @@ void tst_Android::assetsNotWritable() QVERIFY(!file.open(QIODevice::Append)); } +void tst_Android::assetsIterating() +{ + QStringList assets = {"assets:/top_level_dir/file_in_top_dir.txt", + "assets:/top_level_dir/sub_dir", + "assets:/top_level_dir/sub_dir/file_in_sub_dir.txt", + "assets:/top_level_dir/sub_dir/sub_dir_2", + "assets:/top_level_dir/sub_dir/sub_dir_2/sub_dir_3", + "assets:/top_level_dir/sub_dir/sub_dir_2/sub_dir_3/file_in_sub_dir_3.txt"}; + + // Note that we have an "assets:/top_level_dir/sub_dir/empty_sub_dir" in the test's + // assets physical directory, but empty folders are not packaged in the built apk, + // so it's expected to not have such folder be listed in the assets on runtime + + QDirIterator it("assets:/top_level_dir", QDirIterator::Subdirectories); + QStringList iteratorAssets; + while (it.hasNext()) + iteratorAssets.append(it.next()); + + QVERIFY(assets == iteratorAssets); + + auto entryList = QDir{"assets:/"_L1}.entryList(QStringList{"*.txt"_L1}); + QCOMPARE(entryList.size(), 1); + QCOMPARE(entryList[0], "test.txt"_L1); +} + void tst_Android::testAndroidSdkVersion() { QVERIFY(QNativeInterface::QAndroidApplication::sdkVersion() > 0); @@ -189,6 +203,175 @@ void tst_Android::testRunOnAndroidMainThread() } } +Q_DECLARE_JNI_CLASS(QtActivityDelegateBase, "org/qtproject/qt/android/QtActivityDelegateBase") + +void setSystemUiVisibility(int visibility) +{ + QNativeInterface::QAndroidApplication::runOnAndroidMainThread([visibility] { + auto context = QNativeInterface::QAndroidApplication::context(); + auto activityDelegate = context.callMethod<QtJniTypes::QtActivityDelegateBase>("getActivityDelegate"); + activityDelegate.callMethod<void>("setSystemUiVisibility", jint(visibility)); + }).waitForFinished(); +} + +// QTBUG-107604 +void tst_Android::testFullScreenDimensions() +{ + static int SYSTEM_UI_VISIBILITY_NORMAL = 0; + static int SYSTEM_UI_VISIBILITY_FULLSCREEN = 1; + static int SYSTEM_UI_VISIBILITY_TRANSLUCENT = 2; + + // this will trigger new layout updates + setSystemUiVisibility(SYSTEM_UI_VISIBILITY_FULLSCREEN); + setSystemUiVisibility(SYSTEM_UI_VISIBILITY_NORMAL); + + QJniObject activity = QNativeInterface::QAndroidApplication::context(); + QVERIFY(activity.isValid()); + + QJniObject windowManager = + activity.callObjectMethod("getWindowManager", "()Landroid/view/WindowManager;"); + QVERIFY(windowManager.isValid()); + + QJniObject display = windowManager.callObjectMethod("getDefaultDisplay", "()Landroid/view/Display;"); + QVERIFY(display.isValid()); + + QJniObject appSize("android/graphics/Point"); + QVERIFY(appSize.isValid()); + + display.callMethod<void>("getSize", "(Landroid/graphics/Point;)V", appSize.object()); + + QJniObject realSize("android/graphics/Point"); + QVERIFY(realSize.isValid()); + + display.callMethod<void>("getRealSize", "(Landroid/graphics/Point;)V", realSize.object()); + + QPlatformScreen *screen = QGuiApplication::primaryScreen()->handle(); + + { + // Normal - + // available geometry == app size (system bars visible and removed from available geometry) + QCoreApplication::processEvents(); + QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;"); + QVERIFY(window.isValid()); + + QJniObject decorView = window.callObjectMethod("getDecorView", "()Landroid/view/View;"); + QVERIFY(decorView.isValid()); + + QJniObject insets = + decorView.callObjectMethod("getRootWindowInsets", "()Landroid/view/WindowInsets;"); + QVERIFY(insets.isValid()); + + int insetsWidth = insets.callMethod<jint>("getSystemWindowInsetRight") + + insets.callMethod<jint>("getSystemWindowInsetLeft"); + + int insetsHeight = insets.callMethod<jint>("getSystemWindowInsetTop") + + insets.callMethod<jint>("getSystemWindowInsetBottom"); + + QTRY_COMPARE(screen->availableGeometry().width(), + int(appSize.getField<jint>("x")) - insetsWidth); + QTRY_COMPARE(screen->availableGeometry().height(), + int(appSize.getField<jint>("y")) - insetsHeight); + + QTRY_COMPARE(screen->geometry().width(), int(realSize.getField<jint>("x"))); + QTRY_COMPARE(screen->geometry().height(), int(realSize.getField<jint>("y"))); + } + + { + setSystemUiVisibility(SYSTEM_UI_VISIBILITY_FULLSCREEN); + + // Fullscreen + // available geometry == full display size (system bars hidden) + QCoreApplication::processEvents(); + QTRY_COMPARE(screen->availableGeometry().width(), int(realSize.getField<jint>("x"))); + QTRY_COMPARE(screen->availableGeometry().height(), int(realSize.getField<jint>("y"))); + + QTRY_COMPARE(screen->geometry().width(), int(realSize.getField<jint>("x"))); + QTRY_COMPARE(screen->geometry().height(), int(realSize.getField<jint>("y"))); + } + + { + setSystemUiVisibility(SYSTEM_UI_VISIBILITY_TRANSLUCENT); + + // Translucent + // available geometry == full display size (system bars visible but drawable under) + QCoreApplication::processEvents(); + QTRY_COMPARE(screen->availableGeometry().width(), int(realSize.getField<jint>("x"))); + QTRY_COMPARE(screen->availableGeometry().height(), int(realSize.getField<jint>("y"))); + + QTRY_COMPARE(screen->geometry().width(), int(realSize.getField<jint>("x"))); + QTRY_COMPARE(screen->geometry().height(), int(realSize.getField<jint>("y"))); + } +} + +void tst_Android::orientationChange_data() +{ + QTest::addColumn<int>("nativeOrientation"); + QTest::addColumn<Qt::ScreenOrientation>("expected"); + QTest::addColumn<QSize>("screenSize"); + + const QSize portraitSize = QGuiApplication::primaryScreen()->size(); + const QSize landscapeSize = QSize(portraitSize.height(), portraitSize.width()); + + // Rotations without 180 degree or inverted portrait, assuming that the device is in portrait + // position. These are ok for Android 6(API 23), 8 (API 27) and 14 (API 34) + QTest::newRow("InvertedLandscape") << 8 << Qt::InvertedLandscapeOrientation << landscapeSize; + QTest::newRow("Portrait") << 1 << Qt::PortraitOrientation << portraitSize; + QTest::newRow("Landscape") << 0 << Qt::LandscapeOrientation << landscapeSize; + QTest::newRow("Portrait2") << 1 << Qt::PortraitOrientation << portraitSize; + + // Rotations over inverted portrait doing only 90 degree turns. + QTest::newRow("InvertedLandscape2") << 8 << Qt::InvertedLandscapeOrientation << landscapeSize; + QTest::newRow("InvertedPortrait") << 9 << Qt::InvertedPortraitOrientation << portraitSize; + QTest::newRow("Landscape2") << 0 << Qt::LandscapeOrientation << landscapeSize; + QTest::newRow("InvertedPortrait2") << 9 << Qt::InvertedPortraitOrientation << portraitSize; + QTest::newRow("InvertedLandscape3") << 8 << Qt::InvertedLandscapeOrientation << landscapeSize; + + // Rotations with 180 degree turns. + // Android 6 (API23) Does not understand these transitions. + if (QNativeInterface::QAndroidApplication::sdkVersion() > __ANDROID_API_M__) { + QTest::newRow("Landscape3") << 0 << Qt::LandscapeOrientation << landscapeSize; + QTest::newRow("InvertedLandscape4") + << 8 << Qt::InvertedLandscapeOrientation << landscapeSize; + QTest::newRow("Portrait3") << 1 << Qt::PortraitOrientation << portraitSize; + } else { + qWarning() << "180 degree turn rotation test cases are not run on Android 6 (API 23) and " + "below."; + } + // Android 8 (API 27) does not understand portrait-'inverted portrait'-portrait transition. + if (QNativeInterface::QAndroidApplication::sdkVersion() > __ANDROID_API_O_MR1__) { + QTest::newRow("InvertedPortrait3") << 9 << Qt::InvertedPortraitOrientation << portraitSize; + QTest::newRow("Portrait4") << 1 << Qt::PortraitOrientation << portraitSize; + } else { + qWarning() << "Portrait-'Inverted portrait'-Portrait rotation test cases are not run on " + "Android 8 (API 27) and below."; + } +} + +void tst_Android::orientationChange() +{ + QFETCH(int, nativeOrientation); + QFETCH(Qt::ScreenOrientation, expected); + QFETCH(QSize, screenSize); + + if (QNativeInterface::QAndroidApplication::sdkVersion() == __ANDROID_API_P__) + QSKIP("Android 9 orientation changes callbacks are buggy (QTBUG-124890)."); + + // For QTBUG-94459 to check that the widget size are consistent after orientation changes + QWidget widget; + widget.show(); + + QScreen *screen = QGuiApplication::primaryScreen(); + QSignalSpy orientationSpy(screen, SIGNAL(orientationChanged(Qt::ScreenOrientation))); + + auto context = QNativeInterface::QAndroidApplication::context(); + context.callMethod<void>("setRequestedOrientation", nativeOrientation); + + orientationSpy.wait(); + QTRY_COMPARE(screen->orientation(), expected); + QCOMPARE(orientationSpy.size(), 1); + QCOMPARE(screen->size(), screenSize); + QCOMPARE(widget.size(), screen->availableSize()); +} + QTEST_MAIN(tst_Android) #include "tst_android.moc" - |