File indexing completed on 2024-04-14 14:19:40

0001 /*  This file is part of the KDE project
0002     Copyright (C) 2010 David Faure <faure@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 
0019 */
0020 
0021 // Emulate being compiled in debug mode, so that kDebug(0) defaults to "on"
0022 #undef QT_NO_DEBUG
0023 
0024 // TODO: a separate test program with #define QT_NO_DEBUG...
0025 
0026 #include <QCoreApplication>
0027 #include <QtGlobal>
0028 #include <QFile>
0029 #include <QDir>
0030 #include <qstandardpaths.h>
0031 #include <kcomponentdata.h>
0032 #include <klocalizedstring.h>
0033 #include <kconfiggroup.h>
0034 #include <kdebug.h>
0035 #include <kcrash.h>
0036 
0037 // This test is actually called by kdebug_unittest
0038 // in order to see how kDebug behaves when there is no KComponentData.
0039 
0040 // Disable drkonqi, to avoid warning about it not being found (which breaks kdebug_unittest)
0041 void disableDrKonqi()
0042 {
0043     KCrash::setDrKonqiEnabled(false);
0044 }
0045 Q_CONSTRUCTOR_FUNCTION(disableDrKonqi)
0046 
0047 int main(int argc, char **argv)
0048 {
0049     QCoreApplication app(argc, argv);
0050 
0051     QStandardPaths::setTestModeEnabled(true);
0052     {
0053         KConfig config("kdebugrc");
0054         config.group(QString()).writeEntry("DisableAll", false); // in case of a global kdebugrc with DisableAll=true
0055     }
0056 
0057     // Test kDebug before and after KComponentData gets created
0058 
0059     // Register dynamic area (e.g. from oxygen style)
0060     const int myArea = KDebug::registerArea("qcoreapp_myarea");
0061     kDebug(myArea) << "Test debug using qcoreapp_myarea" << myArea;
0062 
0063     // Another dynamic area, but it was turned off (well, to file) by kdebug_unittest
0064     const int myFileArea = KDebug::registerArea("myarea");
0065     kDebug(myFileArea) << "Test logging to file debug using myarea";
0066 
0067     // Test that KDebug works
0068     kDebug(264) << "Debug in area 264, off by default, no output";
0069     kDebug(100) << "Debug in area 100"; // unknown area, will use area 0 instead
0070     // This should appear, but disabling kdebug_qcoreapptest in kdebugdialog should make it go away.";
0071     kDebug() << "Simple debug";
0072 
0073     if (KComponentData::hasMainComponent()) {
0074         abort();
0075         return 2;
0076     }
0077 
0078     KConfig config("kdebugrc");
0079     Q_ASSERT(!config.hasGroup(QString::number(myArea)));
0080     // only true when called by kdebug_unnittest
0081     //Q_ASSERT(config.hasGroup("myarea"));
0082     //Q_ASSERT(config.group("myarea").readEntry("InfoOutput", 2) == 0);
0083 
0084     if (KComponentData::hasMainComponent()) {
0085         abort();
0086         return 3;
0087     }
0088 
0089     // Test what happens when creating a main component data _now_
0090     // In KF5: nothing.
0091     KComponentData mainData("kdebug_qcoreapptest_mainData");
0092     if (!KComponentData::hasMainComponent()) {
0093         abort();
0094         return 4;
0095     }
0096     kDebug() << "This should appear, under the kdebug_qcoreapptest area";
0097     kDebug(264) << "Debug in area 264, still off by default";
0098     kDebug(100) << "Debug in area 100"; // unknown area, will use area 0 instead
0099 
0100     return 0;
0101 }
0102