diff options
author | David Herman <davidherman@google.com> | 2018-08-21 16:49:35 -0700 |
---|---|---|
committer | TreeHugger Robot <treehugger-gerrit@google.com> | 2018-08-22 18:17:33 +0000 |
commit | 3966dbcfbf6a90515e06b2a6b572e64361c11359 (patch) | |
tree | 4ef6cc8b9795706554da2fd721d66a0a34c1d036 /swingp/testSrc | |
parent | 2dae56ec8bb99289e13c9e10d8c469df992f9fc0 (diff) | |
download | idea-3966dbcfbf6a90515e06b2a6b572e64361c11359.tar.gz |
Clean up Swingp JSON deserialization using Gson annotations
Bug: N/A
Test: Added
Change-Id: I2741e263591f381c66aef97b6ecf86a97a19816f
Diffstat (limited to 'swingp/testSrc')
-rw-r--r-- | swingp/testSrc/com/android/tools/swingp/MethodStatTest.java | 18 | ||||
-rw-r--r-- | swingp/testSrc/com/android/tools/swingp/json/SwingpSerializationTest.java | 263 |
2 files changed, 263 insertions, 18 deletions
diff --git a/swingp/testSrc/com/android/tools/swingp/MethodStatTest.java b/swingp/testSrc/com/android/tools/swingp/MethodStatTest.java index 0438d81dc6f..c85f467eaa8 100644 --- a/swingp/testSrc/com/android/tools/swingp/MethodStatTest.java +++ b/swingp/testSrc/com/android/tools/swingp/MethodStatTest.java @@ -16,8 +16,6 @@ package com.android.tools.swingp; import org.jetbrains.annotations.NotNull; -import com.google.common.truth.Truth; -import com.google.gson.JsonArray; import org.junit.Test; public class MethodStatTest { @@ -37,22 +35,6 @@ public class MethodStatTest { RenderStatsManager.setIsEnabled(false); } - @Test - public void arraySerializesCorrectly() { - int[] intArray = {5, 7}; - JsonArray jsonIntArray = SerializationHelpers.arrayToJsonArray(intArray); - Truth.assertThat(jsonIntArray.size()).isEqualTo(2); - Truth.assertThat(jsonIntArray.get(0).getAsInt()).isEqualTo(intArray[0]); - Truth.assertThat(jsonIntArray.get(1).getAsInt()).isEqualTo(intArray[1]); - - double[] doubleArray = {3.5, 9.5, 11}; - JsonArray jsonDoubleArray = SerializationHelpers.arrayToJsonArray(doubleArray); - Truth.assertThat(jsonDoubleArray.size()).isEqualTo(3); - Truth.assertThat(jsonDoubleArray.get(0).getAsDouble()).isEqualTo(doubleArray[0]); - Truth.assertThat(jsonDoubleArray.get(1).getAsDouble()).isEqualTo(doubleArray[1]); - Truth.assertThat(jsonDoubleArray.get(2).getAsDouble()).isEqualTo(doubleArray[2]); - } - /** * Trivial extension of {@link MethodStat} (since it's abstract) to test its implementation. */ diff --git a/swingp/testSrc/com/android/tools/swingp/json/SwingpSerializationTest.java b/swingp/testSrc/com/android/tools/swingp/json/SwingpSerializationTest.java new file mode 100644 index 00000000000..bce006918c7 --- /dev/null +++ b/swingp/testSrc/com/android/tools/swingp/json/SwingpSerializationTest.java @@ -0,0 +1,263 @@ +/* + * Copyright (C) 2018 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.tools.swingp.json; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.android.tools.swingp.BufferStrategyPaintMethodStat; +import com.android.tools.swingp.MethodStat; +import com.android.tools.swingp.PaintChildrenMethodStat; +import com.android.tools.swingp.PaintComponentMethodStat; +import com.android.tools.swingp.PaintImmediatelyMethodStat; +import com.android.tools.swingp.RenderStatsManager; +import com.android.tools.swingp.ThreadStat; +import com.android.tools.swingp.WindowPaintMethodStat; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import java.awt.Container; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Point; +import java.awt.Window; +import java.awt.geom.AffineTransform; +import java.lang.ref.SoftReference; +import javax.swing.JLabel; +import javax.swing.JPanel; +import org.junit.Test; + +public class SwingpSerializationTest { + @Test + public void affineTransformCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + AffineTransform transform = new AffineTransform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f); + + assertThat(gson.toJsonTree(transform).getAsJsonArray()).containsExactly( + new JsonPrimitive(1.0f), + new JsonPrimitive(2.0f), + new JsonPrimitive(3.0f), + new JsonPrimitive(4.0f), + new JsonPrimitive(5.0f), + new JsonPrimitive(6.0f)).inOrder(); + } + + @Test + public void pointCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + Point point = new Point(1, 2); + + assertThat(gson.toJsonTree(point).getAsJsonArray()).containsExactly( + new JsonPrimitive(1), + new JsonPrimitive(2)).inOrder(); + } + + @Test + public void softReferenceCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + + String unusedValue = "DUMMY"; + SoftReference<String> setReference = new SoftReference<>(unusedValue); + SoftReference<String> unsetReference = new SoftReference<>(null); + + assertThat(gson.toJsonTree(setReference).getAsJsonPrimitive().getAsString()).isEqualTo("String"); + assertThat(gson.toJsonTree(unsetReference).getAsJsonPrimitive().getAsString()).isEqualTo("<gc>"); + } + + @Test + public void bufferStrategyPaintMethodStatCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + + Object owner = new Object(); + BufferStrategyPaintMethodStat stat = new BufferStrategyPaintMethodStat(owner, true); + stat.endMethod(); + + JsonObject statObj = gson.toJsonTree(stat).getAsJsonObject(); + + assertThat(statObj.getAsJsonPrimitive("classType").getAsString()).isEqualTo("BufferStrategyPaintMethodStat"); + assertThat(statObj.getAsJsonPrimitive("owner").getAsString()).isEqualTo("Object"); + assertThat(statObj.getAsJsonPrimitive("isBufferStrategy").getAsBoolean()).isTrue(); + assertThat(statObj.getAsJsonPrimitive("startTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("endTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonArray("callee")).isEmpty(); + } + + @Test + public void paintChildrenMethodStatCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + + JLabel dummyLabel = new JLabel("DUMMY"); + AffineTransform transform = new AffineTransform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f); + + PaintChildrenMethodStat stat = new PaintChildrenMethodStat(dummyLabel, transform); + stat.endMethod(); + + JsonObject statObj = gson.toJsonTree(stat).getAsJsonObject(); + + assertThat(statObj.getAsJsonPrimitive("classType").getAsString()).isEqualTo("PaintChildrenMethodStat"); + assertThat(statObj.getAsJsonPrimitive("owner").getAsString()).isEqualTo("JLabel"); + assertThat(statObj.getAsJsonPrimitive("startTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("endTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonArray("callee")).isEmpty(); + assertThat(statObj.getAsJsonArray("xform")).containsExactly( + new JsonPrimitive(1.0f), + new JsonPrimitive(2.0f), + new JsonPrimitive(3.0f), + new JsonPrimitive(4.0f), + new JsonPrimitive(5.0f), + new JsonPrimitive(6.0f)).inOrder(); + } + + @Test + public void paintComponentMethodStatCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + + JLabel dummyLabel = new JLabel("DUMMY"); + AffineTransform transform = new AffineTransform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f); + Graphics g = mock(Graphics2D.class); + + PaintComponentMethodStat stat = new PaintComponentMethodStat(dummyLabel, g, transform, -1, -2, 3, 4); + stat.endMethod(); + + JsonObject statObj = gson.toJsonTree(stat).getAsJsonObject(); + + assertThat(statObj.getAsJsonPrimitive("classType").getAsString()).isEqualTo("PaintComponentMethodStat"); + assertThat(statObj.getAsJsonPrimitive("owner").getAsString()).isEqualTo("JLabel"); + assertThat(statObj.getAsJsonPrimitive("startTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("endTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("isImage").getAsBoolean()).isFalse(); + + assertThat(statObj.getAsJsonArray("clip")).containsExactly( + new JsonPrimitive(-1), + new JsonPrimitive(-2), + new JsonPrimitive(3), + new JsonPrimitive(4)).inOrder(); + assertThat(statObj.getAsJsonArray("xform")).containsExactly( + new JsonPrimitive(1.0f), + new JsonPrimitive(2.0f), + new JsonPrimitive(3.0f), + new JsonPrimitive(4.0f), + new JsonPrimitive(5.0f), + new JsonPrimitive(6.0f)).inOrder(); + + assertThat(statObj.getAsJsonArray("callee")).isEmpty(); + } + + @Test + public void paintImmediatelyMethodStatCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + + Object owner = new Object(); + JPanel panel = new JPanel(); + panel.setSize(123, 456); + Graphics2D g = mock(Graphics2D.class); + when(g.getTransform()).thenReturn(new AffineTransform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f)); + + PaintImmediatelyMethodStat stat = new PaintImmediatelyMethodStat(owner, panel, g, -1, -2, 3, 4); + stat.endMethod(); + + JsonObject statObj = gson.toJsonTree(stat).getAsJsonObject(); + + assertThat(statObj.getAsJsonPrimitive("classType").getAsString()).isEqualTo("PaintImmediatelyMethodStat"); + assertThat(statObj.getAsJsonPrimitive("owner").getAsString()).isEqualTo("Object"); + assertThat(statObj.getAsJsonPrimitive("startTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("endTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("bufferType").getAsString()).isEqualTo("JPanel"); + assertThat(statObj.getAsJsonPrimitive("bufferId").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("endTime").getAsLong()).isNotEqualTo(0); + + assertThat(statObj.getAsJsonArray("bounds")).containsExactly( + new JsonPrimitive(-1), + new JsonPrimitive(-2), + new JsonPrimitive(3), + new JsonPrimitive(4) + ); + assertThat(statObj.getAsJsonArray("bufferBounds")).containsExactly( + new JsonPrimitive(0), + new JsonPrimitive(0), + new JsonPrimitive(123), + new JsonPrimitive(456) + ); + assertThat(statObj.getAsJsonArray("constrain")).containsExactly( + new JsonPrimitive(0), + new JsonPrimitive(0)).inOrder(); + assertThat(statObj.getAsJsonArray("xform")).containsExactly( + new JsonPrimitive(1.0f), + new JsonPrimitive(2.0f), + new JsonPrimitive(3.0f), + new JsonPrimitive(4.0f), + new JsonPrimitive(5.0f), + new JsonPrimitive(6.0f)).inOrder(); + + assertThat(statObj.getAsJsonArray("callee")).isEmpty(); + } + + @Test + public void windowPaintMethodStatCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + + Container parent = mock(Container.class); + when(parent.isShowing()).thenReturn(false); + + Graphics2D g = mock(Graphics2D.class); + when(g.getTransform()).thenReturn(new AffineTransform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f)); + + Window window = mock(Window.class); + when(window.getGraphics()).thenReturn(g); + when(window.getParent()).thenReturn(parent); + when(window.getLocationOnScreen()).thenReturn(new Point(123, 456)); + + WindowPaintMethodStat stat = new WindowPaintMethodStat(window); + stat.endMethod(); + + JsonObject statObj = gson.toJsonTree(stat).getAsJsonObject(); + + assertThat(statObj.getAsJsonPrimitive("classType").getAsString()).isEqualTo("WindowPaintMethodStat"); + // Actual name is Window$MockitoMock$<ID>. Would be "Window" in production. + assertThat(statObj.getAsJsonPrimitive("owner").getAsString()).startsWith("Window"); + assertThat(statObj.getAsJsonPrimitive("startTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("endTime").getAsLong()).isNotEqualTo(0); + assertThat(statObj.getAsJsonPrimitive("windowId").getAsLong()).isNotEqualTo(0); + + assertThat(statObj.getAsJsonArray("location")).containsExactly( + new JsonPrimitive(123), + new JsonPrimitive(456)).inOrder(); + + assertThat(statObj.getAsJsonArray("callee")).isEmpty(); + } + + @Test + public void threadStatCanBeSerialized() { + Gson gson = RenderStatsManager.createSwingpGson(); + + Thread thread = mock(Thread.class); + when(thread.getId()).thenReturn((long)123); + thread.setName("FakeThread"); + + ThreadStat stat = new ThreadStat(thread).setIsRecording(true); + stat.pushMethod(new MethodStat(new Object()) { + }); + + JsonObject statObj = gson.toJsonTree(stat).getAsJsonObject(); + + assertThat(statObj.getAsJsonPrimitive("classType").getAsString()).isEqualTo("ThreadStat"); + assertThat(statObj.getAsJsonPrimitive("threadName").getAsString()).isEqualTo("FakeThread"); + assertThat(statObj.getAsJsonPrimitive("threadId").getAsLong()).isEqualTo(123); + + assertThat(statObj.getAsJsonArray("events")).isEmpty(); + } +} |