Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions oss-licenses-plugin/testapp/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ androidComponents {
// We explicitly enable them for all variants to ensure both Debug and Release coverage.
variantBuilder.hostTests[HostTestBuilder.UNIT_TEST_TYPE]?.enable = true
}

onVariants { variant ->
val generateTask = tasks.register<GenerateVersionTask>("generateVersionResource_${variant.name}") {
libraryDependenciesReport.set(variant.artifacts.get(com.android.build.api.artifact.SingleArtifact.METADATA_LIBRARY_DEPENDENCIES_REPORT))
outputDir.set(layout.buildDirectory.dir("generated/oss_res/${variant.name}"))
}

variant.sources.res?.addGeneratedSourceDirectory(
generateTask,
GenerateVersionTask::outputDir
)
}
}

kotlin {
Expand Down Expand Up @@ -106,3 +118,38 @@ dependencies {
testImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.test.manifest)
}

abstract class GenerateVersionTask : DefaultTask() {
@get:org.gradle.api.tasks.InputFile
@get:org.gradle.api.tasks.Optional
abstract val libraryDependenciesReport: org.gradle.api.file.RegularFileProperty

@get:org.gradle.api.tasks.OutputDirectory
abstract val outputDir: org.gradle.api.file.DirectoryProperty

@org.gradle.api.tasks.TaskAction
fun doAction() {
val versionFile = outputDir.get().file("raw/version.txt").asFile
versionFile.parentFile.mkdirs()

if (!libraryDependenciesReport.isPresent) {
versionFile.writeText("UNKNOWN")
return
}

val reportFile = libraryDependenciesReport.get().asFile
val appDependencies = reportFile.inputStream().use {
com.android.tools.build.libraries.metadata.AppDependencies.parseFrom(it)
}

val ossLicensesLibrary = appDependencies.libraryList.find {
it.libraryOneofCase.name == "MAVEN_LIBRARY" &&
it.mavenLibrary.groupId == "com.google.android.gms" &&
it.mavenLibrary.artifactId == "play-services-oss-licenses"
}

val version = ossLicensesLibrary?.mavenLibrary?.version ?: "UNKNOWN"
versionFile.writeText(version)
println("Generated version.txt with version: $version")
}
}
13 changes: 12 additions & 1 deletion oss-licenses-plugin/testapp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:label="OSS Licenses Test App"
Expand All @@ -13,5 +14,15 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.oss.licenses.v2.OssLicensesMenuActivity"
android:theme="@style/Theme.CustomOssThemeV2"
tools:replace="android:theme"
android:exported="true" />
<activity
android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity"
android:theme="@style/Theme.CustomOssTheme"
tools:replace="android:theme"
android:exported="true" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.material3.lightColorScheme
import androidx.compose.ui.graphics.Color
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity as V1Activity
import com.google.android.gms.oss.licenses.v2.OssLicensesMenuActivity as V2Activity

Expand All @@ -44,15 +46,47 @@ class MainActivity : ComponentActivity() {
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Text("Library Version: ${getLibraryVersion()}")
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = { startActivity(Intent(this@MainActivity, V1Activity::class.java)) }) {
Text("Launch V1 Licenses")
Text("Launch V1 (XML Theme)")
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
val customColors = lightColorScheme(
background = Color(0xFFE0F7FA), // Light Cyan
surface = Color(0xFFE0F7FA), // Light Cyan for TopAppBar
onBackground = Color.Black,
onSurface = Color.Black
)
V2Activity.setTheme(customColors, customColors, null)
V2Activity.setActivityTitle("Custom Title from App")
Comment thread
timothyfroehlich marked this conversation as resolved.
startActivity(Intent(this@MainActivity, V2Activity::class.java))
}) {
Text("Launch V2 (Compose Theme)")
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = { startActivity(Intent(this@MainActivity, V2Activity::class.java)) }) {
Text("Launch V2 Licenses")
Button(onClick = {
V2Activity.setTheme(null, null, null)
V2Activity.setActivityTitle("Title from XML Theme")
startActivity(Intent(this@MainActivity, V2Activity::class.java))
}) {
Text("Launch V2 (XML Theme)")
}
}
}
}
}

private fun getLibraryVersion(): String {
val id = resources.getIdentifier("version", "raw", packageName)
if (id == 0) return "UNKNOWN"
return try {
resources.openRawResource(id).use { inputStream ->
inputStream.bufferedReader().use { it.readText() }
}
} catch (e: Exception) {
"UNKNOWN"
}
}
}
13 changes: 13 additions & 0 deletions oss-licenses-plugin/testapp/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.CustomOssTheme" parent="Theme.AppCompat.Light">
<item name="android:windowBackground">#E0F7FA</item> <!-- Light Cyan -->
<item name="colorPrimary">#E0F7FA</item> <!-- Light Cyan for Action Bar -->
<item name="colorPrimaryDark">#B2EBF2</item> <!-- Darker Cyan for Status Bar if needed -->
</style>
<style name="Theme.CustomOssThemeV2" parent="Theme.AppCompat.Light">
<item name="android:windowBackground">#FCE4EC</item> <!-- Light Pink -->
<item name="colorPrimary">#FCE4EC</item>
<item name="colorPrimaryDark">#F8BBD0</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2026 Google LLC
*
* 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
*
* https://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.google.android.gms.oss.licenses.testapp

