summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/module/telemetry/PerformanceEventTest.java
blob: 6f256b4e564ad2ca0e47c77fc8607c677958bdf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.mapbox.mapboxsdk.module.telemetry;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.support.test.runner.AndroidJUnit4;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mapbox.mapboxsdk.Mapbox;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)
public class PerformanceEventTest {

  @Test
  public void checksPerformanceEventWithMetaData() throws Exception {
    PerformanceEvent event = obtainPerformanceEvent();
    assertNotNull(event);

    Parcel parcel = Parcel.obtain();

    event.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);

    PerformanceEvent newPerfEvent = PerformanceEvent.CREATOR.createFromParcel(parcel);
    assertNotNull(newPerfEvent);

    compare(event, newPerfEvent, "attributes", "style_id");
    compare(event, newPerfEvent, "counters", "int_value");
    compare(event, newPerfEvent, "counters", "long_value");
    compare(event, newPerfEvent, "counters", "double_value");
    assertEquals(getMetadata(event), getMetadata(newPerfEvent));
  }

  @Test
  public void checksPerformanceEventOnlyRequiredData() throws Exception {
    Bundle bundle = new Bundle();
    bundle.putString("property ignored", "value will be ignored");
    PerformanceEvent event = new PerformanceEvent(UUID.randomUUID().toString(), bundle);
    assertNotNull(event);

    Parcel parcel = Parcel.obtain();
    event.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);

    PerformanceEvent newPerfEvent = PerformanceEvent.CREATOR.createFromParcel(parcel);
    assertNotNull(newPerfEvent);

    assertEquals(getAttributes(event).size(), 0);
    assertEquals(getCounters(event).size(), 0);
    assertEquals(getAttributes(newPerfEvent).size(), 0);
    assertEquals(getCounters(newPerfEvent).size(), 0);
    assertEquals(getMetadata(event), getMetadata(newPerfEvent));
  }


  private PerformanceEvent obtainPerformanceEvent() {
    String styleStr = "mapbox://styles/mapbox/streets-v11";
    boolean testPerfEvent = true;
    Double doubleValue = 40.5;
    Long longValue = 40L;
    Integer intValue = 40;

    List<Attribute<String>> attributes = new ArrayList<>();
    attributes.add(
            new Attribute<>("style_id", styleStr));
    attributes.add(
            new Attribute<>("test_perf_event", String.valueOf(testPerfEvent)));

    List<Attribute<? extends Number>> counters = new ArrayList();
    counters.add(new Attribute<>("long_value", longValue));
    counters.add(new Attribute<>("double_value", doubleValue));
    counters.add(new Attribute<>("int_value", intValue));

    Gson gson = new Gson();

    Bundle bundle = new Bundle();
    bundle.putString("attributes", gson.toJson(attributes));
    bundle.putString("counters", gson.toJson(counters));

    JsonObject metaData = new JsonObject();
    metaData.addProperty("os", "android");
    metaData.addProperty("manufacturer", Build.MANUFACTURER);
    metaData.addProperty("brand", Build.BRAND);
    metaData.addProperty("device", Build.MODEL);
    metaData.addProperty("version", Build.VERSION.RELEASE);
    metaData.addProperty("abi", Build.CPU_ABI);
    metaData.addProperty("country", Locale.getDefault().getISO3Country());
    metaData.addProperty("ram", getRam());
    metaData.addProperty("screenSize", getWindowSize());
    bundle.putString("metadata", metaData.toString());

    return new PerformanceEvent(UUID.randomUUID().toString(), bundle);
  }

  private void compare(PerformanceEvent event1, PerformanceEvent event2, String listFieldName, String name)
          throws NoSuchFieldException, IllegalAccessException {
    Object value1 = getValue(event1, listFieldName, name);
    Object value2  = getValue(event2, listFieldName, name);

    if (value1 instanceof Double && value2 instanceof Double) {
      assertEquals((Double)value1, (Double)value2, 0.00006);
    } else {
      assertEquals(value1, value2);
    }
  }

  private Object getPrivateFieldValue(Object object, String fieldName)
    throws NoSuchFieldException, IllegalAccessException {
    Field field = object.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(object);
  }

  private Object getValue(PerformanceEvent event, String listFieldName, String name)
    throws NoSuchFieldException, IllegalAccessException {
    ArrayList list = (ArrayList)getPrivateFieldValue(event, listFieldName);
    for (Object element : list) {
      Object elementName = getPrivateFieldValue(element, "name");
      if (elementName != null && elementName.equals((String)name)) {
        return getPrivateFieldValue(element, "value");
      }
    }
    return null;
  }

  private JsonObject getMetadata(PerformanceEvent event)
          throws NoSuchFieldException, IllegalAccessException {
    return (JsonObject)getPrivateFieldValue(event, "metadata");
  }

  private ArrayList getAttributes(PerformanceEvent event)
          throws NoSuchFieldException, IllegalAccessException {
    return (ArrayList)getPrivateFieldValue(event, "attributes");
  }

  private ArrayList getCounters(PerformanceEvent event)
          throws NoSuchFieldException, IllegalAccessException {
    return (ArrayList)getPrivateFieldValue(event, "counters");
  }

  private static String getRam() {
    ActivityManager actManager =
            (ActivityManager) Mapbox.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
    actManager.getMemoryInfo(memInfo);
    return String.valueOf(memInfo.totalMem);
  }

  private static String getWindowSize() {
    WindowManager windowManager =
            (WindowManager) Mapbox.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    return "{" + width + "," + height + "}";
  }

  private class Attribute<T> {
    private String name;
    private T value;

    Attribute(String name, T value) {
      this.name = name;
      this.value = value;
    }
  }
}