Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Add back the @jvmoverloads to the Glean SDK public API
Browse files Browse the repository at this point in the history
This additionally adds unit testing in Java to make sure
we're not regressing this again.
  • Loading branch information
Dexterp37 committed Oct 30, 2019
1 parent 27260e3 commit 45ed093
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mozilla.components.service.glean

import android.content.Context
import androidx.annotation.MainThread
import androidx.annotation.VisibleForTesting
import mozilla.components.service.glean.config.Configuration
import mozilla.components.service.glean.private.RecordedExperimentData
Expand All @@ -31,6 +32,8 @@ object Glean {
* as shared preferences
* @param configuration A Glean [Configuration] object with global settings.
*/
@JvmOverloads
@MainThread
fun initialize(
applicationContext: Context,
configuration: Configuration = Configuration()
Expand Down Expand Up @@ -82,6 +85,7 @@ object Glean {
* @param branch The experiment branch (maximum 30 bytes)
* @param extra Optional metadata to output with the ping
*/
@JvmOverloads
fun setExperimentActive(
experimentId: String,
branch: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ import mozilla.telemetry.glean.config.Configuration as GleanCoreConfiguration
* @property maxEvents the number of events to store before the events ping is sent
* @property httpClient The HTTP client implementation to use for uploading pings.
*/
data class Configuration(
val serverEndpoint: String = GleanCoreConfiguration.DEFAULT_TELEMETRY_ENDPOINT,
data class Configuration @JvmOverloads constructor (
val serverEndpoint: String = DEFAULT_TELEMETRY_ENDPOINT,
val channel: String? = null,
val maxEvents: Int? = null,
val httpClient: PingUploader = ConceptFetchHttpUploader(lazy { HttpURLConnectionClient() })
) {
// The following is required to support calling our API from Java.
companion object {
const val DEFAULT_TELEMETRY_ENDPOINT = GleanCoreConfiguration.DEFAULT_TELEMETRY_ENDPOINT
}

/**
* Convert the Android Components configuration object to the Glean SDK
* configuration object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.glean;

import androidx.test.core.app.ApplicationProvider;

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

import java.util.HashMap;
import java.util.Map;

import mozilla.components.service.glean.config.Configuration;

@RunWith(RobolectricTestRunner.class)
public class GleanFromJavaTest {
// The only purpose of these tests is to make sure the Glean API is
// callable from Java. If something goes wrong, it should complain about missing
// methods at build-time.

@Test
public void testInitGleanWithDefaults() {
Glean.INSTANCE.initialize(ApplicationProvider.getApplicationContext());
}

@Test
public void testInitGleanWithConfiguration() {
Configuration config =
new Configuration(Configuration.DEFAULT_TELEMETRY_ENDPOINT, "test-channel");
Glean.INSTANCE.initialize(ApplicationProvider.getApplicationContext(), config);
}

@Test
public void testGleanExperimentsAPIWithDefaults() {
Glean.INSTANCE.setExperimentActive("test-exp-id-1", "test-branch-1");
}

@Test
public void testGleanExperimentsAPIWithOptional() {
Map<String, String> experimentProperties = new HashMap<>();
experimentProperties.put("test-prop1", "test-prop-result1");

Glean.INSTANCE.setExperimentActive(
"test-exp-id-1",
"test-branch-1",
experimentProperties
);
}
}

0 comments on commit 45ed093

Please sign in to comment.