blob: f685c68f4160ee0b472a9e41cfcb5bdefd991687 [file] [log] [blame]
Chris Wrencdea3b72019-11-22 16:58:17 -05001/*
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 */
16package com.android.server.notification;
17
18import static com.android.server.notification.NotificationManagerService.REPORT_REMOTE_VIEWS;
19
20import static junit.framework.Assert.assertEquals;
21import static junit.framework.Assert.assertNotSame;
22
23import static org.junit.Assert.assertTrue;
24import static org.junit.Assert.fail;
25
26import android.service.notification.nano.NotificationRemoteViewsProto;
27import android.test.MoreAsserts;
28import android.util.proto.ProtoOutputStream;
29
30import androidx.test.filters.SmallTest;
31
32import com.android.server.UiServiceTestCase;
33
34import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
35
36import org.junit.Test;
37
38import java.io.ByteArrayOutputStream;
39import java.util.ArrayList;
40import java.util.List;
41
42@SmallTest
43public class PulledStatsTest extends UiServiceTestCase {
44
45 @Test
46 public void testPulledStats_Empty() {
47 PulledStats stats = new PulledStats(0L);
48 assertEquals(0L, stats.endTimeMs());
49 }
50
51 @Test
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
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
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}