1
1
package com .bumptech .glide ;
2
2
3
+ import static com .google .common .truth .Truth .assertThat ;
4
+
3
5
import android .content .Context ;
4
6
import android .graphics .drawable .Drawable ;
7
+ import android .os .Build ;
8
+ import android .os .Bundle ;
5
9
import android .widget .ImageView ;
6
10
import androidx .annotation .NonNull ;
11
+ import androidx .lifecycle .Lifecycle .State ;
12
+ import androidx .test .core .app .ActivityScenario ;
13
+ import androidx .test .core .app .ActivityScenario .ActivityAction ;
7
14
import androidx .test .core .app .ApplicationProvider ;
8
15
import androidx .test .ext .junit .runners .AndroidJUnit4 ;
9
16
import com .bumptech .glide .manager .Lifecycle ;
10
17
import com .bumptech .glide .manager .LifecycleListener ;
18
+ import com .bumptech .glide .manager .RequestManagerFragment ;
11
19
import com .bumptech .glide .manager .RequestManagerTreeNode ;
20
+ import com .bumptech .glide .manager .SupportRequestManagerFragment ;
12
21
import com .bumptech .glide .request .target .Target ;
13
22
import com .bumptech .glide .test .ConcurrencyHelper ;
23
+ import com .bumptech .glide .test .GlideWithAsDifferentSupertypesActivity ;
14
24
import com .bumptech .glide .test .ResourceIds ;
15
25
import com .bumptech .glide .test .ResourceIds .raw ;
16
26
import com .bumptech .glide .test .TearDownGlide ;
27
+ import com .google .common .collect .Iterables ;
28
+ import java .util .ArrayList ;
29
+ import java .util .List ;
17
30
import org .junit .Before ;
18
31
import org .junit .Rule ;
19
32
import org .junit .Test ;
@@ -77,7 +90,7 @@ public void run() {
77
90
78
91
/** Tests b/69361054. */
79
92
@ Test
80
- public void clear_withNonOwningRequestManager_onBackgroundTHread_doesNotThrow () {
93
+ public void clear_withNonOwningRequestManager_onBackgroundThread_doesNotThrow () {
81
94
concurrency .runOnMainThread (
82
95
new Runnable () {
83
96
@ Override
@@ -96,4 +109,57 @@ public void run() {
96
109
}
97
110
});
98
111
}
112
+
113
+ @ Test
114
+ public void with_asDifferentSuperTypes_doesNotAddMultipleFragments () {
115
+ ActivityScenario <GlideWithAsDifferentSupertypesActivity > scenario =
116
+ ActivityScenario .launch (GlideWithAsDifferentSupertypesActivity .class );
117
+ scenario .moveToState (State .RESUMED );
118
+ scenario .onActivity (
119
+ new ActivityAction <GlideWithAsDifferentSupertypesActivity >() {
120
+ @ Override
121
+ public void perform (GlideWithAsDifferentSupertypesActivity activity ) {
122
+ Iterable <SupportRequestManagerFragment > glideSupportFragments =
123
+ Iterables .filter (
124
+ activity .getSupportFragmentManager ().getFragments (),
125
+ SupportRequestManagerFragment .class );
126
+ Iterable <RequestManagerFragment > normalFragments =
127
+ Iterables .filter (
128
+ getAllFragments (activity .getFragmentManager ()), RequestManagerFragment .class );
129
+ assertThat (normalFragments ).hasSize (0 );
130
+ assertThat (glideSupportFragments ).hasSize (1 );
131
+ }
132
+ });
133
+ }
134
+
135
+ private List <android .app .Fragment > getAllFragments (android .app .FragmentManager fragmentManager ) {
136
+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .O
137
+ ? fragmentManager .getFragments ()
138
+ : getAllFragmentsPreO (fragmentManager );
139
+ }
140
+
141
+ // Hacks based on the implementation of FragmentManagerImpl in the non-support libraries that
142
+ // allow us to iterate over and retrieve all active Fragments in a FragmentManager.
143
+ private static final String FRAGMENT_INDEX_KEY = "key" ;
144
+
145
+ private List <android .app .Fragment > getAllFragmentsPreO (
146
+ android .app .FragmentManager fragmentManager ) {
147
+ Bundle tempBundle = new Bundle ();
148
+ int index = 0 ;
149
+ List <android .app .Fragment > result = new ArrayList <>();
150
+ while (true ) {
151
+ tempBundle .putInt (FRAGMENT_INDEX_KEY , index ++);
152
+ android .app .Fragment fragment = null ;
153
+ try {
154
+ fragment = fragmentManager .getFragment (tempBundle , FRAGMENT_INDEX_KEY );
155
+ } catch (Exception e ) {
156
+ // This generates log spam from FragmentManager anyway.
157
+ }
158
+ if (fragment == null ) {
159
+ break ;
160
+ }
161
+ result .add (fragment );
162
+ }
163
+ return result ;
164
+ }
99
165
}
0 commit comments