Skip to content

Commit

Permalink
[f] 442 - As devices' user, I want to receive the push message and di…
Browse files Browse the repository at this point in the history
…splay

to screen
  • Loading branch information
KienLeVan authored and hoangphanea committed Jan 9, 2016
1 parent 6463a42 commit b74f190
Show file tree
Hide file tree
Showing 22 changed files with 710 additions and 67 deletions.
25 changes: 25 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 23
Expand All @@ -10,18 +11,42 @@ android {
targetSdkVersion 23
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

compile 'com.google.android.gms:play-services-gcm:8.3.0'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-jackson:2.0.0-beta2'

androidTestCompile ('com.android.support.test:runner:0.4.1') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test:rules:0.4.1') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

}
42 changes: 42 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"project_info": {
"project_id": "watd-gcm",
"project_number": "120607825117",
"name": "WaTD-GCm"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:120607825117:android:6359edd5290a5eb2",
"client_id": "android:com.eahackathon.watd.whosatthedoor",
"client_type": 1,
"android_client_info": {
"package_name": "com.eahackathon.watd.whosatthedoor"
}
},
"oauth_client": [],
"api_key": [],
"services": {
"analytics_service": {
"status": 1
},
"cloud_messaging_service": {
"status": 2,
"apns_config": []
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"google_signin_service": {
"status": 1
},
"ads_service": {
"status": 1
}
}
}
],
"client_info": [],
"ARTIFACT_VERSION": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.eahackathon.watd.whosatthedoor;

import android.content.Intent;
import android.support.test.rule.ActivityTestRule;

import com.eahackathon.watd.whosatthedoor.helpers.GcmRegistrationHelper;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
* Created by Hoang Phan on 1/9/2016.
*/
public class MainActivityTest {
private MainActivity mActivity;

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

@Before
public void setUp() {
mActivity = mActivityTestRule.getActivity();
}

@After
public void tearDown() {
GcmRegistrationHelper.setInstance(null);
}

@Test
public void testMain_click_on_start_service() throws Exception {
GcmRegistrationHelper mockHelper = mock(GcmRegistrationHelper.class);
GcmRegistrationHelper.setInstance(mockHelper);

onView(withId(R.id.btn_start)).perform(click());
Thread.sleep(1000);

verify(mockHelper).register();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.eahackathon.watd.whosatthedoor;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;

import com.eahackathon.watd.whosatthedoor.models.ResponseModel;
import com.eahackathon.watd.whosatthedoor.models.UpdatePersonNameRequest;
import com.eahackathon.watd.whosatthedoor.network.APIService;
import com.eahackathon.watd.whosatthedoor.network.WaTDAPI;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import java.io.IOException;
import java.io.InputStream;

import retrofit.Call;
import retrofit.Callback;
import retrofit.Response;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.core.IsNot.not;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Created by Hoang Phan on 1/9/2016.
*/
public class NotificationActivityTest {

private NotificationActivity mActivity;
private WaTDAPI mMockWaTDAPI;
private ArgumentCaptor<Callback> mCallCaptor;
private Call<ResponseModel> mMockCall;

@Rule
public ActivityTestRule<NotificationActivity> mActivityTestRule = new ActivityTestRule<NotificationActivity>(NotificationActivity.class) {
@Override
protected Intent getActivityIntent() {
Context targetContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext();
Intent result = new Intent(targetContext, NotificationActivity.class);
result.putExtra(Constants.PERSON_NAME, "My name");
result.putExtra(Constants.PERSON_ID, "1");
AssetManager manager = targetContext.getAssets();
try {
InputStream is = manager.open("test.jpg");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
result.putExtra(Constants.PERSON_DATA, buffer);
} catch (IOException e) {
e.printStackTrace();
}

return result;
}
};

@Before
public void setUp() {
mActivity = mActivityTestRule.getActivity();
}

@After
public void tearDown() {
APIService.setInstance(null);
}

@Test
public void testNotification_show_name_and_image() throws InterruptedException {
onView(withId(R.id.et_person)).check(matches(withText("My name")));
onView(withId(R.id.tv_person)).check(matches(withText("My name")));
onView(withId(R.id.et_person)).check(matches(not(isDisplayed())));
onView(withId(R.id.tv_person)).check(matches(isDisplayed()));
onView(withId(R.id.iv_person)).check(matches(isDisplayed()));
onView(withId(R.id.btn_ok)).check(matches(isDisplayed()));
onView(withId(R.id.btn_another)).check(matches(isDisplayed()));
}

@Test
public void testNotification_click_another_btn() throws InterruptedException {
onView(withId(R.id.btn_another)).perform(click());

onView(withId(R.id.et_person)).check(matches(isDisplayed()));
onView(withId(R.id.tv_person)).check(matches(not(isDisplayed())));
onView(withId(R.id.btn_another)).check(matches(not(isDisplayed())));
}

@Test
public void testNotification_click_ok_success() throws InterruptedException {
Response<ResponseModel> fakeSuccessResponse = Response.success(new ResponseModel());
setUpMockCall();

onView(withId(R.id.btn_ok)).perform(click());
Thread.sleep(1000);

verify(mMockCall).enqueue(mCallCaptor.capture());
mCallCaptor.getValue().onResponse(fakeSuccessResponse, null);

assertTrue(mActivity.isFinishing());
}

@Test
public void testNotification_click_ok_failure() throws InterruptedException {
setUpMockCall();

onView(withId(R.id.btn_ok)).perform(click());
Thread.sleep(1000);

verify(mMockCall).enqueue(mCallCaptor.capture());
mCallCaptor.getValue().onFailure(null);

assertFalse(mActivity.isFinishing());
}

private void setUpMockCall() {
mMockWaTDAPI = Mockito.mock(WaTDAPI.class);
mCallCaptor = ArgumentCaptor.forClass(Callback.class);
mMockCall = Mockito.mock(Call.class);

when(mMockWaTDAPI.updatePersonName(any(String.class), any(UpdatePersonNameRequest.class))).thenReturn(mMockCall);
APIService.setInstance(mMockWaTDAPI);
}
}
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />

<category android:name="com.example.gcm" />
</intent-filter>
</receiver>

<service
android:name=".services.GCMListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>

<activity android:name=".NotificationActivity"></activity>
</application>

</manifest>
Binary file added app/src/main/assets/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.eahackathon.watd.whosatthedoor;

/**
* Created by KienDu on 1/9/2016.
*/
public interface Constants {
String API_ENDPOINT = "http://dcb86d07.ngrok.io";

String PERSON_NAME = "name";
String PERSON_ID = "id";
String PERSON_DATA = "data";
}
Loading

0 comments on commit b74f190

Please sign in to comment.