LikeTweetAction extends BaseTweetAction implements View.OnClickListener { @Override public void onClick(View view) { if (view instanceof ToggleImageButton) { final ToggleImageButton toggleImageButton = (ToggleImageButton) view; if (tweet.favorited) { tweetRepository.unfavorite(tweet.id, new LikeCallback(toggleImageButton, tweet, getActionCallback())); } else { tweetRepository.favorite(tweet.id, new LikeCallback(toggleImageButton, tweet, getActionCallback())); } } } LikeTweetAction(Tweet tweet, TweetUi tweetUi, Callback<Tweet> cb); @Override void onClick(View view);  }
@Test public void testOnClick_performFavorite() { final ArgumentCaptor<LikeTweetAction.LikeCallback> favoriteCbCaptor = ArgumentCaptor.forClass(LikeTweetAction.LikeCallback.class); final LikeTweetAction likeAction = new LikeTweetAction(TestFixtures.TEST_TWEET, mockTweetUi, mockCallback); final ToggleImageButton mockToggleButton = mock(ToggleImageButton.class); likeAction.onClick(mockToggleButton); verify(mockTweetRepository).favorite(eq(TestFixtures.TEST_TWEET.id), favoriteCbCaptor.capture()); assertEquals(mockToggleButton, favoriteCbCaptor.getValue().button); assertFalse(favoriteCbCaptor.getValue().tweet.favorited); }
@Test public void testOnClick_performUnfavorite() { final ArgumentCaptor<LikeTweetAction.LikeCallback> favoriteCbCaptor = ArgumentCaptor.forClass(LikeTweetAction.LikeCallback.class); final LikeTweetAction favoriteAction = new LikeTweetAction( TestFixtures.TEST_FAVORITED_TWEET, mockTweetUi, mockCallback); final ToggleImageButton mockToggleButton = mock(ToggleImageButton.class); favoriteAction.onClick(mockToggleButton); verify(mockTweetRepository).unfavorite(eq(TestFixtures.TEST_TWEET.id), favoriteCbCaptor.capture()); assertEquals(mockToggleButton, favoriteCbCaptor.getValue().button); assertTrue(favoriteCbCaptor.getValue().tweet.favorited); }