1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <sal/config.h>
12 #include <comphelper/profilezone.hxx>
13 #include <comphelper/traceevent.hxx>
15 #include <rtl/ustring.hxx>
17 #include <cppunit/TestFixture.h>
18 #include <cppunit/extensions/HelperMacros.h>
20 class TestTraceEvent
: public CppUnit::TestFixture
25 CPPUNIT_TEST_SUITE(TestTraceEvent
);
27 CPPUNIT_TEST_SUITE_END();
32 void trace_event_test()
35 // When we start recording is off and this will not generate any 'X' event when we leave the scope
36 comphelper::ProfileZone
aZone0("test0");
38 // This will not generate any 'b' and 'e' events either
39 auto pAsync1(std::make_shared
<comphelper::AsyncEvent
>("async1"));
42 // No 'X' by this either
43 comphelper::ProfileZone
aZone1("block1");
45 // Now we turn on recording
46 comphelper::TraceEvent::startRecording();
49 // This will generate an 'i' event for instant1
50 comphelper::TraceEvent::addInstantEvent("instant1");
52 std::shared_ptr
<comphelper::AsyncEvent
> pAsync25
;
54 comphelper::ProfileZone
aZone2("block2");
56 // This does not generate any 'e' event as it was created when recording was off
57 // And the nested async2 object will thus not generate anything either
60 // This will generate 'b' event and an 'e' event when the pointer is reset or goes out of scope
61 pAsync25
= std::make_shared
<comphelper::AsyncEvent
>("async2.5");
63 // Leaving this scope will generate an 'X' event for block2
66 // This will generate a 'b' event for async3
67 std::map
<OUString
, OUString
> aArgsAsync3({ { "foo", "bar" }, { "tem", "42" } });
68 auto pAsync3(std::make_shared
<comphelper::AsyncEvent
>("async3", aArgsAsync3
));
71 comphelper::ProfileZone
aZone3("block3");
73 // Leaving this scope will generate an 'X' event for block3
76 // This will generate an 'e' event for async2.5
79 comphelper::ProfileZone
aZone4("test2");
81 // This will generate an 'i' event for instant2"
82 std::map
<OUString
, OUString
> aArgsInstant2({ { "foo2", "bar2" }, { "tem2", "42" } });
83 comphelper::TraceEvent::addInstantEvent("instant2", aArgsInstant2
);
85 // Leaving this scope will generate 'X' events for test2 and a
86 // 'e' event for async4in3, async7in3, and async3.
89 // This incorrect use of overlapping (not nested) ProfileZones
90 // will generate a SAL_WARN but should not crash
91 auto p1
= new comphelper::ProfileZone("error1");
92 auto p2
= new comphelper::ProfileZone("error2");
98 void TestTraceEvent::test()
101 auto aEvents
= comphelper::TraceEvent::getEventVectorAndClear();
102 for (const auto& s
: aEvents
)
104 std::cerr
<< s
<< "\n";
107 CPPUNIT_ASSERT_EQUAL(9, static_cast<int>(aEvents
.size()));
109 CPPUNIT_ASSERT(aEvents
[0].startsWith("{\"name:\"instant1\",\"ph\":\"i\","));
110 CPPUNIT_ASSERT(aEvents
[1].startsWith("{\"name\":\"async2.5\",\"ph\":\"S\",\"id\":1,"));
111 CPPUNIT_ASSERT(aEvents
[2].startsWith("{\"name\":\"block2\",\"ph\":\"X\","));
112 CPPUNIT_ASSERT(aEvents
[3].startsWith(
113 "{\"name\":\"async3\",\"ph\":\"S\",\"id\":2,\"args\":{\"foo\":\"bar\",\"tem\":\"42\"},"));
114 CPPUNIT_ASSERT(aEvents
[4].startsWith("{\"name\":\"block3\",\"ph\":\"X\","));
115 CPPUNIT_ASSERT(aEvents
[5].startsWith("{\"name\":\"async2.5\",\"ph\":\"F\",\"id\":1,"));
116 CPPUNIT_ASSERT(aEvents
[6].startsWith(
117 "{\"name:\"instant2\",\"ph\":\"i\",\"args\":{\"foo2\":\"bar2\",\"tem2\":\"42\"},"));
118 CPPUNIT_ASSERT(aEvents
[7].startsWith("{\"name\":\"test2\",\"ph\":\"X\""));
119 CPPUNIT_ASSERT(aEvents
[8].startsWith(
120 "{\"name\":\"async3\",\"ph\":\"F\",\"id\":2,\"args\":{\"foo\":\"bar\",\"tem\":\"42\"},"));
123 CPPUNIT_TEST_SUITE_REGISTRATION(TestTraceEvent
);
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */