• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
8 import static org.robolectric.Shadows.shadowOf;
9 
10 import android.app.Activity;
11 import android.app.Application;
12 import android.content.ActivityNotFoundException;
13 import android.content.Context;
14 import android.content.Intent;
15 import android.os.Bundle;
16 import android.os.Handler;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.view.ViewParent;
20 import androidx.test.core.app.ApplicationProvider;
21 import androidx.test.ext.junit.runners.AndroidJUnit4;
22 import java.io.ByteArrayOutputStream;
23 import java.io.PrintStream;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.robolectric.annotation.Config;
29 import org.robolectric.annotation.Implementation;
30 import org.robolectric.annotation.Implements;
31 import org.robolectric.shadows.ShadowApplication;
32 import org.robolectric.shadows.ShadowLooper;
33 import org.robolectric.shadows.ShadowView;
34 import org.robolectric.util.ReflectionHelpers;
35 
36 @RunWith(AndroidJUnit4.class)
37 public class RobolectricTest {
38 
39   private PrintStream originalSystemOut;
40   private ByteArrayOutputStream buff;
41   private String defaultLineSeparator;
42   private Application context;
43 
44   @Before
setUp()45   public void setUp() {
46     originalSystemOut = System.out;
47     defaultLineSeparator = System.getProperty("line.separator");
48 
49     System.setProperty("line.separator", "\n");
50     buff = new ByteArrayOutputStream();
51     PrintStream testOut = new PrintStream(buff);
52     System.setOut(testOut);
53     context = ApplicationProvider.getApplicationContext();
54   }
55 
56   @After
tearDown()57   public void tearDown() throws Exception {
58     System.setProperty("line.separator", defaultLineSeparator);
59     System.setOut(originalSystemOut);
60   }
61 
62   @Test(expected = RuntimeException.class)
clickOn_shouldThrowIfViewIsDisabled()63   public void clickOn_shouldThrowIfViewIsDisabled() throws Exception {
64     View view = new View(context);
65     view.setEnabled(false);
66     ShadowView.clickOn(view);
67   }
68 
69   @Test
shouldResetBackgroundSchedulerBeforeTests()70   public void shouldResetBackgroundSchedulerBeforeTests() throws Exception {
71     assertThat(Robolectric.getBackgroundThreadScheduler().isPaused()).isFalse();
72     Robolectric.getBackgroundThreadScheduler().pause();
73   }
74 
75   @Test
shouldResetBackgroundSchedulerAfterTests()76   public void shouldResetBackgroundSchedulerAfterTests() throws Exception {
77     assertThat(Robolectric.getBackgroundThreadScheduler().isPaused()).isFalse();
78     Robolectric.getBackgroundThreadScheduler().pause();
79   }
80 
81   @Test
idleMainLooper_executesScheduledTasks()82   public void idleMainLooper_executesScheduledTasks() {
83     final boolean[] wasRun = new boolean[]{false};
84     new Handler().postDelayed(new Runnable() {
85       @Override
86       public void run() {
87         wasRun[0] = true;
88       }
89     }, 2000);
90 
91     assertFalse(wasRun[0]);
92     ShadowLooper.idleMainLooper(1999);
93     assertFalse(wasRun[0]);
94     ShadowLooper.idleMainLooper(1);
95     assertTrue(wasRun[0]);
96   }
97 
98   @Test
clickOn_shouldCallClickListener()99   public void clickOn_shouldCallClickListener() throws Exception {
100     View view = new View(context);
101     shadowOf(view).setMyParent(ReflectionHelpers.createNullProxy(ViewParent.class));
102     OnClickListener testOnClickListener = mock(OnClickListener.class);
103     view.setOnClickListener(testOnClickListener);
104     ShadowView.clickOn(view);
105 
106     verify(testOnClickListener).onClick(view);
107   }
108 
109   @Test(expected = ActivityNotFoundException.class)
checkActivities_shouldSetValueOnShadowApplication()110   public void checkActivities_shouldSetValueOnShadowApplication() throws Exception {
111     ShadowApplication.getInstance().checkActivities(true);
112     context.startActivity(
113         new Intent("i.dont.exist.activity").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
114   }
115 
116   @Test @Config(sdk = 16)
setupActivity_returnsAVisibleActivity()117   public void setupActivity_returnsAVisibleActivity() throws Exception {
118     LifeCycleActivity activity = Robolectric.setupActivity(LifeCycleActivity.class);
119 
120     assertThat(activity.isCreated()).isTrue();
121     assertThat(activity.isStarted()).isTrue();
122     assertThat(activity.isResumed()).isTrue();
123     assertThat(activity.isVisible()).isTrue();
124   }
125 
126   @Implements(View.class)
127   public static class TestShadowView {
128     @Implementation
getContext()129     protected Context getContext() {
130       return null;
131     }
132   }
133 
134   private static class LifeCycleActivity extends Activity {
135     private boolean created;
136     private boolean started;
137 
138     @Override
onCreate(Bundle savedInstanceState)139     protected void onCreate(Bundle savedInstanceState) {
140       super.onCreate(savedInstanceState);
141       created = true;
142     }
143 
144     @Override
onStart()145     protected void onStart() {
146       super.onStart();
147       started = true;
148     }
149 
isStarted()150     public boolean isStarted() {
151       return started;
152     }
153 
isCreated()154     public boolean isCreated() {
155       return created;
156     }
157 
isVisible()158     public boolean isVisible() {
159       return getWindow().getDecorView().getWindowToken() != null;
160     }
161   }
162 
163 }
164