aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/simpletest/test/frames_test.php
blob: 29309700e3115ff1b30ac08ec9f522466f9c96ee (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
// $Id: frames_test.php 1899 2009-07-28 19:33:42Z lastcraft $
require_once(dirname(__FILE__) . '/../autorun.php');
require_once(dirname(__FILE__) . '/../tag.php');
require_once(dirname(__FILE__) . '/../page.php');
require_once(dirname(__FILE__) . '/../frames.php');
Mock::generate('SimplePage');
Mock::generate('SimpleForm');

class TestOfFrameset extends UnitTestCase {

    function testTitleReadFromFramesetPage() {
        $page = new MockSimplePage();
        $page->setReturnValue('getTitle', 'This page');
        $frameset = new SimpleFrameset($page);
        $this->assertEqual($frameset->getTitle(), 'This page');
    }

    function TestHeadersReadFromFramesetByDefault() {
        $page = new MockSimplePage();
        $page->setReturnValue('getHeaders', 'Header: content');
        $page->setReturnValue('getMimeType', 'text/xml');
        $page->setReturnValue('getResponseCode', 401);
        $page->setReturnValue('getTransportError', 'Could not parse headers');
        $page->setReturnValue('getAuthentication', 'Basic');
        $page->setReturnValue('getRealm', 'Safe place');

        $frameset = new SimpleFrameset($page);

        $this->assertIdentical($frameset->getHeaders(), 'Header: content');
        $this->assertIdentical($frameset->getMimeType(), 'text/xml');
        $this->assertIdentical($frameset->getResponseCode(), 401);
        $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
        $this->assertIdentical($frameset->getAuthentication(), 'Basic');
        $this->assertIdentical($frameset->getRealm(), 'Safe place');
    }

    function testEmptyFramesetHasNoContent() {
        $page = new MockSimplePage();
        $page->setReturnValue('getRaw', 'This content');
        $frameset = new SimpleFrameset($page);
        $this->assertEqual($frameset->getRaw(), '');
    }

    function testRawContentIsFromOnlyFrame() {
        $page = new MockSimplePage();
        $page->expectNever('getRaw');

        $frame = new MockSimplePage();
        $frame->setReturnValue('getRaw', 'Stuff');

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame);
        $this->assertEqual($frameset->getRaw(), 'Stuff');
    }

    function testRawContentIsFromAllFrames() {
        $page = new MockSimplePage();
        $page->expectNever('getRaw');

        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getRaw', 'Stuff1');

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getRaw', 'Stuff2');

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1);
        $frameset->addFrame($frame2);
        $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
    }

    function testTextContentIsFromOnlyFrame() {
        $page = new MockSimplePage();
        $page->expectNever('getText');

        $frame = new MockSimplePage();
        $frame->setReturnValue('getText', 'Stuff');

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame);
        $this->assertEqual($frameset->getText(), 'Stuff');
    }

    function testTextContentIsFromAllFrames() {
        $page = new MockSimplePage();
        $page->expectNever('getText');

        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getText', 'Stuff1');

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getText', 'Stuff2');

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1);
        $frameset->addFrame($frame2);
        $this->assertEqual($frameset->getText(), 'Stuff1 Stuff2');
    }

    function testFieldFoundIsFirstInFramelist() {
        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getField', null);
        $frame1->expectOnce('getField', array(new SimpleByName('a')));

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getField', 'A');
        $frame2->expectOnce('getField', array(new SimpleByName('a')));

        $frame3 = new MockSimplePage();
        $frame3->expectNever('getField');

        $page = new MockSimplePage();
        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1);
        $frameset->addFrame($frame2);
        $frameset->addFrame($frame3);
        $this->assertIdentical($frameset->getField(new SimpleByName('a')), 'A');
    }

    function testFrameReplacementByIndex() {
        $page = new MockSimplePage();
        $page->expectNever('getRaw');

        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getRaw', 'Stuff1');

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getRaw', 'Stuff2');

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1);
        $frameset->setFrame(array(1), $frame2);
        $this->assertEqual($frameset->getRaw(), 'Stuff2');
    }

    function testFrameReplacementByName() {
        $page = new MockSimplePage();
        $page->expectNever('getRaw');

        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getRaw', 'Stuff1');

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getRaw', 'Stuff2');

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1, 'a');
        $frameset->setFrame(array('a'), $frame2);
        $this->assertEqual($frameset->getRaw(), 'Stuff2');
    }
}

class TestOfFrameNavigation extends UnitTestCase {

    function testStartsWithoutFrameFocus() {
        $page = new MockSimplePage();
        $frameset = new SimpleFrameset($page);
        $frameset->addFrame(new MockSimplePage());
        $this->assertFalse($frameset->getFrameFocus());
    }

    function testCanFocusOnSingleFrame() {
        $page = new MockSimplePage();
        $page->expectNever('getRaw');

        $frame = new MockSimplePage();
        $frame->setReturnValue('getFrameFocus', array());
        $frame->setReturnValue('getRaw', 'Stuff');

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame);

        $this->assertFalse($frameset->setFrameFocusByIndex(0));
        $this->assertTrue($frameset->setFrameFocusByIndex(1));
        $this->assertEqual($frameset->getRaw(), 'Stuff');
        $this->assertFalse($frameset->setFrameFocusByIndex(2));
        $this->assertIdentical($frameset->getFrameFocus(), array(1));
    }

    function testContentComesFromFrameInFocus() {
        $page = new MockSimplePage();

        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getRaw', 'Stuff1');
        $frame1->setReturnValue('getFrameFocus', array());

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getRaw', 'Stuff2');
        $frame2->setReturnValue('getFrameFocus', array());

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1);
        $frameset->addFrame($frame2);

        $this->assertTrue($frameset->setFrameFocusByIndex(1));
        $this->assertEqual($frameset->getFrameFocus(), array(1));
        $this->assertEqual($frameset->getRaw(), 'Stuff1');

        $this->assertTrue($frameset->setFrameFocusByIndex(2));
        $this->assertEqual($frameset->getFrameFocus(), array(2));
        $this->assertEqual($frameset->getRaw(), 'Stuff2');

        $this->assertFalse($frameset->setFrameFocusByIndex(3));
        $this->assertEqual($frameset->getFrameFocus(), array(2));

        $frameset->clearFrameFocus();
        $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
    }

    function testCanFocusByName() {
        $page = new MockSimplePage();

        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getRaw', 'Stuff1');
        $frame1->setReturnValue('getFrameFocus', array());

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getRaw', 'Stuff2');
        $frame2->setReturnValue('getFrameFocus', array());

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1, 'A');
        $frameset->addFrame($frame2, 'B');

        $this->assertTrue($frameset->setFrameFocus('A'));
        $this->assertEqual($frameset->getFrameFocus(), array('A'));
        $this->assertEqual($frameset->getRaw(), 'Stuff1');

        $this->assertTrue($frameset->setFrameFocusByIndex(2));
        $this->assertEqual($frameset->getFrameFocus(), array('B'));
        $this->assertEqual($frameset->getRaw(), 'Stuff2');

        $this->assertFalse($frameset->setFrameFocus('z'));

        $frameset->clearFrameFocus();
        $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
    }
}

class TestOfFramesetPageInterface extends UnitTestCase {
    private $page_interface;
    private $frameset_interface;

    function __construct() {
        parent::__construct();
        $this->page_interface = $this->getPageMethods();
        $this->frameset_interface = $this->getFramesetMethods();
    }

    function assertListInAnyOrder($list, $expected) {
        sort($list);
        sort($expected);
        $this->assertEqual($list, $expected);
    }

    private function getPageMethods() {
        $methods = array();
        foreach (get_class_methods('SimplePage') as $method) {
            if (strtolower($method) == strtolower('SimplePage')) {
                continue;
            }
            if (strtolower($method) == strtolower('getFrameset')) {
                continue;
            }
            if (strncmp($method, '_', 1) == 0) {
                continue;
            }
            if (in_array($method, array('setTitle', 'setBase', 'setForms', 'normalise', 'setFrames', 'addLink'))) {
                continue;
            }
            $methods[] = $method;
        }
        return $methods;
    }

    private function getFramesetMethods() {
        $methods = array();
        foreach (get_class_methods('SimpleFrameset') as $method) {
            if (strtolower($method) == strtolower('SimpleFrameset')) {
                continue;
            }
            if (strncmp($method, '_', 1) == 0) {
                continue;
            }
            if (strncmp($method, 'add', 3) == 0) {
                continue;
            }
            $methods[] = $method;
        }
        return $methods;
    }

    function testFramsetHasPageInterface() {
        $difference = array();
        foreach ($this->page_interface as $method) {
            if (! in_array($method, $this->frameset_interface)) {
                $this->fail("No [$method] in Frameset class");
                return;
            }
        }
        $this->pass('Frameset covers Page interface');
    }

