summaryrefslogtreecommitdiffstats
path: root/kword/tests/FrameViewTester.cpp
blob: c91de7e62239bd6c59eb1d2abbe244bcebed3b4d (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* This file is part of the KOffice project
 * Copyright (C) 2005 Thomas Zander <[email protected]>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; version 2.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include <tdeunittest/runner.h>
#include <tdeunittest/module.h>
#include <kdebug.h>

#include "FrameViewTester.h"
#include "FrameTester.h"

#include <KWFrame.h>
#include <KWFrameSet.h>
#include <KWFrameViewManager.h>
#include <KWFrameView.h>
#include <defs.h> // for mousemeaning

using namespace KUnitTest;

TDEUNITTEST_MODULE(tdeunittest_FrameViewTester, "FrameView Tester");
TDEUNITTEST_MODULE_REGISTER_TESTER(KWFrameViewTester);

void KWFrameViewTester::allTests() {
    testListeners();
    testGetView();
    testMouseMeaning();
}

class MockListener : public KWFramesListener {
public:
    int fsa, fsr, fa,fr;
    KWFrameSet *lastFs;
    KWFrame *lastF;
    MockListener() {
        fsa=0;
        fsr=0;
        fa=0;
        fr=0;
        lastFs=0;
        lastF=0;
    }
    void frameSetAdded(KWFrameSet *fs) {
        fsa++;
        lastFs=fs;
    }
    void frameSetRemoved(KWFrameSet *fs) {
        fsr++;
        lastFs=fs;
    }
    void frameAdded(KWFrame *f) {
        fa++;
        lastF=f;
    }
    void frameRemoved(KWFrame *f) {
        fr++;
        lastF=f;
    }
    void frameMoved(KWFrame *f) {
        lastF=f;
    }
    void frameResized(KWFrame *f) {
        lastF=f;
    }
};

class TestFrameViewManager : public KWFrameViewManager {
public:
    TestFrameViewManager() {
        m_fireEventsRequested = false;
    }
    bool m_fireEventsRequested;

    void doFireEvents() { fireEvents(); }
protected:
    void requestFireEvents() {
        m_fireEventsRequested = true;
    }
};

void KWFrameViewTester::testListeners() {
    KWFrameSet *fs = new TestFrameSet();
    KWFrame *f = new KWFrame(fs, 0, 1, 10, 11);

    TestFrameViewManager *viewManager = new TestFrameViewManager();
    MockListener *listener = new MockListener();
    viewManager->addKWFramesListener(listener);
    CHECK(listener->fsa, 0);
    // frameSetAdded
    viewManager->slotFrameSetAdded(fs);
    CHECK(listener->fsa, 0);
    CHECK(viewManager->m_fireEventsRequested, true);
    viewManager->doFireEvents();
    CHECK(listener->fsa, 1);
    CHECK(listener->fsr, 0);
    CHECK(listener->fa, 0);
    CHECK(listener->fr, 0);
    CHECK(listener->lastFs, fs);

    // reset
    listener->fsa = 0;
    viewManager->m_fireEventsRequested = false;
    // frameSetRemoved
    viewManager->slotFrameSetRemoved(fs);
    CHECK(viewManager->m_fireEventsRequested, true);
    viewManager->doFireEvents();
    CHECK(listener->fsa, 0);
    CHECK(listener->fsr, 1);
    CHECK(listener->fa, 0);
    CHECK(listener->fr, 0);
    CHECK(listener->lastFs, fs);

    // reset
    listener->fsr = 0;
    viewManager->m_fireEventsRequested = false;
    // frameSetRemoved
    viewManager->slotFrameAdded(f);
    CHECK(viewManager->m_fireEventsRequested, true);
    viewManager->doFireEvents();
    CHECK(listener->fsr, 0);
    CHECK(listener->fsa, 0);
    CHECK(listener->fa, 1);
    CHECK(listener->fr, 0);
    CHECK(listener->lastF, f);

    // reset
    listener->fa = 0;
    viewManager->m_fireEventsRequested = false;
    // frameSetRemoved
    viewManager->slotFrameRemoved(f);
    CHECK(viewManager->m_fireEventsRequested, true);
    viewManager->doFireEvents();
    CHECK(listener->fsr, 0);
    CHECK(listener->fsa, 0);
    CHECK(listener->fa, 0);
    CHECK(listener->fr, 1);
    CHECK(listener->lastF, f);

    // reset
    listener->fr = 0;
    viewManager->m_fireEventsRequested = false;
    // multiple
    KWFrame *f2 = new KWFrame(fs, 0, 1, 2, 3);
    viewManager->slotFrameRemoved(f);
    viewManager->slotFrameSetAdded(fs);
    viewManager->slotFrameAdded(f);
    viewManager->slotFrameAdded(f);
    viewManager->slotFrameAdded(f2);
    viewManager->doFireEvents();
    CHECK(listener->fsr, 0);
    CHECK(listener->fsa, 1);
    CHECK(listener->fa, 3);
    CHECK(listener->fr, 1);
    CHECK(listener->lastF, f2);

    viewManager->slotFrameSetAdded(fs);
    viewManager->removeKWFramesListener(listener);
    viewManager->doFireEvents();
    CHECK(listener->fsr, 0);

    // frame moved
    // frame resized
}

void KWFrameViewTester::testGetView() {
    TestFrameViewManager fvm;
    KWFrameSet *fs = new TestFrameSet();
    KWFrame *frame = new KWFrame(fs, 0, 0, 100, 100);
    fs->addFrame(frame);

    fvm.slotFrameAdded(frame);
    KWFrameView *fv = fvm.view(frame);
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame);

    fv = fvm.view( KoPoint(0,0), KWFrameViewManager::frameOnTop);
    CHECK(fv != 0, true); // should exist
    if(!fv) return;
    CHECK(fv->frame(), frame);

    fv = fvm.view( KoPoint(1,1), KWFrameViewManager::frameOnTop);
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame);

    fv = fvm.view( KoPoint(110,110), KWFrameViewManager::frameOnTop);
    CHECK(fv == 0, true);

    fv = fvm.view( KoPoint(100,100), KWFrameViewManager::frameOnTop); // note that 100,100 _is_ inside 0,0 100x100
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame);

    fv->setSelected(true);
    fv = fvm.view( KoPoint(1,1), KWFrameViewManager::unselected);
    CHECK(fv == 0, true);

    frame->setZOrder(4);
    KWFrame *frame2 = new KWFrame(fs, 80, 50, 60, 60);
    frame2->setZOrder(2);
    fvm.slotFrameAdded(frame2);
    KWFrame *frame3 = new KWFrame(fs, 30, 60, 60, 60);
    frame3->setZOrder(5);
    fvm.slotFrameAdded(frame3);
    KWFrame *frame4 = new KWFrame(fs, 200, 0, 100, 100);
    fvm.slotFrameAdded(frame4);
    frame4->setZOrder(1);

kdDebug() << "frame:" << frame << ", frame2:" << frame2<< ", frame2:" << frame3<< ", frame2:" << frame4 << endl;
    fv = fvm.view( KoPoint(90,60), KWFrameViewManager::frameOnTop);
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame3);
    fv = fvm.view( KoPoint(90,60), KWFrameViewManager::frameOnTop); // second time should give the same results
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame3);
    fv = fvm.view( KoPoint(90,60), KWFrameViewManager::selected);
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame);
    fv = fvm.view( KoPoint(90,60), KWFrameViewManager::selected); // second time should give the same results
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame);

    fv = fvm.view( KoPoint(90,60), KWFrameViewManager::nextUnselected);
    CHECK(fv != 0, true);
    if(!fv) return;
    CHECK(fv->frame(), frame2); // the one directly below the selected frame
}

void KWFrameViewTester::testMouseMeaning() {
    TestFrameViewManager fvm;
    KWFrameSet *fs = new TestFrameSet();
    KWFrame *frame = new KWFrame(fs, 50, 50, 50, 50);

    fvm.slotFrameAdded(frame);
    KWFrameView *fv = fvm.view(frame);
    if(fv == 0) return; // means previous test already failed a lot.

    MouseMeaning answers[9 * 3] = { MEANING_TOPLEFT, MEANING_LEFT, MEANING_BOTTOMLEFT,
                     MEANING_TOPLEFT, MEANING_LEFT, MEANING_BOTTOMLEFT,
                     MEANING_TOPLEFT, MEANING_LEFT, MEANING_BOTTOMLEFT,
                    MEANING_TOP, MEANING_MOUSE_INSIDE_TEXT, MEANING_BOTTOM,
                    MEANING_TOP, MEANING_MOUSE_INSIDE_TEXT, MEANING_BOTTOM,
                    MEANING_TOP, MEANING_MOUSE_INSIDE_TEXT, MEANING_BOTTOM,
                    MEANING_TOPRIGHT, MEANING_RIGHT, MEANING_BOTTOMRIGHT,
                    MEANING_TOPRIGHT, MEANING_RIGHT, MEANING_BOTTOMRIGHT,
                    MEANING_TOPRIGHT, MEANING_RIGHT, MEANING_BOTTOMRIGHT };
    int count=0;

    KoPoint point(10, 10);
    CHECK(fv->mouseMeaning(point, 0), MEANING_NONE);
    for(int x = 50; x <= 100; x += 25) {
        for(int offset=-6; offset <= 6; offset+=6) {
            point.setX(x + offset);
            for(int y = 50; y <= 100; y += 25) {
                for(int offset2=-6; offset2 <= 6; offset2+=6) {
                    point.setY(y + offset2);
                    CHECK(fv->contains(point, true), true);
                    if(x < 50 || x > 100 || y < 50 || y > 100)
                        CHECK(fv->contains(point), false);
                    else
                        CHECK(fv->contains(point), true);
                    if(x > 60 && x < 90 && y > 60 && y < 90)
                        CHECK(fv->isBorderHit(point), false);
                    else
                        CHECK(fv->isBorderHit(point), true);
                    CHECK(fv->mouseMeaning(point, 0), answers[count]);
                }
                count++;
            }
        }
    }

    point.setX(50);
    point.setY(120);
    CHECK(fv->mouseMeaning(point, 0), MEANING_NONE);

}
//    TQCursor mouseCursor( const KoPoint &point, int keyState ) const;