1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.util;
18
19import junit.framework.TestCase;
20
21import java.io.ByteArrayOutputStream;
22import java.io.PrintWriter;
23
24/**
25 * Tests for {@link IndentingPrintWriter}.
26 */
27public class IndentingPrintWriterTest extends TestCase {
28
29    private ByteArrayOutputStream mStream;
30    private PrintWriter mWriter;
31
32    @Override
33    protected void setUp() throws Exception {
34        super.setUp();
35
36        mStream = new ByteArrayOutputStream();
37        mWriter = new PrintWriter(mStream);
38    }
39
40    public void testMultipleIndents() throws Exception {
41        final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "  ");
42
43        pw.print("Hello");
44        pw.increaseIndent();
45        pw.println();
46        pw.print("World");
47        pw.increaseIndent();
48        pw.println();
49        pw.print("And");
50        pw.decreaseIndent();
51        pw.println();
52        pw.print("Goodbye");
53        pw.decreaseIndent();
54        pw.println();
55        pw.print("World");
56        pw.println();
57
58        pw.flush();
59        assertEquals("Hello\n  World\n    And\n  Goodbye\nWorld\n", mStream.toString());
60    }
61
62    public void testAdjustIndentAfterNewline() throws Exception {
63        final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "  ");
64
65        pw.println("Hello");
66        pw.increaseIndent();
67        pw.println("World");
68
69        pw.flush();
70        assertEquals("Hello\n  World\n", mStream.toString());
71    }
72
73    public void testWrapping() throws Exception {
74        final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "", 10);
75
76        pw.print("dog ");
77        pw.print("cat ");
78        pw.print("cow ");
79        pw.print("meow ");
80
81        pw.flush();
82        assertEquals("dog cat \ncow meow ", mStream.toString());
83    }
84
85    public void testWrappingIndented() throws Exception {
86        final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "    ", 10);
87
88        pw.increaseIndent();
89        pw.print("dog ");
90        pw.print("meow ");
91        pw.print("a ");
92        pw.print("b ");
93        pw.print("cow ");
94
95        pw.flush();
96        assertEquals("    dog \n    meow \n    a b \n    cow ", mStream.toString());
97    }
98
99    public void testWrappingEmbeddedNewlines() throws Exception {
100        final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "  ", 10);
101
102        pw.increaseIndent();
103        pw.print("Lorem ipsum \ndolor sit \namet, consectetur \nadipiscing elit.");
104
105        pw.flush();
106        assertEquals("  Lorem ip\n  sum \n  dolor si\n  t \n  amet, co\n"
107                + "  nsectetu\n  r \n  adipisci\n  ng elit.\n", mStream.toString());
108    }
109
110    public void testWrappingSingleGiant() throws Exception {
111        final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "  ", 10);
112
113        pw.increaseIndent();
114        pw.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
115
116        pw.flush();
117        assertEquals("  Lorem ip\n  sum dolo\n  r sit am\n  et, cons\n"
118                + "  ectetur \n  adipisci\n  ng elit.\n", mStream.toString());
119    }
120
121    public void testWrappingPrefixedGiant() throws Exception {
122        final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "  ", 10);
123
124        pw.increaseIndent();
125        pw.print("foo");
126        pw.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
127
128        pw.flush();
129        assertEquals("  foo\n  Lorem ip\n  sum dolo\n  r sit am\n  et, cons\n"
130                + "  ectetur \n  adipisci\n  ng elit.\n", mStream.toString());
131    }
132}
133