Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI test #15

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.example.citylist;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.espresso.Espresso.onView;

import static org.hamcrest.CoreMatchers.anything;

import androidx.test.espresso.Espresso;
import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {

@Rule
public ActivityScenarioRule<MainActivity> activityRule =
new ActivityScenarioRule<>(MainActivity.class);

@Test
public void testAppName() {
onView(withText("CityList")).check(matches(isDisplayed())); //Check the name on the screen
}

@Test
public void testAddCity(){
onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list
onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name
onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list
onView(withText("Edmonton")).check(matches(isDisplayed())); //Check the name on the screen
}

@Test
public void testClearCity(){
onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list
onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name
onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list
//Add another city to the list
onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list
onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Toronto")); //Type a city name
onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list
//Clear the list
onView(withId(R.id.button_clear)).perform(click());
onView(withText("Edmonton")).check(doesNotExist());

}
@Test
public void testListView(){
onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list
onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name
onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list

onData(anything()).inAdapterView(withId(R.id.city_list)).atPosition(0).
check(matches((withText("Edmonton")))); //Check the content on the list - no content in this case
}

@Test
public void testListViewClickAndBack(){
onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list
onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name
onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list

onData(anything()).inAdapterView(withId(R.id.city_list)).atPosition(0).perform(click()); //Check the content on the list - no content in this case
Espresso.pressBack(); //Back button
}

}
9 changes: 7 additions & 2 deletions app/src/main/java/com/example/citylist/City.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.citylist;

public class City {
public class City implements Comparable<City>{
private String city;
private String province;

Expand All @@ -16,4 +16,9 @@ String getCityName(){
String getProvinceName(){
return this.province;
}
}

@Override
public int compareTo(City city) {
return this.city.compareTo(city.getCityName());
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/example/citylist/CityList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.citylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* This is a class that keeps track of a list of city objects
*/
public class CityList {
private List<City> cities = new ArrayList<>();

/**
* This adds a city to the list if that city does not exist
* @param city
* This is the city to add
*/
public void add(City city) {
if (cities.contains(city)) {
throw new IllegalArgumentException();
}
cities.add(city);
}

/**
* This returns a sorted list of cities
* @return
* Return the sorted list of cities
*/
public List<City> getCities() {
List<City> cityList = cities;
Collections.sort(cityList);
return cityList;
}
}
44 changes: 0 additions & 44 deletions app/src/main/java/com/example/citylist/CustomList.java

This file was deleted.

53 changes: 39 additions & 14 deletions app/src/main/java/com/example/citylist/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,61 @@

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

// Declare the variables so that you will be able to reference it later.
ListView cityList;
ArrayAdapter<City> cityAdapter;
ArrayList<City> cityDataList;
EditText newName;
LinearLayout nameField;
ArrayAdapter<String> cityAdapter;
ArrayList<String> dataList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cityList = findViewById(R.id.city_list);
nameField = findViewById(R.id.field_nameEntry);
newName = findViewById(R.id.editText_name);

String cities[] = {"Edmonton", "Vancouver", "Toronto", "Calgary", "Hamilton", "Waterloo" };
String provinces[] = {"AB", "BC", "ON", "AB", "ON", "ON"};
cityList = findViewById(R.id.city_list);
dataList = new ArrayList<>();
cityAdapter = new ArrayAdapter<>(this, R.layout.content, dataList);
cityList.setAdapter(cityAdapter);

cityDataList = new ArrayList<>();
final Button addButton = findViewById(R.id.button_add);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
nameField.setVisibility(View.VISIBLE);
}
});

final Button confirmButton = findViewById(R.id.button_confirm);
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String cityName = newName.getText().toString();
cityAdapter.add(cityName);
newName.getText().clear();
nameField.setVisibility(View.INVISIBLE);
}
});

final Button deleteButton = findViewById(R.id.button_clear);
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cityAdapter.clear();
}
});

for(int i=0; i<cities.length; i++){
cityDataList.add(new City(cities[i], provinces[i]));
}
}

cityAdapter = new CustomList(this, cityDataList);

cityList.setAdapter(cityAdapter);
}
}
54 changes: 51 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,64 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/button_add"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="ADD CITY" />

<Button
android:id="@+id/button_clear"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="ClEAR ALL" />
</LinearLayout>

<ListView
android:id="@+id/city_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:listSelector="#999999">

</ListView>

</LinearLayout>
<LinearLayout
android:id="@+id/field_nameEntry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom"
android:visibility="invisible">

<EditText
android:id="@+id/editText_name"
style="@style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="City Name"
android:inputType="textCapWords"
android:textColor="#000000" />

<Button
android:id="@+id/button_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CONFIRM" />

</LinearLayout>

</LinearLayout>
29 changes: 6 additions & 23 deletions app/src/main/res/layout/content.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/city_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="City"
android:textSize="30sp">
</TextView>

<TextView
android:id="@+id/province_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Province"
android:textSize="30sp">
</TextView>
android:padding="20dp"
android:text="test"
android:textSize="40sp">

</LinearLayout>
</TextView>
Loading