Permalink
Browse files

Open source Drawee span tests

Reviewed By: kirwan

Differential Revision: D4333532

fbshipit-source-id: 26012d7cd20610098c9e43d342df242eef54f981
  • Loading branch information...
1 parent 38a6172 commit a89bdfb56f366dadeb6496b5099278766e9804e6 @oprisnik oprisnik committed with facebook-github-bot Dec 15, 2016
@@ -10,6 +10,14 @@ dependencies {
compile project(path: ':drawee')
compile project(path: ':fbcore')
compile project(path: ':imagepipeline-base')
+
+ testCompile "junit:junit:${JUNIT_VERSION}"
+ testCompile "org.easytesting:fest-assert-core:${FEST_ASSERT_CORE_VERSION}"
+ testCompile "org.mockito:mockito-core:${MOCKITO_CORE_VERSION}"
+ testCompile("org.robolectric:robolectric:${ROBOLECTRIC_VERSION}") {
+ exclude group: 'commons-logging', module: 'commons-logging'
+ exclude group: 'org.apache.httpcomponents', module: 'httpclient'
+ }
}
apply from: rootProject.file('release.gradle')
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2015-present, Facebook, Inc.
+ * All rights reserved.
+ *
+ * This source code is licensed under the BSD-style license found in the
+ * LICENSE file in the root directory of this source tree. An additional grant
+ * of patent rights can be found in the PATENTS file in the same directory.
+ */
+
+
+package com.facebook.drawee.span;
+
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+import android.view.View;
+
+import com.facebook.drawee.view.DraweeHolder;
+import com.facebook.widget.text.span.BetterImageSpan;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.fest.assertions.api.Assertions.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests {@link DraweeSpanStringBuilder}
+ */
+@RunWith(RobolectricTestRunner.class)
+public class DraweeSpanStringBuilderTest {
+
+ private static final String TEXT = "ABCDEFG";
+ private static final int DRAWABLE_WIDTH = 10;
+ private static final int DRAWABLE_HEIGHT = 32;
+
+ @Mock public DraweeHolder mDraweeHolder;
+ @Mock public Drawable mTopLevelDrawable;
+ @Mock public Rect mDrawableBounds;
+ @Mock public View mView;
+
+ private DraweeSpanStringBuilder mDraweeSpanStringBuilder;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ when(mDraweeHolder.getTopLevelDrawable()).thenReturn(mTopLevelDrawable);
+ when(mTopLevelDrawable.getBounds()).thenReturn(mDrawableBounds);
+ when(mDrawableBounds.width()).thenReturn(0);
+ when(mDrawableBounds.height()).thenReturn(0);
+ mDraweeSpanStringBuilder = new DraweeSpanStringBuilder(TEXT);
+ }
+
+ @Test
+ public void testTextCorrect() {
+ assertThat(mDraweeSpanStringBuilder.toString()).isEqualTo(TEXT);
+ }
+
+ @Test
+ public void testNoDraweeSpan() {
+ assertThat(mDraweeSpanStringBuilder.hasDraweeSpans()).isFalse();
+ }
+
+ @Test
+ public void testDraweeSpanAdded() {
+ addDraweeSpan(mDraweeSpanStringBuilder, mDraweeHolder, 3, 1);
+
+ assertThat(mDraweeSpanStringBuilder.toString()).isEqualTo(TEXT);
+ assertThat(mDraweeSpanStringBuilder.hasDraweeSpans()).isTrue();
+ }
+
+ @Test
+ public void testLifecycle() {
+ addDraweeSpan(mDraweeSpanStringBuilder, mDraweeHolder, 3, 1);
+
+ mDraweeSpanStringBuilder.onAttach();
+ verify(mDraweeHolder).onAttach();
+
+ mDraweeSpanStringBuilder.onDetach();
+ verify(mDraweeHolder).onDetach();
+ }
+
+ @Test
+ public void testDraweeSpanInSpannable() {
+ addDraweeSpan(mDraweeSpanStringBuilder, mDraweeHolder, 3, 1);
+ DraweeSpan[] draweeSpans = mDraweeSpanStringBuilder.getSpans(
+ 0,
+ mDraweeSpanStringBuilder.length(),
+ DraweeSpan.class);
+
+ assertThat(draweeSpans).hasSize(1);
+ assertThat(draweeSpans[0].getDrawable()).isEqualTo(mTopLevelDrawable);
+ }
+
+ private static void addDraweeSpan(
+ DraweeSpanStringBuilder draweeSpanStringBuilder,
+ DraweeHolder draweeHolder,
+ int index,
+ int spanLength) {
+ draweeSpanStringBuilder.setImageSpan(
+ draweeHolder, /* draweeHolder */
+ index, /* startIndex */
+ index + spanLength, /* endIndex */
+ DRAWABLE_WIDTH, /* drawableWidthPx */
+ DRAWABLE_HEIGHT, /* drawableHeightPx */
+ false, /* enableResizing */
+ BetterImageSpan.ALIGN_CENTER); /* verticalAlignment */
+ }
+}
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2015-present, Facebook, Inc.
+ * All rights reserved.
+ *
+ * This source code is licensed under the BSD-style license found in the
+ * LICENSE file in the root directory of this source tree. An additional grant
+ * of patent rights can be found in the PATENTS file in the same directory.
+ */
+
+
+package com.facebook.drawee.span;
+
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+
+import com.facebook.drawee.view.DraweeHolder;
+import com.facebook.widget.text.span.BetterImageSpan;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.fest.assertions.api.Assertions.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests {@link DraweeSpan}
+ */
+@RunWith(RobolectricTestRunner.class)
+public class DraweeSpanTest {
+
+ @Mock public DraweeHolder mDraweeHierarchy;
+ @Mock public Rect mBounds;
+ @Mock public Drawable mDrawable;
+
+ private DraweeSpan mDraweeSpan;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ when(mDraweeHierarchy.getTopLevelDrawable()).thenReturn(mDrawable);
+ when(mDrawable.getBounds()).thenReturn(mBounds);
+ mDraweeSpan = new DraweeSpan(mDraweeHierarchy, BetterImageSpan.ALIGN_CENTER);
+ }
+
+ @Test
+ public void testLifecycle() {
+ mDraweeSpan.onAttach();
+ verify(mDraweeHierarchy).onAttach();
+
+ mDraweeSpan.onDetach();
+ verify(mDraweeHierarchy).onDetach();
+ }
+
+ @Test
+ public void testGetDrawable() {
+ Drawable drawable = mDraweeSpan.getDrawable();
+ verify(mDraweeHierarchy).getTopLevelDrawable();
+ assertThat(drawable).isEqualTo(mDrawable);
+ }
+}

0 comments on commit a89bdfb

Please sign in to comment.