|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2019 iText Group NV |
| 4 | +Authors: iText Software. |
| 5 | +
|
| 6 | +This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU Affero General Public License version 3 |
| 8 | +as published by the Free Software Foundation with the addition of the |
| 9 | +following permission added to Section 15 as permitted in Section 7(a): |
| 10 | +FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 11 | +ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 12 | +OF THIRD PARTY RIGHTS |
| 13 | +
|
| 14 | +This program is distributed in the hope that it will be useful, but |
| 15 | +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | +or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +See the GNU Affero General Public License for more details. |
| 18 | +You should have received a copy of the GNU Affero General Public License |
| 19 | +along with this program; if not, see http://www.gnu.org/licenses or write to |
| 20 | +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | +Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 22 | +http://itextpdf.com/terms-of-use/ |
| 23 | +
|
| 24 | +The interactive user interfaces in modified source and object code versions |
| 25 | +of this program must display Appropriate Legal Notices, as required under |
| 26 | +Section 5 of the GNU Affero General Public License. |
| 27 | +
|
| 28 | +In accordance with Section 7(b) of the GNU Affero General Public License, |
| 29 | +a covered work must retain the producer line in every PDF that is created |
| 30 | +or manipulated using iText. |
| 31 | +
|
| 32 | +You can be released from the requirements of the license by purchasing |
| 33 | +a commercial license. Buying such a license is mandatory as soon as you |
| 34 | +develop commercial activities involving the iText software without |
| 35 | +disclosing the source code of your own applications. |
| 36 | +These activities include: offering paid services to customers as an ASP, |
| 37 | +serving PDFs on the fly in a web application, shipping iText with a closed |
| 38 | +source product. |
| 39 | +
|
| 40 | +For more information, please contact iText Software Corp. at this |
| 41 | + |
| 42 | +*/ |
| 43 | +using System; |
| 44 | +using System.IO; |
| 45 | +using iText.IO.Util; |
| 46 | +using iText.StyledXmlParser.Css; |
| 47 | +using iText.Test; |
| 48 | + |
| 49 | +namespace iText.StyledXmlParser.Css.Parse { |
| 50 | + public class CssStyleSheetParserTest : ExtendedITextTest { |
| 51 | + private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 52 | + .CurrentContext.TestDirectory) + "/resources/itext/styledxmlparser/css/parse/CssStyleSheetParserTest/"; |
| 53 | + |
| 54 | + [NUnit.Framework.Test] |
| 55 | + public virtual void Test01() { |
| 56 | + String cssFile = sourceFolder + "css01.css"; |
| 57 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 58 | + )); |
| 59 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cssFile), styleSheet.ToString()); |
| 60 | + } |
| 61 | + |
| 62 | + [NUnit.Framework.Test] |
| 63 | + public virtual void Test02() { |
| 64 | + String cssFile = sourceFolder + "css02.css"; |
| 65 | + String cmpFile = sourceFolder + "cmp_css02.css"; |
| 66 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 67 | + )); |
| 68 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 69 | + } |
| 70 | + |
| 71 | + [NUnit.Framework.Test] |
| 72 | + public virtual void Test03() { |
| 73 | + String cssFile = sourceFolder + "css03.css"; |
| 74 | + String cmpFile = sourceFolder + "cmp_css03.css"; |
| 75 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 76 | + )); |
| 77 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 78 | + } |
| 79 | + |
| 80 | + [NUnit.Framework.Test] |
| 81 | + public virtual void Test04() { |
| 82 | + String cssFile = sourceFolder + "css04.css"; |
| 83 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 84 | + )); |
| 85 | + NUnit.Framework.Assert.AreEqual("", styleSheet.ToString()); |
| 86 | + } |
| 87 | + |
| 88 | + [NUnit.Framework.Test] |
| 89 | + public virtual void Test05() { |
| 90 | + String cssFile = sourceFolder + "css05.css"; |
| 91 | + String cmpFile = sourceFolder + "cmp_css05.css"; |
| 92 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 93 | + )); |
| 94 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 95 | + } |
| 96 | + |
| 97 | + [NUnit.Framework.Test] |
| 98 | + public virtual void Test06() { |
| 99 | + String cssFile = sourceFolder + "css06.css"; |
| 100 | + String cmpFile = sourceFolder + "cmp_css06.css"; |
| 101 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 102 | + )); |
| 103 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 104 | + } |
| 105 | + |
| 106 | + [NUnit.Framework.Test] |
| 107 | + public virtual void Test07() { |
| 108 | + String cssFile = sourceFolder + "css07.css"; |
| 109 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 110 | + )); |
| 111 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cssFile), styleSheet.ToString()); |
| 112 | + } |
| 113 | + |
| 114 | + [NUnit.Framework.Test] |
| 115 | + public virtual void Test08() { |
| 116 | + String cssFile = sourceFolder + "css08.css"; |
| 117 | + String cmpFile = sourceFolder + "cmp_css08.css"; |
| 118 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 119 | + )); |
| 120 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 121 | + } |
| 122 | + |
| 123 | + [NUnit.Framework.Test] |
| 124 | + public virtual void Test09() { |
| 125 | + String cssFile = sourceFolder + "css09.css"; |
| 126 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 127 | + )); |
| 128 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cssFile), styleSheet.ToString()); |
| 129 | + } |
| 130 | + |
| 131 | + [NUnit.Framework.Test] |
| 132 | + public virtual void Test10() { |
| 133 | + String cssFile = sourceFolder + "css10.css"; |
| 134 | + String cmpFile = sourceFolder + "cmp_css10.css"; |
| 135 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 136 | + )); |
| 137 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 138 | + } |
| 139 | + |
| 140 | + [NUnit.Framework.Test] |
| 141 | + public virtual void Test11() { |
| 142 | + // TODO in this test declarations of the page at-rule with compound selector are duplicated. |
| 143 | + // See CssPageRule#addBodyCssDeclarations() method for the reason and possible solution if this becomes important. |
| 144 | + String cssFile = sourceFolder + "css11.css"; |
| 145 | + String cmpFile = sourceFolder + "cmp_css11.css"; |
| 146 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 147 | + )); |
| 148 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 149 | + } |
| 150 | + |
| 151 | + [NUnit.Framework.Test] |
| 152 | + public virtual void Test12() { |
| 153 | + String cssFile = sourceFolder + "css12.css"; |
| 154 | + String cmpFile = sourceFolder + "cmp_css12.css"; |
| 155 | + CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(cssFile, FileMode.Open, FileAccess.Read |
| 156 | + )); |
| 157 | + NUnit.Framework.Assert.AreEqual(GetCssFileContents(cmpFile), styleSheet.ToString()); |
| 158 | + } |
| 159 | + |
| 160 | + private String GetCssFileContents(String filePath) { |
| 161 | + byte[] bytes = StreamUtil.InputStreamToArray(new FileStream(filePath, FileMode.Open, FileAccess.Read)); |
| 162 | + String content = iText.IO.Util.JavaUtil.GetStringForBytes(bytes); |
| 163 | + content = content.Trim(); |
| 164 | + content = content.Replace("\r\n", "\n"); |
| 165 | + return content; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments