summaryrefslogtreecommitdiff
path: root/vcl/qa/cppunit/lifecycle.cxx
blob: 2a46022eb5696142c1704ed8913cdf29998ffea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* -*- 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 <unotest/filters-test.hxx>
#include <test/bootstrapfixture.hxx>

#include <vcl/wrkwin.hxx>
#include <vcl/button.hxx>
#include <vcl/edit.hxx>
#include <vcl/combobox.hxx>
#include <vcl/field.hxx>

class LifecycleTest : public test::BootstrapFixture
{
    void testWidgets(vcl::Window *pParent);

public:
    LifecycleTest() : BootstrapFixture(true, false) {}

    void testCast();
    void testMultiDispose();
    void testIsolatedWidgets();
    void testParentedWidgets();
    void testChildDispose();

    CPPUNIT_TEST_SUITE(LifecycleTest);
    CPPUNIT_TEST(testCast);
    CPPUNIT_TEST(testMultiDispose);
    CPPUNIT_TEST(testIsolatedWidgets);
    CPPUNIT_TEST(testParentedWidgets);
    CPPUNIT_TEST(testChildDispose);
    CPPUNIT_TEST_SUITE_END();
};

// A compile time sanity check
void LifecycleTest::testCast()
{
    VclPtr<PushButton> xButton(new PushButton(NULL, 0));
    VclPtr<vcl::Window> xWindow(xButton);

    VclPtr<MetricField> xField(new MetricField(NULL, 0));
    VclPtr<SpinField> xSpin(xField);
    VclPtr<Edit> xEdit(xField);

// the following line should NOT compile
//    VclPtr<PushButton> xButton2(xWindow);
}

void LifecycleTest::testMultiDispose()
{
    VclPtr<WorkWindow> xWin(new WorkWindow((vcl::Window *)NULL,
                                           WB_APP|WB_STDWORK));
    CPPUNIT_ASSERT(xWin.get() != NULL);
    xWin->dispose();
    xWin->dispose();
    xWin->dispose();
    CPPUNIT_ASSERT(xWin->GetWindow(0) == NULL);
    CPPUNIT_ASSERT(xWin->GetChild(0) == NULL);
    CPPUNIT_ASSERT(xWin->GetChildCount() == 0);
}

void LifecycleTest::testWidgets(vcl::Window *pParent)
{
    { VclPtr<PushButton>   aPtr(new PushButton(pParent));   }
    { VclPtr<OKButton>     aPtr(new OKButton(pParent));     }
    { VclPtr<CancelButton> aPtr(new CancelButton(pParent)); }
    { VclPtr<HelpButton>   aPtr(new HelpButton(pParent));   }

    // Some widgets really insist on adoption.
    if (pParent)
    {
        { VclPtr<CheckBox>     aPtr(new CheckBox(pParent));     }
        { VclPtr<Edit>         aPtr(new Edit(pParent));         }
        { VclPtr<ComboBox>     aPtr(new ComboBox(pParent));     }
        { VclPtr<RadioButton>  aPtr(new RadioButton(pParent));  }
    }
}

void LifecycleTest::testIsolatedWidgets()
{
    testWidgets(NULL);
}

void LifecycleTest::testParentedWidgets()
{
    VclPtr<WorkWindow> xWin(new WorkWindow((vcl::Window *)NULL,
                                                 WB_APP|WB_STDWORK));
    CPPUNIT_ASSERT(xWin.get() != NULL);
    xWin->Show();
    testWidgets(xWin);
}

class DisposableChild : public vcl::Window
{
public:
    DisposableChild(vcl::Window *pParent) : vcl::Window(pParent) {}
    virtual ~DisposableChild()
    {
        dispose();
    }
};

void LifecycleTest::testChildDispose()
{
    VclPtr<WorkWindow> xWin(new WorkWindow((vcl::Window *)NULL,
                                                 WB_APP|WB_STDWORK));
    CPPUNIT_ASSERT(xWin.get() != NULL);
    VclPtr<DisposableChild> xChild(new DisposableChild(xWin.get()));
    xWin->Show();
    xChild->dispose();
    xWin->dispose();
}

CPPUNIT_TEST_SUITE_REGISTRATION(LifecycleTest);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */