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.internal.util; 18 19import junit.framework.TestCase; 20 21public class ProgressReporterTest extends TestCase { 22 private ProgressReporter r; 23 24 @Override 25 protected void setUp() throws Exception { 26 super.setUp(); 27 r = new ProgressReporter(0); 28 } 29 30 private void assertProgress(int expected) { 31 assertEquals(expected, r.getProgress()); 32 } 33 34 private void assertRange(int start, int len) { 35 final int[] range = r.getSegmentRange(); 36 assertEquals("start", start, range[0]); 37 assertEquals("len", len, range[1]); 38 } 39 40 public void testBasic() throws Exception { 41 assertProgress(0); 42 43 r.setProgress(20); 44 assertProgress(20); 45 46 r.setProgress(-20); 47 assertProgress(0); 48 49 r.setProgress(1024); 50 assertProgress(100); 51 } 52 53 public void testSegment() throws Exception { 54 r.setProgress(20); 55 assertProgress(20); 56 57 final int[] lastRange = r.startSegment(40); 58 { 59 assertProgress(20); 60 61 r.setProgress(50); 62 assertProgress(40); 63 } 64 r.endSegment(lastRange); 65 assertProgress(60); 66 67 r.setProgress(80); 68 assertProgress(80); 69 } 70 71 public void testSegmentOvershoot() throws Exception { 72 r.setProgress(20); 73 assertProgress(20); 74 75 final int[] lastRange = r.startSegment(40); 76 { 77 r.setProgress(-100, 2); 78 assertProgress(20); 79 80 r.setProgress(1, 2); 81 assertProgress(40); 82 83 r.setProgress(100, 2); 84 assertProgress(60); 85 } 86 r.endSegment(lastRange); 87 assertProgress(60); 88 } 89 90 public void testSegmentNested() throws Exception { 91 r.setProgress(20); 92 assertProgress(20); 93 assertRange(0, 100); 94 95 final int[] lastRange = r.startSegment(40); 96 assertRange(20, 40); 97 { 98 r.setProgress(50); 99 assertProgress(40); 100 101 final int[] lastRange2 = r.startSegment(25); 102 assertRange(40, 10); 103 { 104 r.setProgress(0); 105 assertProgress(40); 106 107 r.setProgress(50); 108 assertProgress(45); 109 110 r.setProgress(100); 111 assertProgress(50); 112 } 113 r.endSegment(lastRange2); 114 assertProgress(50); 115 } 116 r.endSegment(lastRange); 117 assertProgress(60); 118 } 119} 120