File indexing completed on 2024-05-19 05:42:28
0001 // ct_lvtqtw_textview.t.cpp 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #include <catch2-local-includes.h> 0021 0022 #include <QDir> 0023 #include <ct_lvtqtw_textview.cpp> 0024 #include <ct_lvttst_fixture_qt.h> 0025 #include <qevent.h> 0026 0027 using namespace Codethink::lvtqtw; 0028 0029 TEST_CASE_METHOD(QTApplicationFixture, "Append text to TextView") 0030 { 0031 TextView tv(1); 0032 tv.show(); 0033 // smoke test 0034 tv.appendText(""); 0035 tv.appendText("Hello"); 0036 tv.appendText("world"); 0037 REQUIRE(tv.toPlainText().toStdString() == "Hello\nworld"); 0038 tv.hide(); 0039 REQUIRE(tv.toPlainText().toStdString().empty()); 0040 tv.show(); 0041 REQUIRE(tv.toPlainText().trimmed().toStdString() == "Hello\nworld"); 0042 } 0043 0044 TEST_CASE_METHOD(QTApplicationFixture, "Save text to file from TextView") 0045 { 0046 // create a TextView object 0047 TextView tv(1); 0048 tv.show(); 0049 const QString testText = "This Is a Unit Test"; 0050 // test appendText method 0051 tv.appendText(testText); 0052 REQUIRE(tv.toPlainText() == testText); 0053 // prepare directory 0054 QString filePath = QDir::tempPath() + QDir::separator() + "test.txt"; 0055 QFile file(filePath); 0056 file.remove(); 0057 // test saveFileTo method 0058 tv.saveFileTo(filePath); 0059 // Reading saved File content 0060 QString savedText; 0061 REQUIRE(file.open(QIODevice::ReadOnly | QIODevice::Text)); 0062 while (!file.atEnd()) { 0063 QByteArray line = file.readLine(); 0064 savedText.append(line); 0065 } 0066 // Check that the text was saved correctly 0067 REQUIRE(savedText.trimmed().toStdString() == testText.toStdString()); 0068 }