From: jackqdyulei Date: Wed, 13 Sep 2017 21:12:13 +0000 (-0700) Subject: Don't init batterystats in onCreate() X-Git-Tag: android-x86-9.0-r1~196^2~6^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=ceaab271c59c5f324bb884ab8d06f89a657cfc53;p=android-x86%2Fpackages-apps-Settings.git Don't init batterystats in onCreate() After ag/2573096, the batterystats will be refreshed after each broadcastReceiver registeration. And this registeration happens in onResume(). So we don't need to init batterystats in onCreate() anymore. Bug: 65629008 Test: RunSettingsRoboTests Change-Id: I4ed2d420c6f2bb77d726dac4dd58fa6704ce6929 --- diff --git a/src/com/android/settings/fuelgauge/PowerUsageBase.java b/src/com/android/settings/fuelgauge/PowerUsageBase.java index 88edb7a3c4..5f46b074b9 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageBase.java +++ b/src/com/android/settings/fuelgauge/PowerUsageBase.java @@ -60,8 +60,6 @@ public abstract class PowerUsageBase extends DashboardFragment mBatteryBroadcastReceiver.setBatteryChangedListener(() -> { restartBatteryStatsLoader(); }); - - getLoaderManager().initLoader(0, icicle, this); } @Override diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageBaseTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageBaseTest.java new file mode 100644 index 0000000000..9e0d4de516 --- /dev/null +++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageBaseTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.fuelgauge; + + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +import android.app.Activity; +import android.app.LoaderManager; +import android.content.Context; +import android.os.Bundle; + +import com.android.internal.os.BatteryStatsHelper; +import com.android.settings.TestConfig; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settings.testutils.shadow.ShadowDashboardFragment; +import com.android.settingslib.core.AbstractPreferenceController; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; + +import java.util.List; + +/** + * Unit tests for {@link PowerUsageBase}. + */ +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, + shadows = ShadowDashboardFragment.class) +public class PowerUsageBaseTest { + @Mock + private BatteryStatsHelper mBatteryStatsHelper; + @Mock + private LoaderManager mLoaderManager; + private TestFragment mFragment; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mFragment = spy(new TestFragment()); + mFragment.setBatteryStatsHelper(mBatteryStatsHelper); + doReturn(mLoaderManager).when(mFragment).getLoaderManager(); + } + + @Test + public void testOnCreate_batteryStatsLoaderNotInvoked() { + mFragment.onCreate(null); + + verify(mLoaderManager, never()).initLoader(anyInt(), any(Bundle.class), any()); + } + + public static class TestFragment extends PowerUsageBase { + + @Override + public int getMetricsCategory() { + return 0; + } + + @Override + protected void refreshUi() { + // Do nothing + } + + @Override + protected String getLogTag() { + return null; + } + + @Override + protected int getPreferenceScreenResId() { + return 0; + } + + @Override + protected List getPreferenceControllers(Context context) { + return null; + } + + public void setBatteryStatsHelper(BatteryStatsHelper batteryStatsHelper) { + mStatsHelper = batteryStatsHelper; + } + } +} diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowDashboardFragment.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowDashboardFragment.java new file mode 100644 index 0000000000..9f648421c8 --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowDashboardFragment.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.android.settings.testutils.shadow; + +import android.os.Bundle; + +import com.android.settings.dashboard.DashboardFragment; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +/** + * Shadow of {@link DashboardFragment}. + * + * Override the {@link #onCreate(Bundle)} to skip a null pointer exception in + * {@link android.content.res.Resources.Theme}, which we cannot mock it. + */ +@Implements(DashboardFragment.class) +public class ShadowDashboardFragment { + + @Implementation + public void onCreate(Bundle icicle) { + // do nothing + } +}