Skip to content

Commit

Permalink
Merge pull request #4241 from sjudd:add_test_for_no_duplicate_fragments
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 314436527
  • Loading branch information
glide-copybara-robot committed Jun 3, 2020
2 parents 7361ab9 + 1d6781f commit 1cfbe3c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle.State;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ActivityScenario.ActivityAction;
Expand All @@ -21,6 +24,7 @@
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.test.ConcurrencyHelper;
import com.bumptech.glide.test.GlideWithAsDifferentSupertypesActivity;
import com.bumptech.glide.test.GlideWithBeforeSuperOnCreateActivity;
import com.bumptech.glide.test.ResourceIds;
import com.bumptech.glide.test.ResourceIds.raw;
import com.bumptech.glide.test.TearDownGlide;
Expand Down Expand Up @@ -110,6 +114,60 @@ public void run() {
});
}

@Test
public void with_beforeActivitySuperOnCreate_onlyAddsOneRequestManagerFragment() {
ActivityScenario<GlideWithBeforeSuperOnCreateActivity> scenario =
ActivityScenario.launch(GlideWithBeforeSuperOnCreateActivity.class);
scenario.moveToState(State.RESUMED);
scenario.onActivity(
new ActivityAction<GlideWithBeforeSuperOnCreateActivity>() {
@Override
public void perform(GlideWithBeforeSuperOnCreateActivity activity) {
List<Fragment> fragments = activity.getSupportFragmentManager().getFragments();
List<Fragment> glideFragments = new ArrayList<>();
for (Fragment fragment : fragments) {
if (fragment instanceof SupportRequestManagerFragment) {
glideFragments.add(fragment);
}
}
// Ideally this would be exactly 1, but it's a bit tricky to implement. For now we're
// content making sure that we're not adding multiple fragments.
assertThat(glideFragments.size()).isAtMost(1);
}
});
scenario.onActivity(
new ActivityAction<GlideWithBeforeSuperOnCreateActivity>() {
@Override
public void perform(final GlideWithBeforeSuperOnCreateActivity activity) {
new Handler(Looper.getMainLooper())
.post(
new Runnable() {
@Override
public void run() {
Glide.with(activity);
}
});
}
});
scenario.onActivity(
new ActivityAction<GlideWithBeforeSuperOnCreateActivity>() {
@Override
public void perform(GlideWithBeforeSuperOnCreateActivity activity) {
List<Fragment> fragments = activity.getSupportFragmentManager().getFragments();
List<Fragment> glideFragments = new ArrayList<>();
for (Fragment fragment : fragments) {
if (fragment instanceof SupportRequestManagerFragment) {
glideFragments.add(fragment);
}
}
// Now that we've called Glide.with() after commitAllowingStateLoss will actually add
// the
// fragment, ie after onCreate, we can expect exactly one Fragment instance.
assertThat(glideFragments.size()).isEqualTo(1);
}
});
}

@Test
public void with_asDifferentSuperTypes_doesNotAddMultipleFragments() {
ActivityScenario<GlideWithAsDifferentSupertypesActivity> scenario =
Expand Down
1 change: 1 addition & 0 deletions instrumentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application>
<activity android:name="com.bumptech.glide.test.GlideWithBeforeSuperOnCreateActivity" />
<activity android:name="com.bumptech.glide.test.GlideWithAsDifferentSupertypesActivity" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bumptech.glide.test;

import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import com.bumptech.glide.Glide;

public class GlideWithBeforeSuperOnCreateActivity extends FragmentActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Glide.with(this);
super.onCreate(savedInstanceState);
setContentView(new TextView(this));
}

@Override
protected void onResume() {
super.onResume();
Glide.with(this);
}
}

0 comments on commit 1cfbe3c

Please sign in to comment.