Fix NullPointerException when showEmptyChart

Bug: 301880705
Fix: 301880705
Test: presubmit
Change-Id: I22a1df6b791396b398038c6bdb8e6b3ca37dc9e1
diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java
index 1ae3bef..bb6593c 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java
@@ -327,6 +327,10 @@
 
     // Show empty hourly chart view only if there is no valid battery usage data.
     void showEmptyChart() {
+        if (mDailyChartView == null || mHourlyChartView == null) {
+            // Chart views are not initialized.
+            return;
+        }
         setChartSummaryVisible(true);
         mDailyChartView.setVisibility(View.GONE);
         mHourlyChartView.setVisibility(View.VISIBLE);
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceControllerTest.java
index cd4e599..275ab34 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceControllerTest.java
@@ -335,6 +335,29 @@
     }
 
     @Test
+    public void showEmptyChart_dailyChartViewIsNull_ignoreShowEmptyChart() {
+        mBatteryChartPreferenceController.mDailyChartView = null;
+        doReturn(View.GONE).when(mHourlyChartView).getVisibility();
+
+        mBatteryChartPreferenceController.showEmptyChart();
+
+        verify(mChartSummaryTextView, never()).setVisibility(View.VISIBLE);
+        verify(mDailyChartView, never()).setVisibility(View.GONE);
+        verify(mHourlyChartView, never()).setVisibility(View.VISIBLE);
+    }
+
+    @Test
+    public void showEmptyChart_hourlyChartViewIsNull_ignoreShowEmptyChart() {
+        mBatteryChartPreferenceController.mHourlyChartView = null;
+
+        mBatteryChartPreferenceController.showEmptyChart();
+
+        verify(mChartSummaryTextView, never()).setVisibility(View.VISIBLE);
+        verify(mDailyChartView, never()).setVisibility(View.GONE);
+        verify(mHourlyChartView, never()).setVisibility(View.VISIBLE);
+    }
+
+    @Test
     public void refreshUi_dailyChartViewIsNull_ignoreRefresh() {
         mBatteryChartPreferenceController.mDailyChartView = null;