1 /* 2 * Copyright (C) 2018 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.tradefed.proto; 17 18 import static org.junit.Assert.assertFalse; 19 import static org.junit.Assert.assertTrue; 20 import static org.junit.Assert.fail; 21 22 import android.os.BatteryStatsProto; 23 import android.service.batterystats.BatteryStatsServiceDumpProto; 24 25 import com.android.tradefed.device.CollectingByteOutputReceiver; 26 import com.android.tradefed.device.ITestDevice; 27 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 28 import com.android.tradefed.testtype.IDeviceTest; 29 30 import com.google.protobuf.InvalidProtocolBufferException; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 /** 36 * Functional tests for platformprotos dependency. 37 * 38 * <p>Since TradeFederation manifest branch does not contain frameworks/base, platformprotos was 39 * added to prebuilds/misc/common as a prebuild so that TradeFederation can read protos in 40 * frameworks/base. PlatformProtosFuncTest guards against proto parsing failure caused by outdated 41 * platformprotos-prebuilt as its source code evolves over time. 42 */ 43 @RunWith(DeviceJUnit4ClassRunner.class) 44 public class PlatformProtosFuncTest implements IDeviceTest { 45 46 private static final String CMD_DUMP_BATTERYSTATS = "dumpsys batterystats --proto"; 47 48 private ITestDevice mDevice; 49 50 @Override setDevice(ITestDevice device)51 public void setDevice(ITestDevice device) { 52 mDevice = device; 53 } 54 55 @Override getDevice()56 public ITestDevice getDevice() { 57 return mDevice; 58 } 59 60 /** 61 * Dump {@link BatteryStatsServiceDumpProto} into a byte array and parse it into proto object. 62 */ 63 @Test testDumpAndReadBatteryStatsProto()64 public void testDumpAndReadBatteryStatsProto() throws Exception { 65 66 CollectingByteOutputReceiver receiver = new CollectingByteOutputReceiver(); 67 getDevice().executeShellCommand(CMD_DUMP_BATTERYSTATS, receiver); 68 69 byte[] protoBytes = receiver.getOutput(); 70 try { 71 BatteryStatsServiceDumpProto bssdp = BatteryStatsServiceDumpProto.parseFrom(protoBytes); 72 assertTrue(bssdp.hasBatterystats()); 73 BatteryStatsProto bs = bssdp.getBatterystats(); 74 assertTrue(bs.hasSystem()); 75 assertFalse(bs.getUidsList().isEmpty()); 76 assertTrue(bs.getUids(0).hasCpu()); 77 } catch (InvalidProtocolBufferException e) { 78 fail("Invalid BatteryStatsServiceDumpProto: " + e.getMessage()); 79 } 80 } 81 } 82