PriorityDumpTest.java revision f4006d9b0be123b2a4e874b89eb4a431d3d49c8b
1/**
2 * Copyright (c) 2016, 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.server.utils;
18
19import static com.android.server.utils.PriorityDump.dump;
20
21import static org.junit.Assert.assertSame;
22import static org.mockito.Matchers.eq;
23import static org.mockito.Matchers.same;
24import static org.mockito.Mockito.verify;
25
26import android.platform.test.annotations.Presubmit;
27import android.support.test.filters.SmallTest;
28import com.android.server.utils.PriorityDump.PriorityDumper;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.junit.runners.JUnit4;
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36
37import java.io.FileDescriptor;
38import java.io.PrintWriter;
39
40@SmallTest
41@Presubmit
42@RunWith(JUnit4.class)
43public class PriorityDumpTest {
44
45    private static final String[] EMPTY_ARGS = {};
46
47    @Mock
48    private PriorityDumper mDumper;
49    @Mock
50    private PrintWriter mPw;
51
52    private final FileDescriptor mFd = FileDescriptor.err;
53
54    @Before
55    public void setup() {
56        MockitoAnnotations.initMocks(this);
57    }
58
59    @Test
60    public void testNullArgs() {
61        dump(mDumper, mFd, mPw, null);
62        verify(mDumper).dump(same(mFd), same(mPw), eq(null));
63    }
64
65    @Test
66    public void testNoArgs() {
67        dump(mDumper, mFd, mPw, EMPTY_ARGS);
68        verify(mDumper).dump(same(mFd), same(mPw), same(EMPTY_ARGS));
69    }
70
71    @Test
72    public void testNonPriorityArgs() {
73        final String[] args = {
74                "--dumb_priority"
75        };
76        dump(mDumper, mFd, mPw, args);
77        verify(mDumper).dump(same(mFd), same(mPw), same(args));
78    }
79
80    @Test
81    public void testMissingPriority() {
82        final String[] args = {
83                "--dump_priority"
84        };
85        dump(mDumper, mFd, mPw, args);
86        verify(mDumper).dump(same(mFd), same(mPw), same(args));
87    }
88
89    @Test
90    public void testInvalidPriorityNoExtraArgs() {
91        final String[] args = {
92                "--dump_priority", "SUPER_HIGH"
93        };
94        dump(mDumper, mFd, mPw, args);
95        verify(mDumper).dump(same(mFd), same(mPw), same(args));
96    }
97
98    @Test
99    public void testInvalidPriorityExtraArgs() {
100        final String[] args = {
101                "--dump_priority", "SUPER_HIGH", "--high", "--five"
102        };
103        dump(mDumper, mFd, mPw, args);
104        verify(mDumper).dump(same(mFd), same(mPw), same(args));
105    }
106
107    @Test
108    public void testNoPriorityCallsAllMethods() {
109        final String[] args = {
110                "1", "2", "3"
111        };
112
113        // Cannot use mDumper here because it would mock the dump() call.
114        final FakeDumper fakeDumper = new FakeDumper();
115
116        dump(fakeDumper, mFd, mPw, args);
117
118        assertSame(mFd, fakeDumper.criticalFd);
119        assertSame(mPw, fakeDumper.criticalPw);
120        assertSame(args, fakeDumper.criticalArgs);
121        assertSame(mFd, fakeDumper.highFd);
122        assertSame(mPw, fakeDumper.highPw);
123        assertSame(args, fakeDumper.highArgs);
124        assertSame(mFd, fakeDumper.normalFd);
125        assertSame(mPw, fakeDumper.normalPw);
126        assertSame(args, fakeDumper.normalArgs);
127    }
128
129    @Test
130    public void testCriticalNoExtraArgs() {
131        dump(mDumper, mFd, mPw, new String[] {
132                "--dump_priority", "CRITICAL"
133        });
134        verify(mDumper).dumpCritical(same(mFd), same(mPw), eq(EMPTY_ARGS));
135    }
136
137    @Test
138    public void testCriticalExtraArgs() {
139        dump(mDumper, mFd, mPw, new String[] {
140                "--dump_priority", "CRITICAL", "--high", "--five"
141        });
142        verify(mDumper).dumpCritical(same(mFd), same(mPw), eq(new String[] {
143                "--high", "--five"
144        }));
145    }
146
147    @Test
148    public void testHighNoExtraArgs() {
149        dump(mDumper, mFd, mPw, new String[] {
150                "--dump_priority", "HIGH"
151        });
152        verify(mDumper).dumpHigh(same(mFd), same(mPw), eq(EMPTY_ARGS));
153    }
154
155    @Test
156    public void testHighExtraArgs() {
157        dump(mDumper, mFd, mPw, new String[] {
158                "--dump_priority", "HIGH", "--high", "--five"
159        });
160        verify(mDumper).dumpHigh(same(mFd), same(mPw), eq(new String[] {
161                "--high", "--five"
162        }));
163    }
164
165    @Test
166    public void testNormalNoExtraArgs() {
167        dump(mDumper, mFd, mPw, new String[] {
168                "--dump_priority", "NORMAL"
169        });
170        verify(mDumper).dumpNormal(same(mFd), same(mPw), eq(EMPTY_ARGS));
171    }
172
173    @Test
174    public void testNormalExtraArgs() {
175        dump(mDumper, mFd, mPw, new String[] {
176                "--dump_priority", "NORMAL", "--high", "--five"
177        });
178        verify(mDumper).dumpNormal(same(mFd), same(mPw), eq(new String[] {
179                "--high", "--five"
180        }));
181    }
182
183    private final class FakeDumper implements PriorityDumper {
184
185        String[] criticalArgs, highArgs, normalArgs;
186        FileDescriptor criticalFd, highFd, normalFd;
187        PrintWriter criticalPw, highPw, normalPw;
188
189        @Override
190        public void dumpCritical(FileDescriptor fd, PrintWriter pw, String[] args) {
191            criticalFd = fd;
192            criticalPw = pw;
193            criticalArgs = args;
194        }
195
196        @Override
197        public void dumpHigh(FileDescriptor fd, PrintWriter pw, String[] args) {
198            highFd = fd;
199            highPw = pw;
200            highArgs = args;
201        }
202
203        @Override
204        public void dumpNormal(FileDescriptor fd, PrintWriter pw, String[] args) {
205            normalFd = fd;
206            normalPw = pw;
207            normalArgs = args;
208        }
209    }
210}
211