1 /* 2 * Copyright (C) 2008 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 android.content.pm.cts; 18 19 import dalvik.annotation.TestLevel; 20 import dalvik.annotation.TestTargetClass; 21 import dalvik.annotation.TestTargetNew; 22 import dalvik.annotation.TestTargets; 23 24 import android.content.pm.PackageStats; 25 import android.os.Parcel; 26 import android.test.AndroidTestCase; 27 28 @TestTargetClass(PackageStats.class) 29 public class PackageStatsTest extends AndroidTestCase { 30 private static final String PACKAGE_NAME = "com.android.cts.stub"; 31 32 @TestTargets({ 33 @TestTargetNew( 34 level = TestLevel.COMPLETE, 35 notes = "Test describeContents", 36 method = "describeContents", 37 args = {} 38 ), 39 @TestTargetNew( 40 level = TestLevel.COMPLETE, 41 notes = "Test constructors", 42 method = "PackageStats", 43 args = {android.content.pm.PackageStats.class} 44 ), 45 @TestTargetNew( 46 level = TestLevel.COMPLETE, 47 notes = "Test constructors", 48 method = "PackageStats", 49 args = {android.os.Parcel.class} 50 ), 51 @TestTargetNew( 52 level = TestLevel.COMPLETE, 53 notes = "Test constructors", 54 method = "PackageStats", 55 args = {java.lang.String.class} 56 ), 57 @TestTargetNew( 58 level = TestLevel.COMPLETE, 59 notes = "Test toString", 60 method = "toString", 61 args = {} 62 ), 63 @TestTargetNew( 64 level = TestLevel.COMPLETE, 65 notes = "Test writeToParcel", 66 method = "writeToParcel", 67 args = {android.os.Parcel.class, int.class} 68 ) 69 }) testPackageStats()70 public void testPackageStats() { 71 // Set mock data to make sure the functionality of constructor 72 long codeSize = 10000; 73 long cacheSize = 10240; 74 long dataSize = 4096; 75 76 // Test PackageStats(String pkgName), PackageStats(PackageStats pStats) 77 PackageStats stats = new PackageStats(PACKAGE_NAME); 78 assertEquals(PACKAGE_NAME, stats.packageName); 79 stats.cacheSize = codeSize; 80 stats.codeSize = cacheSize; 81 stats.dataSize = dataSize; 82 PackageStats infoFromExisted = new PackageStats(stats); 83 checkInfoSame(stats, infoFromExisted); 84 85 // Test toString, describeContents 86 assertNotNull(stats.toString()); 87 assertEquals(0, stats.describeContents()); 88 89 // Test writeToParcel, PackageStats(Parcel source) 90 Parcel p = Parcel.obtain(); 91 stats.writeToParcel(p, 0); 92 p.setDataPosition(0); 93 // CREATOR invokes public PackageStats(Parcel source) 94 PackageStats infoFromParcel = PackageStats.CREATOR.createFromParcel(p); 95 checkInfoSame(stats, infoFromParcel); 96 p.recycle(); 97 } 98 checkInfoSame(PackageStats expected, PackageStats actual)99 private void checkInfoSame(PackageStats expected, PackageStats actual) { 100 assertEquals(expected.packageName, actual.packageName); 101 assertEquals(expected.cacheSize, actual.cacheSize); 102 assertEquals(expected.dataSize, actual.dataSize); 103 assertEquals(expected.codeSize, actual.codeSize); 104 } 105 } 106