tdf#161412 - UI: fix warning in PDF password dialog didn't disappear
[LibreOffice.git] / comphelper / qa / unit / test_guards.cxx
blob83034a2dcc6ae5b869d5639b37f07e5cf2949ac4
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 <comphelper/flagguard.hxx>
11 #include <unotest/bootstrapfixturebase.hxx>
13 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testScopeGuard)
15 // Test that comphelper::ScopeGuard executes its parameter on destruction
17 // initial value "true", out-of-scope ScopeGuard function executes and changes the value to "false"
18 bool bFlag = true;
20 comphelper::ScopeGuard aGuard([&bFlag] { bFlag = false; });
21 CPPUNIT_ASSERT(bFlag);
23 CPPUNIT_ASSERT(!bFlag);
26 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testFlagGuard)
28 // Test that comphelper::FlagGuard properly sets and resets the flag
30 // initial value "false", change to "true", out-of-scope change to "false"
31 bool bFlag = false;
33 comphelper::FlagGuard aGuard(bFlag);
34 CPPUNIT_ASSERT(bFlag);
36 // comphelper::FlagGuard must reset flag to false on destruction unconditionally
37 CPPUNIT_ASSERT(!bFlag);
39 // initial value "true", retain the value at "true", out-of-scope change to "false"
40 bFlag = true;
42 comphelper::FlagGuard aGuard(bFlag);
43 CPPUNIT_ASSERT(bFlag);
45 // comphelper::FlagGuard must reset flag to false on destruction unconditionally
46 CPPUNIT_ASSERT(!bFlag);
49 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testFlagRestorationGuard)
51 // Test that comphelper::FlagRestorationGuard properly sets and resets the flag
53 // initial value "true", change to "false", out-of-scope change to "true"
55 bool bFlag = true;
57 comphelper::FlagRestorationGuard aGuard(bFlag, false);
58 CPPUNIT_ASSERT(!bFlag);
60 // comphelper::FlagRestorationGuard must reset flag to initial state on destruction
61 CPPUNIT_ASSERT(bFlag);
64 CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testValueRestorationGuard)
66 // Test that comphelper::ValueRestorationGuard properly sets and resets the (int) value
68 int value = 199;
70 // set value and restore after scope ends
72 CPPUNIT_ASSERT_EQUAL(199, value);
73 comphelper::ValueRestorationGuard aGuard(value, 100);
74 CPPUNIT_ASSERT_EQUAL(100, value);
76 CPPUNIT_ASSERT_EQUAL(199, value);
78 // set value, manually setto another value and restore after scope ends
80 CPPUNIT_ASSERT_EQUAL(199, value);
81 comphelper::ValueRestorationGuard aGuard(value, 100);
82 CPPUNIT_ASSERT_EQUAL(100, value);
83 value = 200;
85 CPPUNIT_ASSERT_EQUAL(199, value);
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */