Merge "Add a ContentResolverWithFakeSettingsProvider class to testutils." into main
This commit is contained in:
commit
7e5665ae44
2 changed files with 77 additions and 0 deletions
|
|
@ -27,12 +27,15 @@ java_library {
|
|||
"framework-connectivity-test-defaults",
|
||||
],
|
||||
libs: [
|
||||
// TODO: is there a way to subclass MockContentResolver without depending on the impl?
|
||||
"android.test.mock.impl",
|
||||
"androidx.annotation_annotation",
|
||||
"net-utils-device-common-bpf", // TestBpfMap extends IBpfMap.
|
||||
],
|
||||
static_libs: [
|
||||
"androidx.test.ext.junit",
|
||||
"collector-device-lib",
|
||||
"frameworks-base-testutils", // For FakeSettingsProvider.
|
||||
"kotlin-reflect",
|
||||
"libnanohttpd",
|
||||
"mockito-target-minus-junit4",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (C) 2025 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.android.testutils
|
||||
|
||||
import android.database.ContentObserver
|
||||
import android.net.Uri
|
||||
import android.os.Process
|
||||
import android.os.UserHandle
|
||||
import android.provider.Settings
|
||||
import android.test.mock.MockContentResolver
|
||||
import android.util.ArrayMap
|
||||
import android.util.Log
|
||||
import android.util.Pair
|
||||
import com.android.internal.util.test.FakeSettingsProvider
|
||||
import java.util.Collections
|
||||
|
||||
private const val TAG = "CRWithFakeSettings"
|
||||
|
||||
class ContentResolverWithFakeSettingsProvider : MockContentResolver() {
|
||||
private val observers = ArrayMap<Uri, Pair<ContentObserver, UserHandle>>()
|
||||
|
||||
init {
|
||||
addProvider(Settings.AUTHORITY, FakeSettingsProvider({ userId: Int, uri: Uri ->
|
||||
val p = observers.get(uri)
|
||||
if (p == null) return@FakeSettingsProvider
|
||||
val observer = p.first
|
||||
val callbackUser = p.second
|
||||
val user = UserHandle(userId)
|
||||
val currentUser = UserHandle.getUserHandleForUid(Process.myUid())
|
||||
if (callbackUser == UserHandle.ALL ||
|
||||
UserHandle.CURRENT.equals(callbackUser) && currentUser.equals(user) ||
|
||||
callbackUser.equals(user)
|
||||
) {
|
||||
Log.d(TAG, "Notifying change in $uri")
|
||||
observer.onChange(false, Collections.singletonList(uri), 0, user)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a ContentObserver that gets callbacks when the given URI changes.
|
||||
* Note: this method does not override any actual ContentResolver methods - they are all final.
|
||||
*/
|
||||
fun registerContentObserver(uri: Uri, observer: ContentObserver) {
|
||||
if (observers.containsKey(uri)) {
|
||||
throw IllegalStateException("Duplicate ContentObserver for URI: $uri")
|
||||
}
|
||||
observers.put(uri, Pair(observer, UserHandle.CURRENT))
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a ContentObserver that gets callbacks when the given URI changes.
|
||||
* Note: this method does not override any actual ContentResolver methods - they are all final.
|
||||
*/
|
||||
fun registerContentObserverAsUser(uri: Uri, observer: ContentObserver, userHandle: UserHandle) {
|
||||
if (observers.containsKey(uri)) {
|
||||
throw IllegalStateException("Duplicate ContentObserver for URI: $uri")
|
||||
}
|
||||
observers.put(uri, Pair(observer, userHandle))
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue