Skip to content

Commit 0797c47

Browse files
committed
Hook screenshot trigger to test app menu
1 parent 831385b commit 0797c47

File tree

5 files changed

+218
-75
lines changed

5 files changed

+218
-75
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,10 @@ public void startFeedback(@NonNull CharSequence infoText) {
332332
taskExecutor,
333333
e -> {
334334
LogWrapper.getInstance().w("Failed to take screenshot for feedback", e);
335-
startFeedback(infoText, null);
335+
doStartFeedback(infoText, null);
336336
})
337337
.addOnSuccessListener(
338-
taskExecutor, screenshotUri -> startFeedback(infoText, screenshotUri));
338+
taskExecutor, screenshotUri -> doStartFeedback(infoText, screenshotUri));
339339
}
340340

341341
@Override
@@ -345,6 +345,15 @@ public void startFeedback(@NonNull int infoTextResourceId, @Nullable Uri screens
345345

346346
@Override
347347
public void startFeedback(@NonNull CharSequence infoText, @Nullable Uri screenshotUri) {
348+
if (!feedbackInProgress.compareAndSet(/* expect= */ false, /* update= */ true)) {
349+
LogWrapper.getInstance()
350+
.i("Ignoring startFeedback() call because feedback is already in progress");
351+
return;
352+
}
353+
doStartFeedback(infoText, screenshotUri);
354+
}
355+
356+
private void doStartFeedback(CharSequence infoText, @Nullable Uri screenshotUri) {
348357
testerSignInManager
349358
.signInTester()
350359
.addOnFailureListener(

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionServiceImplTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import static org.mockito.Mockito.spy;
4444
import static org.mockito.Mockito.times;
4545
import static org.mockito.Mockito.verify;
46+
import static org.mockito.Mockito.verifyNoInteractions;
4647
import static org.mockito.Mockito.when;
4748
import static org.robolectric.Shadows.shadowOf;
4849

@@ -668,7 +669,7 @@ public void startFeedback_signsInTesterAndStartsActivity() throws InterruptedExc
668669
}
669670

670671
@Test
671-
public void startFeedback_calledMultipleTimes_onlyStartsOnce() throws InterruptedException {
672+
public void startFeedback_withoutUri_onlyStartsOnce() throws InterruptedException {
672673
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));
673674

674675
firebaseAppDistribution.startFeedback("Some terms and conditions");
@@ -678,6 +679,37 @@ public void startFeedback_calledMultipleTimes_onlyStartsOnce() throws Interrupte
678679
verify(activity, times(1)).startActivity(any());
679680
}
680681

682+
@Test
683+
public void startFeedback_withUri_doesNotTakeScreenshot() throws InterruptedException {
684+
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));
685+
Uri providedUri = Uri.parse("file:/provided/uri");
686+
firebaseAppDistribution.startFeedback("Some terms and conditions", providedUri);
687+
TestUtils.awaitAsyncOperations(taskExecutor);
688+
689+
verifyNoInteractions(mockScreenshotTaker);
690+
verify(mockTesterSignInManager).signInTester();
691+
Intent expectedIntent = new Intent(activity, FeedbackActivity.class);
692+
Intent actualIntent = shadowOf(RuntimeEnvironment.getApplication()).getNextStartedActivity();
693+
assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());
694+
assertThat(actualIntent.getStringExtra(RELEASE_NAME_EXTRA_KEY)).isEqualTo("release-name");
695+
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_EXTRA_KEY))
696+
.isEqualTo(providedUri.toString());
697+
assertThat(actualIntent.getStringExtra(INFO_TEXT_EXTRA_KEY))
698+
.isEqualTo("Some terms and conditions");
699+
assertThat(firebaseAppDistribution.isFeedbackInProgress()).isTrue();
700+
}
701+
702+
@Test
703+
public void startFeedback_withUri_onlyStartsOnce() throws InterruptedException {
704+
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));
705+
706+
firebaseAppDistribution.startFeedback("Some terms and conditions", TEST_SCREENSHOT_URI);
707+
firebaseAppDistribution.startFeedback("Some other terms and conditions", TEST_SCREENSHOT_URI);
708+
TestUtils.awaitAsyncOperations(taskExecutor);
709+
710+
verify(activity, times(1)).startActivity(any());
711+
}
712+
681713
@Test
682714
public void startFeedback_closingActivity_setsInProgressToFalse() throws InterruptedException {
683715
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));

firebase-appdistribution/test-app/src/main/java/com/googletest/firebase/appdistribution/testapp/AppDistroTestApplication.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ class AppDistroTestApplication : Application() {
66
override fun onCreate() {
77
super.onCreate()
88

9-
// Default feedback triggers can also be initialized here
9+
// Perform any required trigger initialization here
10+
ScreenshotDetectionFeedbackTrigger.initialize(this, R.string.terms_and_conditions);
11+
12+
// Default feedback triggers can optionally be enabled application-wide here
1013
// ShakeForFeedback.enable(this)
14+
// ScreenshotDetectionFeedbackTrigger.enable()
1115
}
1216
}

firebase-appdistribution/test-app/src/main/java/com/googletest/firebase/appdistribution/testapp/MainActivity.kt

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.app.AlertDialog
44
import android.content.Intent
55
import android.os.Build
66
import android.os.Bundle
7-
import android.os.Handler
8-
import android.os.HandlerThread
97
import android.util.Log
108
import android.view.Menu
119
import android.view.MenuInflater
@@ -49,9 +47,6 @@ class MainActivity : AppCompatActivity() {
4947
lateinit var progressBar: ProgressBar
5048
lateinit var feedbackTriggerMenu: TextInputLayout
5149

52-
private lateinit var screenshotTriggerThread: HandlerThread
53-
private lateinit var screenshotTrigger: ScreenshotDetectionFeedbackTrigger
54-
5550
override fun onCreate(savedInstanceState: Bundle?) {
5651
super.onCreate(savedInstanceState)
5752
setContentView(R.layout.activity_main)
@@ -71,18 +66,13 @@ class MainActivity : AppCompatActivity() {
7166
signInStatus = findViewById(R.id.sign_in_status)
7267
progressBar = findViewById(R.id.progress_bar)
7368

74-
screenshotTriggerThread = HandlerThread("AppDistroFeedbackTrigger")
75-
screenshotTriggerThread.start()
76-
screenshotTrigger =
77-
ScreenshotDetectionFeedbackTrigger(
78-
this,
79-
R.string.terms_and_conditions,
80-
Handler(screenshotTriggerThread.looper)
81-
)
82-
8369
// Set up feedback trigger menu
8470
feedbackTriggerMenu = findViewById(R.id.feedbackTriggerMenu)
85-
val items = listOf(FeedbackTrigger.NONE.label, FeedbackTrigger.SHAKE.label)
71+
val items = listOf(
72+
FeedbackTrigger.NONE.label,
73+
FeedbackTrigger.SHAKE.label,
74+
FeedbackTrigger.SCREENSHOT.label
75+
)
8676
val adapter = ArrayAdapter(this, R.layout.list_item, items)
8777
val autoCompleteTextView = feedbackTriggerMenu.editText!! as AutoCompleteTextView
8878
autoCompleteTextView.setAdapter(adapter)
@@ -91,17 +81,28 @@ class MainActivity : AppCompatActivity() {
9181
// TODO: support enabling/disabling other triggers
9282
when(text.toString()) {
9383
FeedbackTrigger.NONE.label -> {
94-
Log.i(TAG, "Disabling shake")
95-
ShakeForFeedback.disable(application)
84+
disableAllFeedbackTriggers()
9685
}
9786
FeedbackTrigger.SHAKE.label -> {
98-
Log.i(TAG, "Enabling shake")
87+
disableAllFeedbackTriggers()
88+
Log.i(TAG, "Enabling shake for feedback trigger")
9989
ShakeForFeedback.enable(application, this)
10090
}
91+
FeedbackTrigger.SCREENSHOT.label -> {
92+
disableAllFeedbackTriggers()
93+
Log.i(TAG, "Enabling screenshot detection trigger")
94+
ScreenshotDetectionFeedbackTrigger.enable()
95+
}
10196
}
10297
}
10398
}
10499

100+
private fun disableAllFeedbackTriggers() {
101+
Log.i(TAG, "Disabling all feedback triggers")
102+
ShakeForFeedback.disable(application)
103+
ScreenshotDetectionFeedbackTrigger.disable()
104+
}
105+
105106
override fun onCreateOptionsMenu(menu: Menu): Boolean {
106107
val inflater: MenuInflater = menuInflater
107108
inflater.inflate(R.menu.action_menu, menu)
@@ -118,19 +119,8 @@ class MainActivity : AppCompatActivity() {
118119
}
119120
}
120121

121-
override fun onDestroy() {
122-
super.onDestroy()
123-
screenshotTriggerThread.quitSafely()
124-
}
125-
126-
override fun onPause() {
127-
super.onPause()
128-
screenshotTrigger.unRegisterScreenshotObserver()
129-
}
130-
131122
override fun onResume() {
132123
super.onResume()
133-
screenshotTrigger.registerScreenshotObserver()
134124

135125
findViewById<TextView>(R.id.app_name).text =
136126
"Sample App v${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})"

0 commit comments

Comments
 (0)