-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerpointUnitTests.cs
More file actions
181 lines (163 loc) · 6.18 KB
/
Copy pathPowerpointUnitTests.cs
File metadata and controls
181 lines (163 loc) · 6.18 KB
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
using DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;
[TestFixture]
public class PowerpointUnitTests
{
[Test]
public void GetPowerpointProperties_AllEmpty_ReturnsNull()
{
using var doc = CreateEmptyDoc();
Assert.That(VerifyOpenXml.GetPowerpointProperties(doc), Is.Null);
}
[Test]
public void GetPowerpointProperties_Populated()
{
using var doc = CreateEmptyDoc();
var props = doc.PackageProperties;
props.Title = "T";
props.Subject = "S";
props.Creator = "C";
props.Keywords = "K";
props.Description = "D";
props.Category = "Cat";
props.LastModifiedBy = "L";
props.ContentStatus = "Draft";
props.Revision = "1";
var result = VerifyOpenXml.GetPowerpointProperties(doc)!;
Assert.That(result["Title"], Is.EqualTo("T"));
Assert.That(result["Revision"], Is.EqualTo("1"));
// Creator and LastModifiedBy are intentionally omitted (DeterministicIoPackaging strips them).
Assert.That(result.ContainsKey("Creator"), Is.False);
Assert.That(result.ContainsKey("LastModifiedBy"), Is.False);
}
[Test]
public void GetPowerpointInfo_NoSlides()
{
using var doc = CreateEmptyDoc();
var info = VerifyOpenXml.GetPowerpointInfo(doc);
Assert.That(info.SlideCount, Is.EqualTo(0));
Assert.That(info.Text, Is.Null);
}
[Test]
public void GetPowerpointInfo_WithSlidesAndText()
{
using var doc = CreateEmptyDoc();
var presPart = doc.PresentationPart!;
AddSlide(presPart, "First");
AddSlide(presPart, "Second");
var info = VerifyOpenXml.GetPowerpointInfo(doc);
Assert.That(info.SlideCount, Is.EqualTo(2));
Assert.That(info.Text, Does.Contain("First"));
Assert.That(info.Text, Does.Contain("Second"));
Assert.That(info.Text, Does.Contain("---"));
}
[Test]
public void GetPowerpointInfo_SlideWithNoText_TextIsNull()
{
using var doc = CreateEmptyDoc();
var presPart = doc.PresentationPart!;
AddEmptySlide(presPart);
var info = VerifyOpenXml.GetPowerpointInfo(doc);
Assert.That(info.SlideCount, Is.EqualTo(1));
Assert.That(info.Text, Is.Null);
}
[Test]
public void AppendSlideText_EmptySlide_ReturnsFalse()
{
using var doc = CreateEmptyDoc();
var slidePart = doc.PresentationPart!.AddNewPart<SlidePart>();
slidePart.Slide = new(
new CommonSlideData(new ShapeTree(
new NonVisualGroupShapeProperties(
new NonVisualDrawingProperties
{
Id = 1,
Name = ""
},
new NonVisualGroupShapeDrawingProperties(),
new ApplicationNonVisualDrawingProperties()),
new GroupShapeProperties(new A.TransformGroup()))));
var builder = new StringBuilder();
Assert.That(VerifyOpenXml.AppendSlideText(builder, slidePart), Is.False);
Assert.That(builder.Length, Is.Zero);
}
[Test]
public void AppendSlideText_WithParagraphs()
{
using var doc = CreateEmptyDoc();
var presPart = doc.PresentationPart!;
var slidePart = AddSlide(presPart, "Line1", "Line2");
var builder = new StringBuilder();
Assert.That(VerifyOpenXml.AppendSlideText(builder, slidePart), Is.True);
var text = builder.ToString();
Assert.That(text, Does.Contain("Line1"));
Assert.That(text, Does.Contain("Line2"));
}
[Test]
public void AppendSlideText_ParagraphWithNoText_SkippedFromOutput()
{
using var doc = CreateEmptyDoc();
var presPart = doc.PresentationPart!;
var slidePart = presPart.AddNewPart<SlidePart>();
slidePart.Slide = BuildSlide(
new A.Paragraph(),
new A.Paragraph(new A.Run(new A.RunProperties(), new A.Text("Only"))));
var builder = new StringBuilder();
VerifyOpenXml.AppendSlideText(builder, slidePart);
Assert.That(builder.ToString(), Is.EqualTo("Only"));
}
static PresentationDocument CreateEmptyDoc()
{
var doc = PresentationDocument.Create(new MemoryStream(), PresentationDocumentType.Presentation);
var presPart = doc.AddPresentationPart();
presPart.Presentation = new(new SlideIdList());
return doc;
}
static SlidePart AddSlide(PresentationPart presPart, params string[] lines)
{
var slidePart = presPart.AddNewPart<SlidePart>();
var paragraphs = lines.Select(_ =>
(OpenXmlElement)new A.Paragraph(
new A.Run(new A.RunProperties(), new A.Text(_)))).ToArray();
slidePart.Slide = BuildSlide(paragraphs);
return slidePart;
}
static SlidePart AddEmptySlide(PresentationPart presPart)
{
var slidePart = presPart.AddNewPart<SlidePart>();
slidePart.Slide = BuildSlide();
return slidePart;
}
static Slide BuildSlide(params OpenXmlElement[] paragraphs)
{
var textBody = new TextBody(new A.BodyProperties(), new A.ListStyle());
foreach (var p in paragraphs)
{
textBody.Append(p);
}
var shape = new Shape(
new NonVisualShapeProperties(
new NonVisualDrawingProperties
{
Id = 2,
Name = "Text"
},
new NonVisualShapeDrawingProperties(),
new ApplicationNonVisualDrawingProperties()),
new ShapeProperties(),
textBody);
return new(
new CommonSlideData(
new ShapeTree(
new NonVisualGroupShapeProperties(
new NonVisualDrawingProperties
{
Id = 1,
Name = ""
},
new NonVisualGroupShapeDrawingProperties(),
new ApplicationNonVisualDrawingProperties()),
new GroupShapeProperties(new A.TransformGroup()),
shape)));
}
}