Skip to content

Instantly share code, notes, and snippets.

@cmathew
cmathew / FrostySoundDataEditor.cs
Created January 3, 2024 07:01
Serialize Frostbite3 SoundWaveAsset chunks into per-channel WAVs
// Call this instead of SoundExportMenuItem_Export
private void SoundExportMenuItem_ExportChannels(IList tracks, String baseFilename)
{
// Assume common metadata, derive with first track
SoundDataTrack firstTrack = (SoundDataTrack)tracks[0];
int channelCount = firstTrack.ChannelCount;
WAV.WAVFormatChunk fmt = new WAV.WAVFormatChunk(WAV.WAVFormatChunk.DataFormats.WAVE_FORMAT_PCM, 1, (uint)firstTrack.SampleRate, (uint)(2 * firstTrack.SampleRate), (ushort)2, 16);
FrostyTaskWindow.Show("Exporting Sound", "", task =>
{
@cmathew
cmathew / AndroidLintJodaExample.kt
Created September 12, 2023 17:19
Example of providing a stubbed dependency to an Android Lint unit test
// Define Joda-Time stub
val JODA_STUB = TestFiles.kotlin(
"""
package org.joda.time
class DateTime(
private val moment: Int,
private val zone: String
) {
companion object {
@cmathew
cmathew / AndroidLintJodaExample.kt
Created September 12, 2023 17:19
Example of providing binary artifacts to an Android Lint unit test
// Reference JAR kept in test-resources folder
val jodaTimeArtifact = TestFiles.bytes("libs/joda-time-2.10.14.jar", javaClass.getResourceAsStream("/joda-time-2.10.14.jar").readBytes())
// Use Joda-Time binary
lint().files(jodaTimeArtifact, ...)
@cmathew
cmathew / ForbidEqualsUsageForBigDecimalsTest.kt
Created September 12, 2023 17:17
Unit test which specifies sample source code and which Android Lint issues(s) we’d like to run
lint()
.files(
kotlin(
"""
package com.wealthfront.lint.test
import java.math.BigDecimal
class MyComparisonTester {
@cmathew
cmathew / WarnLocalDateConversion.kt
Created September 12, 2023 15:25
Android Lint code to remind developers that DateTime.toLocalDate is a lossy operation
class WarnLocalDateConversion : Detector(), SourceCodeScanner {
override fun getApplicableMethodNames(): List<String> = listOf("toLocalDateTime", "toLocalDate")
override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
val receiverClassName = (node.receiverType as PsiClassReferenceType).reference.qualifiedName
if (receiverClassName == "org.joda.time.DateTime") {
context.report(
WARN_LOCAL_DATE_CONVERSION,
node,
context.getLocation(node),
@cmathew
cmathew / WealthfrontRootMatchers.kt
Created May 5, 2021 00:19
Custom Espresso entry points
/** Modified default root matcher at {@link androidx.test.espresso.matcher.RootMatchers.DEFAULT} */
internal val activityRootMatcher: Matcher<Root> = allOf(
hasWindowLayoutParams(),
allOf(
IsSubwindowOfCurrentActivity(),
isFocusable()
)
)
/** Modified default root matcher at {@link androidx.test.espresso.matcher.RootMatchers.DEFAULT} */
@cmathew
cmathew / AndroidManifest.xml
Created May 5, 2021 00:17
Package visibility declaration
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
@cmathew
cmathew / PlayStoreLauncherImpl.kt
Created May 5, 2021 00:14
Launch Play Store without resolveActivity
val playStoreIntent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.wealthfront")).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
try {
context.startActivity(playStoreIntent)
} catch (ex: ActivityNotFoundException) {
goToPlayStoreUrl()
}
@cmathew
cmathew / SampleBottomSheetTest.kt
Last active April 4, 2024 11:25
Outline for an Espresso test that interacts with BottomSheets in a reliable way
@Test
fun clickButtonInsideExpandedBottomSheet() {
val sheetBehavior = activity.getBottomSheetBehavior()
val expandedSheetIdlingResource = BottomSheetStateResource(sheetBehavior, STATE_EXPANDED)
val settledSheetIdlingResource = BottomSheetSettledResource(sheetBehavior)
// Wait for settled state
withBottomSheetResource(settledSheetIdlingResource) {
// Use BottomSheetSetStateAction to expand the Bottom Sheet
}
@cmathew
cmathew / BottomSheetStateResource.kt
Last active May 24, 2021 23:33
IdlingResource which waits for a BottomSheet to enter a particular state.
class BottomSheetStateResource(
bottomSheetBehavior: BottomSheetBehavior<View>,
@BottomSheetBehavior.State private val desiredState: Int
) : BottomSheetResource(bottomSheetBehavior) {
override fun getName(): String {
return "BottomSheet awaiting state: $desiredState"
}
override fun isDesiredState(@BottomSheetBehavior.State state: Int): Boolean {