@Test public void testDoesNotRecycleChildResourceSynchronously() { Resource<?> parent = mockResource(); final Resource<?> child = mockResource(); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { recycler.recycle(child); return null; } }).when(parent).recycle(); Shadows.shadowOf(Looper.getMainLooper()).pause(); recycler.recycle(parent); verify(parent).recycle(); verify(child, never()).recycle(); Shadows.shadowOf(Looper.getMainLooper()).runOneTask(); verify(child).recycle(); }
@Test public void testCumulativePoolAndMemoryCacheSizesAreSmallerOnLowMemoryDevices() { Shadows.shadowOf(harness.activityManager).setMemoryClass(getLargeEnoughMemoryClass() / 2); final int normalMemoryCacheSize = harness.getCalculator().getMemoryCacheSize(); final int normalBitmapPoolSize = harness.getCalculator().getBitmapPoolSize(); Util.setSdkVersionInt(10); // Keep the bitmap pool size constant, even though normally it would change. harness.byteArrayPoolSizeBytes *= 2; final int smallMemoryCacheSize = harness.getCalculator().getMemoryCacheSize(); final int smallBitmapPoolSize = harness.getCalculator().getBitmapPoolSize(); assertThat(smallMemoryCacheSize).isLessThan(normalMemoryCacheSize); assertThat(smallBitmapPoolSize).isLessThan(normalBitmapPoolSize); }
private void checkBroadcastResponse(ByteString expectedResponseBytes) throws InvalidProtocolBufferException { List<Intent> broadcasts = Shadows.shadowOf(RuntimeEnvironment.application).getBroadcastIntents(); assertThat(broadcasts.size()).isEqualTo(1); Intent broadcastIntent = broadcasts.get(0); assertThat(broadcastIntent.getAction()) .isEqualTo("example:0000000000000080"); assertThat(broadcastIntent.getCategories()).containsExactly(QueryUtil.BBQ_CATEGORY); assertThat(broadcastIntent.getPackage()).isEqualTo(mQuery.getRequestingApp()); assertThat(broadcastIntent.getByteArrayExtra(QueryUtil.EXTRA_RESPONSE_MESSAGE)).isNotNull(); byte[] responseBytes = broadcastIntent.getByteArrayExtra(QueryUtil.EXTRA_RESPONSE_MESSAGE); BroadcastQueryResponse response = BroadcastQueryResponse.parseFrom(responseBytes); assertThat(response.getRequestId()).isEqualTo(mQuery.getRequestId()); assertThat(response.getResponseId()).isEqualTo(mQuery.getResponseId()); assertThat(response.getResponseMessage()).isEqualTo(expectedResponseBytes); }
@Test public void should_start_url_intent() { // Given String url = "geo:22.5430883,114.1043205"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); // When boolean result = Intents.startUri(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isTrue(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
@Test public void should_return_false_when_nothing_on_the_device_can_open_the_uri() { // Given String url = "http://www.google.com"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), (ResolveInfo) null); // When boolean result = Intents.startUri(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isFalse(); assertThat(startedIntent).isNull(); }
@Test public void should_start_external_uri() { // Given String url = "http://www.google.com"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); // When Intents.startExternalUrl(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
@Test public void service() { UpdateDetailsRequest request = new UpdateDetailsRequest.Builder() .unitId("HAL") .versionId("42") .build(); TestCallback callback = new TestCallback(); UpdateCheckHelper helper = new UpdateCheckHelper("deadbeef"); helper.bind(RuntimeEnvironment.application, callback); helper.requestUpdate(request); Intent intent = Shadows.shadowOf(RuntimeEnvironment.application).getNextStartedService(); Assert.assertNotNull(intent); Assert.assertEquals(intent.getComponent().getClassName(), UpdateCheckService.class.getName()); Assert.assertEquals(intent.getAction(), UpdateCheckService.ACTION_CHECK); Assert.assertEquals(callback.hashCode(), intent.getIntExtra(UpdateCheckService.EXTRA_CALLBACK, 0)); UpdateDetailsRequest request2 = intent.getParcelableExtra(UpdateCheckService.EXTRA_REQUEST); Assert.assertNotNull(request2); helper.unbind(RuntimeEnvironment.application); }
@Test public void service() throws FileNotFoundException { UpdateDetails response = Utils.getUpdateDetailsFromFile("download_success.json"); TestCallback callback = new TestCallback(); PackageDownloadHelper helper = new PackageDownloadHelper("deadbeef"); helper.bind(RuntimeEnvironment.application, callback); helper.requestDownload(response); Intent intent = Shadows.shadowOf(RuntimeEnvironment.application).getNextStartedService(); Assert.assertNotNull(intent); Assert.assertEquals(intent.getComponent().getClassName(), PackageDownloadService.class.getName()); Assert.assertEquals(intent.getAction(), PackageDownloadService.ACTION_DOWNLOAD_PACKAGE); Assert.assertEquals(callback.hashCode(), intent.getIntExtra(PackageDownloadService.EXTRA_CALLBACK, 0)); UpdateDetails response2 = intent.getParcelableExtra(PackageDownloadService.EXTRA_UPDATE_DETAILS); Assert.assertNotNull(response2); helper.unbind(RuntimeEnvironment.application); }
@Test public void createsOperationDeleteRemovedProject() { final ProjectDataHandler projectDataHandler = new ProjectDataHandler(mContext) { @Override protected Cursor query() { final Cursor cursor = mock(Cursor.class); when(cursor.getCount()).thenReturn(1); when(cursor.moveToFirst()).thenReturn(true); when(cursor.getString(eq(COL_PROJECT_KEY))).thenReturn("removedId"); return cursor; } }; final ArrayList<ContentProviderOperation> operations = projectDataHandler.makeContentProviderOperations(new ArrayList<ProjectDto>()); final ContentProviderOperation operation = operations.get(0); assertThat(operation.getUri(), equalTo(CONTENT_URI)); final ShadowContentProviderOperation shadowOperation = Shadows.shadowOf(operation); assertThat(shadowOperation.getType(), equalTo(TYPE_DELETE)); }
@Test public void createsOperationInsertProject() { final List<ProjectDto> projects = new ArrayList<>(); final ProjectDto projectDto = new ProjectDto(); projectDto.setId(1L); projectDto.setProjectKey("TEST"); projectDto.setName("Test project"); projects.add(projectDto); final ArrayList<ContentProviderOperation> operations = new ProjectDataHandler(mContext) .makeContentProviderOperations(projects); final ContentProviderOperation operation = operations.get(0); assertThat(operation.getUri(), equalTo(CONTENT_URI)); final ShadowContentProviderOperation shadowOperation = Shadows.shadowOf(operation); assertThat(shadowOperation.getType(), equalTo(TYPE_INSERT)); final ContentValues contentValues = shadowOperation.getContentValues(); assertThat(contentValues.getAsLong(ProjectEntry._ID), equalTo(projectDto.getId())); assertThat(contentValues.getAsString(ProjectEntry.PROJECT_KEY), equalTo(projectDto.getProjectKey())); assertThat(contentValues.getAsString(ProjectEntry.NAME), equalTo(projectDto.getName())); }
@Test public void createsOperationDeleteRemovedComment() { final CommentDataHandler commentDataHandler = new CommentDataHandler(mContext) { @Override protected Cursor query() { final Cursor cursor = mock(Cursor.class); when(cursor.getCount()).thenReturn(1); when(cursor.moveToFirst()).thenReturn(true); when(cursor.getLong(eq(COL_ID))).thenReturn(1L); return cursor; } }; final ArrayList<ContentProviderOperation> operations = commentDataHandler .makeContentProviderOperations(new ArrayList<CommentDto>()); final ContentProviderOperation operation = operations.get(0); assertThat(operation.getUri(), equalTo(CONTENT_URI)); final ShadowContentProviderOperation shadowOperation = Shadows.shadowOf(operation); assertThat(shadowOperation.getType(), equalTo(TYPE_DELETE)); }
@Test public void createsOperationDeleteRemovedIssue() { final IssueDataHandler issueDataHandler = new IssueDataHandler(mContext) { @Override protected Cursor query() { final Cursor cursor = mock(Cursor.class); when(cursor.getCount()).thenReturn(1); when(cursor.moveToFirst()).thenReturn(true); when(cursor.getLong(eq(COL_ID))).thenReturn(1L); return cursor; } }; final ArrayList<ContentProviderOperation> operations = issueDataHandler .makeContentProviderOperations(new ArrayList<IssueDto>()); final ContentProviderOperation operation = operations.get(0); assertThat(operation.getUri(), equalTo(IssueEntry.CONTENT_URI)); final ShadowContentProviderOperation shadowOperation = Shadows.shadowOf(operation); assertThat(shadowOperation.getType(), equalTo(TYPE_DELETE)); }
@Test public void createsOperationDeleteRemoveUser(){ final UserDataHandler userDataHandler = new UserDataHandler(mContext) { @Override protected Cursor query() { final Cursor cursor = mock(Cursor.class); when(cursor.getCount()).thenReturn(1); when(cursor.moveToFirst()).thenReturn(true); when(cursor.getString(eq(COL_USER_ID))).thenReturn("removedUserId"); return cursor; } }; final ArrayList<ContentProviderOperation> operations = userDataHandler .makeContentProviderOperations(new ArrayList<UserDto>()); final ContentProviderOperation operation = operations.get(0); assertThat(operation.getUri(), equalTo(UserEntry.CONTENT_URI)); final ShadowContentProviderOperation shadowOperation = Shadows.shadowOf(operation); assertThat(shadowOperation.getType(), equalTo(TYPE_DELETE)); }
@Test public void createsOperationInsertUser() { final List<UserDto> users = new ArrayList<>(); final UserDto userDto = new UserDto(); userDto.setId(1L); userDto.setUserId("admin"); userDto.setName("admin"); users.add(userDto); final ArrayList<ContentProviderOperation> operations = new UserDataHandler(mContext) .makeContentProviderOperations(users); final ContentProviderOperation operation = operations.get(0); assertThat(operation.getUri(), equalTo(UserEntry.CONTENT_URI)); final ShadowContentProviderOperation shadowOperation = Shadows.shadowOf(operation); assertThat(shadowOperation.getType(), equalTo(TYPE_INSERT)); final ContentValues contentValues = shadowOperation.getContentValues(); assertThat(contentValues.getAsLong(UserEntry._ID), equalTo(userDto.getId())); assertThat(contentValues.getAsString(UserEntry.USER_ID), equalTo(userDto.getUserId())); assertThat(contentValues.getAsString(UserEntry.NAME), equalTo(userDto.getName())); }
@Test public void createsOperationInsertIssueType() { final Set<IssueTypeDto> issueTypes = new HashSet<>(); final IssueTypeDto issueTypeDto = new IssueTypeDto(); issueTypeDto.setId(ISSUE_TYPE_ID); issueTypeDto.setProjectId(PROJECT_ID); issueTypeDto.setName("Bug"); issueTypeDto.setColor("#990000"); issueTypes.add(issueTypeDto); final ArrayList<ContentProviderOperation> operations = new IssueTypeDataHandler() .makeContentProviderOperations(issueTypes); final ContentProviderOperation operation = operations.get(INDEX_TYPE_INSERT); assertThat(operation.getUri(), equalTo(IssueTypeEntry.buildIssueTypeFromProjectIdUri(PROJECT_ID))); final ShadowContentProviderOperation shadowOperation = Shadows.shadowOf(operation); assertThat(shadowOperation.getType(), equalTo(TYPE_INSERT)); final ContentValues contentValues = shadowOperation.getContentValues(); assertThat(contentValues.getAsLong(IssueTypeEntry._ID), equalTo(issueTypeDto.getId())); assertThat(contentValues.getAsLong(IssueTypeEntry.PROJECT_ID), equalTo(issueTypeDto.getProjectId())); assertThat(contentValues.getAsString(IssueTypeEntry.NAME), equalTo(issueTypeDto.getName())); assertThat(contentValues.getAsString(IssueTypeEntry.COLOR), equalTo(issueTypeDto.getColor())); }
@Test public void testActivity() { //加载Activity MainActivity activity = Robolectric.setupActivity(MainActivity.class); //加载控件 TextView tv = (TextView) activity.findViewById(R.id.tv); Button bt = (Button) activity.findViewById(R.id.bt); //执行点击 bt.performClick(); //判断TextView是否改变 String text = tv.getText().toString(); LogUtil.print("tv.text=" + text); assertThat(text).isEqualTo("我要变"); //判断Activity是否跳转 Intent actualIntent = Shadows.shadowOf(activity).getNextStartedActivity(); Intent expectedIntent = new Intent(); expectedIntent.setAction(IntentAction.ACTIVITY_SECOND); LogUtil.print("actualIntent.action=" + actualIntent.getAction()); assertThat(expectedIntent.getAction()).isEqualTo(actualIntent.getAction()); }
@Test public void shouldStartNextActivity() { // given List<QuestionListAdapter.QuestionAdapterData> questions = Lists.newArrayList( new QuestionListAdapter.QuestionAdapterData().setTitle("Topic 1").setLink("http://top1"), new QuestionListAdapter.QuestionAdapterData().setTitle("Topic 2").setLink("http://top2") ); list.setAdapter(new QuestionListAdapter(activity, questions)); // when int index = 1; Shadows.shadowOf(list).performItemClick(index); // then Intent expectedIntent = new Intent(activity, DetailsActivity.class); expectedIntent.putExtra(DetailsActivity.EXTRA_URL, questions.get(index).getLink()); assertThat(Shadows.shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent); }
@Before public void init() { context = Robolectric.setupActivity(Activity.class); shadowContext = Shadows.shadowOf(context); testingQueues = new TestingQueues(); serviceInstance = spy(new Goro.GoroImpl(testingQueues)); serviceCompName = new ComponentName(context, GoroService.class); GoroService service = new GoroService(); binder = new GoroService.GoroBinderImpl(serviceInstance, service.new GoroTasksListener()); ShadowApplication.getInstance() .setComponentNameAndServiceForBindService( serviceCompName, binder ); reset(serviceInstance); }
@Test public void should_start_baggers_list_activity_when_city_is_selected() { // Given City city = new City(); city.name = "Paris"; city.lat = 42d; city.lng = 1d; // When activity.onCitySelected(city); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent intent = shadowActivity.getNextStartedActivity(); assertThat(intent.getComponent().getClassName()).isEqualTo(BaggersListActivity.class.getName()); assertThat(((City) intent.getParcelableExtra("mCity")).name).isEqualTo("Paris"); }