import android.content.Intent
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.android.gms.oss.licenses.v2.OssLicensesMenuActivity
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class OssLicensesV2CustomTitleTest {

@get:Rule val composeTestRule = createEmptyComposeRule()

@Test
fun testV2ActivityCustomTitleViaIntent() {
val customTitle = "My Custom Licenses Title"
val intent =
Intent(ApplicationProvider.getApplicationContext(), OssLicensesMenuActivity::class.java)
.apply { putExtra("title", customTitle) }

ActivityScenario.launch<OssLicensesMenuActivity>(intent).use {
composeTestRule.onNodeWithText(customTitle).assertExists()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import androidx.compose.ui.test.performClick
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import android.graphics.Color
import android.util.TypedValue
import com.google.android.gms.oss.licenses.v2.OssLicensesMenuActivity
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Ignore
import org.junit.Rule
Expand Down Expand Up @@ -83,16 +86,35 @@ class OssLicensesV2Test {
}
}



@Test
fun testV2ActivityCustomTitleViaIntent() {
val customTitle = "My Custom Licenses Title"
val intent =
Intent(ApplicationProvider.getApplicationContext(), OssLicensesMenuActivity::class.java)
.apply { putExtra("title", customTitle) }

ActivityScenario.launch<OssLicensesMenuActivity>(intent).use {
// The v2 library does not update activity.title, it only displays it in the Compose UI.
fun testV2ActivitySetActivityTitle() {
val customTitle = "Test Title via API"
OssLicensesMenuActivity.setActivityTitle(customTitle)

ActivityScenario.launch(OssLicensesMenuActivity::class.java).use {
composeTestRule.onNodeWithText(customTitle).assertExists()
}
}

@Test
fun testV2ActivityUsesXmlThemeFallback() {
// Ensure no programmatic theme is set
OssLicensesMenuActivity.setTheme(null, null, null)

ActivityScenario.launch(OssLicensesMenuActivity::class.java).use { scenario ->
scenario.onActivity { activity ->
val typedValue = TypedValue()
val theme = activity.theme
val success = theme.resolveAttribute(android.R.attr.windowBackground, typedValue, true)

assertTrue("Failed to resolve windowBackground attribute", success)

// The expected color is #FCE4EC (Light Pink) defined in Theme.CustomOssThemeV2
val expectedColor = Color.parseColor("#FCE4EC")
assertEquals("Theme background color mismatch", expectedColor, typedValue.data)
}
}
}
}
2 changes: 1 addition & 1 deletion oss-licenses-plugin/testapp/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ org.gradle.configuration-cache.problems=fail
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8

# Plugin specific flags
# This property silences warnings about vulnerable protobuf generated types
# This property silences warnings about vulnerable protobuf generated types
# in older versions of AGP/Gradle.
com.google.protobuf.use_unsafe_pre22_gencode=true

Expand Down
2 changes: 1 addition & 1 deletion oss-licenses-plugin/testapp/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ junit = "4.13.2"
kotlin = "2.1.10"
minSdk = "24"
oss-licenses-plugin = "+"
oss-licenses-library = "17.4.0"
oss-licenses-library = "+" # Always use the latest available
robolectric = "4.16.1"
targetSdk = "36"

Expand Down
11 changes: 8 additions & 3 deletions oss-licenses-plugin/testapp/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,24 @@ plugins {
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
// Allow overriding the 'play-services-oss-licenses' runtime library with a local version.
// Usage: ./gradlew :app:test -PlibraryRepoPath=/path/to/your/mavenrepo
// // Allow overriding the 'play-services-oss-licenses' runtime library with a local version.
// // Usage: ./gradlew :app:test -PlibraryRepoPath=/path/to/your/mavenrepo
val libraryRepo = providers.gradleProperty("libraryRepoPath").orNull
if (libraryRepo != null) {
println("Registering libraryRepoPath: $libraryRepo")
exclusiveContent {
forRepository { maven { url = uri(libraryRepo) } }
forRepository {
maven {
url = uri(file(libraryRepo))
}
}
filter {
includeModule("com.google.android.gms", "play-services-oss-licenses")
}
}
}


google()
mavenCentral()
}
Expand Down
Loading