Home | History | Annotate | Download | only in tips
      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.settings.fuelgauge.batterytip.tips;
     17 
     18 import static com.google.common.truth.Truth.assertThat;
     19 
     20 import static org.mockito.Mockito.verify;
     21 
     22 import android.content.Context;
     23 import android.os.Parcel;
     24 
     25 import com.android.internal.logging.nano.MetricsProto;
     26 import com.android.settings.R;
     27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
     29 
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.mockito.Mock;
     34 import org.mockito.MockitoAnnotations;
     35 import org.robolectric.RuntimeEnvironment;
     36 
     37 @RunWith(SettingsRobolectricTestRunner.class)
     38 public class LowBatteryTipTest {
     39 
     40     private static final CharSequence SUMMARY = "Only 15 minutes left";
     41 
     42     @Mock
     43     private MetricsFeatureProvider mMetricsFeatureProvider;
     44     private Context mContext;
     45     private LowBatteryTip mLowBatteryTip;
     46 
     47     @Before
     48     public void setUp() {
     49         MockitoAnnotations.initMocks(this);
     50 
     51         mContext = RuntimeEnvironment.application;
     52         mLowBatteryTip = new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */,
     53                 SUMMARY);
     54     }
     55 
     56     @Test
     57     public void testParcelable() {
     58         Parcel parcel = Parcel.obtain();
     59         mLowBatteryTip.writeToParcel(parcel, mLowBatteryTip.describeContents());
     60         parcel.setDataPosition(0);
     61 
     62         final LowBatteryTip parcelTip = new LowBatteryTip(parcel);
     63 
     64         assertThat(parcelTip.isPowerSaveModeOn()).isFalse();
     65         assertThat(parcelTip.getSummary(mContext)).isEqualTo(SUMMARY);
     66     }
     67 
     68     @Test
     69     public void getSummary_tipHandled_showSummary() {
     70         mLowBatteryTip.mState = BatteryTip.StateType.HANDLED;
     71 
     72         assertThat(mLowBatteryTip.getSummary(mContext)).isEqualTo("Some features may be limited");
     73     }
     74 
     75     @Test
     76     public void getSummary_tipNew_showSummary() {
     77         mLowBatteryTip.mState = BatteryTip.StateType.NEW;
     78 
     79         assertThat(mLowBatteryTip.getSummary(mContext)).isEqualTo(SUMMARY);
     80     }
     81 
     82     @Test
     83     public void log_lowBatteryActionWithCorrectState() {
     84         mLowBatteryTip.log(mContext, mMetricsFeatureProvider);
     85 
     86         verify(mMetricsFeatureProvider).action(mContext,
     87                 MetricsProto.MetricsEvent.ACTION_LOW_BATTERY_TIP, BatteryTip.StateType.NEW);
     88     }
     89 }
     90