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

전남대 Android_장수민_4주차 과제(Step2)(재제출) #105

Open
wants to merge 14 commits into
base: sumintnals
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
129 changes: 74 additions & 55 deletions app/src/androidTest/java/campus/tech/kakao/map/SearchActivityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SearchActivityTest {

@get:Rule
Expand All @@ -48,63 +49,81 @@ class SearchActivityTest {

@Test
fun `검색창_X누르면_지워진다`() {
// given
onView(withId(R.id.search)).perform(typeText("cafe"))

// when
onView(withId(R.id.xmark)).perform(click())

// then
onView(withId(R.id.search)).check(matches(withText("")))
}

// @Test
// fun `검색결과_누르면_메인액티비티로_이동한다`() {
// // Activity 실행 시점에 RecyclerView의 IdlingResource 등록
// activityRule.scenario.onActivity { activity ->
// val recyclerView = activity.findViewById<RecyclerView>(R.id.placeResult)
// idlingResource = RecyclerViewIdlingResource(recyclerView)
// IdlingRegistry.getInstance().register(idlingResource)
//
// // 수동으로 RecyclerView에 데이터 삽입
// val testData = listOf(
// Document(
// addressName = "서울 강남구 삼성동 159",
// categoryGroupCode = "",
// categoryGroupName = "",
// categoryName = "가정,생활 > 문구,사무용품 > 디자인문구 > 카카오프렌즈",
// distance = "418",
// id = "26338954",
// phone = "02-6002-1880",
// placeName = "카카오프렌즈 코엑스점",
// placeUrl = "http://place.map.kakao.com/26338954",
// roadAddressName = "서울 강남구 영동대로 513",
// x = "127.05902969025047",
// y = "37.51207412593136"
// )
// )
//
// val placeAdapter = PlaceAdapter(
// testData,
// LayoutInflater.from(activity),
// object : PlaceAdapter.OnItemClickListener {
// override fun onItemClick(position: Int) {
// val item = placeAdapter.getItem(position)
// val intent = Intent(activity, MainActivity::class.java)
// intent.putExtra("longitude", item.x)
// intent.putExtra("latitude", item.y)
// intent.putExtra("name", item.placeName)
// intent.putExtra("address", item.addressName)
// activity.startActivity(intent)
// }
// })
//
// recyclerView.adapter = placeAdapter
// placeAdapter.notifyDataSetChanged()
// }
//
// // RecyclerView 데이터 로드 확인
// onView(withId(R.id.placeResult)).check(matches(hasMinimumChildCount(1)))
//
// // 첫 번째 아이템 클릭
// onView(withId(R.id.placeResult)).perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click()))
//
// // MainActivity로 이동 확인
// Intents.intended(hasComponent(MainActivity::class.java.name))
// }
}
@Test
fun 검색결과_누르면_메인액티비티로_이동한다() {
// given
val testData = listOf(
Document(
addressName = "서울 강남구 삼성동 159",
categoryGroupCode = "",
categoryGroupName = "",
categoryName = "가정,생활 > 문구,사무용품 > 디자인문구 > 카카오프렌즈",
distance = "418",
id = "26338954",
phone = "02-6002-1880",
placeName = "카카오프렌즈 코엑스점",
placeUrl = "http://place.map.kakao.com/26338954",
roadAddressName = "서울 강남구 영동대로 513",
x = "127.05902969025047",
y = "37.51207412593136"
)
)

// when
activityRule.scenario.onActivity { activity ->
val recyclerView = activity.findViewById<RecyclerView>(R.id.placeResult)

// 커스텀 IdlingResource 생성 및 등록
idlingResource = object : IdlingResource {
private var resourceCallback: IdlingResource.ResourceCallback? = null
override fun getName(): String = "RecyclerView Idling Resource"
override fun isIdleNow(): Boolean = recyclerView.adapter?.itemCount ?: 0 > 0
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
resourceCallback = callback
}
}
IdlingRegistry.getInstance().register(idlingResource)

// 데이터 설정
val placeAdapter = PlaceAdapter(
testData,
LayoutInflater.from(activity),
object : PlaceAdapter.OnItemClickListener {
override fun onItemClick(position: Int) {
val item = testData[position]
val intent = Intent(activity, MainActivity::class.java)
intent.putExtra("longitude", item.x)
intent.putExtra("latitude", item.y)
intent.putExtra("name", item.placeName)
intent.putExtra("address", item.addressName)
activity.startActivity(intent)
}
})

recyclerView.adapter = placeAdapter
placeAdapter.notifyDataSetChanged()
}

// 대기
IdlingRegistry.getInstance().resources.forEach {
if (!it.isIdleNow) {
Thread.sleep(1000) // 1초 대기
}
}

// then
onView(withId(R.id.placeResult)).check(matches(hasMinimumChildCount(1)))
onView(withId(R.id.placeResult)).perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click()))
Intents.intended(hasComponent(MainActivity::class.java.name))
}
}
37 changes: 0 additions & 37 deletions app/src/main/java/campus/tech/kakao/map/SearchViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package campus.tech.kakao.map

import android.content.Context
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -11,8 +10,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class SearchViewModel(context: Context) : ViewModel() {
private val dbHelper: DBHelper = DBHelper(context)
private val db = dbHelper.writableDatabase
private val preferenceManager = MapApplication.prefs
var repository = RetrofitRepository()

Expand All @@ -33,40 +30,6 @@ class SearchViewModel(context: Context) : ViewModel() {
val locationList: LiveData<List<Document>>
get() = _locationList

fun insertPlace(place: Place) {
dbHelper.insert(db, place)
}

override fun onCleared() {
super.onCleared()
if (db.isOpen) db.close()
}

fun getSearchResult(searchText: String) {
if (searchText.isEmpty()) {
_placeList.postValue(emptyList())
} else {
val rDb = dbHelper.readableDatabase
val places = mutableListOf<Place>()
val query = "SELECT * FROM ${PlaceContract.TABLE_NAME} WHERE ${PlaceContract.TABLE_COLUMN_NAME} LIKE ?"
val cursor = rDb.rawQuery(query, arrayOf("%$searchText%"))

if (cursor != null) {
if (cursor.moveToFirst()) {
do {
val name = cursor.getString(cursor.getColumnIndexOrThrow(PlaceContract.TABLE_COLUMN_NAME))
val address = cursor.getString(cursor.getColumnIndexOrThrow(PlaceContract.TABLE_COLUMN_ADDRESS))
val category = cursor.getString(cursor.getColumnIndexOrThrow(PlaceContract.TABLE_COLUMN_CATEGORY))
val place = Place(name, address, category)
places.add(place)
} while (cursor.moveToNext())
}
cursor.close()
}
_placeList.postValue(places)
}
}

fun getSearchHistoryList() {
_searchHistoryList.value = getSearchHistory()
}
Expand Down
Loading