Skip to content

Commit

Permalink
Close mozilla-mobile#27023: Add capability to override telemetry URL
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketsroger committed Oct 31, 2022
1 parent ef79099 commit 2bb5549
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/src/main/java/org/mozilla/fenix/FenixApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import org.mozilla.fenix.components.metrics.MetricServiceType
import org.mozilla.fenix.components.metrics.MozillaProductDetector
import org.mozilla.fenix.components.toolbar.ToolbarPosition
import org.mozilla.fenix.ext.containsQueryParameters
import org.mozilla.fenix.ext.getCustomGleanServerUrlIfAvailable
import org.mozilla.fenix.ext.ifAvailableUse
import org.mozilla.fenix.ext.isCustomEngine
import org.mozilla.fenix.ext.isKnownSearchDomain
import org.mozilla.fenix.ext.settings
Expand Down Expand Up @@ -154,14 +156,18 @@ open class FenixApplication : LocaleAwareApplication(), Provider {

logger.debug("Initializing Glean (uploadEnabled=$telemetryEnabled})")

// for testing, if custom glean server url is set in the secret menu, use it to initialize Glean
val customEndpoint = getCustomGleanServerUrlIfAvailable(this)
val configuration = Configuration(
channel = BuildConfig.BUILD_TYPE,
httpClient = ConceptFetchHttpUploader(
lazy(LazyThreadSafetyMode.NONE) { components.core.client },
),
)

Glean.initialize(
applicationContext = this,
configuration = Configuration(
channel = BuildConfig.BUILD_TYPE,
httpClient = ConceptFetchHttpUploader(
lazy(LazyThreadSafetyMode.NONE) { components.core.client },
),
),
configuration = configuration ifAvailableUse customEndpoint,
uploadEnabled = telemetryEnabled,
buildInfo = GleanBuildInfo.buildInfo,
)
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/org/mozilla/fenix/ext/Configuration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* 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 org.mozilla.fenix.ext

import android.content.Context
import androidx.preference.PreferenceManager
import mozilla.components.service.glean.config.Configuration
import org.mozilla.fenix.R

/**
* Get custom Glean server URL if available.
*/
fun getCustomGleanServerUrlIfAvailable(context: Context): String? {
return PreferenceManager.getDefaultSharedPreferences(context).getString(
context.getPreferenceKey(R.string.pref_key_custom_glean_server_url),
null,
)
}

/**
* Applies the custom Glean server URL to the Configuration if available.
*/
infix fun Configuration.ifAvailableUse(serverEndpoint: String?): Configuration {
if (!serverEndpoint.isNullOrEmpty()) {
return copy(serverEndpoint = serverEndpoint)
}

return this
}
1 change: 1 addition & 0 deletions app/src/main/res/values/preference_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,5 @@
<string name="pref_key_nimbus_use_preview" translatable="false">pref_key_nimbus_use_preview</string>
<string name="pref_key_history_metadata_feature" translatable="false">pref_key_history_metadata_feature</string>
<string name="pref_key_show_unified_search" translatable="false">pref_key_show_unified_search</string>
<string name="pref_key_custom_glean_server_url" translatable="false">pref_key_custom_glean_server_url</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/static_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<string name="preferences_debug_settings_task_continuity" translatable="false">Enable Task Continuity</string>
<!-- Label for enabling the Unified Search feature -->
<string name="preferences_debug_settings_unified_search" translatable="false">Enable Unified Search (requires restart)</string>
<!-- Label for custom Glean server URL -->
<string name="preferences_debug_settings_custom_glean_server_url" translatable="false">Custom Glean server URL (requires restart)</string>
<!-- Title of preference for sync debugging (only shown in the when the secret debug menu is enabled) -->
<string name="preferences_sync_debug">Sync Debug</string>
<!-- Preference to override the Push server -->
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/secret_settings_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
android:key="@string/pref_key_show_unified_search"
android:title="@string/preferences_debug_settings_unified_search"
app:iconSpaceReserved="false" />
<EditTextPreference
android:key="@string/pref_key_custom_glean_server_url"
android:title="@string/preferences_debug_settings_custom_glean_server_url"
android:inputType="textUri"
app:useSimpleSummaryProvider="true"
app:iconSpaceReserved="false" />
</PreferenceScreen>
34 changes: 34 additions & 0 deletions app/src/test/java/org/mozilla/fenix/ext/ConfigurationKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* 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 org.mozilla.fenix.ext

import io.mockk.mockk
import mozilla.components.service.glean.config.Configuration
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test

class ConfigurationKtTest {
@Test
fun `GIVEN server endpoint is null THEN return the same configuration`() {
val configuration = Configuration(httpClient = mockk())

assertEquals(configuration, configuration.ifAvailableUse(null))
}

@Test
fun `GIVEN server endpoint is not empty THEN make a copy of configuration with server endpoint`() {
val configuration = Configuration(httpClient = mockk())

assertNotEquals(configuration, configuration.ifAvailableUse("test"))
}

@Test
fun `GIVEN server endpoint is empty THEN return the same configuration`() {
val configuration = Configuration(httpClient = mockk())

assertEquals(configuration, configuration.ifAvailableUse(""))
}
}

0 comments on commit 2bb5549

Please sign in to comment.