1 /* 2 * Copyright (C) 2019 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 package com.android.server.notification; 17 18 import static com.android.server.notification.NotificationManagerService.REPORT_REMOTE_VIEWS; 19 20 import static junit.framework.Assert.assertEquals; 21 import static junit.framework.Assert.assertNotSame; 22 23 import static org.junit.Assert.assertTrue; 24 import static org.junit.Assert.fail; 25 26 import android.service.notification.nano.NotificationRemoteViewsProto; 27 import android.test.MoreAsserts; 28 import android.util.proto.ProtoOutputStream; 29 30 import androidx.test.filters.SmallTest; 31 32 import com.android.server.UiServiceTestCase; 33 34 import com.google.protobuf.nano.InvalidProtocolBufferNanoException; 35 36 import org.junit.Test; 37 38 import java.io.ByteArrayOutputStream; 39 import java.util.ArrayList; 40 import java.util.List; 41 42 @SmallTest 43 public class PulledStatsTest extends UiServiceTestCase { 44 45 @Test testPulledStats_Empty()46 public void testPulledStats_Empty() { 47 PulledStats stats = new PulledStats(0L); 48 assertEquals(0L, stats.endTimeMs()); 49 } 50 51 @Test testPulledStats_UnknownReport()52 public void testPulledStats_UnknownReport() { 53 PulledStats stats = new PulledStats(0L); 54 stats.addUndecoratedPackage("foo", 456); 55 stats.addUndecoratedPackage("bar", 123); 56 57 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 58 final ProtoOutputStream proto = new ProtoOutputStream(bytes); 59 stats.writeToProto(1023123, proto); // a very large number 60 proto.flush(); 61 62 // expect empty output in response to an unrecognized request 63 assertEquals(0L, bytes.size()); 64 } 65 66 @Test testPulledStats_RemoteViewReportPackages()67 public void testPulledStats_RemoteViewReportPackages() { 68 List<String> expectedPkgs = new ArrayList<>(2); 69 expectedPkgs.add("foo"); 70 expectedPkgs.add("bar"); 71 72 PulledStats stats = new PulledStats(0L); 73 for(String pkg: expectedPkgs) { 74 stats.addUndecoratedPackage(pkg, 111); 75 } 76 77 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 78 final ProtoOutputStream protoStream = new ProtoOutputStream(bytes); 79 stats.writeToProto(REPORT_REMOTE_VIEWS, protoStream); 80 protoStream.flush(); 81 82 try { 83 NotificationRemoteViewsProto proto = 84 NotificationRemoteViewsProto.parseFrom(bytes.toByteArray()); 85 List<String> actualPkgs = new ArrayList<>(2); 86 for(int i = 0 ; i < proto.packageRemoteViewInfo.length; i++) { 87 actualPkgs.add(proto.packageRemoteViewInfo[i].packageName); 88 } 89 assertEquals(2, actualPkgs.size()); 90 assertTrue("missing packages", actualPkgs.containsAll(expectedPkgs)); 91 assertTrue("unexpected packages", expectedPkgs.containsAll(actualPkgs)); 92 } catch (InvalidProtocolBufferNanoException e) { 93 e.printStackTrace(); 94 fail("writeToProto generated unparsable output"); 95 } 96 97 } 98 @Test testPulledStats_RemoteViewReportEndTime()99 public void testPulledStats_RemoteViewReportEndTime() { 100 List<String> expectedPkgs = new ArrayList<>(2); 101 expectedPkgs.add("foo"); 102 expectedPkgs.add("bar"); 103 104 PulledStats stats = new PulledStats(0L); 105 long t = 111; 106 for(String pkg: expectedPkgs) { 107 t += 1000; 108 stats.addUndecoratedPackage(pkg, t); 109 } 110 assertEquals(t, stats.endTimeMs()); 111 } 112 113 } 114