001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hslf.model;
018:
019: import junit.framework.TestCase;
020:
021: import java.io.*;
022: import java.awt.*;
023:
024: import org.apache.poi.hslf.usermodel.SlideShow;
025: import org.apache.poi.hslf.HSLFSlideShow;
026:
027: /**
028: * Test <code>Fill</code> object.
029: *
030: * @author Yegor Kozlov
031: */
032: public class TestBackground extends TestCase {
033:
034: /**
035: * Default background for slide, shape and slide master.
036: */
037: public void testDefaults() throws Exception {
038: SlideShow ppt = new SlideShow();
039:
040: assertEquals(Fill.FILL_SOLID, ppt.getSlidesMasters()[0]
041: .getBackground().getFill().getFillType());
042:
043: Slide slide = ppt.createSlide();
044: assertTrue(slide.getFollowMasterBackground());
045: assertEquals(Fill.FILL_SOLID, slide.getBackground().getFill()
046: .getFillType());
047:
048: Shape shape = new AutoShape(ShapeTypes.Rectangle);
049: assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
050: }
051:
052: /**
053: * Read fill information from an reference ppt file
054: */
055: public void testReadBackground() throws Exception {
056: SlideShow ppt = new SlideShow(new HSLFSlideShow(System
057: .getProperty("HSLF.testdata.path")
058: + "/backgrounds.ppt"));
059: Fill fill;
060: Shape shape;
061:
062: Slide[] slide = ppt.getSlides();
063:
064: fill = slide[0].getBackground().getFill();
065: assertEquals(Fill.FILL_PICTURE, fill.getFillType());
066: shape = slide[0].getShapes()[0];
067: assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
068:
069: fill = slide[1].getBackground().getFill();
070: assertEquals(Fill.FILL_PATTERN, fill.getFillType());
071: shape = slide[1].getShapes()[0];
072: assertEquals(Fill.FILL_BACKGROUND, shape.getFill()
073: .getFillType());
074:
075: fill = slide[2].getBackground().getFill();
076: assertEquals(Fill.FILL_TEXTURE, fill.getFillType());
077: shape = slide[2].getShapes()[0];
078: assertEquals(Fill.FILL_PICTURE, shape.getFill().getFillType());
079:
080: fill = slide[3].getBackground().getFill();
081: assertEquals(Fill.FILL_SHADE_CENTER, fill.getFillType());
082: shape = slide[3].getShapes()[0];
083: assertEquals(Fill.FILL_SHADE, shape.getFill().getFillType());
084: }
085:
086: /**
087: * Create a ppt with various fill effects
088: */
089: public void testBackgroundPicture() throws Exception {
090: SlideShow ppt = new SlideShow();
091: Slide slide;
092: Fill fill;
093: Shape shape;
094: int idx;
095:
096: //slide 1
097: slide = ppt.createSlide();
098: slide.setFollowMasterBackground(false);
099: fill = slide.getBackground().getFill();
100: idx = ppt.addPicture(new File(System
101: .getProperty("HSLF.testdata.path")
102: + "/tomcat.png"), Picture.PNG);
103: fill.setFillType(Fill.FILL_PICTURE);
104: fill.setPictureData(idx);
105:
106: shape = new AutoShape(ShapeTypes.Rectangle);
107: shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
108: fill = shape.getFill();
109: fill.setFillType(Fill.FILL_SOLID);
110: slide.addShape(shape);
111:
112: //slide 2
113: slide = ppt.createSlide();
114: slide.setFollowMasterBackground(false);
115: fill = slide.getBackground().getFill();
116: idx = ppt.addPicture(new File(System
117: .getProperty("HSLF.testdata.path")
118: + "/tomcat.png"), Picture.PNG);
119: fill.setFillType(Fill.FILL_PATTERN);
120: fill.setPictureData(idx);
121: fill.setBackgroundColor(Color.green);
122: fill.setForegroundColor(Color.red);
123:
124: shape = new AutoShape(ShapeTypes.Rectangle);
125: shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
126: fill = shape.getFill();
127: fill.setFillType(Fill.FILL_BACKGROUND);
128: slide.addShape(shape);
129:
130: //slide 3
131: slide = ppt.createSlide();
132: slide.setFollowMasterBackground(false);
133: fill = slide.getBackground().getFill();
134: idx = ppt.addPicture(new File(System
135: .getProperty("HSLF.testdata.path")
136: + "/tomcat.png"), Picture.PNG);
137: fill.setFillType(Fill.FILL_TEXTURE);
138: fill.setPictureData(idx);
139:
140: shape = new AutoShape(ShapeTypes.Rectangle);
141: shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
142: fill = shape.getFill();
143: fill.setFillType(Fill.FILL_PICTURE);
144: idx = ppt.addPicture(new File(System
145: .getProperty("HSLF.testdata.path")
146: + "/clock.jpg"), Picture.JPEG);
147: fill.setPictureData(idx);
148: slide.addShape(shape);
149:
150: // slide 4
151: slide = ppt.createSlide();
152: slide.setFollowMasterBackground(false);
153: fill = slide.getBackground().getFill();
154: fill.setFillType(Fill.FILL_SHADE_CENTER);
155: fill.setBackgroundColor(Color.white);
156: fill.setForegroundColor(Color.darkGray);
157:
158: shape = new AutoShape(ShapeTypes.Rectangle);
159: shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
160: fill = shape.getFill();
161: fill.setFillType(Fill.FILL_SHADE);
162: fill.setBackgroundColor(Color.red);
163: fill.setForegroundColor(Color.green);
164: slide.addShape(shape);
165:
166: //serialize and read again
167: ByteArrayOutputStream out = new ByteArrayOutputStream();
168: ppt.write(out);
169: out.close();
170:
171: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
172: out.toByteArray())));
173: Slide[] slides = ppt.getSlides();
174:
175: fill = slides[0].getBackground().getFill();
176: assertEquals(Fill.FILL_PICTURE, fill.getFillType());
177: shape = slides[0].getShapes()[0];
178: assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
179:
180: fill = slides[1].getBackground().getFill();
181: assertEquals(Fill.FILL_PATTERN, fill.getFillType());
182: shape = slides[1].getShapes()[0];
183: assertEquals(Fill.FILL_BACKGROUND, shape.getFill()
184: .getFillType());
185:
186: fill = slides[2].getBackground().getFill();
187: assertEquals(Fill.FILL_TEXTURE, fill.getFillType());
188: shape = slides[2].getShapes()[0];
189: assertEquals(Fill.FILL_PICTURE, shape.getFill().getFillType());
190:
191: fill = slides[3].getBackground().getFill();
192: assertEquals(Fill.FILL_SHADE_CENTER, fill.getFillType());
193: shape = slides[3].getShapes()[0];
194: assertEquals(Fill.FILL_SHADE, shape.getFill().getFillType());
195:
196: }
197:
198: }
|