tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / io / qa / textinputstream.cxx
blobe53a35ba38015303b495eba013f4399d28c667e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <algorithm>
13 #include <cassert>
14 #include <cstring>
16 #include <com/sun/star/io/NotConnectedException.hpp>
17 #include <com/sun/star/io/TextInputStream.hpp>
18 #include <com/sun/star/io/XInputStream.hpp>
19 #include <com/sun/star/io/XTextInputStream2.hpp>
20 #include <com/sun/star/uno/Sequence.hxx>
21 #include <com/sun/star/uno/Reference.hxx>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppunit/TestAssert.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/plugin/TestPlugIn.h>
26 #include <osl/mutex.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
29 #include <unotest/bootstrapfixturebase.hxx>
31 namespace {
33 class Input: public cppu::WeakImplHelper<css::io::XInputStream> {
34 public:
35 Input(char* inputData, sal_Int32 inputSize):
36 open_(true),
37 index_(0),
38 size(inputSize),
39 data(inputData) {}
41 private:
42 virtual ~Input() override {}
44 sal_Int32 SAL_CALL readBytes(css::uno::Sequence<sal_Int8> &, sal_Int32)
45 override
46 { CPPUNIT_FAIL("readLine is supposed to call readSomeBytes instead"); }
48 sal_Int32 SAL_CALL readSomeBytes(
49 css::uno::Sequence<sal_Int8 > & aData, sal_Int32 nMaxBytesToRead) override
51 assert(nMaxBytesToRead >= 0);
52 osl::MutexGuard g(mutex_);
53 checkClosed();
54 assert(index_ >= 0 && index_ <= size);
55 sal_Int32 n = std::min<sal_Int32>(
56 std::min<sal_Int32>(nMaxBytesToRead, 2), size - index_);
57 assert(n >= 0 && n <= size - index_);
58 aData.realloc(n);
59 std::memcpy(aData.getArray(), data + index_, n);
60 index_ += n;
61 assert(index_ >= 0 && index_ <= size);
62 return n;
65 void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
67 assert(nBytesToSkip >= 0);
68 osl::MutexGuard g(mutex_);
69 checkClosed();
70 assert(index_ >= 0 && index_ <= size);
71 index_ += std::min<sal_Int32>(nBytesToSkip, size - index_);
72 assert(index_ >= 0 && index_ <= size);
75 sal_Int32 SAL_CALL available() override
77 osl::MutexGuard g(mutex_);
78 checkClosed();
79 assert(index_ >= 0 && index_ <= size);
80 return size - index_;
83 void SAL_CALL closeInput() override
85 osl::MutexGuard g(mutex_);
86 checkClosed();
87 open_ = true;
90 void checkClosed() {
91 if (!open_) {
92 throw css::io::NotConnectedException(
93 u"test input stream already closed"_ustr);
97 osl::Mutex mutex_;
98 bool open_;
99 sal_Int32 index_;
101 sal_Int32 size;
102 char* data;
106 class Test: public test::BootstrapFixtureBase {
107 private:
108 CPPUNIT_TEST_SUITE(Test);
109 CPPUNIT_TEST(testReadLine);
110 CPPUNIT_TEST(testReadLineEndChars);
111 CPPUNIT_TEST_SUITE_END();
113 void testReadLine();
114 void testReadLineEndChars();
116 OUString readFirstLine(char data1[], int size);
119 OUString Test::readFirstLine(char *inputData, int inputSize) {
120 css::uno::Reference<css::io::XTextInputStream2> s(
121 css::io::TextInputStream::create(getComponentContext()));
122 s->setInputStream(new Input(inputData, inputSize));
123 return s->readLine();
126 void Test::testReadLine() {
127 char inputData[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
128 OUString l(readFirstLine(inputData, sizeof(inputData)));
129 CPPUNIT_ASSERT_EQUAL(u"123456789"_ustr, l);
132 void Test::testReadLineEndChars() {
133 std::vector<char> inputData = {'a', 'b', 'c', '\r'};
134 OUString l(readFirstLine(inputData.data(), inputData.size()));
135 CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l);
137 inputData = {'a', 'b', 'c', '\n'};
138 l = readFirstLine(inputData.data(), inputData.size());
139 CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l);
141 inputData = {'a', 'b', 'c', '\r', '\n'};
142 l = readFirstLine(inputData.data(), inputData.size());
143 CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l);
145 inputData = {'a', 'b', 'c', '\r', 'd', 'e', 'f'};
146 l = readFirstLine(inputData.data(), inputData.size());
147 CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l);
149 inputData = {'a', 'b', 'c', '\n', 'd', 'e', 'f'};
150 l = readFirstLine(inputData.data(), inputData.size());
151 CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l);
153 css::uno::Reference<css::io::XTextInputStream2> s(
154 css::io::TextInputStream::create(getComponentContext()));
155 inputData = {'a', 'b', 'c', '\r', '\n', 'd', 'e', 'f'};
156 s->setInputStream(new Input(inputData.data(), inputData.size()));
157 l = s->readLine();
158 CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l);
159 l = s->readLine();
160 CPPUNIT_ASSERT_EQUAL(u"def"_ustr, l);
164 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
168 CPPUNIT_PLUGIN_IMPLEMENT();
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */