Bug 1566367 - [4.0] Add test for onTelemetryReceived. r=chutten,Dexter,snorp

Differential Revision: https://phabricator.services.mozilla.com/D40774

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Agi Sferro 2019-08-09 04:51:05 +00:00
Родитель 5efb6dc130
Коммит a2f05cba15
8 изменённых файлов: 151 добавлений и 8 удалений

Просмотреть файл

@ -52,6 +52,17 @@ port.onMessage.addListener(async message => {
sendResponse(id, browser.test.getRequestedLocales()); sendResponse(id, browser.test.getRequestedLocales());
} }
break; break;
case "AddHistogram":
{
const {
id,
args: { id: histogramId, value },
} = message;
browser.test.addHistogram(histogramId, value);
sendResponse(id, null);
}
break;
} }
}); });

Просмотреть файл

@ -104,6 +104,10 @@ this.test = class extends ExtensionAPI {
async getRequestedLocales() { async getRequestedLocales() {
return Services.locale.requestedLocales; return Services.locale.requestedLocales;
}, },
async addHistogram(id, value) {
return Services.telemetry.getHistogramById(id).add(value);
},
}, },
}; };
} }

Просмотреть файл

@ -72,6 +72,22 @@
"async": true, "async": true,
"description": "Gets the requested locales.", "description": "Gets the requested locales.",
"parameters": [] "parameters": []
},
{
"name": "addHistogram",
"type": "function",
"async": true,
"description": "Add a sample with the given value to the histogram with the given id.",
"parameters": [
{
"type": "string",
"name": "id"
},
{
"type": "any",
"name": "value"
}
]
} }
] ]
} }

Просмотреть файл

@ -27,6 +27,7 @@ import org.hamcrest.Matchers.*
import org.json.JSONObject import org.json.JSONObject
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Ignore
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule import org.mozilla.geckoview.test.rule.GeckoSessionTestRule
@ -583,6 +584,7 @@ class NavigationDelegateTest : BaseSessionTest() {
innerWidth, closeTo(mobileInnerWidth, 0.1)) innerWidth, closeTo(mobileInnerWidth, 0.1))
} }
@Ignore // This test needs to set RuntimeSettings, TODO: Bug 1572245
@Test fun telemetrySnapshots() { @Test fun telemetrySnapshots() {
sessionRule.session.loadTestPath(HELLO_HTML_PATH) sessionRule.session.loadTestPath(HELLO_HTML_PATH)
sessionRule.waitForPageStop() sessionRule.waitForPageStop()

Просмотреть файл

@ -0,0 +1,56 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
package org.mozilla.geckoview.test
import android.support.test.filters.MediumTest
import android.support.test.runner.AndroidJUnit4
import android.util.Log
import org.hamcrest.CoreMatchers.equalTo
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.geckoview.GeckoResult
import org.mozilla.geckoview.RuntimeTelemetry
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.AssertCalled
@RunWith(AndroidJUnit4::class)
@MediumTest
class TelemetryTest : BaseSessionTest() {
@Test
fun testOnTelemetryReceived() {
sessionRule.addExternalDelegateUntilTestEnd(
RuntimeTelemetry.Delegate::class,
sessionRule::setTelemetryDelegate,
{ sessionRule.setTelemetryDelegate(null) },
object : RuntimeTelemetry.Delegate {}
)
// Let's make sure we batch the two telemetry calls
sessionRule.setPrefsUntilTestEnd(
mapOf("toolkit.telemetry.geckoview.batchDurationMS" to 100000))
sessionRule.addHistogram("TELEMETRY_TEST_STREAMING", 401)
sessionRule.addHistogram("TELEMETRY_TEST_STREAMING", 12)
sessionRule.addHistogram("TELEMETRY_TEST_STREAMING", 1)
sessionRule.addHistogram("TELEMETRY_TEST_STREAMING", 109)
// Forces flushing telemetry data at next histogram
sessionRule.setPrefsUntilTestEnd(mapOf("toolkit.telemetry.geckoview.batchDurationMS" to 0))
val telemetryReceived = GeckoResult<Void>()
sessionRule.delegateDuringNextWait(object : RuntimeTelemetry.Delegate {
@AssertCalled
override fun onTelemetryReceived(metric: RuntimeTelemetry.Metric) {
assertThat("Metric name should be correct", metric.name,
equalTo("TELEMETRY_TEST_STREAMING"))
assertThat("Metric name should be correct", metric.values,
equalTo(longArrayOf(401, 12, 1, 109, 2000)))
telemetryReceived.complete(null)
}
})
sessionRule.addHistogram("TELEMETRY_TEST_STREAMING", 2000)
sessionRule.waitForResult(telemetryReceived)
}
}

Просмотреть файл

@ -16,6 +16,7 @@ import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime; import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession; import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoSessionSettings; import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.geckoview.RuntimeTelemetry;
import org.mozilla.geckoview.SessionTextInput; import org.mozilla.geckoview.SessionTextInput;
import org.mozilla.geckoview.WebExtension; import org.mozilla.geckoview.WebExtension;
import org.mozilla.geckoview.test.util.HttpBin; import org.mozilla.geckoview.test.util.HttpBin;
@ -551,7 +552,7 @@ public class GeckoSessionTestRule implements TestRule {
public void delegate(final @Nullable GeckoSession session, public void delegate(final @Nullable GeckoSession session,
final @NonNull Object callback) { final @NonNull Object callback) {
for (final Class<?> ifce : DEFAULT_DELEGATES) { for (final Class<?> ifce : mAllDelegates) {
if (!ifce.isInstance(callback)) { if (!ifce.isInstance(callback)) {
continue; continue;
} }
@ -859,6 +860,10 @@ public class GeckoSessionTestRule implements TestRule {
return RuntimeCreator.getRuntime(); return RuntimeCreator.getRuntime();
} }
public void setTelemetryDelegate(RuntimeTelemetry.Delegate delegate) {
RuntimeCreator.setTelemetryDelegate(delegate);
}
public @Nullable GeckoDisplay getDisplay() { public @Nullable GeckoDisplay getDisplay() {
return mDisplay; return mDisplay;
} }
@ -1221,6 +1226,7 @@ public class GeckoSessionTestRule implements TestRule {
mLastWaitStart = 0; mLastWaitStart = 0;
mLastWaitEnd = 0; mLastWaitEnd = 0;
mTimeoutMillis = 0; mTimeoutMillis = 0;
RuntimeCreator.setTelemetryDelegate(null);
} }
@Override @Override
@ -2036,6 +2042,24 @@ public class GeckoSessionTestRule implements TestRule {
} }
} }
/**
* Adds value to the given histogram.
*
* @param id the histogram id to increment.
* @param value to add to the histogram.
*/
public void addHistogram(final String id, final long value) {
try {
final JSONObject args = new JSONObject();
args.put("id", id);
args.put("value", value);
webExtensionApiCall("AddHistogram", args);
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
}
private Object webExtensionApiCall(final String apiName, JSONObject args) throws JSONException { private Object webExtensionApiCall(final String apiName, JSONObject args) throws JSONException {
// Ensure background script is connected // Ensure background script is connected
UiThreadUtils.waitForCondition(() -> RuntimeCreator.backgroundPort() != null, UiThreadUtils.waitForCondition(() -> RuntimeCreator.backgroundPort() != null,

Просмотреть файл

@ -3,6 +3,7 @@ package org.mozilla.geckoview.test.util;
import org.mozilla.geckoview.GeckoResult; import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime; import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoRuntimeSettings; import org.mozilla.geckoview.GeckoRuntimeSettings;
import org.mozilla.geckoview.RuntimeTelemetry;
import org.mozilla.geckoview.WebExtension; import org.mozilla.geckoview.WebExtension;
import org.mozilla.geckoview.test.TestCrashHandler; import org.mozilla.geckoview.test.TestCrashHandler;
@ -28,6 +29,22 @@ public class RuntimeCreator {
"test-support@mozilla.com", "test-support@mozilla.com",
WebExtension.Flags.ALLOW_CONTENT_MESSAGING); WebExtension.Flags.ALLOW_CONTENT_MESSAGING);
// The RuntimeTelemetry.Delegate can only be set when creating the RuntimeCreator, to
// let tests set their own Delegate we need to create a proxy here.
public static class RuntimeTelemetryDelegate implements RuntimeTelemetry.Delegate {
public RuntimeTelemetry.Delegate delegate = null;
@Override
public void onTelemetryReceived(@NonNull RuntimeTelemetry.Metric metric) {
if (delegate != null) {
delegate.onTelemetryReceived(metric);
}
}
}
public static final RuntimeTelemetryDelegate sRuntimeTelemetryProxy =
new RuntimeTelemetryDelegate();
private static WebExtension.Port sBackgroundPort; private static WebExtension.Port sBackgroundPort;
private static WebExtension.PortDelegate sPortDelegate; private static WebExtension.PortDelegate sPortDelegate;
@ -66,6 +83,17 @@ public class RuntimeCreator {
}); });
} }
/**
* Set the {@link RuntimeTelemetry.Delegate} instance for this test. Application code can only
* register this delegate when the {@link GeckoRuntime} is created, so we need to proxy it
* for test code.
*
* @param delegate the {@link RuntimeTelemetry.Delegate} for this test run.
*/
public static void setTelemetryDelegate(RuntimeTelemetry.Delegate delegate) {
sRuntimeTelemetryProxy.delegate = delegate;
}
public static void setPortDelegate(WebExtension.PortDelegate portDelegate) { public static void setPortDelegate(WebExtension.PortDelegate portDelegate) {
sPortDelegate = portDelegate; sPortDelegate = portDelegate;
} }
@ -76,19 +104,20 @@ public class RuntimeCreator {
return sRuntime; return sRuntime;
} }
final GeckoRuntimeSettings.Builder runtimeSettingsBuilder = TEST_SUPPORT_WEB_EXTENSION.setMessageDelegate(sMessageDelegate, "browser");
new GeckoRuntimeSettings.Builder();
runtimeSettingsBuilder.arguments(new String[]{"-purgecaches"}) final GeckoRuntimeSettings runtimeSettings = new GeckoRuntimeSettings.Builder()
.arguments(new String[]{"-purgecaches"})
.extras(InstrumentationRegistry.getArguments()) .extras(InstrumentationRegistry.getArguments())
.remoteDebuggingEnabled(true) .remoteDebuggingEnabled(true)
.consoleOutput(true) .consoleOutput(true)
.crashHandler(TestCrashHandler.class); .crashHandler(TestCrashHandler.class)
.telemetryDelegate(sRuntimeTelemetryProxy)
TEST_SUPPORT_WEB_EXTENSION.setMessageDelegate(sMessageDelegate, "browser"); .build();
sRuntime = GeckoRuntime.create( sRuntime = GeckoRuntime.create(
InstrumentationRegistry.getTargetContext(), InstrumentationRegistry.getTargetContext(),
runtimeSettingsBuilder.build()); runtimeSettings);
registerTestSupport(); registerTestSupport();

Просмотреть файл

@ -8925,6 +8925,7 @@
"low": 1, "low": 1,
"high": 2147483646, "high": 2147483646,
"n_buckets": 10, "n_buckets": 10,
"releaseChannelCollection": "opt-out",
"bug_numbers": [1566366], "bug_numbers": [1566366],
"description": "a testing histogram; not meant to be touched" "description": "a testing histogram; not meant to be touched"
}, },