|
1 | 1 | package com.google.unity.ads.nativead; |
2 | 2 |
|
3 | 3 | import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 6 | +import static org.mockito.Mockito.when; |
4 | 7 |
|
| 8 | +import android.content.Context; |
5 | 9 | import android.graphics.Color; |
6 | 10 | import android.graphics.drawable.ColorDrawable; |
| 11 | +import android.view.LayoutInflater; |
| 12 | +import androidx.test.core.app.ApplicationProvider; |
| 13 | +import com.google.android.ads.nativetemplates.NativeTemplateStyle; |
| 14 | +import com.google.android.ads.nativetemplates.TemplateView; |
7 | 15 | import org.junit.Before; |
| 16 | +import org.junit.Rule; |
8 | 17 | import org.junit.Test; |
9 | 18 | import org.junit.runner.RunWith; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.junit.MockitoJUnit; |
| 21 | +import org.mockito.junit.MockitoRule; |
10 | 22 | import org.robolectric.RobolectricTestRunner; |
11 | 23 |
|
12 | 24 | /** Tests for {@link UnityNativeTemplateStyle} */ |
13 | 25 | @RunWith(RobolectricTestRunner.class) |
14 | 26 | public final class UnityNativeTemplateStyleTest { |
15 | 27 |
|
| 28 | + @Rule public final MockitoRule mockito = MockitoJUnit.rule(); |
| 29 | + @Mock private LayoutInflater mockLayoutInflater; |
| 30 | + |
16 | 31 | private UnityNativeTemplateStyle unityNativeTemplateStyle; |
| 32 | + private Context context; |
17 | 33 |
|
18 | 34 | @Before |
19 | 35 | public void setUp() { |
@@ -41,6 +57,7 @@ public void setUp() { |
41 | 57 | /* backgroundColor= */ new ColorDrawable(Color.BLUE), |
42 | 58 | /* fontStyle= */ UnityNativeTemplateFontStyle.ITALIC, |
43 | 59 | /* size= */ 10.0)); |
| 60 | + context = ApplicationProvider.getApplicationContext(); |
44 | 61 | } |
45 | 62 |
|
46 | 63 | @Test |
@@ -107,4 +124,37 @@ public void unityNativeTemplateStyle_canBeCompared() throws Exception { |
107 | 124 | assertThat(unityNativeTemplateStyle.hashCode()) |
108 | 125 | .isNotEqualTo(anotherUnityNativeTemplateStyle.hashCode()); |
109 | 126 | } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void unityNativeTemplateStyle_asTemplateView_canBeInflated() throws Exception { |
| 130 | + // Arrange. |
| 131 | + TemplateView fakeTemplateView = new TemplateView(context); |
| 132 | + assertThat(fakeTemplateView.getStyles()).isNull(); |
| 133 | + when(mockLayoutInflater.inflate(anyInt(), any())).thenReturn(fakeTemplateView); |
| 134 | + |
| 135 | + // Act. |
| 136 | + unityNativeTemplateStyle.setLayoutInflater(mockLayoutInflater); |
| 137 | + fakeTemplateView = unityNativeTemplateStyle.asTemplateView(context); |
| 138 | + NativeTemplateStyle styles = fakeTemplateView.getStyles(); |
| 139 | + |
| 140 | + // Assert (styles should have been applied). |
| 141 | + assertThat(styles).isNotNull(); |
| 142 | + assertThat(styles.getMainBackgroundColor().getColor()).isEqualTo(Color.RED); |
| 143 | + assertThat(styles.getCallToActionBackgroundColor().getColor()).isEqualTo(Color.BLUE); |
| 144 | + assertThat(styles.getCallToActionTypefaceColor()).isEqualTo(Color.GREEN); |
| 145 | + assertThat(styles.getCallToActionTextTypeface()).isNotNull(); |
| 146 | + assertThat(styles.getCallToActionTextSize()).isEqualTo(1.0f); |
| 147 | + assertThat(styles.getPrimaryTextBackgroundColor().getColor()).isEqualTo(Color.BLUE); |
| 148 | + assertThat(styles.getPrimaryTextTypefaceColor()).isEqualTo(Color.RED); |
| 149 | + assertThat(styles.getPrimaryTextTypeface()).isNotNull(); |
| 150 | + assertThat(styles.getPrimaryTextSize()).isEqualTo(5.0f); |
| 151 | + assertThat(styles.getSecondaryTextBackgroundColor().getColor()).isEqualTo(Color.BLUE); |
| 152 | + assertThat(styles.getSecondaryTextTypefaceColor()).isEqualTo(Color.RED); |
| 153 | + assertThat(styles.getSecondaryTextTypeface()).isNotNull(); |
| 154 | + assertThat(styles.getSecondaryTextSize()).isEqualTo(20.0f); |
| 155 | + assertThat(styles.getTertiaryTextBackgroundColor().getColor()).isEqualTo(Color.BLUE); |
| 156 | + assertThat(styles.getTertiaryTextTypefaceColor()).isEqualTo(Color.RED); |
| 157 | + assertThat(styles.getTertiaryTextTypeface()).isNotNull(); |
| 158 | + assertThat(styles.getTertiaryTextSize()).isEqualTo(10.0f); |
| 159 | + } |
110 | 160 | } |
0 commit comments