diff --git a/README.md b/README.md index 67c59610c..3e014915c 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,10 @@ A collection of samples demonstrating different frameworks and techniques for au **[BasicSample](https://github.com/googlesamples/android-testing/tree/master/ui/uiautomator/BasicSample)** - Basic UI Automator sample +### Robolectric Sample + +**[BasicRobolectricSample](https://github.com/googlesamples/android-testing/tree/master/unit/BasicRobolectricSample)** - Basic Robolectric sample + ### AndroidJUnitRunner Sample **[AndroidJunitRunnerSample](https://github.com/googlesamples/android-testing/tree/master/runner/AndroidJunitRunnerSample)** - Showcases test annotations, parameterized tests and testsuite creation diff --git a/projects.conf b/projects.conf index 56de9769c..9233d12be 100644 --- a/projects.conf +++ b/projects.conf @@ -11,4 +11,5 @@ ui/espresso/RecyclerViewSample ui/espresso/WebBasicSample ui/uiautomator/BasicSample unit/BasicSample +unit/BasicRobolectricSample unit/BasicUnitAndroidTest diff --git a/unit/BasicRobolectricSample/.gitignore b/unit/BasicRobolectricSample/.gitignore new file mode 100644 index 000000000..1406630a7 --- /dev/null +++ b/unit/BasicRobolectricSample/.gitignore @@ -0,0 +1,6 @@ +.gradle +local.properties +.idea +.DS_Store +build +*.iml diff --git a/unit/BasicRobolectricSample/README.md b/unit/BasicRobolectricSample/README.md new file mode 100644 index 000000000..4b149deeb --- /dev/null +++ b/unit/BasicRobolectricSample/README.md @@ -0,0 +1,19 @@ +# Basic sample for Robolectric + +*If you are new to Robolectric, try this sample first.* + +This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio is recommended. + +1. Download the project code, preferably using `git clone`. +1. In Android Studio, select *File* | *Open...* and point to the `./build.gradle` file. +1. Check out the relevant code: + * The application under test is located in `src/main/java` + * Tests are in `src/test/java` +1. Create the test configuration: + * Open *Run* menu | *Edit Configurations* + * Add a new *Android JUnit Tests* configuration + * Choose a module + * Set the working directory to the module directory. +1. Run the newly created configuration + +If you are using Android Studio, the *Run* window will show the test results. diff --git a/unit/BasicRobolectricSample/app/build.gradle b/unit/BasicRobolectricSample/app/build.gradle new file mode 100644 index 000000000..f03789608 --- /dev/null +++ b/unit/BasicRobolectricSample/app/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 26 + buildToolsVersion rootProject.buildToolsVersion + defaultConfig { + applicationId "com.example.android.testing.robolectric.BasicRobolectricSample" + minSdkVersion 14 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + } + lintOptions { + abortOnError false + } + + // Include resources in Robolectric tests (requires Android Studio 3.0+). + testOptions { + unitTests { + includeAndroidResources = true + } + } +} + +dependencies { + implementation 'com.google.guava:guava:20.0' + + // Testing-only dependencies + testImplementation "junit:junit:4.12" + + // Robolectric dependencies + testImplementation "org.robolectric:robolectric:" + rootProject.robolectricVersion +} diff --git a/unit/BasicRobolectricSample/app/src/main/AndroidManifest.xml b/unit/BasicRobolectricSample/app/src/main/AndroidManifest.xml new file mode 100644 index 000000000..c2ea44c98 --- /dev/null +++ b/unit/BasicRobolectricSample/app/src/main/AndroidManifest.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + diff --git a/unit/BasicRobolectricSample/app/src/main/java/com/example/android/testing/robolectric/BasicRobolectricSample/MainActivity.java b/unit/BasicRobolectricSample/app/src/main/java/com/example/android/testing/robolectric/BasicRobolectricSample/MainActivity.java new file mode 100644 index 000000000..0db2645d8 --- /dev/null +++ b/unit/BasicRobolectricSample/app/src/main/java/com/example/android/testing/robolectric/BasicRobolectricSample/MainActivity.java @@ -0,0 +1,69 @@ +/* + * Copyright 2017, The Android Open Source Project + * + * 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. + */ + +package com.example.android.testing.robolectric.BasicRobolectricSample; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; + +/** + * An {@link Activity} that gets a text string from the user and displays it back when the user + * clicks on one of the two buttons. The first one shows it in the same activity and the second + * one opens another activity and displays the message. + */ +public class MainActivity extends Activity implements View.OnClickListener { + + // The TextView used to display the message inside the Activity. + private TextView mTextView; + + // The EditText where the user types the message. + private EditText mEditText; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + // Set the listeners for the buttons. + findViewById(R.id.changeTextBt).setOnClickListener(this); + findViewById(R.id.activityChangeTextBtn).setOnClickListener(this); + + mTextView = findViewById(R.id.textToBeChanged); + mEditText = findViewById(R.id.editTextUserInput); + } + + @Override + public void onClick(View view) { + // Get the text from the EditText view. + final String text = mEditText.getText().toString(); + + switch (view.getId()) { + case R.id.changeTextBt: + // First button's interaction: set a text in a text view. + mTextView.setText(text); + break; + case R.id.activityChangeTextBtn: + // Second button's interaction: start an activity and send a message to it. + Intent intent = ShowTextActivity.newStartIntent(this, text); + startActivity(intent); + break; + } + } +} diff --git a/unit/BasicRobolectricSample/app/src/main/java/com/example/android/testing/robolectric/BasicRobolectricSample/ShowTextActivity.java b/unit/BasicRobolectricSample/app/src/main/java/com/example/android/testing/robolectric/BasicRobolectricSample/ShowTextActivity.java new file mode 100644 index 000000000..c00964487 --- /dev/null +++ b/unit/BasicRobolectricSample/app/src/main/java/com/example/android/testing/robolectric/BasicRobolectricSample/ShowTextActivity.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017, The Android Open Source Project + * + * 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. + */ + +package com.example.android.testing.robolectric.BasicRobolectricSample; + +import com.google.common.base.Strings; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.widget.TextView; + +/** + * A simple {@link Activity} that shows a message. + */ +public class ShowTextActivity extends Activity { + + // The name of the extra data sent through an {@link Intent}. + public final static String KEY_EXTRA_MESSAGE = + "com.example.android.testing.robolectric.basicsample.MESSAGE"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_show_text); + + // Get the message from the Intent. + Intent intent = getIntent(); + String message = Strings.nullToEmpty(intent.getStringExtra(KEY_EXTRA_MESSAGE)); + + // Show message. + ((TextView)findViewById(R.id.show_text_view)).setText(message); + } + + /** + * Creates an {@link Intent} for {@link ShowTextActivity} with the message to be displayed. + * @param context the {@link Context} where the {@link Intent} will be used + * @param message a {@link String} with text to be displayed + * @return an {@link Intent} used to start {@link ShowTextActivity} + */ + static protected Intent newStartIntent(Context context, String message) { + Intent newIntent = new Intent(context, ShowTextActivity.class); + newIntent.putExtra(KEY_EXTRA_MESSAGE, message); + return newIntent; + } +} diff --git a/unit/BasicRobolectricSample/app/src/main/res/drawable-hdpi/ic_launcher.png b/unit/BasicRobolectricSample/app/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100755 index 000000000..19a2cb6fa Binary files /dev/null and b/unit/BasicRobolectricSample/app/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/unit/BasicRobolectricSample/app/src/main/res/drawable-mdpi/ic_launcher.png b/unit/BasicRobolectricSample/app/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100755 index 000000000..4d5290892 Binary files /dev/null and b/unit/BasicRobolectricSample/app/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/unit/BasicRobolectricSample/app/src/main/res/drawable-xhdpi/ic_launcher.png b/unit/BasicRobolectricSample/app/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100755 index 000000000..9c2a2b6d7 Binary files /dev/null and b/unit/BasicRobolectricSample/app/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/unit/BasicRobolectricSample/app/src/main/res/drawable-xxhdpi/ic_launcher.png b/unit/BasicRobolectricSample/app/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100755 index 000000000..357eaff45 Binary files /dev/null and b/unit/BasicRobolectricSample/app/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/unit/BasicRobolectricSample/app/src/main/res/drawable-xxxhdpi/ic_launcher.png b/unit/BasicRobolectricSample/app/src/main/res/drawable-xxxhdpi/ic_launcher.png new file mode 100755 index 000000000..555912e12 Binary files /dev/null and b/unit/BasicRobolectricSample/app/src/main/res/drawable-xxxhdpi/ic_launcher.png differ diff --git a/unit/BasicRobolectricSample/app/src/main/res/layout/activity_main.xml b/unit/BasicRobolectricSample/app/src/main/res/layout/activity_main.xml new file mode 100644 index 000000000..9239af843 --- /dev/null +++ b/unit/BasicRobolectricSample/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,60 @@ + + + + + + + + + + +