Removed butterknife and cleaned up the test (#5446)

This commit is contained in:
Paul Hawke 2024-01-17 00:35:14 -06:00 committed by GitHub
parent 87d1255323
commit 56e21ab7c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 53 deletions

View file

@ -12,13 +12,12 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import fr.free.nrw.commons.AboutActivity; import fr.free.nrw.commons.AboutActivity;
import fr.free.nrw.commons.CommonsApplication; import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.LoginActivity; import fr.free.nrw.commons.auth.LoginActivity;
import fr.free.nrw.commons.databinding.FragmentMoreBottomSheetLoggedOutBinding;
import fr.free.nrw.commons.di.ApplicationlessInjection; import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.kvstore.JsonKvStore; import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.logging.CommonsLogSender; import fr.free.nrw.commons.logging.CommonsLogSender;
@ -29,6 +28,7 @@ import timber.log.Timber;
public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment { public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment {
private FragmentMoreBottomSheetLoggedOutBinding binding;
@Inject @Inject
CommonsLogSender commonsLogSender; CommonsLogSender commonsLogSender;
@Inject @Inject
@ -39,30 +39,40 @@ public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment
@Override @Override
public View onCreateView(@NonNull final LayoutInflater inflater, public View onCreateView(@NonNull final LayoutInflater inflater,
@Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) { @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState); binding = FragmentMoreBottomSheetLoggedOutBinding.inflate(inflater, container, false);
final View view = inflater.inflate(R.layout.fragment_more_bottom_sheet_logged_out, container, false); return binding.getRoot();
ButterKnife.bind(this, view); }
return view;
@Override
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
binding.moreLogin.setOnClickListener(v -> onLogoutClicked());
binding.moreFeedback.setOnClickListener(v -> onFeedbackClicked());
binding.moreAbout.setOnClickListener(v -> onAboutClicked());
binding.moreSettings.setOnClickListener(v -> onSettingsClicked());
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
} }
@Override @Override
public void onAttach(@NonNull final Context context) { public void onAttach(@NonNull final Context context) {
super.onAttach(context); super.onAttach(context);
ApplicationlessInjection ApplicationlessInjection
.getInstance(getActivity().getApplicationContext()) .getInstance(requireActivity().getApplicationContext())
.getCommonsApplicationComponent() .getCommonsApplicationComponent()
.inject(this); .inject(this);
} }
@OnClick(R.id.more_login)
public void onLogoutClicked() { public void onLogoutClicked() {
applicationKvStore.putBoolean("login_skipped", false); applicationKvStore.putBoolean("login_skipped", false);
Intent intent = new Intent(getContext(), LoginActivity.class); final Intent intent = new Intent(getContext(), LoginActivity.class);
getActivity().finish(); //Kill the activity from which you will go to next activity requireActivity().finish(); //Kill the activity from which you will go to next activity
startActivity(intent); startActivity(intent);
} }
@OnClick(R.id.more_feedback)
public void onFeedbackClicked() { public void onFeedbackClicked() {
showAlertDialog(); showAlertDialog();
} }
@ -71,7 +81,7 @@ public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment
* This method shows the alert dialog when a user wants to send feedback about the app. * This method shows the alert dialog when a user wants to send feedback about the app.
*/ */
private void showAlertDialog() { private void showAlertDialog() {
new AlertDialog.Builder(getActivity()) new AlertDialog.Builder(requireActivity())
.setMessage(R.string.feedback_sharing_data_alert) .setMessage(R.string.feedback_sharing_data_alert)
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.ok, (dialog, which) -> { .setPositiveButton(R.string.ok, (dialog, which) -> {
@ -81,8 +91,8 @@ public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment
} }
/** /**
* This method collects the feedback message and starts and activity with implicit intent * This method collects the feedback message and starts and activity with implicit intent to
* to available email client. * available email client.
*/ */
private void sendFeedback() { private void sendFeedback() {
final String technicalInfo = commonsLogSender.getExtraInfo(); final String technicalInfo = commonsLogSender.getExtraInfo();
@ -103,18 +113,16 @@ public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment
} }
} }
@OnClick(R.id.more_about)
public void onAboutClicked() { public void onAboutClicked() {
final Intent intent = new Intent(getActivity(), AboutActivity.class); final Intent intent = new Intent(getActivity(), AboutActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getActivity().startActivity(intent); requireActivity().startActivity(intent);
} }
@OnClick(R.id.more_settings)
public void onSettingsClicked() { public void onSettingsClicked() {
final Intent intent = new Intent(getActivity(), SettingsActivity.class); final Intent intent = new Intent(getActivity(), SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getActivity().startActivity(intent); requireActivity().startActivity(intent);
} }
private class BaseLogoutListener implements CommonsApplication.LogoutListener { private class BaseLogoutListener implements CommonsApplication.LogoutListener {
@ -127,7 +135,7 @@ public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(nearbyIntent); startActivity(nearbyIntent);
getActivity().finish(); requireActivity().finish();
} }
} }
} }

View file

@ -1,77 +1,60 @@
package fr.free.nrw.commons.navtab package fr.free.nrw.commons.navtab
import android.content.Context
import android.os.Looper import android.os.Looper
import androidx.fragment.app.FragmentManager import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.core.app.ApplicationProvider import androidx.lifecycle.Lifecycle
import androidx.test.ext.junit.runners.AndroidJUnit4
import fr.free.nrw.commons.TestCommonsApplication import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.profile.ProfileActivity
import org.junit.Assert
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows import org.robolectric.Shadows
import org.robolectric.annotation.Config import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode import org.robolectric.annotation.LooperMode
import fr.free.nrw.commons.R
@RunWith(RobolectricTestRunner::class) @RunWith(AndroidJUnit4::class)
@Config(sdk = [21], application = TestCommonsApplication::class) @Config(sdk = [21], application = TestCommonsApplication::class)
@LooperMode(LooperMode.Mode.PAUSED) @LooperMode(LooperMode.Mode.PAUSED)
class MoreBottomSheetLoggedOutFragmentUnitTests { class MoreBottomSheetLoggedOutFragmentUnitTests {
private lateinit var scenario: FragmentScenario<MoreBottomSheetLoggedOutFragment>
private lateinit var fragment: MoreBottomSheetLoggedOutFragment
private lateinit var context: Context
@Before @Before
fun setUp() { fun setUp() {
scenario = launchFragmentInContainer(
context = ApplicationProvider.getApplicationContext() initialState = Lifecycle.State.RESUMED,
themeResId = R.style.LightAppTheme
val activity = Robolectric.buildActivity(ProfileActivity::class.java).create().get() ) {
fragment = MoreBottomSheetLoggedOutFragment() MoreBottomSheetLoggedOutFragment()
val fragmentManager: FragmentManager = activity.supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.add(fragment, null)
fragmentTransaction.commit()
} }
@Test
@Throws(Exception::class)
fun checkFragmentNotNull() {
Assert.assertNotNull(fragment)
} }
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testOnSettingsClicked() { fun testOnSettingsClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onSettingsClicked() scenario.onFragment { it.onSettingsClicked() }
} }
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testOnAboutClicked() { fun testOnAboutClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onAboutClicked() scenario.onFragment { it.onAboutClicked() }
} }
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testOnFeedbackClicked() { fun testOnFeedbackClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onFeedbackClicked() scenario.onFragment { it.onFeedbackClicked() }
} }
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testOnLogoutClicked() { fun testOnLogoutClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onLogoutClicked() scenario.onFragment { it.onLogoutClicked() }
} }
} }