CS 213 SOFTWARE METHODOLOGY
LILY CHANG ASSOCIATE TEACHING PROFESSOR DEPARTMENT OF COMPUTER SCIENCE RUTGERS UNIVERSITY – NEW BRUNSWICK FALL 2021
More on Android app Development
Copyright By PowCoder代写 加微信 powcoder
A Fragment represents a reusable portion of your app’s UI.
A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events.
Fragments cannot live on their own–they must be hosted by an activity or another fragment.
The fragment’s view hierarchy becomes part of, or attaches to, the host’s view hierarchy.
Fragments – Micro Activities
•Fragments introduce modularity and reusability into your activity’s UI by allowing you to divide the UI into discrete chunks.
•Dividing your UI into fragments makes it easier to modify your activity’s appearance at runtime.
Create a Fragment
•Fragments require a dependency on the AndroidX Fragment library. You need to add the Google Maven repository to your project’s build.gradle file in order to include this dependency.
Create a Fragment class
•To create a fragment, extend the AndroidX Fragment class, and override its methods to insert your app logic, similar to the way you would create an Activity class.
•To create a minimal fragment that defines its own layout, provide your fragment’s layout resource to the base constructor,
Adding a Fragment to an Activity
•Generally, your fragment must be embedded within an AndroidX FragmentActivity to contribute a portion of UI to that activity’s layout.
•You need to add a FragmentContainerView that defines the location where the fragment should be placed within the activity’s view hierarchy
•The android:name attribute specifies the class name of the Fragment to instantiate. When the activity’s layout is inflated, the specified fragment is instantiated, onInflate() is called on the newly instantiated fragment, and a FragmentTransaction is created to add the fragment to the FragmentManager.
•Add a fragment programmatically, follow the link below https://developer.android.com/guide/fragments/create#add-programmatic
Testing your app
•Local unit tests located at module- name/src/test/java/
• These are tests that run on your machine’s local Java Virtual Machine (JVM)
•Instrumented tests located at module- name/src/androidTest/java/
• These are tests that run on a hardware device or emulator
• Use these tests when writing integration and functional UI tests to automate user interaction, or when your tests have Android dependencies that mock objects cannot satisfy.
Add a new test
•Open the Java file containing the code you want to test.
•Click the class or method you want to test, then press Ctrl+Shift+T (⇧⌘T).
•In the menu that appears, click Create New Test. •In the Create Test dialog, edit any fields and select
any methods to generate, and then click OK.
•In the Choose Destination Directory dialog, click the source set corresponding to the type of test you want to create: androidTest for an instrumented test or test for a local unit test. Then click OK.
Build.Gradle
Also be sure you specify the test library dependencies in your app module’s build.gradle file:
Gradle is an open-source build automation tool
• Creation of a software build, including compiling computer source code into binary code, packaging binary code, and running automated tests.
Gradle runs on the JVM and you must have a Java Development Kit (JDK) installed to use it.
Gradle Scripts
• build.gradle (project) • build.gradle(app)
Build.gradle (app)
Automating your test
•JUnit test framework
• Test the methods in a class in isolation without the UI
•Espresso testing framework, provided by AndroidX Test, provides APIs for writing UI tests to simulate user interactions within a single target app
• One approach to UI testing is to simply have a human tester perform a set of user operations on the target app and verify that it is behaving correctly. However, this manual approach can be time-consuming, tedious, and error-prone.
• A more efficient approach is to write your UI tests such that user actions are performed in an automated way. The automated approach allows you to run your tests quickly and reliably in a repeatable manner.
Find the UI component you want to test in an Activity (for example, a sign-in button in the app) by calling the onView() method, or the onData() method for AdapterView controls
Creating an Espresso test class
Simulate a specific user interaction to perform on that UI component, by calling the ViewInteraction.perform() or DataInteraction.perform() method and passing in the user action (for example, click on the sign-in button).
Use the ViewAssertions methods to check that the UI reflects the expected state or behavior, after these user interactions are performed.
Espresso test example
•Before building your UI test with Espresso, make sure to set a dependency reference to the Espresso library in the build.gradle (app)
Another expresso test example
Expresso packages
•espresso-core – Contains core and basic View matchers, actions, and assertions. See Basics and Recipes.
•espresso-web – Contains resources for WebView support.
•espresso-idling-resource – Espresso’s mechanism for synchronization with background
•espresso-contrib – External contributions that contain DatePicker, RecyclerView and Drawer actions, accessibility checks, and CountingIdlingResource.
•espresso-intents – Extension to validate and stub intents for hermetic testing. •espresso-remote – Location of Espresso’s multi-process functionality.
Espresso test recorder
•The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code.
•To start recording a test,
• Click Run > Record Espresso Test.
• In the Select Deployment Target window, choose the device on which you want to record the test. If necessary, create a new Android Virtual Device. Click OK.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com