Skip to content

Commit

Permalink
completed edition of sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsguy committed Aug 21, 2016
1 parent e11aeb4 commit e173e6f
Show file tree
Hide file tree
Showing 22 changed files with 413 additions and 220 deletions.
7 changes: 7 additions & 0 deletions Testing/EspressoIdle/README.markdown
@@ -0,0 +1,7 @@
This sample Android app demonstrates
the use of OkHttp 3.x.

This app is covered in
[the chapter on Internet access](https://commonsware.com/Android/previews/internet-access)
in [*The Busy Coder's Guide to Android Development*](https://commonsware.com/Android/).

28 changes: 14 additions & 14 deletions Testing/EspressoIdle/app/build.gradle
@@ -1,22 +1,22 @@
apply plugin: 'com.android.application'

dependencies {
androidTestCompile 'com.android.support:support-annotations:24.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.okhttp3:okhttp:3.4.0'
androidTestCompile 'com.android.support:support-annotations:24.1.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'
}

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
compileSdkVersion 24
buildToolsVersion "24.0.1"

defaultConfig {
minSdkVersion 14
targetSdkVersion 22
testApplicationId "com.commonsware.android.async.test"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

packagingOptions {
exclude 'LICENSE.txt'
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
testApplicationId "com.commonsware.android.espresso.idle"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}

This file was deleted.

@@ -0,0 +1,93 @@
/***
Copyright (c) 2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.okhttp;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.ViewAssertion;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.widget.AdapterView;
import com.jakewharton.espresso.OkHttp3IdlingResource;
import junit.framework.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public class OkHttpTests {
@Rule
public final ActivityTestRule<MainActivity> main
=new ActivityTestRule(MainActivity.class, true);

private static final String URL=
"https://wares.commonsware.com/test.json";
private static final String EXPECTED="{\"Hello\": \"world\"}";

@Test
public void syncTest() throws IOException {
OkHttpClient client=new OkHttpClient.Builder().build();
Request request=new Request.Builder().url(URL).build();
Response response=client.newCall(request).execute();

Assert.assertEquals(EXPECTED, response.body().string());
}

@Test
public void unreliableAsyncTest() {
onView(withId(android.R.id.list))
.check(new AdapterCountAssertion(100));
}

@Test
public void moreReliableAsyncTest() {
IdlingResource idleWild=
OkHttp3IdlingResource.create("okhttp3",
main.getActivity().getOkHttpClient());

Espresso.registerIdlingResources(idleWild);

try {
onView(withId(android.R.id.list))
.check(new AdapterCountAssertion(100));
}
finally {
Espresso.unregisterIdlingResources(idleWild);
}
}

static class AdapterCountAssertion implements ViewAssertion {
private final int count;

AdapterCountAssertion(int count) {
this.count=count;
}

@Override
public void check(View view,
NoMatchingViewException noViewFoundException) {
Assert.assertTrue(view instanceof AdapterView);
Assert.assertEquals(count,
((AdapterView)view).getAdapter().getCount());
}
}}
19 changes: 6 additions & 13 deletions Testing/EspressoIdle/app/src/main/AndroidManifest.xml
@@ -1,25 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.commonsware.android.async"
package="com.commonsware.android.okhttp"
android:versionCode="1"
android:versionName="1.0">

<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"/>

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light.DarkActionBar">
android:theme="@style/Theme.Apptheme">
<activity
android:name=".AsyncDemo"
android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand All @@ -29,4 +22,4 @@
</activity>
</application>

</manifest>
</manifest>

This file was deleted.

@@ -1,5 +1,5 @@
/***
Copyright (c) 2012-14 CommonsWare, LLC
Copyright (c) 2013-2015 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
Expand All @@ -12,20 +12,14 @@
https://commonsware.com/Android
*/

package com.commonsware.android.async;
package com.commonsware.android.okhttp;

import android.app.Activity;
import android.os.Bundle;

public class AsyncDemo extends Activity {
public class Item {
String title;
String link;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
getFragmentManager().beginTransaction()
.add(android.R.id.content,
new AsyncDemoFragment()).commit();
}
public String toString() {
return(title);
}
}
@@ -0,0 +1,59 @@
/***
Copyright (c) 2013-2015 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.okhttp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import de.greenrobot.event.EventBus;
import okhttp3.OkHttpClient;

public class MainActivity extends Activity {
private final OkHttpClient client=
new OkHttpClient.Builder().build();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
getFragmentManager().beginTransaction()
.add(android.R.id.content,
new QuestionsFragment()).commit();
}
}

@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}

@Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}

public void onEventMainThread(QuestionClickedEvent event) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(event.item.link)));
}

OkHttpClient getOkHttpClient() {
return(client);
}
}

0 comments on commit e173e6f

Please sign in to comment.