Skip to content

Commit cea0474

Browse files
authored
Merge pull request #5156 from realm/merge-b59c82-to-master
Fix merge from b59c82 to master
2 parents 8902700 + ec38176 commit cea0474

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2017 Realm Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.realm.internal;
17+
18+
import android.support.test.runner.AndroidJUnit4;
19+
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import io.realm.internal.android.AndroidCapabilities;
25+
import io.realm.rule.RunInLooperThread;
26+
import io.realm.rule.RunTestInLooperThread;
27+
28+
import static org.junit.Assert.assertFalse;
29+
import static org.junit.Assert.assertTrue;
30+
31+
@RunWith(AndroidJUnit4.class)
32+
public class AndroidCapabilitiesTest {
33+
34+
@Rule
35+
public final RunInLooperThread looperThread = new RunInLooperThread();
36+
37+
@Test
38+
@RunTestInLooperThread()
39+
public void emulateMainThread_false() {
40+
assertFalse(new AndroidCapabilities().isMainThread());
41+
looperThread.testComplete();
42+
}
43+
44+
@Test
45+
@RunTestInLooperThread(emulateMainThread = true)
46+
public void emulateMainThread_true() {
47+
assertTrue(new AndroidCapabilities().isMainThread());
48+
looperThread.testComplete();
49+
}
50+
51+
}

realm/realm-library/src/androidTest/java/io/realm/rule/RunInLooperThread.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.realm.Realm;
4141
import io.realm.RealmConfiguration;
4242
import io.realm.TestHelper;
43+
import io.realm.internal.android.AndroidCapabilities;
4344

4445

4546
/**
@@ -281,7 +282,7 @@ protected void after() {
281282
// Wait for all async tasks to have completed to ensure a successful deleteRealm call.
282283
// If it times out, it will throw.
283284
TestHelper.waitRealmThreadExecutorFinish();
284-
285+
AndroidCapabilities.EMULATE_MAIN_THREAD = false;
285286
super.after();
286287

287288
// probably belt *and* suspenders...
@@ -376,6 +377,7 @@ public void evaluate() throws Throwable {
376377
runnableBefore.newInstance().run(getConfiguration());
377378
}
378379

380+
AndroidCapabilities.EMULATE_MAIN_THREAD = annotation.emulateMainThread();
379381
runTest(annotation.threadName());
380382
}
381383

realm/realm-library/src/androidTest/java/io/realm/rule/RunTestInLooperThread.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
public @interface RunTestInLooperThread {
3434
String threadName() default "RunTestInLooperThread";
3535
Class<?extends RunInLooperThread.RunnableBefore> before() default RunInLooperThread.RunnableBefore.class;
36+
boolean emulateMainThread() default false;
3637
}

realm/realm-library/src/main/java/io/realm/internal/android/AndroidCapabilities.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.os.Looper;
1919

20+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2021
import javax.annotation.Nullable;
2122

2223
import io.realm.internal.Capabilities;
@@ -27,6 +28,13 @@
2728
*/
2829
public class AndroidCapabilities implements Capabilities {
2930

31+
// Public so it can be set from tests.
32+
// If set, it will treat the current looper thread as the main thread.
33+
// It is up to the caller to handle any race conditions around this. Right now only
34+
// RunInLooperThread.java does this as part of setting up the test.
35+
@SuppressFBWarnings("MS_SHOULD_BE_FINAL")
36+
public static boolean EMULATE_MAIN_THREAD = false;
37+
3038
private final Looper looper;
3139
private final boolean isIntentServiceThread;
3240

@@ -54,7 +62,7 @@ public void checkCanDeliverNotification(@Nullable String exceptionMessage) {
5462

5563
@Override
5664
public boolean isMainThread() {
57-
return looper != null && looper == Looper.getMainLooper();
65+
return looper != null && (EMULATE_MAIN_THREAD || looper == Looper.getMainLooper());
5866
}
5967

6068
private boolean hasLooper() {

0 commit comments

Comments
 (0)