style_converters_test.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * ezcDocumentOdtFormattingPropertiesTest.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Document
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Test suite for class.
  29. *
  30. * @package Document
  31. * @subpackage Tests
  32. */
  33. class ezcDocumentOdtPcssConvertersTest extends ezcTestCase
  34. {
  35. protected $domElement;
  36. public static function suite()
  37. {
  38. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  39. }
  40. protected function setUp()
  41. {
  42. $domDocument = new DOMDocument();
  43. $this->domElement = $domDocument->appendChild(
  44. $domDocument->createElement( 'parent' )
  45. );
  46. }
  47. protected function assertAttributesCorrect( array $expectedAttributes )
  48. {
  49. $this->assertEquals(
  50. count( $expectedAttributes ),
  51. $this->domElement->attributes->length,
  52. 'Inconsistent number of text property element attributes.'
  53. );
  54. foreach ( $expectedAttributes as $attrDef )
  55. {
  56. $this->assertTrue(
  57. $this->domElement->hasAttributeNS(
  58. $attrDef[0],
  59. $attrDef[1]
  60. ),
  61. "Missing attribute '{$attrDef[0]}:{$attrDef[1]}'."
  62. );
  63. $this->assertEquals(
  64. $attrDef[2],
  65. ( $actAttrVal = $this->domElement->getAttributeNS(
  66. $attrDef[0],
  67. $attrDef[1]
  68. ) ),
  69. "Attribute '{$attrDef[0]}:{$attrDef[1]}' has incorrect value '$actAttrVal', expected '{$attrDef[2]}'."
  70. );
  71. }
  72. }
  73. /**
  74. * @dataProvider getTextDecorationTestSets
  75. */
  76. public function testConvertTextDecoration( $styleValue, $expectedAttributes )
  77. {
  78. $converter = new ezcDocumentOdtPcssTextDecorationConverter();
  79. $converter->convert( $this->domElement, 'text-decoration', $styleValue );
  80. $this->assertAttributesCorrect(
  81. $expectedAttributes
  82. );
  83. }
  84. /**
  85. * Test sets for the 'text-decoration' style attribute.
  86. */
  87. public static function getTextDecorationTestSets()
  88. {
  89. return array(
  90. 'line-through' => array(
  91. // style
  92. new ezcDocumentPcssStyleListValue( array( 'line-through' ) ),
  93. // expected attributes
  94. array(
  95. // NS, attribute name, value
  96. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-line-through-type', 'single' ),
  97. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-line-through-style', 'solid' ),
  98. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-line-through-width', 'auto' ),
  99. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-line-through-color', 'font-color' ),
  100. )
  101. ),
  102. 'underline' => array(
  103. // style
  104. new ezcDocumentPcssStyleListValue( array( 'underline' ) ),
  105. // expected attributes
  106. array(
  107. // NS, attribute name, value
  108. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-type', 'single' ),
  109. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-style', 'solid' ),
  110. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-width', 'auto' ),
  111. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-color', 'font-color' ),
  112. )
  113. ),
  114. 'overline' => array(
  115. // style
  116. new ezcDocumentPcssStyleListValue( array( 'overline' ) ),
  117. // expected attributes
  118. array(
  119. )
  120. ),
  121. 'blink' => array(
  122. // style
  123. new ezcDocumentPcssStyleListValue( array( 'blink' ) ),
  124. // expected attributes
  125. array(
  126. // NS, attribute name, value
  127. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-blinking', 'true' ),
  128. )
  129. ),
  130. 'multiple' => array(
  131. // style
  132. new ezcDocumentPcssStyleListValue( array( 'blink', 'underline' ) ),
  133. // expected attributes
  134. array(
  135. // NS, attribute name, value
  136. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-blinking', 'true' ),
  137. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-type', 'single' ),
  138. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-style', 'solid' ),
  139. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-width', 'auto' ),
  140. array( ezcDocumentOdt::NS_ODT_STYLE, 'text-underline-color', 'font-color' ),
  141. )
  142. ),
  143. );
  144. }
  145. /**
  146. * @dataProvider getColorTestSets
  147. */
  148. public function testConvertColor( $styleValue, $expectedAttributes )
  149. {
  150. $converter = new ezcDocumentOdtPcssColorConverter();
  151. $converter->convert( $this->domElement, 'color', $styleValue );
  152. $this->assertAttributesCorrect(
  153. $expectedAttributes
  154. );
  155. }
  156. /**
  157. * Test sets for color style attributes.
  158. */
  159. public static function getColorTestSets()
  160. {
  161. return array(
  162. 'non-transparent' => array(
  163. // style
  164. new ezcDocumentPcssStyleColorValue(
  165. array(
  166. 'red' => 1.0,
  167. 'green' => 1.0,
  168. 'blue' => 1.0,
  169. 'alpha' => 0.4,
  170. )
  171. ),
  172. // expected attributes
  173. array(
  174. // NS, attribute name, value
  175. array( ezcDocumentOdt::NS_ODT_FO, 'color', '#ffffff' ),
  176. )
  177. ),
  178. 'transparent' => array(
  179. // style
  180. new ezcDocumentPcssStyleColorValue(
  181. array(
  182. 'red' => 1.0,
  183. 'green' => 1.0,
  184. 'blue' => 1.0,
  185. 'alpha' => 0.5,
  186. )
  187. ),
  188. // expected attributes
  189. array(
  190. // NS, attribute name, value
  191. array( ezcDocumentOdt::NS_ODT_FO, 'color', 'transparent' ),
  192. )
  193. ),
  194. 'value' => array(
  195. // style
  196. new ezcDocumentPcssStyleColorValue(
  197. array(
  198. 'red' => 0.75294117647059,
  199. 'green' => 1.0,
  200. 'blue' => 0,
  201. 'alpha' => 0.0,
  202. )
  203. ),
  204. // expected attributes
  205. array(
  206. // NS, attribute name, value
  207. array( ezcDocumentOdt::NS_ODT_FO, 'color', '#c0ff00' ),
  208. )
  209. ),
  210. );
  211. }
  212. /**
  213. * @dataProvider getBackgroundColorTestSets
  214. */
  215. public function testConvertBackgroundColor( $styleValue, $expectedAttributes )
  216. {
  217. $converter = new ezcDocumentOdtPcssColorConverter();
  218. $converter->convert( $this->domElement, 'background-color', $styleValue );
  219. $this->assertAttributesCorrect(
  220. $expectedAttributes
  221. );
  222. }
  223. /**
  224. * Test sets for background-color style attributes.
  225. */
  226. public static function getBackgroundColorTestSets()
  227. {
  228. // Re-use color test sets, but with background-color attribute name
  229. $colorTestSets = self::getColorTestSets();
  230. foreach ( $colorTestSets as $setId => $set )
  231. {
  232. foreach( $set[1] as $attrId => $attrDef )
  233. {
  234. $attrDef[1] = 'background-color';
  235. $colorTestSets[$setId][1][$attrId] = $attrDef;
  236. }
  237. }
  238. return $colorTestSets;
  239. }
  240. /**
  241. * @dataProvider getFontSizeTestSets
  242. */
  243. public function testConvertFontSize( $styleValue, $expectedAttributes )
  244. {
  245. $converter = new ezcDocumentOdtPcssFontSizeConverter();
  246. $converter->convert( $this->domElement, 'font-size', $styleValue );
  247. $this->assertAttributesCorrect(
  248. $expectedAttributes
  249. );
  250. }
  251. /**
  252. * Test sets for font style attributes.
  253. */
  254. public static function getFontSizeTestSets()
  255. {
  256. return array(
  257. 'font-size' => array(
  258. // styles
  259. new ezcDocumentPcssStyleMeasureValue( 23 ),
  260. // expected attributes
  261. array(
  262. // NS, attribute name, value
  263. array( ezcDocumentOdt::NS_ODT_FO, 'font-size', '23mm' ),
  264. array( ezcDocumentOdt::NS_ODT_STYLE, 'font-size-asian', '23mm' ),
  265. array( ezcDocumentOdt::NS_ODT_STYLE, 'font-size-complex', '23mm' ),
  266. )
  267. ),
  268. );
  269. }
  270. /**
  271. * @dataProvider getTextFontNameTestSets
  272. */
  273. public function testConvertMiscFontProperty( $styleValue, $expectedAttributes )
  274. {
  275. $converter = new ezcDocumentOdtPcssFontNameConverter();
  276. $converter->convert( $this->domElement, 'font-name', $styleValue );
  277. $this->assertAttributesCorrect(
  278. $expectedAttributes
  279. );
  280. }
  281. public static function getTextFontNameTestSets()
  282. {
  283. return array(
  284. 'font-name' => array(
  285. // styles
  286. new ezcDocumentPcssStyleStringValue( 'DejaVu Sans' ),
  287. // expected attributes
  288. array(
  289. // NS, attribute name, value
  290. array( ezcDocumentOdt::NS_ODT_STYLE, 'font-name', 'DejaVu Sans' ),
  291. array( ezcDocumentOdt::NS_ODT_STYLE, 'font-name-asian', 'DejaVu Sans' ),
  292. array( ezcDocumentOdt::NS_ODT_STYLE, 'font-name-complex', 'DejaVu Sans' ),
  293. )
  294. ),
  295. );
  296. }
  297. /**
  298. * @dataProvider getTextAlignTestSets
  299. */
  300. public function testConvertMiscProperty( $styleValue, $expectedAttributes )
  301. {
  302. $converter = new ezcDocumentOdtDefaultPcssConverter();
  303. $converter->convert( $this->domElement, 'text-align', $styleValue );
  304. $this->assertAttributesCorrect(
  305. $expectedAttributes
  306. );
  307. }
  308. public static function getTextAlignTestSets()
  309. {
  310. return array(
  311. array(
  312. // style
  313. new ezcDocumentPcssStyleStringValue( 'center' ),
  314. // expected attributes
  315. array(
  316. // NS, attribute name, value
  317. array( ezcDocumentOdt::NS_ODT_FO, 'text-align', 'center' ),
  318. )
  319. ),
  320. );
  321. }
  322. /**
  323. * @dataProvider getMarginTestSets
  324. */
  325. public function testConvertMarginProperty( $styleValue, $expectedAttributes )
  326. {
  327. $converter = new ezcDocumentOdtPcssMarginConverter();
  328. $converter->convert( $this->domElement, 'margin', $styleValue );
  329. $this->assertAttributesCorrect(
  330. $expectedAttributes
  331. );
  332. }
  333. /**
  334. * Test sets for the 'margin' style attribute.
  335. */
  336. public static function getMarginTestSets()
  337. {
  338. return array(
  339. 'margin full' => array(
  340. // style
  341. new ezcDocumentPcssStyleMeasureBoxValue(
  342. array(
  343. 'top' => 1,
  344. 'left' => 2,
  345. 'bottom' => 3,
  346. 'right' => 4
  347. )
  348. ),
  349. // expected attributes
  350. array(
  351. // NS, attribute name, value
  352. array( ezcDocumentOdt::NS_ODT_FO, 'margin-top', '1mm' ),
  353. array( ezcDocumentOdt::NS_ODT_FO, 'margin-left', '2mm' ),
  354. array( ezcDocumentOdt::NS_ODT_FO, 'margin-bottom', '3mm' ),
  355. array( ezcDocumentOdt::NS_ODT_FO, 'margin-right', '4mm' ),
  356. )
  357. ),
  358. 'margin missings' => array(
  359. // style
  360. new ezcDocumentPcssStyleMeasureBoxValue(
  361. array(
  362. 'top' => 1,
  363. 'right' => 4
  364. )
  365. ),
  366. // expected attributes
  367. array(
  368. // NS, attribute name, value
  369. array( ezcDocumentOdt::NS_ODT_FO, 'margin-top', '1mm' ),
  370. array( ezcDocumentOdt::NS_ODT_FO, 'margin-right', '4mm' ),
  371. )
  372. ),
  373. 'margin empty' => array(
  374. // style
  375. new ezcDocumentPcssStyleMeasureBoxValue(
  376. array(
  377. 'top' => 1,
  378. 'left' => 0,
  379. 'bottom' => 3,
  380. 'right' => null
  381. )
  382. ),
  383. // expected attributes
  384. array(
  385. // NS, attribute name, value
  386. array( ezcDocumentOdt::NS_ODT_FO, 'margin-top', '1mm' ),
  387. array( ezcDocumentOdt::NS_ODT_FO, 'margin-left', '0mm' ),
  388. array( ezcDocumentOdt::NS_ODT_FO, 'margin-bottom', '3mm' ),
  389. array( ezcDocumentOdt::NS_ODT_FO, 'margin-right', '0mm' ),
  390. )
  391. ),
  392. );
  393. }
  394. /**
  395. * @dataProvider getBorderTestSets
  396. */
  397. public function testConvertBorderProperty( $styleValue, $expectedAttributes )
  398. {
  399. $converter = new ezcDocumentOdtPcssBorderConverter();
  400. $converter->convert( $this->domElement, 'border', $styleValue );
  401. $this->assertAttributesCorrect(
  402. $expectedAttributes
  403. );
  404. }
  405. /**
  406. * Test sets for the 'margin' style attribute.
  407. */
  408. public static function getBorderTestSets()
  409. {
  410. return array(
  411. 'border full' => array(
  412. // style
  413. new ezcDocumentPcssStyleBorderBoxValue(
  414. array(
  415. 'top' => array(
  416. 'width' => 1,
  417. 'style' => 'solid',
  418. 'color' => array(
  419. 'red' => 1,
  420. 'green' => 0,
  421. 'blue' => 0,
  422. 'alpha' => 0
  423. )
  424. ),
  425. 'left' => array(
  426. 'width' => 10,
  427. 'style' => 'solid',
  428. 'color' => array(
  429. 'red' => 0,
  430. 'green' => 1,
  431. 'blue' => 0,
  432. 'alpha' => 0
  433. )
  434. ),
  435. 'bottom' => array(
  436. 'width' => 1,
  437. 'style' => 'solid',
  438. 'color' => array(
  439. 'red' => 0,
  440. 'green' => 0,
  441. 'blue' => 1,
  442. 'alpha' => .8
  443. )
  444. ),
  445. 'right' => array(
  446. 'width' => 1,
  447. 'style' => 'dotted',
  448. 'color' => array(
  449. 'red' => .3,
  450. 'green' => .2,
  451. 'blue' => .4,
  452. 'alpha' => .2
  453. )
  454. ),
  455. )
  456. ),
  457. // expected attributes
  458. array(
  459. // NS, attribute name, value
  460. array( ezcDocumentOdt::NS_ODT_FO, 'border-top', '1mm solid #ff0000' ),
  461. array( ezcDocumentOdt::NS_ODT_FO, 'border-left', '10mm solid #00ff00' ),
  462. array( ezcDocumentOdt::NS_ODT_FO, 'border-bottom', '1mm solid transparent' ),
  463. array( ezcDocumentOdt::NS_ODT_FO, 'border-right', '1mm dotted #4d3366' ),
  464. )
  465. ),
  466. );
  467. }
  468. }
  469. ?>