From de9100f8e1acab04daec530ccb6c7dd8be45ca16 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Wed, 23 Mar 2022 20:04:46 +0100 Subject: [PATCH 1/2] fix stuff --- AndroidManifest.xml | 6 +++ .../securesms/ConversationFragment.java | 11 +++--- .../ConversationListRelayingActivity.java | 37 +++++++++++++++++++ .../thoughtcrime/securesms/ShareActivity.java | 9 +++-- 4 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/ConversationListRelayingActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 78bc56328..8b0458fc0 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -153,6 +153,12 @@ android:value="org.thoughtcrime.securesms.ConversationListActivity" /> + + messageRecords) { - Intent composeIntent = new Intent(getActivity(), ConversationListActivity.class); + Intent composeIntent = new Intent(); int[] msgIds = DcMsg.msgSetToIds(messageRecords); setForwardingMessageIds(composeIntent, msgIds); - startActivityForResult(composeIntent, REQUEST_RELAY); + ConversationListRelayingActivity.start(this, composeIntent); getActivity().overridePendingTransition(R.anim.slide_from_right, R.anim.fade_scale_out); } diff --git a/src/org/thoughtcrime/securesms/ConversationListRelayingActivity.java b/src/org/thoughtcrime/securesms/ConversationListRelayingActivity.java new file mode 100644 index 000000000..a4e74b50f --- /dev/null +++ b/src/org/thoughtcrime/securesms/ConversationListRelayingActivity.java @@ -0,0 +1,37 @@ +package org.thoughtcrime.securesms; + +import static org.thoughtcrime.securesms.util.RelayUtil.REQUEST_RELAY; + +import android.app.Activity; +import android.content.ComponentName; +import android.content.Intent; + +import androidx.fragment.app.Fragment; + +/** + * "Relaying" means "Forwarding or Sharing". + * + * When forwarding or sharing, we show the ConversationListActivity to the user. + * However, ConversationListActivity has `launchMode="singleTask"`, which means that this will + * destroy the existing ConversationListActivity. + * + * In API 20-29, `startActivityForResult()` could be used instead of `startActivity()` + * to override this behavior and get two instances of ConversationListActivity. + * + * As this is not possible anymore starting with API 30, we needed another solution, and created + * this activity here. + * + * See https://github.com/deltachat/deltachat-android/issues/1704. + */ + +public class ConversationListRelayingActivity extends ConversationListActivity { + public static void start(Fragment fragment, Intent intent) { + intent.setComponent(new ComponentName(fragment.getContext(), ConversationListRelayingActivity.class)); + fragment.startActivityForResult(intent, REQUEST_RELAY); + } + + public static void start(Activity activity, Intent intent) { + intent.setComponent(new ComponentName(activity, ConversationListRelayingActivity.class)); + activity.startActivityForResult(intent, REQUEST_RELAY); + } +} diff --git a/src/org/thoughtcrime/securesms/ShareActivity.java b/src/org/thoughtcrime/securesms/ShareActivity.java index 2797e5d04..99ae50f62 100644 --- a/src/org/thoughtcrime/securesms/ShareActivity.java +++ b/src/org/thoughtcrime/securesms/ShareActivity.java @@ -17,7 +17,6 @@ package org.thoughtcrime.securesms; -import static org.thoughtcrime.securesms.util.RelayUtil.REQUEST_RELAY; import static org.thoughtcrime.securesms.util.RelayUtil.setSharedText; import android.Manifest; @@ -263,11 +262,13 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity implement if (chatId != -1) { composeIntent = getBaseShareIntent(ConversationActivity.class); composeIntent.putExtra(EXTRA_CHAT_ID, chatId); + RelayUtil.setSharedUris(composeIntent, resolvedExtras); + startActivity(composeIntent); } else { - composeIntent = getBaseShareIntent(ConversationListActivity.class); + composeIntent = getBaseShareIntent(ConversationListRelayingActivity.class); + RelayUtil.setSharedUris(composeIntent, resolvedExtras); + ConversationListRelayingActivity.start(this, composeIntent); } - RelayUtil.setSharedUris(composeIntent, resolvedExtras); - startActivityForResult(composeIntent, REQUEST_RELAY); // We use startActivityForResult() here so that the conversations list is correctly updated. (hide "Device messages", ...)a // With startActivity() the list was not always updated before and after sharing and incorrectly showed or did not show the device talk. finish(); From c4b3fb9d80758a3959442abd14af1242f18a3b7c Mon Sep 17 00:00:00 2001 From: Hocuri Date: Thu, 24 Mar 2022 17:39:01 +0100 Subject: [PATCH 2/2] Add a UI test --- .../uitests/offline/ForwardingTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 androidTest/com/b44t/messenger/uitests/offline/ForwardingTest.java diff --git a/androidTest/com/b44t/messenger/uitests/offline/ForwardingTest.java b/androidTest/com/b44t/messenger/uitests/offline/ForwardingTest.java new file mode 100644 index 000000000..80fb164e4 --- /dev/null +++ b/androidTest/com/b44t/messenger/uitests/offline/ForwardingTest.java @@ -0,0 +1,97 @@ +package com.b44t.messenger.uitests.offline; + +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.Espresso.pressBack; +import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.action.ViewActions.longClick; +import static androidx.test.espresso.assertion.ViewAssertions.matches; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; + +import androidx.test.espresso.IdlingPolicies; +import androidx.test.espresso.contrib.RecyclerViewActions; +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.LargeTest; + +import com.b44t.messenger.DcContact; +import com.b44t.messenger.DcContext; +import com.b44t.messenger.TestUtils; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.thoughtcrime.securesms.ConversationListActivity; +import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.connect.DcHelper; + +import java.util.concurrent.TimeUnit; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class ForwardingTest { + private static int createdGroupId; + + @BeforeClass + public static void beforeClass() { + IdlingPolicies.setMasterPolicyTimeout(10, TimeUnit.SECONDS); + IdlingPolicies.setIdlingResourceTimeout(10, TimeUnit.SECONDS); + } + + @Rule + public ActivityScenarioRule activityRule = TestUtils.getOfflineActivityRule(); + + @Before + public void createChats() { + DcContext dcContext = DcHelper.getContext(getInstrumentation().getTargetContext()); + dcContext.createChatByContactId(DcContact.DC_CONTACT_ID_SELF); + // Disable bcc_self so that DC doesn't try to send messages to the server. + // If we didn't do this, messages would stay in DC_STATE_OUT_PENDING forever. + // The thing is, DC_STATE_OUT_PENDING show a rotating circle animation, and Espresso doesn't work + // with animations, and the tests would hang and never finish. + dcContext.setConfig("bcc_self", "0"); + activityRule.getScenario().onActivity(a -> createdGroupId = DcHelper.getContext(a).createGroupChat(false, "group")); + } + + @After + public void cleanup() { + TestUtils.cleanup(); + } + + @Test + public void testSimpleForwarding() { + // Open device talk + // The group is at position 0, self chat is at position 1, device talk is at position 2 + onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(2, click())); + onView(withId(R.id.title)).check(matches(withText(R.string.device_talk))); + onView(withId(android.R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, longClick())); + onView(withId(R.id.menu_context_forward)).perform(click()); + // Send it to self chat (which is sorted to the top because we're forwarding) + onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); + + onView(withId(R.id.title)).check(matches(withText(R.string.device_talk))); + + pressBack(); + + onView(withId(R.id.toolbar_title)).check(matches(withText(R.string.connectivity_not_connected))); + // Self chat moved up because we sent a message there + onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); + onView(withId(R.id.title)).check(matches(withText(R.string.saved_messages))); + onView(withId(android.R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, longClick())); + onView(withId(R.id.menu_context_forward)).perform(click()); + // Send it to the group + onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(1, click())); + onView(withText(android.R.string.ok)).perform(click()); + onView(withId(R.id.title)).check(matches(withText("group"))); + + pressBack(); + onView(withId(R.id.title)).check(matches(withText(R.string.saved_messages))); + + pressBack(); + onView(withId(R.id.toolbar_title)).check(matches(withText(R.string.connectivity_not_connected))); + } +}