From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- comphelper/qa/unit/test_traceevent.cxx | 125 +++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 comphelper/qa/unit/test_traceevent.cxx (limited to 'comphelper/qa/unit/test_traceevent.cxx') diff --git a/comphelper/qa/unit/test_traceevent.cxx b/comphelper/qa/unit/test_traceevent.cxx new file mode 100644 index 000000000..34d10f519 --- /dev/null +++ b/comphelper/qa/unit/test_traceevent.cxx @@ -0,0 +1,125 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include + +#include + +#include +#include + +class TestTraceEvent : public CppUnit::TestFixture +{ +public: + void test(); + + CPPUNIT_TEST_SUITE(TestTraceEvent); + CPPUNIT_TEST(test); + CPPUNIT_TEST_SUITE_END(); +}; + +namespace +{ +void trace_event_test() +{ + { + // When we start recording is off and this will not generate any 'X' event when we leave the scope + comphelper::ProfileZone aZone0("test0"); + + // This will not generate any 'b' and 'e' events either + auto pAsync1(std::make_shared("async1")); + + { + // No 'X' by this either + comphelper::ProfileZone aZone1("block1"); + + // Now we turn on recording + comphelper::TraceEvent::startRecording(); + } + + // This will generate an 'i' event for instant1 + comphelper::TraceEvent::addInstantEvent("instant1"); + + std::shared_ptr pAsync25; + { + comphelper::ProfileZone aZone2("block2"); + + // This does not generate any 'e' event as it was created when recording was off + // And the nested async2 object will thus not generate anything either + pAsync1.reset(); + + // This will generate 'b' event and an 'e' event when the pointer is reset or goes out of scope + pAsync25 = std::make_shared("async2.5"); + + // Leaving this scope will generate an 'X' event for block2 + } + + // This will generate a 'b' event for async3 + std::map aArgsAsync3({ { "foo", "bar" }, { "tem", "42" } }); + auto pAsync3(std::make_shared("async3", aArgsAsync3)); + + { + comphelper::ProfileZone aZone3("block3"); + + // Leaving this scope will generate an 'X' event for block3 + } + + // This will generate an 'e' event for async2.5 + pAsync25.reset(); + + comphelper::ProfileZone aZone4("test2"); + + // This will generate an 'i' event for instant2" + std::map aArgsInstant2({ { "foo2", "bar2" }, { "tem2", "42" } }); + comphelper::TraceEvent::addInstantEvent("instant2", aArgsInstant2); + + // Leaving this scope will generate 'X' events for test2 and a + // 'e' event for async4in3, async7in3, and async3. + } + + // This incorrect use of overlapping (not nested) ProfileZones + // will generate a SAL_WARN but should not crash + auto p1 = new comphelper::ProfileZone("error1"); + auto p2 = new comphelper::ProfileZone("error2"); + delete p1; + delete p2; +} +} + +void TestTraceEvent::test() +{ + trace_event_test(); + auto aEvents = comphelper::TraceEvent::getEventVectorAndClear(); + for (const auto& s : aEvents) + { + std::cerr << s << "\n"; + } + + CPPUNIT_ASSERT_EQUAL(9, static_cast(aEvents.size())); + + CPPUNIT_ASSERT(aEvents[0].startsWith("{\"name:\"instant1\",\"ph\":\"i\",")); + CPPUNIT_ASSERT(aEvents[1].startsWith("{\"name\":\"async2.5\",\"ph\":\"S\",\"id\":1,")); + CPPUNIT_ASSERT(aEvents[2].startsWith("{\"name\":\"block2\",\"ph\":\"X\",")); + CPPUNIT_ASSERT(aEvents[3].startsWith( + "{\"name\":\"async3\",\"ph\":\"S\",\"id\":2,\"args\":{\"foo\":\"bar\",\"tem\":\"42\"},")); + CPPUNIT_ASSERT(aEvents[4].startsWith("{\"name\":\"block3\",\"ph\":\"X\",")); + CPPUNIT_ASSERT(aEvents[5].startsWith("{\"name\":\"async2.5\",\"ph\":\"F\",\"id\":1,")); + CPPUNIT_ASSERT(aEvents[6].startsWith( + "{\"name:\"instant2\",\"ph\":\"i\",\"args\":{\"foo2\":\"bar2\",\"tem2\":\"42\"},")); + CPPUNIT_ASSERT(aEvents[7].startsWith("{\"name\":\"test2\",\"ph\":\"X\"")); + CPPUNIT_ASSERT(aEvents[8].startsWith( + "{\"name\":\"async3\",\"ph\":\"F\",\"id\":2,\"args\":{\"foo\":\"bar\",\"tem\":\"42\"},")); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(TestTraceEvent); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3