1 /* 2 * Copyright (C) 2017 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.settings.fuelgauge.anomaly.action; 18 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.verify; 21 22 import android.app.AppOpsManager; 23 import android.content.Context; 24 import android.util.Pair; 25 26 import com.android.internal.logging.nano.MetricsProto; 27 import com.android.settings.fuelgauge.anomaly.Anomaly; 28 import com.android.settings.testutils.FakeFeatureFactory; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Answers; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class AnomalyActionTest { 40 41 private static final String PACKAGE_NAME = "com.android.app"; 42 private static final int UID = 111; 43 private static final int ACTION_KEY = 2; 44 private static final int METRIC_KEY = 3; 45 46 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 47 private Context mContext; 48 @Mock 49 private AppOpsManager mAppOpsManager; 50 private Anomaly mAnomaly; 51 private TestAnomalyAction mTestAnomalyAction; 52 private FakeFeatureFactory mFeatureFactory; 53 54 @Before 55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 58 mFeatureFactory = FakeFeatureFactory.setupForTest(); 59 doReturn(mAppOpsManager).when(mContext).getSystemService(Context.APP_OPS_SERVICE); 60 61 mAnomaly = new Anomaly.Builder().setUid(UID).setPackageName(PACKAGE_NAME).build(); 62 mTestAnomalyAction = new TestAnomalyAction(mContext); 63 } 64 65 @Test 66 public void testHandlePositiveAction_logAction() { 67 mTestAnomalyAction.handlePositiveAction(mAnomaly, METRIC_KEY); 68 69 verify(mFeatureFactory.metricsFeatureProvider).action(mContext, ACTION_KEY, PACKAGE_NAME, 70 Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, METRIC_KEY)); 71 } 72 73 private class TestAnomalyAction extends AnomalyAction { 74 private TestAnomalyAction(Context context) { 75 super(context); 76 mActionMetricKey = ACTION_KEY; 77 } 78 79 @Override 80 public boolean isActionActive(Anomaly anomaly) { 81 return false; 82 } 83 84 @Override 85 public int getActionType() { 86 return 0; 87 } 88 } 89 } 90