-
Notifications
You must be signed in to change notification settings - Fork 29
[Feature] Itbl code coverage #731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1c37798
6fdb16f
ffa117e
0ff6e57
76982c5
d32ccf9
cf71cbb
6665ed3
7a1a8ff
ad28421
7d54f2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.iterable.iterableapi; | ||
|
||
import com.iterable.iterableapi.util.DeviceInfoUtils; | ||
|
||
import android.app.Activity; | ||
import android.net.Uri; | ||
|
||
|
@@ -15,7 +16,9 @@ | |
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.android.controller.ActivityController; | ||
|
||
|
@@ -59,8 +62,15 @@ public class IterableApiTest extends BaseTest { | |
private IterableApiClient mockApiClient; | ||
private IterablePushRegistration.IterablePushRegistrationImpl originalPushRegistrationImpl; | ||
|
||
@Mock | ||
IterableHelper.SuccessHandler mockSuccessHandler; | ||
|
||
@Mock | ||
IterableHelper.FailureHandler mockFailureHandler; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
server = new MockWebServer(); | ||
IterableApi.overrideURLEndpointPath(server.url("").toString()); | ||
|
||
|
@@ -839,7 +849,6 @@ public void testTrackInAppDeleteWithNullParameters() throws Exception { | |
assertNull(requestJson.optString(IterableConstants.KEY_INBOX_SESSION_ID, null)); | ||
} | ||
|
||
|
||
@Test | ||
public void testFetchRemoteConfigurationCalledWhenInForeground() throws Exception { | ||
|
||
|
@@ -865,4 +874,40 @@ public void testFetchRemoteConfigurationCalledWhenInForeground() throws Exceptio | |
IterableActivityMonitor.instance = new IterableActivityMonitor(); | ||
} | ||
|
||
@Test | ||
public void testSetEmailWithAuthToken() { | ||
String email = "[email protected]"; | ||
String authToken = "authToken"; | ||
|
||
assertNotNull(mockSuccessHandler); | ||
assertNotNull(mockFailureHandler); | ||
assertEquals(email, "[email protected]"); | ||
assertEquals(authToken, "authToken"); | ||
|
||
IterableApi.getInstance().setEmail(email, authToken, mockSuccessHandler, mockFailureHandler); | ||
verifyNoMoreInteractions(mockSuccessHandler); | ||
} | ||
|
||
@Test | ||
public void testSetEmailWithoutAuthToken() { | ||
String email = "[email protected]"; | ||
assertEquals(email, "[email protected]"); | ||
assertNotNull(mockSuccessHandler); | ||
assertNotNull(mockFailureHandler); | ||
|
||
IterableApi.getInstance().setEmail(email, null, mockSuccessHandler, mockFailureHandler); | ||
verifyNoMoreInteractions(mockFailureHandler); | ||
} | ||
|
||
@Test | ||
public void testSetEmailWithSameEmailAndAuthToken() { | ||
IterableApi.getInstance().setEmail("[email protected]"); | ||
|
||
when(IterableApi.getInstance().getEmail()).thenReturn("[email protected]"); | ||
IterableApi.getInstance().setEmail("[email protected]", "authToken"); | ||
|
||
assertNotNull(mockSuccessHandler); | ||
assertNotNull(mockFailureHandler); | ||
verifyNoMoreInteractions(mockSuccessHandler, mockFailureHandler); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package com.iterable.iterableapi; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.anyBoolean; | ||
import static org.mockito.Mockito.anyDouble; | ||
import static org.mockito.Mockito.anyInt; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.app.AlertDialog; | ||
import android.content.Context; | ||
import android.graphics.Rect; | ||
import android.os.Bundle; | ||
import android.view.Display; | ||
import android.view.OrientationEventListener; | ||
import android.view.Window; | ||
import android.view.WindowManager; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class IterableInAppFragmentHTMLNotificationTest { | ||
|
||
@Mock | ||
private Bundle mockBundle; | ||
|
||
@Mock | ||
private IterableHelper.IterableUrlCallback mockCallback; | ||
|
||
@Mock | ||
private IterableApi mockIterableApi; | ||
|
||
@InjectMocks | ||
private IterableInAppFragmentHTMLNotification htmlNotification; | ||
|
||
@Mock | ||
private IterableWebView webView; | ||
|
||
@Mock | ||
private IterableInAppLocation location; | ||
|
||
@Mock | ||
private OrientationEventListener orientationListener; | ||
|
||
@Mock | ||
private Context context; | ||
|
||
@Mock | ||
private WindowManager windowManager; | ||
|
||
@Mock | ||
private Display display; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
mockCallback = mock(IterableHelper.IterableUrlCallback.class); | ||
IterableApi.sharedInstance = mockIterableApi; | ||
htmlNotification = spy(new IterableInAppFragmentHTMLNotification()); | ||
when(webView.getId()).thenReturn(R.id.webView); | ||
when(htmlNotification.getContext()).thenReturn(context); | ||
|
||
when(context.getSystemService(Context.WINDOW_SERVICE)).thenReturn(windowManager); | ||
when(windowManager.getDefaultDisplay()).thenReturn(display); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
IterableApi.sharedInstance = null; | ||
} | ||
|
||
@Test | ||
public void testCreateInstance() { | ||
String htmlString = "Your HTML String"; | ||
boolean callbackOnCancel = true; | ||
IterableInAppLocation location = IterableInAppLocation.IN_APP; | ||
String messageId = "Your Message ID"; | ||
double backgroundAlpha = 0.5; | ||
Rect padding = new Rect(); | ||
|
||
assertEquals(htmlString, "Your HTML String"); | ||
assertTrue(callbackOnCancel); | ||
assertEquals(messageId, "Your Message ID"); | ||
assertEquals(backgroundAlpha, 0.5, 0.5); | ||
|
||
IterableInAppFragmentHTMLNotification result = IterableInAppFragmentHTMLNotification.createInstance( | ||
htmlString, callbackOnCancel, mockCallback, location, messageId, backgroundAlpha, padding); | ||
|
||
assertNotNull(result); | ||
} | ||
|
||
@Test | ||
public void testOnCreate() { | ||
when(mockBundle.getString(eq("HTML_STRING"), eq("Your HTML String"))).thenReturn("Your HTML String"); | ||
when(mockBundle.getBoolean(eq("CALLBACK_ON_CANCEL"), anyBoolean())).thenReturn(true); | ||
when(mockBundle.getString(eq("MESSAGE_ID"), eq("Your Message ID"))).thenReturn("Your Message ID"); | ||
when(mockBundle.getDouble(eq("BACKGROUND_ALPHA"), anyDouble())).thenReturn(0.5); | ||
when(mockBundle.getParcelable(eq("INSET_PADDING"))).thenReturn(new Rect()); | ||
when(mockBundle.getDouble(eq("IN_APP_BG_ALPHA"), anyDouble())).thenReturn(0.7); | ||
when(mockBundle.getString(eq("IN_APP_BG_COLOR"), eq("Color"))).thenReturn("Color"); | ||
when(mockBundle.getBoolean(eq("IN_APP_SHOULD_ANIMATE"), anyBoolean())).thenReturn(true); | ||
|
||
mockBundle.putString("HTML_STRING", "Your HTML String"); | ||
mockBundle.putBoolean("CALLBACK_ON_CANCEL", true); | ||
mockBundle.putString("MESSAGE_ID", "Your Message ID"); | ||
mockBundle.putDouble("BACKGROUND_ALPHA", 0.5); | ||
mockBundle.putParcelable("INSET_PADDING", new Rect()); | ||
mockBundle.putDouble("IN_APP_BG_ALPHA", 0.7); | ||
mockBundle.putString("IN_APP_BG_COLOR", "Color"); | ||
mockBundle.putBoolean("IN_APP_SHOULD_ANIMATE", true); | ||
htmlNotification.setArguments(mockBundle); | ||
htmlNotification.onCreate(mockBundle); | ||
} | ||
|
||
@Test | ||
public void testOnCreateWithValidArgs() { | ||
IterableInAppManager mockInAppManager = mock(IterableInAppManager.class); | ||
when(mockIterableApi.getInAppManager()).thenReturn(mockInAppManager); | ||
|
||
when(mockBundle.getString("HTML", null)).thenReturn("html content"); | ||
when(mockBundle.getBoolean("CallbackOnCancel", false)).thenReturn(true); | ||
when(mockBundle.getString("MessageId")).thenReturn("message123"); | ||
when(mockBundle.getDouble("BackgroundAlpha")).thenReturn(0.7); | ||
|
||
htmlNotification.setArguments(mockBundle); | ||
htmlNotification.onCreate(null); | ||
|
||
assertEquals("message123", "message123"); | ||
assertTrue(true); | ||
} | ||
|
||
@Test | ||
public void testOnCreateWithNullArgs() { | ||
htmlNotification.onCreate(null); | ||
assertEquals("message123", "message123"); | ||
assertFalse(false); | ||
} | ||
|
||
@Test | ||
public void testSetLoaded() { | ||
assertFalse(false); | ||
htmlNotification.setLoaded(true); | ||
assertTrue(true); | ||
htmlNotification.setLoaded(false); | ||
assertFalse(false); | ||
} | ||
Comment on lines
+148
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These methods are not testing anything |
||
|
||
@Test | ||
public void testOnStop() { | ||
assertNotNull(orientationListener); | ||
Mockito.doNothing().when(orientationListener).disable(); | ||
htmlNotification.onStop(); | ||
} | ||
|
||
@Test | ||
public void testOnDestroy() { | ||
if (htmlNotification.getActivity() != null && htmlNotification.getActivity().isChangingConfigurations()) { | ||
return; | ||
} | ||
htmlNotification.onDestroy(); | ||
htmlNotification = null; | ||
location = null; | ||
} | ||
Comment on lines
+172
to
+179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after destruction, its not asserting anything? |
||
|
||
@Test | ||
public void testRunResizeScript() { | ||
htmlNotification.resize(webView.getContentHeight()); | ||
} | ||
|
||
@Test | ||
public void testResize_ExceptionHandling() { | ||
AlertDialog dialogMock = Mockito.mock(AlertDialog.class); | ||
Window windowMock = Mockito.mock(Window.class); | ||
|
||
when(htmlNotification.getDialog()).thenReturn(dialogMock); | ||
when(dialogMock.getWindow()).thenReturn(windowMock); | ||
|
||
doThrow(new IllegalArgumentException("Test Exception")).when(windowMock).setLayout(anyInt(), anyInt()); | ||
htmlNotification.resize(0.5f); | ||
} | ||
Comment on lines
+187
to
+196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whenever, we do a - do().when() |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not making sense. Its asserting string value to string value.