    function testHeadersReadFromFrameIfInFocus() {
        $frame = new MockSimplePage();
        $frame->setReturnValue('getUrl', new SimpleUrl('http://localhost/stuff'));

        $frame->setReturnValue('getRequest', 'POST stuff');
        $frame->setReturnValue('getMethod', 'POST');
        $frame->setReturnValue('getRequestData', array('a' => 'A'));
        $frame->setReturnValue('getHeaders', 'Header: content');
        $frame->setReturnValue('getMimeType', 'text/xml');
        $frame->setReturnValue('getResponseCode', 401);
        $frame->setReturnValue('getTransportError', 'Could not parse headers');
        $frame->setReturnValue('getAuthentication', 'Basic');
        $frame->setReturnValue('getRealm', 'Safe place');

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame);
        $frameset->setFrameFocusByIndex(1);

        $url = new SimpleUrl('http://localhost/stuff');
        $url->setTarget(1);
        $this->assertIdentical($frameset->getUrl(), $url);

        $this->assertIdentical($frameset->getRequest(), 'POST stuff');
        $this->assertIdentical($frameset->getMethod(), 'POST');
        $this->assertIdentical($frameset->getRequestData(), array('a' => 'A'));
        $this->assertIdentical($frameset->getHeaders(), 'Header: content');
        $this->assertIdentical($frameset->getMimeType(), 'text/xml');
        $this->assertIdentical($frameset->getResponseCode(), 401);
        $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
        $this->assertIdentical($frameset->getAuthentication(), 'Basic');
        $this->assertIdentical($frameset->getRealm(), 'Safe place');
    }

    function testUrlsComeFromBothFrames() {
        $page = new MockSimplePage();
        $page->expectNever('getUrls');

        $frame1 = new MockSimplePage();
        $frame1->setReturnValue(
                'getUrls',
                array('http://www.lastcraft.com/', 'http://myserver/'));

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue(
                'getUrls',
                array('http://www.lastcraft.com/', 'http://test/'));

        $frameset = new SimpleFrameset($page);
        $frameset->addFrame($frame1);
        $frameset->addFrame($frame2);
        $this->assertListInAnyOrder(
                $frameset->getUrls(),
                array('http://www.lastcraft.com/', 'http://myserver/', 'http://test/'));
    }

    function testLabelledUrlsComeFromBothFrames() {
        $frame1 = new MockSimplePage();
        $frame1->setReturnValue(
                'getUrlsByLabel',
                array(new SimpleUrl('goodbye.php')),
                array('a'));

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue(
                'getUrlsByLabel',
                array(new SimpleUrl('hello.php')),
                array('a'));

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame1);
        $frameset->addFrame($frame2, 'Two');

        $expected1 = new SimpleUrl('goodbye.php');
        $expected1->setTarget(1);
        $expected2 = new SimpleUrl('hello.php');
        $expected2->setTarget('Two');
        $this->assertEqual(
                $frameset->getUrlsByLabel('a'),
                array($expected1, $expected2));
    }

    function testUrlByIdComesFromFirstFrameToRespond() {
        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getUrlById', new SimpleUrl('four.php'), array(4));
        $frame1->setReturnValue('getUrlById', false, array(5));

        $frame2 = new MockSimplePage();
        $frame2->setReturnValue('getUrlById', false, array(4));
        $frame2->setReturnValue('getUrlById', new SimpleUrl('five.php'), array(5));

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame1);
        $frameset->addFrame($frame2);

        $four = new SimpleUrl('four.php');
        $four->setTarget(1);
        $this->assertEqual($frameset->getUrlById(4), $four);
        $five = new SimpleUrl('five.php');
        $five->setTarget(2);
        $this->assertEqual($frameset->getUrlById(5), $five);
    }

    function testReadUrlsFromFrameInFocus() {
        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getUrls', array('a'));
        $frame1->setReturnValue('getUrlsByLabel', array(new SimpleUrl('l')));
        $frame1->setReturnValue('getUrlById', new SimpleUrl('i'));

        $frame2 = new MockSimplePage();
        $frame2->expectNever('getUrls');
        $frame2->expectNever('getUrlsByLabel');
        $frame2->expectNever('getUrlById');

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame1, 'A');
        $frameset->addFrame($frame2, 'B');
        $frameset->setFrameFocus('A');

        $this->assertIdentical($frameset->getUrls(), array('a'));
        $expected = new SimpleUrl('l');
        $expected->setTarget('A');
        $this->assertIdentical($frameset->getUrlsByLabel('label'), array($expected));
        $expected = new SimpleUrl('i');
        $expected->setTarget('A');
        $this->assertIdentical($frameset->getUrlById(99), $expected);
    }

    function testReadFrameTaggedUrlsFromFrameInFocus() {
        $frame = new MockSimplePage();

        $by_label = new SimpleUrl('l');
        $by_label->setTarget('L');
        $frame->setReturnValue('getUrlsByLabel', array($by_label));

        $by_id = new SimpleUrl('i');
        $by_id->setTarget('I');
        $frame->setReturnValue('getUrlById', $by_id);

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame, 'A');
        $frameset->setFrameFocus('A');

        $this->assertIdentical($frameset->getUrlsByLabel('label'), array($by_label));
        $this->assertIdentical($frameset->getUrlById(99), $by_id);
    }

    function testFindingFormsById() {
        $frame = new MockSimplePage();
        $form = new MockSimpleForm();
        $frame->returns('getFormById', $form, array('a'));

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame(new MockSimplePage(), 'A');
        $frameset->addFrame($frame, 'B');
        $this->assertSame($frameset->getFormById('a'), $form);

        $frameset->setFrameFocus('A');
        $this->assertNull($frameset->getFormById('a'));

        $frameset->setFrameFocus('B');
        $this->assertSame($frameset->getFormById('a'), $form);
    }

    function testFindingFormsBySubmit() {
        $frame = new MockSimplePage();
        $form = new MockSimpleForm();
        $frame->returns(
                'getFormBySubmit',
                $form,
                array(new SimpleByLabel('a')));

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame(new MockSimplePage(), 'A');
        $frameset->addFrame($frame, 'B');
        $this->assertSame($frameset->getFormBySubmit(new SimpleByLabel('a')), $form);

        $frameset->setFrameFocus('A');
        $this->assertNull($frameset->getFormBySubmit(new SimpleByLabel('a')));

        $frameset->setFrameFocus('B');
        $this->assertSame($frameset->getFormBySubmit(new SimpleByLabel('a')), $form);
    }

    function testFindingFormsByImage() {
        $frame = new MockSimplePage();
        $form = new MockSimpleForm();
        $frame->returns(
                'getFormByImage',
                $form,
                array(new SimpleByLabel('a')));

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame(new MockSimplePage(), 'A');
        $frameset->addFrame($frame, 'B');
        $this->assertSame($frameset->getFormByImage(new SimpleByLabel('a')), $form);

        $frameset->setFrameFocus('A');
        $this->assertNull($frameset->getFormByImage(new SimpleByLabel('a')));

        $frameset->setFrameFocus('B');
        $this->assertSame($frameset->getFormByImage(new SimpleByLabel('a')), $form);
    }

    function testSettingAllFrameFieldsWhenNoFrameFocus() {
        $frame1 = new MockSimplePage();
        $frame1->expectOnce('setField', array(new SimpleById(22), 'A'));

        $frame2 = new MockSimplePage();
        $frame2->expectOnce('setField', array(new SimpleById(22), 'A'));

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame1, 'A');
        $frameset->addFrame($frame2, 'B');
        $frameset->setField(new SimpleById(22), 'A');
    }

    function testOnlySettingFieldFromFocusedFrame() {
        $frame1 = new MockSimplePage();
        $frame1->expectOnce('setField', array(new SimpleByLabelOrName('a'), 'A'));

        $frame2 = new MockSimplePage();
        $frame2->expectNever('setField');

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame1, 'A');
        $frameset->addFrame($frame2, 'B');
        $frameset->setFrameFocus('A');
        $frameset->setField(new SimpleByLabelOrName('a'), 'A');
    }

    function testOnlyGettingFieldFromFocusedFrame() {
        $frame1 = new MockSimplePage();
        $frame1->setReturnValue('getField', 'f', array(new SimpleByName('a')));

        $frame2 = new MockSimplePage();
        $frame2->expectNever('getField');

        $frameset = new SimpleFrameset(new MockSimplePage());
        $frameset->addFrame($frame1, 'A');
        $frameset->addFrame($frame2, 'B');
        $frameset->setFrameFocus('A');
        $this->assertIdentical($frameset->getField(new SimpleByName('a')), 'f');
    }
}
?>