1 /* 2 * Copyright (C) 2013 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 17 package com.android.tradefed.device; 18 19 import junit.framework.TestCase; 20 21 /** 22 * Unit tests for {@link DumpsysPackageReceiver}. 23 */ 24 public class DumpsysPackageReceiverTest extends TestCase { 25 26 /** 27 * Verifies parse correctly handles 'dumpsys package p' output from 4.2 and below. 28 */ 29 public void testParse_classic() throws Exception { 30 final String[] froyoPkgTxt = new String[] {"Packages:", 31 "Package [com.android.soundrecorder] (462f6b38):", 32 "targetSdk=8", 33 "versionName=3.1.36 (88)", 34 "pkgFlags=0x1 installStatus=1 enabled=0"}; 35 36 DumpsysPackageReceiver p = new DumpsysPackageReceiver(); 37 p.processNewLines(froyoPkgTxt); 38 assertEquals("failed to parse package data", 1, p.getPackages().size()); 39 PackageInfo pkg = p.getPackages().get("com.android.soundrecorder"); 40 assertEquals("com.android.soundrecorder", pkg.getPackageName()); 41 assertTrue(pkg.isSystemApp()); 42 assertFalse(pkg.isUpdatedSystemApp()); 43 assertEquals("3.1.36 (88)", pkg.getVersionName()); 44 } 45 46 /** 47 * Verifies parse correctly handles new textual 'dumpsys package p' output from newer releases. 48 */ 49 public void testParse_future() throws Exception { 50 final String[] pkgTxt = new String[] {"Packages:", 51 "Package [com.android.soundrecorder] (462f6b38):", 52 "targetSdk=8", 53 "pkgFlags=[ SYSTEM HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ]", 54 "installed=true"}; 55 56 DumpsysPackageReceiver p = new DumpsysPackageReceiver(); 57 p.processNewLines(pkgTxt); 58 assertEquals("failed to parse package data", 1, p.getPackages().size()); 59 PackageInfo pkg = p.getPackages().get("com.android.soundrecorder"); 60 assertNotNull("failed to parse package data", pkg); 61 assertEquals("com.android.soundrecorder", pkg.getPackageName()); 62 assertTrue(pkg.isSystemApp()); 63 assertFalse(pkg.isUpdatedSystemApp()); 64 } 65 66 /** 67 * Verifies parse correctly handles 'dumpsys package p' output with hidden system package info 68 */ 69 public void testParse_hidden() throws Exception { 70 final String[] pkgsTxt = new String[] {"Packages:", 71 "Package [com.android.soundrecorder] (462f6b38):", 72 "targetSdk=8", 73 "pkgFlags=[ SYSTEM HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ]", 74 "installed=true", 75 "Hidden system packages:", 76 " Package [com.android.soundrecorder] (686868):"}; 77 78 DumpsysPackageReceiver p = new DumpsysPackageReceiver(); 79 p.processNewLines(pkgsTxt); 80 assertEquals("failed to parse package data", 1, p.getPackages().size()); 81 PackageInfo pkg = p.getPackages().get("com.android.soundrecorder"); 82 assertEquals("com.android.soundrecorder", pkg.getPackageName()); 83 assertTrue(pkg.isSystemApp()); 84 assertTrue(pkg.isUpdatedSystemApp()); 85 } 86 87 /** 88 * Verifies parse handles empty input 89 */ 90 public void testParse_empty() { 91 DumpsysPackageReceiver parser = new DumpsysPackageReceiver(); 92 assertEquals(0, parser.getPackages().size()); 93 } 94 } 95