Fix cursor is empty in ApnEditor

If cursor.moveToFirst() returns false, the cursor is empty which cause
crash.

Check whether the curse is empty to fix.

Fix: 288716180
Test: manual - on ApnEditor page
Test: m RunSettingsRoboTests
Change-Id: If850cd3feca3d3d520a653c2490d1049a8a87fdc
This commit is contained in:
Chaohui Wang
2023-06-28 19:13:42 +08:00
parent a09fd45249
commit b17ef6f91e
2 changed files with 19 additions and 2 deletions

View File

@@ -1440,8 +1440,7 @@ public class ApnEditor extends SettingsPreferenceFragment
null /* selection */,
null /* selectionArgs */,
null /* sortOrder */)) {
if (cursor != null) {
cursor.moveToFirst();
if (cursor != null && cursor.moveToFirst()) {
apnData = new ApnData(uri, cursor);
}
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.network.apn;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
@@ -27,6 +28,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.ContentValues;
@@ -41,15 +43,18 @@ import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.network.ProxySubscriptionManager;
import com.android.settings.network.apn.ApnEditor.ApnData;
import com.android.settings.testutils.shadow.ShadowFragment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -156,6 +161,19 @@ public class ApnEditorTest {
any(String.class));
}
@Test
public void getApnDataFromUri_emptyCursor_returnsNull() {
var mockContentResolver = mock(ContentResolver.class);
var mockCursor = mock(Cursor.class);
doReturn(mockContentResolver).when(mActivity).getContentResolver();
when(mockContentResolver.query(any(), any(), any(), any(), any())).thenReturn(mockCursor);
when(mockCursor.moveToFirst()).thenReturn(false);
var apnData = mApnEditorUT.getApnDataFromUri(mock(Uri.class));
assertThat(apnData).isNull();
}
@Test
public void testSetStringValue_valueChanged_shouldSetValue() {
// GIVEN an APN value which is different than the APN value in database