Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e11aeb4
commit e173e6f
Showing
22 changed files
with
413 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
Testing/EspressoIdle/app/src/androidTest/java/com/commonsware/android/async/AsyncTest.java
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
...ing/EspressoIdle/app/src/androidTest/java/com/commonsware/android/okhttp/OkHttpTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 0 additions & 97 deletions
97
Testing/EspressoIdle/app/src/main/java/com/commonsware/android/async/AsyncDemoFragment.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Testing/EspressoIdle/app/src/main/java/com/commonsware/android/okhttp/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.