Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Fix several project lint report warnings #498

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ Third Party Content
-------------------
Select text used for describing the plants (in `plants.json`) are used from Wikipedia via CC BY-SA 3.0 US (license in `ASSETS_LICENSE`).

"[seed](https://thenounproject.com/search/?q=seed&i=1585971)" by [Aisyah](https://thenounproject.com/aisyahalmasyira/) is licensed under [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/us/legalcode)

License
-------

Expand Down
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ dependencies {
implementation "androidx.navigation:navigation-ui-ktx:$rootProject.navigationVersion"
implementation "androidx.recyclerview:recyclerview:$rootProject.recyclerViewVersion"
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.viewpager2:viewpager2:$rootProject.viewPagerVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
implementation "androidx.work:work-runtime-ktx:$rootProject.workVersion"
implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"
implementation "com.google.android.material:material:$rootProject.materialVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,12 @@

package com.google.samples.apps.sunflower

import android.view.Gravity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.DrawerMatchers.isClosed
import androidx.test.espresso.contrib.DrawerMatchers.isOpen
import androidx.test.espresso.contrib.NavigationViewActions.navigateTo
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import androidx.test.uiautomator.UiDevice
import com.google.samples.apps.sunflower.utilities.getToolbarNavigationContentDescription
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

Expand All @@ -41,57 +30,13 @@ class GardenActivityTest {
@Rule @JvmField
var activityTestRule = ActivityTestRule(GardenActivity::class.java)

@Test fun clickOnAndroidHomeIcon_OpensAndClosesNavigation() {
// Check that drawer is closed at startup
onView(withId(R.id.drawer_layout)).check(matches(isClosed(Gravity.START)))
@Test fun clickAddPlant_OpensPlantList() {
// Given that no Plants are added to the user's garden

clickOnHomeIconToOpenNavigationDrawer()
checkDrawerIsOpen()
}

@Test fun onRotate_NavigationStaysOpen() {
clickOnHomeIconToOpenNavigationDrawer()

with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())) {
// Rotate device to landscape
setOrientationLeft()
checkDrawerIsOpen()

// Rotate device back to portrait
setOrientationRight()
checkDrawerIsOpen()
}
}

@Test fun clickOnPlantListDrawerMenuItem_StartsPlantListActivity() {
clickOnHomeIconToOpenNavigationDrawer()
// When the "Add Plant" button is clicked
onView(withId(R.id.add_plant)).perform(click())

// Press on Plant List navigation item
onView(withId(R.id.navigation_view))
.perform(navigateTo(R.id.plant_list_fragment))

// Check that the PlantListFragment is visible
// Then the ViewPager should change to the Plant List page
onView(withId(R.id.plant_list)).check(matches(isDisplayed()))
}

@Test fun pressDeviceBack_CloseDrawer_Then_PressBack_Close_App() {
clickOnHomeIconToOpenNavigationDrawer()
onView(isRoot()).perform(ViewActions.pressBack())
checkDrawerIsNotOpen()
assertEquals(activityTestRule.activity.isFinishing, false)
assertEquals(activityTestRule.activity.isDestroyed, false)
}

private fun clickOnHomeIconToOpenNavigationDrawer() {
onView(withContentDescription(getToolbarNavigationContentDescription(
activityTestRule.activity, R.id.toolbar))).perform(click())
}

private fun checkDrawerIsOpen() {
onView(withId(R.id.drawer_layout)).check(matches(isOpen(Gravity.START)))
}

private fun checkDrawerIsNotOpen() {
onView(withId(R.id.drawer_layout)).check(matches(isClosed(Gravity.START)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import android.accessibilityservice.AccessibilityService
import android.content.Intent
import android.os.Bundle
import androidx.navigation.findNavController
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.intent.Intents
Expand All @@ -30,11 +28,14 @@ import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction
import androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra
import androidx.test.espresso.intent.matcher.IntentMatchers.hasType
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import com.google.samples.apps.sunflower.utilities.chooser
import com.google.samples.apps.sunflower.utilities.testPlant
import org.hamcrest.CoreMatchers.allOf
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -51,11 +52,12 @@ class PlantDetailFragmentTest {
activityTestRule.activity.apply {
runOnUiThread {
val bundle = Bundle().apply { putString("plantId", testPlant.plantId) }
findNavController(R.id.garden_nav_fragment).navigate(R.id.plant_detail_fragment, bundle)
findNavController(R.id.nav_host).navigate(R.id.plant_detail_fragment, bundle)
}
}
}

@Ignore("Share button redesign pending")
@Test
fun testShareTextIntent() {
val shareText = activityTestRule.activity.getString(R.string.share_text_plant, testPlant.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import com.google.samples.apps.sunflower.utilities.testCalendar
import com.google.samples.apps.sunflower.utilities.testGardenPlanting
import com.google.samples.apps.sunflower.utilities.testPlant
import com.google.samples.apps.sunflower.utilities.testPlants
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.equalTo
import org.junit.After
import org.junit.Assert.assertNull
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -40,7 +42,7 @@ class GardenPlantingDaoTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()

@Before fun createDb() {
@Before fun createDb() = runBlocking<Unit> {
val context = InstrumentationRegistry.getInstrumentation().targetContext
database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
gardenPlantingDao = database.gardenPlantingDao()
Expand All @@ -53,7 +55,7 @@ class GardenPlantingDaoTest {
database.close()
}

@Test fun testGetGardenPlantings() {
@Test fun testGetGardenPlantings() = runBlocking<Unit> {
val gardenPlanting2 = GardenPlanting(
testPlants[1].plantId,
testCalendar,
Expand All @@ -63,15 +65,7 @@ class GardenPlantingDaoTest {
assertThat(getValue(gardenPlantingDao.getGardenPlantings()).size, equalTo(2))
}

@Test
fun testGetGardenPlanting() {
assertThat(
getValue(gardenPlantingDao.getGardenPlanting(testGardenPlantingId)),
equalTo(testGardenPlanting)
)
}

@Test fun testDeleteGardenPlanting() {
@Test fun testDeleteGardenPlanting() = runBlocking<Unit> {
val gardenPlanting2 = GardenPlanting(
testPlants[1].plantId,
testCalendar,
Expand All @@ -84,27 +78,22 @@ class GardenPlantingDaoTest {
}

@Test fun testGetGardenPlantingForPlant() {
assertThat(getValue(gardenPlantingDao.getGardenPlantingForPlant(testPlant.plantId)),
equalTo(testGardenPlanting))
assertTrue(getValue(gardenPlantingDao.isPlanted(testPlant.plantId)))
}

@Test fun testGetGardenPlantingForPlant_notFound() {
assertNull(getValue(gardenPlantingDao.getGardenPlantingForPlant(testPlants[2].plantId)))
assertFalse(getValue(gardenPlantingDao.isPlanted(testPlants[2].plantId)))
}

@Test fun testGetPlantAndGardenPlantings() {
val plantAndGardenPlantings = getValue(gardenPlantingDao.getPlantAndGardenPlantings())
assertThat(plantAndGardenPlantings.size, equalTo(3))
val plantAndGardenPlantings = getValue(gardenPlantingDao.getPlantedGardens())
assertThat(plantAndGardenPlantings.size, equalTo(1))

/**
* Only the [testPlant] has been planted, and thus has an associated [GardenPlanting]
*/
assertThat(plantAndGardenPlantings[0].plant, equalTo(testPlant))
assertThat(plantAndGardenPlantings[0].gardenPlantings.size, equalTo(1))
assertThat(plantAndGardenPlantings[0].gardenPlantings[0], equalTo(testGardenPlanting))

// The other plants in the database have not been planted and thus have no GardenPlantings
assertThat(plantAndGardenPlantings[1].gardenPlantings.size, equalTo(0))
assertThat(plantAndGardenPlantings[2].gardenPlantings.size, equalTo(0))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.samples.apps.sunflower.utilities.getValue
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.equalTo
import org.junit.After
import org.junit.Assert.assertThat
Expand All @@ -40,7 +41,7 @@ class PlantDaoTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()

@Before fun createDb() {
@Before fun createDb() = runBlocking<Unit> {
val context = InstrumentationRegistry.getInstrumentation().targetContext
database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
plantDao = database.plantDao()
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/Theme.Sunflower">
<activity
android:name=".GardenActivity"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/Theme.Sunflower.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,13 @@ package com.google.samples.apps.sunflower

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.databinding.DataBindingUtil.setContentView
import com.google.samples.apps.sunflower.databinding.ActivityGardenBinding

class GardenActivity : AppCompatActivity() {

private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val binding: ActivityGardenBinding = DataBindingUtil.setContentView(this,
R.layout.activity_garden)
drawerLayout = binding.drawerLayout

navController = findNavController(R.id.garden_nav_fragment)
appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)

// Set up ActionBar
setSupportActionBar(binding.toolbar)
setupActionBarWithNavController(navController, appBarConfiguration)

// Set up navigation menu
binding.navigationView.setupWithNavController(navController)
}

override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
setContentView<ActivityGardenBinding>(this, R.layout.activity_garden)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.observe
import androidx.viewpager2.widget.ViewPager2
import com.google.samples.apps.sunflower.adapters.GardenPlantingAdapter
import com.google.samples.apps.sunflower.adapters.PLANT_LIST_PAGE_INDEX
import com.google.samples.apps.sunflower.databinding.FragmentGardenBinding
import com.google.samples.apps.sunflower.utilities.InjectorUtils
import com.google.samples.apps.sunflower.viewmodels.GardenPlantingListViewModel

class GardenFragment : Fragment() {

private lateinit var binding: FragmentGardenBinding

private val viewModel: GardenPlantingListViewModel by viewModels {
InjectorUtils.provideGardenPlantingListViewModelFactory(requireContext())
}
Expand All @@ -39,21 +43,27 @@ class GardenFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentGardenBinding.inflate(inflater, container, false)
binding = FragmentGardenBinding.inflate(inflater, container, false)
val adapter = GardenPlantingAdapter()
binding.gardenList.adapter = adapter

binding.addPlant.setOnClickListener {
navigateToPlantListPage()
}

subscribeUi(adapter, binding)
return binding.root
}

private fun subscribeUi(adapter: GardenPlantingAdapter, binding: FragmentGardenBinding) {
viewModel.gardenPlantings.observe(viewLifecycleOwner) { plantings ->
binding.hasPlantings = !plantings.isNullOrEmpty()
}

viewModel.plantAndGardenPlantings.observe(viewLifecycleOwner) { result ->
if (!result.isNullOrEmpty())
adapter.submitList(result)
binding.hasPlantings = !result.isNullOrEmpty()
adapter.submitList(result)
}
}

// TODO: convert to data binding if applicable
private fun navigateToPlantListPage() {
requireActivity().findViewById<ViewPager2>(R.id.view_pager).currentItem = PLANT_LIST_PAGE_INDEX
}
}
Loading