Skip to content

Hook screenshot trigger to test app menu #4151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ public void startFeedback(@NonNull CharSequence infoText) {
taskExecutor,
e -> {
LogWrapper.getInstance().w("Failed to take screenshot for feedback", e);
startFeedback(infoText, null);
doStartFeedback(infoText, null);
})
.addOnSuccessListener(
taskExecutor, screenshotUri -> startFeedback(infoText, screenshotUri));
taskExecutor, screenshotUri -> doStartFeedback(infoText, screenshotUri));
}

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

@Override
public void startFeedback(@NonNull CharSequence infoText, @Nullable Uri screenshotUri) {
if (!feedbackInProgress.compareAndSet(/* expect= */ false, /* update= */ true)) {
LogWrapper.getInstance()
.i("Ignoring startFeedback() call because feedback is already in progress");
return;
}
doStartFeedback(infoText, screenshotUri);
}

private void doStartFeedback(CharSequence infoText, @Nullable Uri screenshotUri) {
testerSignInManager
.signInTester()
.addOnFailureListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

Expand Down Expand Up @@ -668,7 +669,7 @@ public void startFeedback_signsInTesterAndStartsActivity() throws InterruptedExc
}

@Test
public void startFeedback_calledMultipleTimes_onlyStartsOnce() throws InterruptedException {
public void startFeedback_withoutUri_onlyStartsOnce() throws InterruptedException {
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));

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

@Test
public void startFeedback_withUri_doesNotTakeScreenshot() throws InterruptedException {
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));
Uri providedUri = Uri.parse("file:/provided/uri");
firebaseAppDistribution.startFeedback("Some terms and conditions", providedUri);
TestUtils.awaitAsyncOperations(taskExecutor);

verifyNoInteractions(mockScreenshotTaker);
verify(mockTesterSignInManager).signInTester();
Intent expectedIntent = new Intent(activity, FeedbackActivity.class);
Intent actualIntent = shadowOf(RuntimeEnvironment.getApplication()).getNextStartedActivity();
assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());
assertThat(actualIntent.getStringExtra(RELEASE_NAME_EXTRA_KEY)).isEqualTo("release-name");
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_EXTRA_KEY))
.isEqualTo(providedUri.toString());
assertThat(actualIntent.getStringExtra(INFO_TEXT_EXTRA_KEY))
.isEqualTo("Some terms and conditions");
assertThat(firebaseAppDistribution.isFeedbackInProgress()).isTrue();
}

@Test
public void startFeedback_withUri_onlyStartsOnce() throws InterruptedException {
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));

firebaseAppDistribution.startFeedback("Some terms and conditions", TEST_SCREENSHOT_URI);
firebaseAppDistribution.startFeedback("Some other terms and conditions", TEST_SCREENSHOT_URI);
TestUtils.awaitAsyncOperations(taskExecutor);

verify(activity, times(1)).startActivity(any());
}

@Test
public void startFeedback_closingActivity_setsInProgressToFalse() throws InterruptedException {
when(mockReleaseIdentifier.identifyRelease()).thenReturn(Tasks.forResult("release-name"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ class AppDistroTestApplication : Application() {
override fun onCreate() {
super.onCreate()

// Default feedback triggers can also be initialized here
// Perform any required trigger initialization here
ScreenshotDetectionFeedbackTrigger.initialize(this, R.string.terms_and_conditions);

// Default feedback triggers can optionally be enabled application-wide here
// ShakeForFeedback.enable(this)
// ScreenshotDetectionFeedbackTrigger.enable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.app.AlertDialog
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
Expand Down Expand Up @@ -49,9 +47,6 @@ class MainActivity : AppCompatActivity() {
lateinit var progressBar: ProgressBar
lateinit var feedbackTriggerMenu: TextInputLayout

private lateinit var screenshotTriggerThread: HandlerThread
private lateinit var screenshotTrigger: ScreenshotDetectionFeedbackTrigger

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -71,18 +66,13 @@ class MainActivity : AppCompatActivity() {
signInStatus = findViewById(R.id.sign_in_status)
progressBar = findViewById(R.id.progress_bar)

screenshotTriggerThread = HandlerThread("AppDistroFeedbackTrigger")
screenshotTriggerThread.start()
screenshotTrigger =
ScreenshotDetectionFeedbackTrigger(
this,
R.string.terms_and_conditions,
Handler(screenshotTriggerThread.looper)
)

// Set up feedback trigger menu
feedbackTriggerMenu = findViewById(R.id.feedbackTriggerMenu)
val items = listOf(FeedbackTrigger.NONE.label, FeedbackTrigger.SHAKE.label)
val items = listOf(
FeedbackTrigger.NONE.label,
FeedbackTrigger.SHAKE.label,
FeedbackTrigger.SCREENSHOT.label
)
val adapter = ArrayAdapter(this, R.layout.list_item, items)
val autoCompleteTextView = feedbackTriggerMenu.editText!! as AutoCompleteTextView
autoCompleteTextView.setAdapter(adapter)
Expand All @@ -91,17 +81,28 @@ class MainActivity : AppCompatActivity() {
// TODO: support enabling/disabling other triggers
when(text.toString()) {
FeedbackTrigger.NONE.label -> {
Log.i(TAG, "Disabling shake")
ShakeForFeedback.disable(application)
disableAllFeedbackTriggers()
}
FeedbackTrigger.SHAKE.label -> {
Log.i(TAG, "Enabling shake")
disableAllFeedbackTriggers()
Log.i(TAG, "Enabling shake for feedback trigger")
ShakeForFeedback.enable(application, this)
}
FeedbackTrigger.SCREENSHOT.label -> {
disableAllFeedbackTriggers()
Log.i(TAG, "Enabling screenshot detection trigger")
ScreenshotDetectionFeedbackTrigger.enable()
}
}
}
}

private fun disableAllFeedbackTriggers() {
Log.i(TAG, "Disabling all feedback triggers")
ShakeForFeedback.disable(application)
ScreenshotDetectionFeedbackTrigger.disable()
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.action_menu, menu)
Expand All @@ -118,19 +119,8 @@ class MainActivity : AppCompatActivity() {
}
}

override fun onDestroy() {
super.onDestroy()
screenshotTriggerThread.quitSafely()
}

override fun onPause() {
super.onPause()
screenshotTrigger.unRegisterScreenshotObserver()
}

override fun onResume() {
super.onResume()
screenshotTrigger.registerScreenshotObserver()

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