Skip to content
Snippets Groups Projects
Commit a1ffa3d3 authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

Added Lab2_Intents

parent a751456d
No related branches found
No related tags found
No related merge requests found
Showing
with 542 additions and 0 deletions
# built application files
*.apk
*.ap_
# files for the dex VM
*.dex
# Java class files
*.class
# generated files
bin/
gen/
# Local configuration file (sdk path, etc)
local.properties
# Eclipse project files
.classpath
.settings
# Proguard folder generated by Eclipse
proguard/
# Intellij project files
*.iml
*.ipr
*.iws
.idea
.idea/workspace.xml
.gradle
build/
captures/
# Mac files
.DS_Store
# Windows thumbnail db
Thumbs.db
File added
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "course.labs.intentslab"
minSdkVersion 21
targetSdkVersion 26
testApplicationId "course.labs.intentslab.tests"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.6.3'
androidTestCompile 'com.android.support.test:rules:1.0.1'
androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
}
File added
package course.labs.intentslab.tests;
import android.test.ActivityInstrumentationTestCase2;
import com.robotium.solo.Solo;
import course.labs.intentslab.ActivityLoaderActivity;
public class ExplicitTest extends
ActivityInstrumentationTestCase2<ActivityLoaderActivity> {
private Solo solo;
public ExplicitTest() {
super(ActivityLoaderActivity.class);
}
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
@Override
public void tearDown() throws Exception {
super.tearDown();
if(solo!=null)
solo.finishOpenedActivities();
}
// Executes the ExplicitTest
public void testRun() {
int delay = 1000;
// =================== Section One =====================
// Wait for activity: 'course.labs.intentslab.ActivityLoaderActivity'
assertTrue(
"ExplicitTest:" + "Section One:"
+ "ActivityLoaderActivity did not load correctly",
solo.waitForActivity(ActivityLoaderActivity.class, delay));
solo.sleep(delay);
// Click on Explicit Activation Button
solo.clickOnView(solo
.getView(course.labs.intentslab.R.id.explicit_activation_button));
solo.sleep(delay);
// Wait for activity: 'course.labs.intentslab.ExplicitlyLoadedActivity'
assertTrue(
"ExplicitTest:" + "Section One:"
+ "ExplicitlyLoadedActivity did not load correctly",
solo.waitForActivity(course.labs.intentslab.ExplicitlyLoadedActivity.class, delay));
solo.sleep(delay);
// Checks that the ExplicitlyLoadedActivity was launched by the correct
// Intent
assertEquals(
"ExplicitTest:"
+ "Section One:"
+ "ExplicitlyLoadedActivity was not launched by the correct Intent",
"Intent { cmp=course.labs.intentslab/.ExplicitlyLoadedActivity }",
solo.getCurrentActivity().getIntent().toString());
// =================== Section Two ==========================
// Hide the soft keyboard
solo.hideSoftKeyboard();
solo.sleep(delay);
// Clear any text in the EditText
solo.clearEditText((android.widget.EditText) solo
.getView(course.labs.intentslab.R.id.editText));
// Enter the text: 'test'
solo.enterText((android.widget.EditText) solo
.getView(course.labs.intentslab.R.id.editText), "test");
// Hide the soft keyboard
solo.hideSoftKeyboard();
solo.sleep(delay);
// Click on Enter Button
solo.clickOnView(solo.getView(course.labs.intentslab.R.id.enter_button));
// Assert that: 'textView1' is shown
assertTrue("ExplicitTest:" + "Section Two:"
+ "textView1 did not show correctly", solo.waitForView(solo
.getView(course.labs.intentslab.R.id.textView1)));
// Assert that the string 'test' is found in the display
assertTrue(
"ExplicitTest:"
+ "Section Two:"
+ "Correct text was not returned from ExplicitlyLoadedActivity to ActivityLoaderActivity",
solo.searchText("test"));
}
}
package course.labs.intentslab.tests;
import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 21)
public class TestImplicitIntent {
private static final String BASIC_SAMPLE_PACKAGE
= "course.labs.intentslab";
private static final int LAUNCH_TIMEOUT = 5000;
private UiDevice mDevice;
@Before
public void startMainActivityFromHomeScreen() {
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Start from the home screen
mDevice.pressHome();
// Wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, is(notNullValue()));
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT);
// Launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
if (null == intent) fail();
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
// Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT);
}
@Test
public void TestMyBrowserAvailable() {
mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "implicit_activation_button")).click();
UiObject2 chooserContents = mDevice.wait(Until.findObject(By.text("MyBrowser")), 2000);
assertNotNull(chooserContents);
mDevice.pressBack();
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.intentslab"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="@string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ExplicitlyLoadedActivity"
android:label="@string/title_explicit" >
</activity>
</application>
</manifest>
\ No newline at end of file
package course.labs.intentslab;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ActivityLoaderActivity extends Activity {
static private final int GET_TEXT_REQUEST_CODE = 1;
static private final String URL = "http://www.google.com";
static private final String TAG = "Lab-Intents";
// For use with app chooser
static private final String CHOOSER_TEXT = "Load " + URL + " with:";
// TextView that displays user-entered text from ExplicitlyLoadedActivity runs
private TextView mUserTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loader_activity);
// Get reference to the textView
mUserTextView = findViewById(R.id.textView1);
// Declare and setup Explicit Activation button
Button explicitActivationButton = findViewById(R.id.explicit_activation_button);
explicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startExplicitActivation() when pressed
@Override
public void onClick(View v) {
startExplicitActivation();
}
});
// Declare and setup Implicit Activation button
Button implicitActivationButton = findViewById(R.id.implicit_activation_button);
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
@Override
public void onClick(View v) {
startImplicitActivation();
}
});
}
// Start the ExplicitlyLoadedActivity
private void startExplicitActivation() {
Log.i(TAG,"Entered startExplicitActivation()");
// TODO - Create a new intent to launch the ExplicitlyLoadedActivity class
// TODO - Start an Activity using that intent and the request code defined above
}
// Start a Browser Activity to view a web page or its URL
private void startImplicitActivation() {
Log.i(TAG, "Entered startImplicitActivation()");
// TODO - Implement getBaseIntent() to Create a base intent for viewing a URL
Intent baseIntent = getBaseIntent();
// TODO - Create a chooser intent, for choosing which Activity
// will carry out the baseIntent
// (HINT: Use the Intent class' createChooser() method)
//TODO - Log the chooser intent action
// TODO - Start the chooser Activity, using the chooser intent
}
public Intent getBaseIntent() {
// TODO - return a base intent for viewing a URL
// (HINT: second parameter uses Uri.parse())
return null;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
// TODO - Process the result only if this method received both a
// RESULT_OK result code and a recognized request code
// If so, update the Textview showing the user-entered text.
}
}
package course.labs.intentslab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ExplicitlyLoadedActivity extends Activity {
static private final String TAG = "Lab-Intents";
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.explicitly_loaded_activity);
// Get a reference to the EditText field
mEditText = findViewById(R.id.editText);
// Declare and setup "Enter" button
Button enterButton = findViewById(R.id.enter_button);
enterButton.setOnClickListener(new OnClickListener() {
// Call enterClicked() when pressed
@Override
public void onClick(View v) {
enterClicked();
}
});
}
// Sets result to send back to calling Activity and finishes
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
// TODO - Save user provided input from the EditText field
// TODO - Create a new intent and save the input from the EditText field as an extra
// TODO - Set Activity's result with result code RESULT_OK
// TODO - Finish the Activity
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/explicit_activation_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/explicit_activation_button_string" />
<Button
android:id="@+id/implicit_activation_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/implicit_activation_button_string" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no_text_entered" />
</LinearLayout>
\ No newline at end of file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/explicit_message" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/enter_text_here_string" >
<requestFocus />
</EditText>
<Button
android:id="@+id/enter_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText"
android:text="@string/enter_string" />
</RelativeLayout>
\ No newline at end of file
Lab2_Intents/app/src/main/res/mipmap-hdpi/ic_launcher.png

8.02 KiB

Lab2_Intents/app/src/main/res/mipmap-ldpi/ic_launcher.png

2.5 KiB

Lab2_Intents/app/src/main/res/mipmap-mdpi/ic_launcher.png

3.97 KiB

Lab2_Intents/app/src/main/res/mipmap-xhdpi/ic_launcher.png

13.2 KiB

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IntentsLab</string>
<string name="title_explicit">Explicit Activation</string>
<string name="title_main">Activity Loader Activity</string>
<string name="explicit_message">You have sent an Explicit Intent!</string>
<string name="explicit_activation_button_string">Explicit Activation</string>
<string name="implicit_activation_button_string">Implicit Activation</string>
<string name="no_text_entered">No Text Entered</string>
<string name="enter_string">Enter</string>
<string name="enter_text_here_string">Enter Text Here</string>
</resources>
\ No newline at end of file
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
\ No newline at end of file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment