summaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
blob: f67560c454adba9256d601c174aa00c0719811dc (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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
package org.zwobble.mammoth.tests.docx;

import com.natpryce.makeiteasy.Maker;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.zwobble.mammoth.internal.documents.*;
import org.zwobble.mammoth.internal.docx.*;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.xml.XmlElement;
import org.zwobble.mammoth.internal.xml.XmlNode;
import org.zwobble.mammoth.internal.xml.XmlNodes;
import org.zwobble.mammoth.tests.DeepReflectionMatcher;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import static com.natpryce.makeiteasy.MakeItEasy.*;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.zwobble.mammoth.internal.documents.NoteReference.endnoteReference;
import static org.zwobble.mammoth.internal.documents.NoteReference.footnoteReference;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.util.Maps.map;
import static org.zwobble.mammoth.internal.util.Streams.toByteArray;
import static org.zwobble.mammoth.internal.xml.XmlNodes.element;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
import static org.zwobble.mammoth.tests.ResultMatchers.hasWarnings;
import static org.zwobble.mammoth.tests.ResultMatchers.isInternalResult;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*;
import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.*;
import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.NUMBERING;
import static org.zwobble.mammoth.tests.docx.OfficeXmlBuilders.*;

public class BodyXmlTests {

    @Test
    public void textFromTextElementIsRead() {
        XmlElement element = textXml("Hello!");
        assertThat(readSuccess(a(bodyReader), element), isTextElement("Hello!"));
    }

    @Test
    public void canReadTextWithinRun() {
        XmlElement element = runXml(list(textXml("Hello!")));
        assertThat(
            readSuccess(a(bodyReader), element),
            isRun(run(text("Hello!"))));
    }

    @Test
    public void canReadTextWithinParagraph() {
        XmlElement element = paragraphXml(list(runXml(list(textXml("Hello!")))));
        assertThat(
            readSuccess(a(bodyReader), element),
            isParagraph(make(a(PARAGRAPH, with(CHILDREN, list(run(text("Hello!"))))))));
    }

    @Test
    public void paragraphHasNoStyleIfItHasNoProperties() {
        XmlElement element = paragraphXml();
        assertThat(
            readSuccess(a(bodyReader), element),
            hasStyle(Optional.empty()));
    }

    @Test
    public void whenParagraphHasStyleIdInStylesThenStyleNameIsReadFromStyles() {
        XmlElement element = paragraphXml(list(
            element("w:pPr", list(
                element("w:pStyle", map("w:val", "Heading1"))))));

        Style style = new Style("Heading1", Optional.of("Heading 1"));
        Styles styles = new Styles(
            map("Heading1", style),
            map());
        assertThat(
            readSuccess(a(bodyReader, with(STYLES, styles)), element),
            hasStyle(Optional.of(style)));
    }

    @Test
    public void warningIsEmittedWhenParagraphStyleCannotBeFound() {
        XmlElement element = paragraphXml(list(
            element("w:pPr", list(
                element("w:pStyle", map("w:val", "Heading1"))))));
        assertThat(
            read(a(bodyReader), element),
            isInternalResult(
                hasStyle(Optional.of(new Style("Heading1", Optional.empty()))),
                list("Paragraph style with ID Heading1 was referenced but not defined in the document")));
    }

    @Test
    public void paragraphHasNoNumberingIfItHasNoNumberingProperties() {
        XmlElement element = paragraphXml();
        assertThat(
            readSuccess(a(bodyReader), element),
            hasNumbering(Optional.empty()));
    }

    @Test
    public void paragraphHasNumberingPropertiesFromParagraphPropertiesIfPresent() {
        XmlElement element = paragraphXml(list(
            element("w:pPr", list(
                element("w:numPr", map(), list(
                    element("w:ilvl", map("w:val", "1")),
                    element("w:numId", map("w:val", "42"))))))));

        Numbering numbering = new Numbering(map("42", map("1", NumberingLevel.ordered("1"))));

        assertThat(
            readSuccess(a(bodyReader, with(NUMBERING, numbering)), element),
            hasNumbering(NumberingLevel.ordered("1")));
    }

    @Test
    public void numberingPropertiesAreIgnoredIfLevelIsMissing() {
        // TODO: emit warning
        XmlElement element = paragraphXml(list(
            element("w:pPr", list(
                element("w:numPr", map(), list(
                    element("w:numId", map("w:val", "42"))))))));

        Numbering numbering = new Numbering(map("42", map("1", NumberingLevel.ordered("1"))));

        assertThat(
            readSuccess(a(bodyReader, with(NUMBERING, numbering)), element),
            hasNumbering(Optional.empty()));
    }

    @Test
    public void numberingPropertiesAreIgnoredIfNumIdIsMissing() {
        // TODO: emit warning
        XmlElement element = paragraphXml(list(
            element("w:pPr", list(
                element("w:numPr", map(), list(
                    element("w:ilvl", map("w:val", "1"))))))));

        Numbering numbering = new Numbering(map("42", map("1", NumberingLevel.ordered("1"))));

        assertThat(
            readSuccess(a(bodyReader, with(NUMBERING, numbering)), element),
            hasNumbering(Optional.empty()));
    }

    @Test
    public void runHasNoStyleIfItHasNoProperties() {
        XmlElement element = runXml(list());
        assertThat(
            readSuccess(a(bodyReader), element),
            hasStyle(Optional.empty()));
    }

    @Test
    public void whenRunHasStyleIdInStylesThenStyleNameIsReadFromStyles() {
        XmlElement element = runXml(list(
            element("w:rPr", list(
                element("w:rStyle", map("w:val", "Heading1Char"))))));

        Style style = new Style("Heading1Char", Optional.of("Heading 1 Char"));
        Styles styles = new Styles(
            map(),
            map("Heading1Char", style));
        assertThat(
            readSuccess(a(bodyReader, with(STYLES, styles)), element),
            hasStyle(Optional.of(style)));
    }

    @Test
    public void warningIsEmittedWhenRunStyleCannotBeFound() {
        XmlElement element = runXml(list(
            element("w:rPr", list(
                element("w:rStyle", map("w:val", "Heading1Char"))))));

        assertThat(
            read(a(bodyReader), element),
            isInternalResult(
                hasStyle(Optional.of(new Style("Heading1Char", Optional.empty()))),
                list("Run style with ID Heading1Char was referenced but not defined in the document")));
    }

    @Test
    public void runIsNotBoldIfBoldElementIsNotPresent() {
        XmlElement element = runXmlWithProperties();

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("bold", equalTo(false)));
    }

    @Test
    public void runIsBoldIfBoldElementIsPresent() {
        XmlElement element = runXmlWithProperties(element("w:b"));

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("bold", equalTo(true)));
    }

    @Test
    public void runIsNotItalicIfItalicElementIsNotPresent() {
        XmlElement element = runXmlWithProperties();

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("italic", equalTo(false)));
    }

    @Test
    public void runIsItalicIfItalicElementIsPresent() {
        XmlElement element = runXmlWithProperties(element("w:i"));

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("italic", equalTo(true)));
    }

    @Test
    public void runIsNotUnderlinedIfUnderlineElementIsNotPresent() {
        XmlElement element = runXmlWithProperties();

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("underline", equalTo(false)));
    }

    @Test
    public void runIsUnderlinedIfUnderlineElementIsPresent() {
        XmlElement element = runXmlWithProperties(element("w:u"));

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("underline", equalTo(true)));
    }

    @Test
    public void runIsNotStruckthroughIfStrikethroughElementIsNotPresent() {
        XmlElement element = runXmlWithProperties();

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("strikethrough", equalTo(false)));
    }

    @Test
    public void runIsStruckthroughIfStrikethroughElementIsPresent() {
        XmlElement element = runXmlWithProperties(element("w:strike"));

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("strikethrough", equalTo(true)));
    }

    @Test
    public void runHasBaselineVerticalAlignmentIfVerticalAlignmentElementIsNotPresent() {
        XmlElement element = runXmlWithProperties();

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("verticalAlignment", equalTo(VerticalAlignment.BASELINE)));
    }

    @Test
    public void runIsSuperscriptIfVerticalAlignmentPropertyIsSetToSuperscript() {
        XmlElement element = runXmlWithProperties(
            element("w:vertAlign", map("w:val", "superscript")));

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("verticalAlignment", equalTo(VerticalAlignment.SUPERSCRIPT)));
    }

    @Test
    public void runIsSubscriptIfVerticalAlignmentPropertyIsSetToSubscript() {
        XmlElement element = runXmlWithProperties(
            element("w:vertAlign", map("w:val", "subscript")));

        assertThat(
            readSuccess(a(bodyReader), element),
            hasProperty("verticalAlignment", equalTo(VerticalAlignment.SUBSCRIPT)));
    }

    @Test
    public void canReadTabElement() {
        XmlElement element = element("w:tab");

        assertThat(
            readSuccess(a(bodyReader), element),
            equalTo(Tab.TAB));
    }

    @Test
    public void brIsReadAsLineBreak() {
        XmlElement element = element("w:br");

        assertThat(
            readSuccess(a(bodyReader), element),
            equalTo(LineBreak.LINE_BREAK));
    }

    @Test
    public void warningOnBreaksThatArentLineBreaks() {
        XmlElement element = element("w:br", map("w:type", "page"));

        assertThat(
            readAll(a(bodyReader), element),
            isInternalResult(equalTo(list()), list("Unsupported break type: page")));
    }

    @Test
    public void canReadTableElements() {
        XmlElement element = element("w:tbl", list(
            element("w:tr", list(
                element("w:tc", list(
                    element("w:p")))))));

        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(new Table(list(
                new TableRow(list(
                    new TableCell(1, 1, list(
                        make(a(PARAGRAPH))
                    ))
                ))
            )))
        );
    }

    @Test
    public void gridspanIsReadAsColspanForTableCell() {
        XmlElement element = element("w:tbl", list(
            element("w:tr", list(
                element("w:tc", list(
                    element("w:tcPr", list(
                        element("w:gridSpan", map("w:val", "2"))
                    )),
                    element("w:p")))))));

        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(new Table(list(
                new TableRow(list(
                    new TableCell(1, 2, list(
                        make(a(PARAGRAPH))
                    ))
                ))
            )))
        );
    }

    @Test
    public void vmergeIsReadAsRowspanForTableCell() {
        XmlElement element = element("w:tbl", list(
            wTr(wTc()),
            wTr(wTc(wTcPr(wVmerge("restart")))),
            wTr(wTc(wTcPr(wVmerge("continue")))),
            wTr(wTc(wTcPr(wVmerge("continue")))),
            wTr(wTc())
        ));

        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(new Table(list(
                new TableRow(list(new TableCell(1, 1, list()))),
                new TableRow(list(new TableCell(3, 1, list()))),
                new TableRow(list()),
                new TableRow(list()),
                new TableRow(list(new TableCell(1, 1, list())))
            )))
        );
    }

    @Test
    public void vmergeWithoutValIsTreatedAsContinue() {
        XmlElement element = element("w:tbl", list(
            wTr(wTc(wTcPr(wVmerge("restart")))),
            wTr(wTc(wTcPr(element("w:vMerge"))))
        ));

        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(new Table(list(
                new TableRow(list(new TableCell(2, 1, list()))),
                new TableRow(list())
            )))
        );
    }

    @Test
    public void vmergeAccountsForCellsSpanningColumns() {
        XmlElement element = element("w:tbl", list(
            wTr(wTc(), wTc(), wTc(wTcPr(wVmerge("restart")))),
            wTr(wTc(wTcPr(wGridspan("2"))), wTc(wTcPr(wVmerge("continue")))),
            wTr(wTc(), wTc(), wTc(wTcPr(wVmerge("continue")))),
            wTr(wTc(), wTc(), wTc())
        ));

        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(new Table(list(
                new TableRow(list(new TableCell(1, 1, list()), new TableCell(1, 1, list()), new TableCell(3, 1, list()))),
                new TableRow(list(new TableCell(1, 2, list()))),
                new TableRow(list(new TableCell(1, 1, list()), new TableCell(1, 1, list()))),
                new TableRow(list(new TableCell(1, 1, list()), new TableCell(1, 1, list()), new TableCell(1, 1, list())))
            )))
        );
    }

    @Test
    public void noVerticalCellMergingIfMergedCellsDoNotLineUp() {
        XmlElement element = element("w:tbl", list(
            wTr(wTc(wTcPr(wGridspan("2"))), wTc(wTcPr(wVmerge("restart")))),
            wTr(wTc(), wTc(wTcPr(wVmerge("continue"))))
        ));

        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(new Table(list(
                new TableRow(list(new TableCell(1, 2, list()), new TableCell(1, 1, list()))),
                new TableRow(list(new TableCell(1, 1, list()), new TableCell(1, 1, list())))
            )))
        );
    }

    @Test
    public void warningIfNonRowInTable() {
        XmlElement element = element("w:tbl", list(
            element("w:p")
        ));

        assertThat(
            read(a(bodyReader), element),
            isInternalResult(
                deepEquals(new Table(list(make(a(PARAGRAPH))))),
                list("unexpected non-row element in table, cell merging may be incorrect")
            )
        );
    }

    @Test
    public void warningIfNonCellInTableRow() {
        XmlElement element = element("w:tbl", list(
            wTr(element("w:p"))
        ));

        assertThat(
            read(a(bodyReader), element),
            isInternalResult(
                deepEquals(new Table(list(new TableRow(list(make(a(PARAGRAPH))))))),
                list("unexpected non-cell element in table row, cell merging may be incorrect")
            )
        );
    }

    @Test
    public void hyperlinkIsReadIfItHasARelationshipId() {
        Relationships relationships = new Relationships(
            map("r42", new Relationship("http://example.com")));
        XmlElement element = element("w:hyperlink", map("r:id", "r42"), list(runXml(list())));
        assertThat(
            readSuccess(a(bodyReader, with(RELATIONSHIPS, relationships)), element),
            deepEquals(Hyperlink.href("http://example.com", list(make(a(RUN))))));
    }

    @Test
    public void hyperlinkIsReadIfItHasAnAnchorAttribute() {
        XmlElement element = element("w:hyperlink", map("w:anchor", "start"), list(runXml(list())));
        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(Hyperlink.anchor("start", list(make(a(RUN))))));
    }

    @Test
    public void hyperlinkIsIgnoredIfItDoesNotHaveARelationshipIdNorAnchor() {
        XmlElement element = element("w:hyperlink", list(runXml(list())));
        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(make(a(RUN))));
    }

    @Test
    public void goBackBookmarkIsIgnored() {
        XmlElement element = element("w:bookmarkStart", map("w:name", "_GoBack"));
        assertThat(
            readAll(a(bodyReader), element),
            isInternalResult(equalTo(list()), list()));
    }

    @Test
    public void bookmarkStartIsReadIfNameIsNotGoBack() {
        XmlElement element = element("w:bookmarkStart", map("w:name", "start"));
        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(new Bookmark("start")));
    }

    @Test
    public void footnoteReferenceHasIdRead() {
        XmlElement element = element("w:footnoteReference", map("w:id", "4"));
        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(footnoteReference("4")));
    }

    @Test
    public void endnoteReferenceHasIdRead() {
        XmlElement element = element("w:endnoteReference", map("w:id", "4"));
        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(endnoteReference("4")));
    }

    @Test
    public void textBoxesHaveContentAppendedAfterContainingParagraph() {
        XmlElement textBox = element("w:pict", list(
            element("v:shape", list(
                element("v:textbox", list(
                    element("w:txbxContent", list(
                        paragraphXml(list(
                            runXml(list(textXml("[textbox-content]")))))))))))));
        XmlElement paragraph = paragraphXml(list(
            runXml(list(textXml("[paragragh start]"))),
            runXml(list(textBox, textXml("[paragragh end]")))));

        List<DocumentElement> expected = list(
            make(a(PARAGRAPH, with(CHILDREN, list(
                make(a(RUN, with(CHILDREN, list(
                    new Text("[paragragh start]"))))),
                make(a(RUN, with(CHILDREN, list(
                    new Text("[paragragh end]"))))))))),
            make(a(PARAGRAPH, with(CHILDREN, list(
                make(a(RUN, with(CHILDREN, list(
                    new Text("[textbox-content]"))))))))));

        assertThat(
            readAll(a(bodyReader), paragraph),
            isInternalResult(deepEquals(expected), list()));
    }

    @Test
    public void canReadImagedataElementsWithIdAttribute() throws IOException {
        assertCanReadEmbeddedImage(image ->
            element("v:imagedata", map("r:id", image.relationshipId, "o:title", image.altText)));
    }

    @Test
    public void canReadInlinePictures() throws IOException {
        assertCanReadEmbeddedImage(image ->
            inlineImageXml(embeddedBlipXml(image.relationshipId), image.altText));
    }

    @Test
    public void canReadAnchoredPictures() throws IOException {
        assertCanReadEmbeddedImage(image ->
            anchoredImageXml(embeddedBlipXml(image.relationshipId), image.altText));
    }

    private void assertCanReadEmbeddedImage(Function<EmbeddedImage, XmlElement> generateXml) throws IOException {
        XmlElement element = generateXml.apply(new EmbeddedImage("rId5", "It's a hat"));
        Relationships relationships = new Relationships(map(
            "rId5", new Relationship("media/hat.png")));
        String imageBytes = "Not an image at all!";
        DocxFile file = InMemoryDocxFile.fromStrings(map("word/media/hat.png", imageBytes));

        Image image = (Image) readSuccess(
            a(bodyReader, with(RELATIONSHIPS, relationships), with(DOCX_FILE, file)),
            element);
        assertThat(image, allOf(
            hasProperty("altText", deepEquals(Optional.of("It's a hat"))),
            hasProperty("contentType", deepEquals(Optional.of("image/png")))));
        assertThat(
            toString(image.open()),
            equalTo(imageBytes));
    }

    private static String toString(InputStream stream) throws IOException {
        return new String(toByteArray(stream), StandardCharsets.UTF_8);
    }

    private class EmbeddedImage {
        private final String relationshipId;
        private final String altText;

        public EmbeddedImage(String relationshipId, String altText) {
            this.relationshipId = relationshipId;
            this.altText = altText;
        }
    }

    @Test
    public void warningIfImageTypeIsUnsupportedByWebBrowsers() {
        XmlElement element = inlineImageXml(embeddedBlipXml("rId5"), "");
        Relationships relationships = new Relationships(map(
            "rId5", new Relationship("media/hat.emf")));
        DocxFile file = InMemoryDocxFile.fromStrings(map("word/media/hat.emf", "Not an image at all!"));
        ContentTypes contentTypes = new ContentTypes(map("emf", "image/x-emf"), map());

        InternalResult<?> result = read(
            a(bodyReader,
                with(RELATIONSHIPS, relationships),
                with(DOCX_FILE, file),
                with(CONTENT_TYPES, contentTypes)),
            element);

        assertThat(
            result,
            hasWarnings(list("Image of type image/x-emf is unlikely to display in web browsers")));
    }

    @Test
    public void canReadLinkedPictures() throws IOException {
        XmlElement element = inlineImageXml(linkedBlipXml("rId5"), "");
        Relationships relationships = new Relationships(map(
            "rId5", new Relationship("file:///media/hat.png")));
        String imageBytes = "Not an image at all!";

        Image image = (Image) readSuccess(
            a(bodyReader,
                with(RELATIONSHIPS, relationships),
                with(FILE_READER, new InMemoryFileReader(map("file:///media/hat.png", imageBytes)))),
            element);

        assertThat(
            toString(image.open()),
            equalTo(imageBytes));
    }

    private XmlElement inlineImageXml(XmlElement blip, String description) {
        return element("w:drawing", list(
            element("wp:inline", imageXml(blip, description))));
    }

    private XmlElement anchoredImageXml(XmlElement blip, String description) {
        return element("w:drawing", list(
            element("wp:anchor", imageXml(blip, description))));
    }

    private List<XmlNode> imageXml(XmlElement blip, String description) {
        return list(
            element("wp:docPr", map("descr", description)),
            element("a:graphic", list(
                element("a:graphicData", list(
                    element("pic:pic", list(
                        element("pic:blipFill", list(blip)))))))));
    }

    private XmlElement embeddedBlipXml(String relationshipId) {
        return blipXml(map("r:embed", relationshipId));
    }

    private XmlElement linkedBlipXml(String relationshipId) {
        return blipXml(map("r:link", relationshipId));
    }

    private XmlElement blipXml(Map<String, String> attributes) {
        return element("a:blip", attributes);
    }

    @Test
    public void appropriateElementsHaveTheirChildrenReadNormally() {
        assertChildrenAreReadNormally("w:ins");
        assertChildrenAreReadNormally("w:smartTag");
        assertChildrenAreReadNormally("w:drawing");
        assertChildrenAreReadNormally("v:roundrect");
        assertChildrenAreReadNormally("v:shape");
        assertChildrenAreReadNormally("v:textbox");
        assertChildrenAreReadNormally("w:txbxContent");
    }

    private void assertChildrenAreReadNormally(String name) {
        XmlElement element = element(name, list(paragraphXml()));

        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(make(a(PARAGRAPH))));
    }

    @Test
    public void ignoredElementsAreIgnoredWithoutWarning() {
        assertIsIgnored("office-word:wrap");
        assertIsIgnored("v:shadow");
        assertIsIgnored("v:shapetype");
        assertIsIgnored("w:bookmarkEnd");
        assertIsIgnored("w:sectPr");
        assertIsIgnored("w:proofErr");
        assertIsIgnored("w:lastRenderedPageBreak");
        assertIsIgnored("w:commentRangeStart");
        assertIsIgnored("w:commentRangeEnd");
        assertIsIgnored("w:commentReference");
        assertIsIgnored("w:del");
        assertIsIgnored("w:footnoteRef");
        assertIsIgnored("w:endnoteRef");
        assertIsIgnored("w:pPr");
        assertIsIgnored("w:rPr");
        assertIsIgnored("w:tblPr");
        assertIsIgnored("w:tblGrid");
        assertIsIgnored("w:tcPr");
    }

    private void assertIsIgnored(String name) {
        XmlElement element = element(name, list(paragraphXml()));

        assertThat(
            readAll(a(bodyReader), element),
            isInternalResult(equalTo(list()), list()));
    }

    @Test
    public void unrecognisedElementsAreIgnoredWithWarning() {
        XmlElement element = element("w:huh");
        assertThat(
            readAll(a(bodyReader), element),
            isInternalResult(equalTo(list()), list("An unrecognised element was ignored: w:huh")));
    }

    @Test
    public void textNodesAreIgnoredWhenReadingChildren() {
        XmlElement element = runXml(list(XmlNodes.text("[text]")));
        assertThat(
            readSuccess(a(bodyReader), element),
            deepEquals(make(a(RUN))));
    }

    private static DocumentElement readSuccess(Maker<BodyXmlReader> reader, XmlElement element) {
        InternalResult<DocumentElement> result = read(reader, element);
        assertThat(result.getWarnings(), emptyIterable());
        return result.getValue();
    }

    private static InternalResult<DocumentElement> read(Maker<BodyXmlReader> reader, XmlElement element) {
        InternalResult<List<DocumentElement>> result = readAll(reader, element);
        assertThat(result.getValue(), Matchers.hasSize(1));
        return result.map(elements -> elements.get(0));
    }

    private static InternalResult<List<DocumentElement>> readAll(Maker<BodyXmlReader> reader, XmlElement element) {
        return reader.make().readElement(element).toResult();
    }

    private XmlElement paragraphXml() {
        return paragraphXml(list());
    }

    private XmlElement paragraphXml(List<XmlNode> children) {
        return element("w:p", children);
    }

    private XmlElement runXml(List<XmlNode> children) {
        return element("w:r", children);
    }

    private XmlElement runXmlWithProperties(XmlNode... children) {
        return element("w:r", list(element("w:rPr", asList(children))));
    }

    private XmlElement textXml(String value) {
        return element("w:t", list(XmlNodes.text(value)));
    }

    private Matcher<DocumentElement> isParagraph(Paragraph expected) {
        return new DeepReflectionMatcher<>(expected);
    }

    private Matcher<DocumentElement> isRun(Run expected) {
        return new DeepReflectionMatcher<>(expected);
    }

    private Matcher<DocumentElement> isTextElement(String value) {
        return new DeepReflectionMatcher<>(new Text(value));
    }

    private Matcher<? super DocumentElement> hasStyle(Optional<Style> expected) {
        return hasProperty("style", deepEquals(expected));
    }

    private Matcher<? super DocumentElement> hasNumbering(NumberingLevel expected) {
        return hasNumbering(Optional.of(expected));
    }

    private Matcher<? super DocumentElement> hasNumbering(Optional<NumberingLevel> expected) {
        return hasProperty("numbering", deepEquals(expected));
    }

    private Run run(DocumentElement... children) {
        return make(a(RUN, with(CHILDREN, asList(children))));
    }

    private Text text(String value) {
        return new Text(value);
    }
}