Fix SettingsDumpServiceTest

SettingsDumpService behavior changed in
Change I9d7fe2e763b9e5840ee76c5eb00bc88288e7ee41

After behavior changed, PrintWriter was wrapped in IndentingPrintWriter,
it's harder to mock the PrintWriter, so check the result directly.

Fix: 280068083
Test: RobolectricTest
Change-Id: I92a7b931c93df4cd9e9c3e47b91a9750df936f15
This commit is contained in:
Chaohui Wang
2023-05-04 14:15:52 +08:00
parent d2ddc84c14
commit e2b9d0680e

View File

@@ -29,8 +29,6 @@ import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import androidx.annotation.NonNull;
import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService; import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
import org.json.JSONException; import org.json.JSONException;
@@ -43,8 +41,8 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class SettingsDumpServiceTest { public class SettingsDumpServiceTest {
@@ -102,15 +100,16 @@ public class SettingsDumpServiceTest {
} }
@Test @Test
public void testDump_ReturnJsonObject() throws JSONException { public void testDump_printServiceAsKey() {
mResolveInfo.activityInfo = new ActivityInfo(); mResolveInfo.activityInfo = new ActivityInfo();
mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER; mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER;
TestPrintWriter printWriter = new TestPrintWriter(System.out); StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
mTestService.dump(null, printWriter, null); mTestService.dump(null, printWriter, null);
JSONObject object = (JSONObject) printWriter.getPrintObject();
assertThat(object.get(TestService.KEY_SERVICE)).isNotNull(); assertThat(stringWriter.toString())
.contains("{\"" + SettingsDumpService.KEY_SERVICE + "\":");
} }
/** /**
@@ -128,24 +127,4 @@ public class SettingsDumpServiceTest {
return mPm; return mPm;
} }
} }
/**
* Test printWriter to store the object to be printed
*/
private class TestPrintWriter extends PrintWriter {
private Object mPrintObject;
private TestPrintWriter(@NonNull OutputStream out) {
super(out);
}
@Override
public void println(Object object) {
mPrintObject = object;
}
private Object getPrintObject() {
return mPrintObject;
}
}
} }