Home | History | Annotate | Download | only in rule
      1 /*
      2  * Copyright (C) 2016 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.loganalysis.rule;
     17 
     18 import com.android.loganalysis.item.ActivityServiceItem;
     19 import com.android.loganalysis.item.BatteryStatsDetailedInfoItem;
     20 import com.android.loganalysis.item.BugreportItem;
     21 import com.android.loganalysis.item.DumpsysBatteryStatsItem;
     22 import com.android.loganalysis.item.DumpsysItem;
     23 import com.android.loganalysis.item.LocationDumpsItem;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import org.json.JSONObject;
     28 
     29 /**
     30  * Unit tests for {@link LocationUsageRule}
     31  */
     32 public class LocationUsageRuleTest extends TestCase {
     33 
     34     BugreportItem mBugreport;
     35     DumpsysItem mDumpsys;
     36     DumpsysBatteryStatsItem mDumpsysBatteryStats;
     37     BatteryStatsDetailedInfoItem mBatteryStatsDetailedInfo;
     38     ActivityServiceItem mActivityService;
     39 
     40     @Override
     41     public void setUp() {
     42         mBugreport =  new BugreportItem();
     43         mDumpsys = new DumpsysItem();
     44         mDumpsysBatteryStats = new DumpsysBatteryStatsItem();
     45         mBatteryStatsDetailedInfo = new BatteryStatsDetailedInfoItem();
     46         mActivityService = new ActivityServiceItem();
     47 
     48         mBatteryStatsDetailedInfo.setTimeOnBattery(3902004);
     49         mDumpsysBatteryStats.setDetailedBatteryStatsItem(mBatteryStatsDetailedInfo);
     50         mDumpsys.setBatteryInfo(mDumpsysBatteryStats);
     51         mBugreport.setDumpsys(mDumpsys);
     52         mBugreport.setActivityService(mActivityService);
     53     }
     54 
     55     /**
     56      * Test location usage analysis
     57      */
     58     public void testLocationUsageAnalysis() throws Exception {
     59         LocationDumpsItem location = new LocationDumpsItem();
     60         location.addLocationClient("com.google.android.gms", 1, 0, 0, "PRIORITY_NO_POWER", 140);
     61         location.addLocationClient("com.google.android.gms", 5, 5, 5,
     62                 "PRIORITY_BALANCED_POWER_ACCURACY", 140);
     63         mActivityService.setLocationDumps(location);
     64 
     65         LocationUsageRule locationUsageRule = new LocationUsageRule(mBugreport);
     66         locationUsageRule.applyRule();
     67         JSONObject analysis = locationUsageRule.getAnalysis();
     68         assertNotNull(analysis);
     69         assertTrue(analysis.has("LOCATION_USAGE_ANALYSIS"));
     70         assertEquals(analysis.getString("LOCATION_USAGE_ANALYSIS"),
     71                 "Package com.google.android.gms is requesting for location updates every 5 secs "
     72                 + "with priority PRIORITY_BALANCED_POWER_ACCURACY.");
     73     }
     74 
     75     public void testNoSignificantLocationUsageAnalysis() throws Exception {
     76         LocationDumpsItem location = new LocationDumpsItem();
     77         location.addLocationClient("com.google.android.gms", 1, 0, 0, "PRIORITY_NO_POWER", 140);
     78         location.addLocationClient("com.google.android.gms", 285, 285, 285,
     79                 "PRIORITY_BALANCED_POWER_ACCURACY", 140);
     80         mActivityService.setLocationDumps(location);
     81 
     82         LocationUsageRule locationUsageRule = new LocationUsageRule(mBugreport);
     83         locationUsageRule.applyRule();
     84         JSONObject analysis = locationUsageRule.getAnalysis();
     85         assertNotNull(analysis);
     86         assertTrue(analysis.has("LOCATION_USAGE_ANALYSIS"));
     87         assertEquals(analysis.getString("LOCATION_USAGE_ANALYSIS"),
     88                 "No apps requested for frequent location updates.");
     89     }
     90 
     91     public void testNoLocationUsageAnalysis() throws Exception {
     92         LocationUsageRule locationUsageRule = new LocationUsageRule(mBugreport);
     93         locationUsageRule.applyRule();
     94         JSONObject analysis = locationUsageRule.getAnalysis();
     95         assertNotNull(analysis);
     96         assertTrue(analysis.has("LOCATION_USAGE_ANALYSIS"));
     97         assertEquals(analysis.getString("LOCATION_USAGE_ANALYSIS"),
     98                 "No apps requested for frequent location updates.");
     99     }
    100 }
    101