Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions projects.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ ui/espresso/RecyclerViewSample
ui/espresso/WebBasicSample
ui/uiautomator/BasicSample
unit/BasicSample
unit/BasicRobolectricSample
unit/BasicUnitAndroidTest
6 changes: 6 additions & 0 deletions unit/BasicRobolectricSample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.gradle
local.properties
.idea
.DS_Store
build
*.iml
19 changes: 19 additions & 0 deletions unit/BasicRobolectricSample/README.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions unit/BasicRobolectricSample/app/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment to indicate that this is required in order for Robolectric to work

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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
}
36 changes: 36 additions & 0 deletions unit/BasicRobolectricSample/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.testing.robolectric.BasicRobolectricSample" >

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android.testing.robolectric.BasicRobolectricSample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.android.testing.robolectric.BasicRobolectricSample.ShowTextActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,60 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
tools:context=".MainActivity">


<TextView
android:id="@+id/textToBeChanged"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/header_margin"
android:layout_marginTop="@dimen/header_margin"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge"/>


<EditText
android:id="@+id/editTextUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="@string/type_something"/>

<Button
style="?android:attr/buttonStyleSmall"
android:id="@+id/changeTextBt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change_text"
android:layout_gravity="center_horizontal"/>


<Button
style="?android:attr/buttonStyleSmall"
android:id="@+id/activityChangeTextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/open_activity_and_change_text"/>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 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.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShowTextActivity">

<TextView
android:id="@+id/show_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_gravity="center"/>

</merge>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 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.
-->
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"/>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 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.
-->
<resources>
<style name="AppTheme" parent="android:Theme.Material"/>
</resources>
Loading