Java 类org.robolectric.internal.ShadowExtractor 实例源码

项目:materialistic    文件:ListFragmentTest.java   
@Test
public void testRefresh() {
    Bundle args = new Bundle();
    args.putString(ListFragment.EXTRA_ITEM_MANAGER, HackerNewsClient.class.getName());
    args.putString(ListFragment.EXTRA_FILTER, ItemManager.TOP_FETCH_MODE);
    activity.getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.list,
                    Fragment.instantiate(activity, ListFragment.class.getName(), args))
            .commit();
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout)
            ShadowExtractor.extract(activity.findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    // should trigger another data request
    verify(itemManager).getStories(any(String.class),
            eq(ItemManager.MODE_DEFAULT),
            any(ResponseListener.class));
    verify(itemManager).getStories(any(String.class),
            eq(ItemManager.MODE_NETWORK),
            any(ResponseListener.class));
    controller.pause().stop().destroy();
}
项目:materialistic    文件:ItemFragmentMultiPageTest.java   
@Test
public void testRefresh() {
    WebItem webItem = mock(WebItem.class);
    when(webItem.getId()).thenReturn("1");
    Bundle args = new Bundle();
    args.putParcelable(ItemFragment.EXTRA_ITEM, webItem);
    Fragment fragment = Fragment.instantiate(RuntimeEnvironment.application,
            ItemFragment.class.getName(), args);
    makeVisible(fragment);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout)
            ShadowExtractor.extract(fragment.getView().findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    verify(hackerNewsClient).getItem(eq("1"),
            eq(ItemManager.MODE_DEFAULT),
            listener.capture());
    verify(hackerNewsClient).getItem(eq("1"),
            eq(ItemManager.MODE_NETWORK),
            listener.capture());
    listener.getAllValues().get(1).onResponse(new TestHnItem(1L));
    assertThat((SwipeRefreshLayout) fragment.getView().findViewById(R.id.swipe_layout))
            .isNotRefreshing();
    verify(((TestItemActivity) fragment.getActivity()).itemChangedListener)
            .onItemChanged(any(Item.class));
}
项目:materialistic    文件:ItemFragmentMultiPageTest.java   
@Test
public void testRefreshFailed() {
    WebItem webItem = mock(WebItem.class);
    when(webItem.getId()).thenReturn("1");
    Bundle args = new Bundle();
    args.putParcelable(ItemFragment.EXTRA_ITEM, webItem);
    Fragment fragment = Fragment.instantiate(RuntimeEnvironment.application,
            ItemFragment.class.getName(), args);
    makeVisible(fragment);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout)
            ShadowExtractor.extract(fragment.getView().findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    verify(hackerNewsClient).getItem(eq("1"),
            eq(ItemManager.MODE_DEFAULT),
            listener.capture());
    verify(hackerNewsClient).getItem(eq("1"),
            eq(ItemManager.MODE_NETWORK),
            listener.capture());
    listener.getAllValues().get(1).onError(null);
    assertThat((SwipeRefreshLayout) fragment.getView().findViewById(R.id.swipe_layout))
            .isNotRefreshing();
}
项目:materialistic    文件:WebFragmentTest.java   
@Test
public void testDownloadContent() {
    ResolveInfo resolverInfo = new ResolveInfo();
    resolverInfo.activityInfo = new ActivityInfo();
    resolverInfo.activityInfo.applicationInfo = new ApplicationInfo();
    resolverInfo.activityInfo.applicationInfo.packageName =
            ListActivity.class.getPackage().getName();
    resolverInfo.activityInfo.name = ListActivity.class.getName();
    RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager();
    final String url = "http://example.com/file.doc";
    rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), resolverInfo);

    WebView webView = (WebView) activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor.extract(webView);
    when(item.getUrl()).thenReturn(url);
    shadowWebView.getDownloadListener().onDownloadStart(url, "", "", "", 0l);
    assertThat((View) activity.findViewById(R.id.empty)).isVisible();
    activity.findViewById(R.id.download_button).performClick();
    assertNotNull(shadowOf(activity).getNextStartedActivity());
}
项目:materialistic    文件:WebFragmentTest.java   
@Test
public void testDownloadPdf() throws InterruptedException {
    ResolveInfo resolverInfo = new ResolveInfo();
    resolverInfo.activityInfo = new ActivityInfo();
    resolverInfo.activityInfo.applicationInfo = new ApplicationInfo();
    resolverInfo.activityInfo.applicationInfo.packageName = ListActivity.class.getPackage().getName();
    resolverInfo.activityInfo.name = ListActivity.class.getName();
    RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager();
    when(item.getUrl()).thenReturn("http://example.com/file.pdf");
    rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getUrl())), resolverInfo);

    WebView webView = activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor.extract(webView);
    WebFragment fragment = (WebFragment) activity.getSupportFragmentManager()
            .findFragmentByTag(WebFragment.class.getName());
    shadowWebView.getDownloadListener().onDownloadStart(item.getUrl(), "", "", "application/pdf", 0l);
    shadowWebView.getWebViewClient().onPageFinished(webView, PDF_LOADER_URL);
    verify(fragment.mFileDownloader).downloadFile(
        eq(item.getUrl()),
        eq("application/pdf"),
        any(FileDownloader.FileDownloaderCallback.class));
}
项目:materialistic    文件:WebFragmentTest.java   
@SuppressLint("NewApi")
@Test
public void testWebControls() {
    ShadowLocalBroadcastManager.getInstance(activity)
            .sendBroadcast(new Intent(WebFragment.ACTION_FULLSCREEN)
                    .putExtra(WebFragment.EXTRA_FULLSCREEN, true));
    ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor
            .extract(activity.findViewById(R.id.web_view));
    activity.findViewById(R.id.button_more).performClick();
    shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener()
            .onMenuItemClick(new RoboMenuItem(R.id.menu_zoom_in));
    assertThat(shadowWebView.getZoomDegree()).isEqualTo(1);
    activity.findViewById(R.id.button_more).performClick();
    shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener()
            .onMenuItemClick(new RoboMenuItem(R.id.menu_zoom_out));
    assertThat(shadowWebView.getZoomDegree()).isEqualTo(0);
    activity.findViewById(R.id.button_forward).performClick();
    assertThat(shadowWebView.getPageIndex()).isEqualTo(1);
    activity.findViewById(R.id.button_back).performClick();
    assertThat(shadowWebView.getPageIndex()).isEqualTo(0);
}
项目:materialistic    文件:WebFragmentTest.java   
@Test
public void testFullScroll() {
    ShadowLocalBroadcastManager.getInstance(activity)
            .sendBroadcast(new Intent(WebFragment.ACTION_FULLSCREEN)
                    .putExtra(WebFragment.EXTRA_FULLSCREEN, true));
    ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor
            .extract(activity.findViewById(R.id.web_view));
    WebFragment fragment = (WebFragment) activity.getSupportFragmentManager()
            .findFragmentByTag(WebFragment.class.getName());
    fragment.scrollToTop();
    assertEquals(0, shadowWebView.getScrollY());
    fragment.scrollToNext();
    assertEquals(1, shadowWebView.getScrollY());
    fragment.scrollToPrevious();
    assertEquals(0, shadowWebView.getScrollY());
}
项目:materialistic    文件:DrawerActivityLoginTest.java   
@Test
public void testAddAccount() {
    ShadowAccountManager.get(activity).addAccountExplicitly(new Account("existing",
            BuildConfig.APPLICATION_ID), "password", null);
    drawerAccount.performClick();
    AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog();
    assertNotNull(alertDialog);
    assertThat(alertDialog.getListView().getAdapter()).hasCount(1);
    alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
    assertThat(alertDialog).isNotShowing();
    ((ShadowSupportDrawerLayout) ShadowExtractor.extract(activity.findViewById(R.id.drawer_layout)))
            .getDrawerListeners().get(0)
            .onDrawerClosed(activity.findViewById(R.id.drawer));
    assertThat(shadowOf(activity).getNextStartedActivity())
            .hasComponent(activity, LoginActivity.class);
}
项目:materialistic    文件:ListFragmentViewHolderTest.java   
@Test
public void testNewStory() {
    reset(itemManager);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout)
            ShadowExtractor.extract(activity.findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    verify(itemManager).getStories(any(),
            eq(ItemManager.MODE_NETWORK),
            storiesListener.capture());
    storiesListener.getValue().onResponse(new Item[]{new TestHnItem(2) {
        @Override
        public int getRank() {
            return 46;
        }
    }});
    ShadowSnackbar.getLatestView().findViewById(R.id.snackbar_action).performClick();
    verify(itemManager, atLeastOnce()).getItem(any(),
            eq(ItemManager.MODE_NETWORK),
            itemListener.capture());
    itemListener.getValue().onResponse(new PopulatedStory(2));
    RecyclerView.ViewHolder holder = adapter.getViewHolder(0);
    assertThat((TextView) holder.itemView.findViewById(R.id.rank)).hasTextString("46*");
}
项目:materialistic    文件:ListFragmentViewHolderTest.java   
@Test
public void testPromoted() {
    reset(itemManager);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout)
            ShadowExtractor.extract(activity.findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    verify(itemManager).getStories(any(),
            eq(ItemManager.MODE_NETWORK),
            storiesListener.capture());
    storiesListener.getValue().onResponse(new Item[]{new TestHnItem(1) {
        @Override
        public int getRank() {
            return 45;
        }
    }});
    verify(itemManager).getItem(any(), eq(ItemManager.MODE_NETWORK), itemListener.capture());
    itemListener.getValue().onResponse(new PopulatedStory(1));
    RecyclerView.ViewHolder holder = adapter.getViewHolder(0);
    assertThat((TextView) holder.itemView.findViewById(R.id.rank)).containsText("+1");
}
项目:materialistic    文件:ListFragmentViewHolderTest.java   
@Test
public void testNewComments() {
    reset(itemManager);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout)
            ShadowExtractor.extract(activity.findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    verify(itemManager).getStories(any(),
            eq(ItemManager.MODE_NETWORK),
            storiesListener.capture());
    storiesListener.getValue().onResponse(new Item[]{new TestHnItem(1)});
    verify(itemManager).getItem(any(), eq(ItemManager.MODE_NETWORK), itemListener.capture());
    itemListener.getValue().onResponse(new PopulatedStory(1) {
        @Override
        public int getDescendants() {
            return 2;
        }

        @Override
        public long[] getKids() {
            return new long[]{2, 3};
        }
    });
    RecyclerView.ViewHolder holder = adapter.getViewHolder(0);
    assertThat((TextView) holder.itemView.findViewById(R.id.comment)).hasTextString("2*");
}
项目:materialistic    文件:ListFragmentViewHolderTest.java   
@Test
public void testPreferenceChange() {
    reset(itemManager);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout)
            ShadowExtractor.extract(activity.findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    verify(itemManager).getStories(any(),
            eq(ItemManager.MODE_NETWORK),
            storiesListener.capture());
    storiesListener.getValue().onResponse(new Item[]{new TestHnItem(2) {
        @Override
        public int getRank() {
            return 46;
        }
    }});
    verify(itemManager).getItem(any(), eq(ItemManager.MODE_NETWORK), itemListener.capture());
    itemListener.getValue().onResponse(new PopulatedStory(2));
    RecyclerView.ViewHolder holder = adapter.getViewHolder(0);
    assertThat((TextView) holder.itemView.findViewById(R.id.rank)).hasTextString("46*");
    PreferenceManager.getDefaultSharedPreferences(activity)
            .edit()
            .putBoolean(activity.getString(R.string.pref_highlight_updated), false)
            .apply();
    holder = adapter.getViewHolder(0);
    assertThat((TextView) holder.itemView.findViewById(R.id.rank)).hasTextString("46");
}
项目:talkback    文件:AccessibilityNodeInfoUtilsTest.java   
@Test
public void searchFromBfsNodeMatchesChild_shouldReturnChild() {
    AccessibilityNodeInfo parent = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo child = AccessibilityNodeInfo.obtain();
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parent)).addChild(child);

    AccessibilityNodeInfoCompat parentCompat = new AccessibilityNodeInfoCompat(parent);
    AccessibilityNodeInfoCompat childCompat = new AccessibilityNodeInfoCompat(child);
    parent.setFocusable(false);
    child.setFocusable(true);
    AccessibilityNodeInfoCompat returned =
            AccessibilityNodeInfoUtils.searchFromBfs(parentCompat, focusableFilter);
    assertEquals(childCompat, returned);
    parentCompat.recycle();
    childCompat.recycle();
    returned.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoUtilsTest.java   
@Test
public void getRootForNonRootNode_shouldReturnRootNode() {
    AccessibilityNodeInfo level1Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level2Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level3Node = AccessibilityNodeInfo.obtain();
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level1Node)).addChild(level2Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level2Node)).addChild(level3Node);

    AccessibilityNodeInfoCompat startNode = new AccessibilityNodeInfoCompat(level3Node);
    AccessibilityNodeInfoCompat targetNode = new AccessibilityNodeInfoCompat(level1Node);
    AccessibilityNodeInfoCompat rootNode = AccessibilityNodeInfoUtils.getRoot(startNode);
    assertEquals(targetNode, rootNode);
    level1Node.recycle();
    level2Node.recycle();
    level3Node.recycle();
    rootNode.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoUtilsTest.java   
@Test
public void getRootForNodeWithLoop_shouldReturnNull() {
    AccessibilityNodeInfo level1Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level2Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level3Node = AccessibilityNodeInfo.obtain();

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level1Node)).addChild(level2Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level2Node)).addChild(level3Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level3Node)).addChild(level1Node);

    AccessibilityNodeInfoCompat startNode = new AccessibilityNodeInfoCompat(level3Node);
    AccessibilityNodeInfoCompat rootNode = AccessibilityNodeInfoUtils.getRoot(startNode);
    assertNull(rootNode);
    level1Node.recycle();
    level2Node.recycle();
    level3Node.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoUtilsTest.java   
@Test(timeout=100)
public void isFocusableWithLoop_shouldNotHang() {
    AccessibilityNodeInfo level1Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level2Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level3Node = AccessibilityNodeInfo.obtain();

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level1Node)).addChild(level2Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level2Node)).addChild(level3Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level3Node)).addChild(level1Node);

    AccessibilityNodeInfoCompat startNode = new AccessibilityNodeInfoCompat(level1Node);
    AccessibilityNodeInfoUtils.isAccessibilityFocusable(startNode);
    level1Node.recycle();
    level2Node.recycle();
    level3Node.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoUtilsTest.java   
@Test(timeout=100)
public void shouldFocusNodeWithLoop_shouldNotHang() {
    AccessibilityNodeInfo level1Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level2Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level3Node = AccessibilityNodeInfo.obtain();

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level1Node)).addChild(level2Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level2Node)).addChild(level3Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level3Node)).addChild(level1Node);

    AccessibilityNodeInfoCompat startNode = new AccessibilityNodeInfoCompat(level1Node);
    AccessibilityNodeInfoUtils.shouldFocusNode(startNode);
    level1Node.recycle();
    level2Node.recycle();
    level3Node.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoRefTest.java   
@Test
public void lastDescentWithChildren_shouldReturnTrueAndSetItselfForLastChildren() {
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);
    parentNode.setContentDescription("Parent");
    AccessibilityNodeInfo child1Node = AccessibilityNodeInfo.obtain();
    child1Node.setVisibleToUser(true);
    child1Node.setContentDescription("Child1");
    AccessibilityNodeInfo child2Node = AccessibilityNodeInfo.obtain();
    child2Node.setVisibleToUser(true);
    child2Node.setContentDescription("Child2");

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child1Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child2Node);

    AccessibilityNodeInfoCompat parentCompat = new AccessibilityNodeInfoCompat(parentNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(parentCompat);
    System.out.println("lastDescentWithChildren_shouldReturnTrueAndSetItselfForLastChildren");
    assertTrue(ref.lastDescendant());
    assertEquals(child2Node, ref.get().getInfo());
    parentNode.recycle();
    child1Node.recycle();
    child2Node.recycle();
    ref.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoRefTest.java   
@Test
public void lastDescentWithLoop_shouldReturnFalse() {
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);
    AccessibilityNodeInfo child1Node = AccessibilityNodeInfo.obtain();
    child1Node.setVisibleToUser(true);
    AccessibilityNodeInfo child2Node = AccessibilityNodeInfo.obtain();
    child2Node.setVisibleToUser(true);

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child1Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child2Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(child2Node)).addChild(parentNode);

    AccessibilityNodeInfoCompat parentCompat = new AccessibilityNodeInfoCompat(parentNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(parentCompat);
    assertFalse(ref.lastDescendant());
    parentNode.recycle();
    child1Node.recycle();
    child2Node.recycle();
    ref.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoRefTest.java   
@Test
public void parentWithParents_shouldReturnTrueAndSetItselfForParent() {
    AccessibilityNodeInfo currentNode = AccessibilityNodeInfo.obtain();
    currentNode.setVisibleToUser(true);
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(currentNode);

    AccessibilityNodeInfoCompat currentCompat = new AccessibilityNodeInfoCompat(currentNode);
    AccessibilityNodeInfoCompat parentCompat = new AccessibilityNodeInfoCompat(parentNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(currentCompat);
    assertTrue(ref.parent());
    assertEquals(parentCompat, ref.get());
    parentNode.recycle();
    currentNode.recycle();
    ref.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoRefTest.java   
@Test
public void parentWithLoopedParents_shouldReturnFalse() {
    AccessibilityNodeInfo curNode = AccessibilityNodeInfo.obtain();
    curNode.setVisibleToUser(true);
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(curNode);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).addChild(parentNode);

    /* Set nodes to be invisible to force the the code to traverse the loop */
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).setVisibleToUser(false);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).setVisibleToUser(false);

    AccessibilityNodeInfoCompat currentCompat = new AccessibilityNodeInfoCompat(curNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(currentCompat);
    assertFalse(ref.parent());
    parentNode.recycle();
    curNode.recycle();
    ref.recycle();
}
项目:talkback    文件:AccessibilityNodeInfoRefTest.java   
@Test
public void parentWithSelfReferencedParent_shouldReturnFalse() {
    AccessibilityNodeInfo curNode = AccessibilityNodeInfo.obtain();
    curNode.setVisibleToUser(true);
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);

    curNode.setContentDescription("Current");
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).addChild(curNode);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).setVisibleToUser(true);

    AccessibilityNodeInfoCompat currentCompat = new AccessibilityNodeInfoCompat(curNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(currentCompat);
    assertFalse(ref.parent());
    parentNode.recycle();
    curNode.recycle();
    ref.recycle();
}
项目:talkback    文件:NodeCachedBoundsCalculatorTest.java   
@Test(timeout=1000)
public void getBoundsWithLoop_shouldNotHang() {
    AccessibilityNodeInfo level1Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level2Node = AccessibilityNodeInfo.obtain();
    AccessibilityNodeInfo level3Node = AccessibilityNodeInfo.obtain();

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level1Node)).addChild(level2Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level2Node)).addChild(level3Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(level3Node)).addChild(level1Node);

    AccessibilityNodeInfoCompat startNode = new AccessibilityNodeInfoCompat(level1Node);

    NodeCachedBoundsCalculator calculator = new NodeCachedBoundsCalculator();
    calculator.getBounds(startNode);

    level1Node.recycle();
    level2Node.recycle();
    level3Node.recycle();
}
项目:talkback    文件:OptionManagerTest.java   
@Before
public void setUp() {
    ShadowAccessibilityNodeInfo.resetObtainedInstances();
    mCompat1 = new SwitchAccessNodeCompat(AccessibilityNodeInfo.obtain());
    mShadowInfo1 = (ShadowAccessibilityNodeInfo)
            ShadowExtractor.extract((AccessibilityNodeInfo) mCompat1.getInfo());
    mCompat2 = new SwitchAccessNodeCompat(AccessibilityNodeInfo.obtain());
    mShadowInfo2 = (ShadowAccessibilityNodeInfo)
            ShadowExtractor.extract((AccessibilityNodeInfo) mCompat2.getInfo());
    mSharedPreferences.edit().clear().commit();
    MockitoAnnotations.initMocks(this);
    mCompat1.setBoundsInScreen(NODE_BOUNDS_1);
    mCompat2.setBoundsInScreen(NODE_BOUNDS_2);
    mActionNode1 = new AccessibilityNodeActionNode(mCompat1,
            new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
                    AccessibilityNodeInfoCompat.ACTION_CLICK, "label1"));
    mActionNode2 = new AccessibilityNodeActionNode(mCompat2,
            new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
                    AccessibilityNodeInfoCompat.ACTION_CLICK, "label2"));
    mSelectionNode = new OptionScanSelectionNode(mActionNode1, mActionNode2);

    when(mOverlayController.getContext()).thenReturn(mContext);
    mOptionManager = new OptionManager(mOverlayController);
}
项目:talkback    文件:OptionManagerTest.java   
@Test
public void testScrollWithParent_shouldScrollAndClearFocus() {
    AccessibilityNodeInfoCompat parent = AccessibilityNodeInfoCompat.obtain();
    ShadowAccessibilityNodeInfo shadowParent =
            (ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parent.getInfo());
    shadowParent.addChild((AccessibilityNodeInfo)  mCompat1.getInfo());
    parent.setScrollable(true);
    mActionNode1.recycle();
    mActionNode1 = new AccessibilityNodeActionNode(mCompat1,
            new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
                    AccessibilityNodeInfoCompat.ACTION_CLICK, "label1"));
    mSelectionNode = new OptionScanSelectionNode(mActionNode1, mActionNode2);
    mOptionManager.clearFocusIfNewTree(mSelectionNode);
    mOptionManager.selectOption(0);
    mOptionManager.performScrollAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
    assertEquals(0, mShadowInfo1.getPerformedActions().size());
    List<Integer> parentActions = shadowParent.getPerformedActions();
    assertEquals(1, parentActions.size());
    assertEquals(new Integer(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD),
            parentActions.get(0));
    verify(mOverlayController, times(3)).clearOverlay();
    parent.recycle();
}
项目:talkback    文件:ShadowAccessibilityNodeInfo.java   
/**
 * Check for leaked objects that were {@code obtain}ed but never
 * {@code recycle}d.
 *
 * @param printUnrecycledNodesToSystemErr - if true, stack traces of calls
 *        to {@code obtain} that lack matching calls to {@code recycle} are
 *        dumped to System.err.
 * @return {@code true} if there are unrecycled nodes
 */
public static boolean areThereUnrecycledNodes(boolean printUnrecycledNodesToSystemErr) {
    if (printUnrecycledNodesToSystemErr) {
        for (final StrictEqualityNodeWrapper wrapper : obtainedInstances.keySet()) {
            final ShadowAccessibilityNodeInfo shadow =
                    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(wrapper.mInfo));

            System.err.println(String.format(
                    "Leaked contentDescription = %s. Stack trace:", shadow.getContentDescription()));
            for (final StackTraceElement stackTraceElement : obtainedInstances.get(wrapper)) {
                System.err.println(stackTraceElement.toString());
            }
        }
    }

    return (obtainedInstances.size() != 0);
}
项目:talkback    文件:SwitchAccessNodeCompatTest.java   
@Before
public void setUp() {
    ShadowAccessibilityNodeInfo.resetObtainedInstances();

    /* Build accessibility node tree */
    mParentNode = AccessibilityNodeInfo.obtain();
    mParentNode.setContentDescription("mParentNode");
    mChildNode0 = AccessibilityNodeInfo.obtain();
    mChildNode0.setContentDescription("mChildNode0");
    mChildNode1 = AccessibilityNodeInfo.obtain();
    mChildNode1.setContentDescription("mChildNode1");

    ShadowAccessibilityNodeInfo shadowParent =
            (ShadowAccessibilityNodeInfo) ShadowExtractor.extract(mParentNode);
    shadowParent.addChild(mChildNode0);
    shadowParent.addChild(mChildNode1);
}
项目:talkback    文件:AccessibilityNodeActionNodeTest.java   
@Test
public void performActionWithArgs_actionAndArgsReceivedByNode() {
    String argsKey = "test";
    Bundle args = new Bundle();
    args.putBoolean(argsKey, true);
    assertTrue(args.getBoolean(argsKey));
    AccessibilityNodeActionNode accessibilityNodeActionNode = new AccessibilityNodeActionNode(
            mNodeInfoCompat, new AccessibilityActionCompat(ACTION_ID, ACTION_NAME), args);
    accessibilityNodeActionNode.performAction();
    AccessibilityNodeInfo info = (AccessibilityNodeInfo) mNodeInfoCompat.getInfo();
    ShadowAccessibilityNodeInfo shadowInfo =
            (ShadowAccessibilityNodeInfo) ShadowExtractor.extract(info);

    List<Pair<Integer, Bundle>> performedActions = shadowInfo.getPerformedActionsWithArgs();
    assertEquals(1, performedActions.size());
    assertEquals(ACTION_ID, performedActions.get(0).first.intValue());
    assertNotNull(performedActions.get(0).second);
    assertTrue(performedActions.get(0).second.getBoolean(argsKey));
    accessibilityNodeActionNode.recycle();
}
项目:talkback    文件:TreeBuilderTest.java   
@Before
public void setUp() {
    mTreeBuilder = new MyTreeBuilder(mContext);
    AccessibilityNodeInfo duplicateBoundsParentInfo = AccessibilityNodeInfo.obtain();
    duplicateBoundsParentInfo.setVisibleToUser(true);
    duplicateBoundsParentInfo.setBoundsInScreen(DUPLICATE_BOUNDS);
    duplicateBoundsParentInfo.setContentDescription("Parent");
    AccessibilityNodeInfo duplicateBoundsChildInfo = AccessibilityNodeInfo.obtain();
    duplicateBoundsChildInfo.setVisibleToUser(true);
    duplicateBoundsChildInfo.setBoundsInScreen(DUPLICATE_BOUNDS);
    duplicateBoundsChildInfo.setContentDescription("Child");
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(duplicateBoundsParentInfo))
            .addChild(duplicateBoundsChildInfo);
    mDuplicateBoundsParent = new SwitchAccessNodeCompat(duplicateBoundsParentInfo);
    mDuplicateBoundsChild = new SwitchAccessNodeCompat(duplicateBoundsChildInfo);
}
项目:AndroidTextManager    文件:ContactEditTextTest.java   
@Before
public void setup() {
    // Print out everything from logcat to the console instead
    ShadowLog.stream = System.out;

    // Setup variables
    mContext = RuntimeEnvironment.application;
    mEditText = new ContactEditText(mContext);

    // Update the owner's contact in TextManager
    ShadowTextManager shadowTextManager = (ShadowTextManager) ShadowExtractor.extract(TextManager.getInstance(mContext));
    shadowTextManager.setSelf(ME);
    shadowTextManager.addContact(NAMELESS);
    shadowTextManager.addContact(ALICE);
    shadowTextManager.addContact(BOB);
    shadowTextManager.addContact(CHARLIE);
    shadowTextManager.addContact(DEBRA);
}
项目:GitHub    文件:MyShadowBitmap.java   
@Implementation
public static Bitmap createBitmap(int width, int height, Bitmap.Config config) {
  Bitmap bitmap = Shadow.newInstanceOf(Bitmap.class);
  MyShadowBitmap shadowBitmap = (MyShadowBitmap) ShadowExtractor.extract(bitmap);
  shadowBitmap.width = width;
  shadowBitmap.height = height;
  shadowBitmap.mPixels = new int[width * height];
  return bitmap;
}
项目:GitHub    文件:MyShadowBitmap.java   
@Implementation
public static Bitmap createBitmap(int colors[], int width, int height, Bitmap.Config config) {
  Bitmap bitmap = Shadow.newInstanceOf(Bitmap.class);
  MyShadowBitmap shadowBitmap = (MyShadowBitmap) ShadowExtractor.extract(bitmap);
  shadowBitmap.width = width;
  shadowBitmap.height = height;
  shadowBitmap.mPixels = new int[width * height];
  for (int i = 0; i < colors.length; i++) {
    shadowBitmap.mPixels[i] = colors[i];
  }
  return bitmap;
}
项目:androidadvanced    文件:RecipeListActivityTest.java   
@Test
public void testOnRecyclerViewScroll_ShouldChangeScrollPosition() throws Exception {
    int scrollPosition = 1;

    RecyclerView recyclerView = (RecyclerView)activity.findViewById(R.id.recyclerView);
    ShadowRecyclerView shadowRecyclerView = (ShadowRecyclerView) ShadowExtractor.extract(recyclerView);

    recyclerView.smoothScrollToPosition(scrollPosition);
    assertEquals(scrollPosition, shadowRecyclerView.getSmoothScrollPosition());
}
项目:androidadvanced    文件:RecipeListActivityTest.java   
@Test
public void testOnToolbarClicked_RecyclerViewShouldScrollToTop() throws Exception {
    int scrollPosition = 1;
    int topScrollPosition = 0;
    Toolbar toolbar = (Toolbar)activity.findViewById(R.id.toolbar);
    RecyclerView recyclerView = (RecyclerView)activity.findViewById(R.id.recyclerView);
    ShadowRecyclerView shadowRecyclerView = (ShadowRecyclerView) ShadowExtractor.extract(recyclerView);
    recyclerView.smoothScrollToPosition(scrollPosition);

    toolbar.performClick();

    assertEquals(topScrollPosition, shadowRecyclerView.getSmoothScrollPosition());
}
项目:androidadvanced    文件:RecipeListActivityTest.java   
private void setUpShadowAdapter(int positionToClick){
    when(recipe.getSourceURL()).thenReturn("http://galileo.edu");
    when(recipeList.get(positionToClick)).thenReturn(recipe);

    RecyclerView recyclerView = (RecyclerView)activity.findViewById(R.id.recyclerView);
    RecipesAdapter adapterPopulated = new RecipesAdapter(recipeList, imageLoader, onItemClickListener);
    recyclerView.setAdapter(adapterPopulated);
    shadowAdapter = (ShadowRecyclerViewAdapter) ShadowExtractor.extract(recyclerView.getAdapter());
}
项目:androidadvanced    文件:RecipesAdapterTest.java   
@Override
public void setUp() throws Exception {
    super.setUp();
    URL = "http://galileo.edu";
    when(recipe.getSourceURL()).thenReturn(URL);

    adapter = new RecipesAdapter(recipeList, imageLoader, onItemClickListener);
    shadowAdapter = (ShadowRecyclerViewAdapter) ShadowExtractor.extract(adapter);

    Activity activity = Robolectric.setupActivity(Activity.class);
    RecyclerView recyclerView = new RecyclerView(activity);
    recyclerView.setLayoutManager(new LinearLayoutManager(activity));

    recyclerView.setAdapter(adapter);
}
项目:androidadvanced    文件:RecipeMainActivityTest.java   
@Test
public void testOnSwipeToKeep_ShouldSaveRecipe() throws Exception {
    activity.setRecipe(currentRecipe);

    ImageView imgRecipe = (ImageView) activity.findViewById(R.id.imgRecipe);
    ShadowImageView shadowImage = (ShadowImageView) ShadowExtractor.extract(imgRecipe);
    shadowImage.performSwipe(200, 200, 500, 250, 50);
    verify(presenter).saveRecipe(currentRecipe);
}
项目:androidadvanced    文件:RecipeMainActivityTest.java   
@Test
public void testOnSwipeToDismiss_ShouldDiscardRecipe() throws Exception {
    ImageView imgRecipe = (ImageView) activity.findViewById(R.id.imgRecipe);
    ShadowImageView shadowImage = (ShadowImageView) ShadowExtractor.extract(imgRecipe);
    shadowImage.performSwipe(200, 200, -500, 250, 50);
    verify(presenter).dismissRecipe();
}
项目:RNLearn_Project1    文件:ShareModuleTest.java   
@Test
public void testShareDialog() {
  final String title = "Title";
  final String message = "Message";
  final String dialogTitle = "Dialog Title";

  JavaOnlyMap content = new JavaOnlyMap();
  content.putString("title", title);
  content.putString("message", message);

  final SimplePromise promise = new SimplePromise();

  mShareModule.share(content, dialogTitle, promise);

  final Intent chooserIntent = 
    ((ShadowApplication)ShadowExtractor.extract(RuntimeEnvironment.application)).getNextStartedActivity();
  assertNotNull("Dialog was not displayed", chooserIntent);
  assertEquals(Intent.ACTION_CHOOSER, chooserIntent.getAction());
  assertEquals(dialogTitle, chooserIntent.getExtras().get(Intent.EXTRA_TITLE));

  final Intent contentIntent = (Intent)chooserIntent.getExtras().get(Intent.EXTRA_INTENT);
  assertNotNull("Intent was not built correctly", contentIntent);
  assertEquals(Intent.ACTION_SEND, contentIntent.getAction());
  assertEquals(title, contentIntent.getExtras().get(Intent.EXTRA_SUBJECT));
  assertEquals(message, contentIntent.getExtras().get(Intent.EXTRA_TEXT));

  assertEquals(1, promise.getResolved());
}
项目:RNLearn_Project1    文件:ShareModuleTest.java   
@Test
public void testShareDialog() {
  final String title = "Title";
  final String message = "Message";
  final String dialogTitle = "Dialog Title";

  JavaOnlyMap content = new JavaOnlyMap();
  content.putString("title", title);
  content.putString("message", message);

  final SimplePromise promise = new SimplePromise();

  mShareModule.share(content, dialogTitle, promise);

  final Intent chooserIntent = 
    ((ShadowApplication)ShadowExtractor.extract(RuntimeEnvironment.application)).getNextStartedActivity();
  assertNotNull("Dialog was not displayed", chooserIntent);
  assertEquals(Intent.ACTION_CHOOSER, chooserIntent.getAction());
  assertEquals(dialogTitle, chooserIntent.getExtras().get(Intent.EXTRA_TITLE));

  final Intent contentIntent = (Intent)chooserIntent.getExtras().get(Intent.EXTRA_INTENT);
  assertNotNull("Intent was not built correctly", contentIntent);
  assertEquals(Intent.ACTION_SEND, contentIntent.getAction());
  assertEquals(title, contentIntent.getExtras().get(Intent.EXTRA_SUBJECT));
  assertEquals(message, contentIntent.getExtras().get(Intent.EXTRA_TEXT));

  assertEquals(1, promise.getResolved());
}