upload android base code part7

This commit is contained in:
August 2018-08-08 18:09:17 +08:00
parent 4e516ec6ed
commit 841ae54672
25229 changed files with 1709508 additions and 0 deletions

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright (C) 2013 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 name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.testingfun.tests"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- We add an application tag here just so that we can indicate that
this package needs to link against the android.test library,
which is needed when building test cases. -->
<application>
<uses-library android:name="android.test.runner" />
</application>
<!--
Specifies the instrumentation test runner used to run the tests.
-->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.android.testingfun"
android:label="Tests for com.example.android.testingfun" />
</manifest>

View file

@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-17

View file

@ -0,0 +1,70 @@
/*
* Copyright (C) 2013 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.testingfun.tests.lesson2;
import com.example.android.testingfun.R;
import com.example.android.testingfun.lesson2.MyFirstTestActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
/**
* Tests for MyFirstTestActivity.
*/
public class MyFirstTestActivityTest extends ActivityInstrumentationTestCase2<MyFirstTestActivity> {
private MyFirstTestActivity mFirstTestActivity;
private TextView mFirstTestText;
public MyFirstTestActivityTest() {
super(MyFirstTestActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
// Starts the activity under test using the default Intent with:
// action = {@link Intent#ACTION_MAIN}
// flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
// All other fields are null or empty.
mFirstTestActivity = getActivity();
mFirstTestText = (TextView) mFirstTestActivity.findViewById(R.id.my_first_test_text_view);
}
/**
* Test if your test fixture has been set up correctly. You should always implement a test that
* checks the correct setup of your test fixture. If this tests fails all other tests are
* likely to fail as well.
*/
public void testPreconditions() {
//Try to add a message to add context to your assertions. These messages will be shown if
//a tests fails and make it easy to understand why a test failed
assertNotNull("mFirstTestActivity is null", mFirstTestActivity);
assertNotNull("mFirstTestText is null", mFirstTestText);
}
/**
* Tests the correctness of the initial text.
*/
public void testMyFirstTestTextView_labelText() {
//It is good practice to read the string from your resources in order to not break
//multiple tests when a string changes.
final String expected = mFirstTestActivity.getString(R.string.my_first_test);
final String actual = mFirstTestText.getText().toString();
assertEquals("mFirstTestText contains wrong text", expected, actual);
}
}

View file

@ -0,0 +1,124 @@
/*
* Copyright (C) 2013 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.testingfun.tests.lesson3;
import com.example.android.testingfun.R;
import com.example.android.testingfun.lesson3.ClickFunActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.ViewAsserts;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
/**
* Tests for ClickFunActivity. Introduces touch mode, test size annotations and TouchUtils.
*/
public class ClickFunActivityTest extends ActivityInstrumentationTestCase2<ClickFunActivity> {
private ClickFunActivity mClickFunActivity;
private Button mClickMeButton;
private TextView mInfoTextView;
public ClickFunActivityTest() {
super(ClickFunActivity.class);
}
/**
* Sets up the test fixture for this test case. This method is always called before every test
* run.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
//Sets the initial touch mode for the Activity under test. This must be called before
//getActivity()
setActivityInitialTouchMode(true);
//Get a reference to the Activity under test, starting it if necessary.
mClickFunActivity = getActivity();
//Get references to all views
mClickMeButton = (Button) mClickFunActivity.findViewById(R.id.launch_next_activity_button);
mInfoTextView = (TextView) mClickFunActivity.findViewById(R.id.info_text_view);
}
/**
* Tests the preconditions of this test fixture.
*/
@MediumTest
public void testPreconditions() {
assertNotNull("mClickFunActivity is null", mClickFunActivity);
assertNotNull("mClickMeButton is null", mClickMeButton);
assertNotNull("mInfoTextView is null", mInfoTextView);
}
@MediumTest
public void testClickMeButton_layout() {
//Retrieve the top-level window decor view
final View decorView = mClickFunActivity.getWindow().getDecorView();
//Verify that the mClickMeButton is on screen
ViewAsserts.assertOnScreen(decorView, mClickMeButton);
//Verify width and heights
final ViewGroup.LayoutParams layoutParams = mClickMeButton.getLayoutParams();
assertNotNull(layoutParams);
assertEquals(layoutParams.width, WindowManager.LayoutParams.MATCH_PARENT);
assertEquals(layoutParams.height, WindowManager.LayoutParams.WRAP_CONTENT);
}
@MediumTest
public void testClickMeButton_labelText() {
//Verify that mClickMeButton uses the correct string resource
final String expectedNextButtonText = mClickFunActivity.getString(R.string.label_click_me);
final String actualNextButtonText = mClickMeButton.getText().toString();
assertEquals(expectedNextButtonText, actualNextButtonText);
}
@MediumTest
public void testInfoTextView_layout() {
//Retrieve the top-level window decor view
final View decorView = mClickFunActivity.getWindow().getDecorView();
//Verify that the mInfoTextView is on screen and is not visible
ViewAsserts.assertOnScreen(decorView, mInfoTextView);
assertTrue(View.GONE == mInfoTextView.getVisibility());
}
@MediumTest
public void testInfoTextViewText_isEmpty() {
//Verify that the mInfoTextView is initialized with the correct default value
assertEquals("", mInfoTextView.getText());
}
@MediumTest
public void testClickMeButton_clickButtonAndExpectInfoText() {
String expectedInfoText = mClickFunActivity.getString(R.string.info_text);
//Perform a click on mClickMeButton
TouchUtils.clickView(this, mClickMeButton);
//Verify the that mClickMeButton was clicked. mInfoTextView is visible and contains
//the correct text.
assertTrue(View.VISIBLE == mInfoTextView.getVisibility());
assertEquals(expectedInfoText, mInfoTextView.getText());
}
}

View file

@ -0,0 +1,93 @@
/*
* Copyright (C) 2013 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.testingfun.tests.lesson4;
import com.example.android.testingfun.R;
import com.example.android.testingfun.lesson4.LaunchActivity;
import com.example.android.testingfun.lesson4.NextActivity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.widget.Button;
/**
* Tests LaunchActivity in isolation from the system.
*/
public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> {
private Intent mLaunchIntent;
public LaunchActivityTest() {
super(LaunchActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
//Create an intent to launch target Activity
mLaunchIntent = new Intent(getInstrumentation().getTargetContext(),
LaunchActivity.class);
}
/**
* Tests the preconditions of this test fixture.
*/
@MediumTest
public void testPreconditions() {
//Start the activity under test in isolation, without values for savedInstanceState and
//lastNonConfigurationInstance
startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
assertNotNull("mLaunchActivity is null", getActivity());
assertNotNull("mLaunchNextButton is null", launchNextButton);
}
@MediumTest
public void testLaunchNextActivityButton_labelText() {
startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
final String expectedButtonText = getActivity().getString(R.string.label_launch_next);
assertEquals("Unexpected button label text", expectedButtonText,
launchNextButton.getText());
}
@MediumTest
public void testNextActivityWasLaunchedWithIntent() {
startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
//Because this is an isolated ActivityUnitTestCase we have to directly click the
//button from code
launchNextButton.performClick();
// Get the intent for the next started activity
final Intent launchIntent = getStartedActivityIntent();
//Verify the intent was not null.
assertNotNull("Intent was null", launchIntent);
//Verify that LaunchActivity was finished after button click
assertTrue(isFinishCalled());
final String payload = launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY);
//Verify that payload data was added to the intent
assertEquals("Payload is empty", LaunchActivity.STRING_PAYLOAD
, payload);
}
}

View file

@ -0,0 +1,112 @@
/*
* Copyright (C) 2013 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.testingfun.tests.lesson5;
import com.example.android.testingfun.R;
import com.example.android.testingfun.lesson5.ReceiverActivity;
import com.example.android.testingfun.lesson5.SenderActivity;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.suitebuilder.annotation.MediumTest;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Functional test across multiple Activities. Tests SenderActivity and ReceiverActivity. Introduces
* advanced Instrumentation testing practices as sending key events and interaction monitoring
* between Activities and the system.
*/
public class SenderActivityTest extends ActivityInstrumentationTestCase2<SenderActivity> {
private static final int TIMEOUT_IN_MS = 5000;
private static final String TEST_MESSAGE = "Hello Receiver";
private SenderActivity mSenderActivity;
public SenderActivityTest() {
super(SenderActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(true);
mSenderActivity = getActivity();
}
/**
* Tests the preconditions of this test fixture.
*/
@MediumTest
public void testPreconditions() {
assertNotNull("mSenderActivity is null", mSenderActivity);
}
@MediumTest
public void testSendMessageToReceiverActivity() {
//Because this functional test tests interaction across multiple components these views
//are part of the actual test method and not of the test fixture
final Button sendToReceiverButton = (Button) mSenderActivity
.findViewById(R.id.send_message_button);
final EditText senderMessageEditText = (EditText) mSenderActivity
.findViewById(R.id.message_input_edit_text);
//Create and add an ActivityMonitor to monitor interaction between the system and the
//ReceiverActivity
Instrumentation.ActivityMonitor receiverActivityMonitor = getInstrumentation()
.addMonitor(ReceiverActivity.class.getName(), null, false);
//Request focus on the EditText field. This must be done on the UiThread because
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
senderMessageEditText.requestFocus();
}
});
//Wait until all events from the MainHandler's queue are processed
getInstrumentation().waitForIdleSync();
//Send the text message
getInstrumentation().sendStringSync(TEST_MESSAGE);
getInstrumentation().waitForIdleSync();
//Click on the sendToReceiverButton to send the message to ReceiverActivity
TouchUtils.clickView(this, sendToReceiverButton);
//Wait until ReceiverActivity was launched and get a reference to it.
ReceiverActivity receiverActivity = (ReceiverActivity) receiverActivityMonitor
.waitForActivityWithTimeout(TIMEOUT_IN_MS);
//Verify that ReceiverActivity was started
assertNotNull("ReceiverActivity is null", receiverActivity);
assertEquals("Monitor for ReceiverActivity has not been called", 1,
receiverActivityMonitor.getHits());
assertEquals("Activity is of wrong type", ReceiverActivity.class,
receiverActivity.getClass());
//Read the message received by ReceiverActivity
final TextView receivedMessage = (TextView) receiverActivity
.findViewById(R.id.received_message_text_view);
//Verify that received message is correct
assertNotNull(receivedMessage);
assertEquals("Wrong received message", TEST_MESSAGE, receivedMessage.getText().toString());
//Unregister monitor for ReceiverActivity
getInstrumentation().removeMonitor(receiverActivityMonitor);
}
}