1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/*
* Copyright 2023 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.
*/
#pragma once
#include <android-base/logging.h>
#include "../dispatcher/InputDispatcher.h"
using android::base::Result;
using android::gui::Pid;
using android::gui::TouchOcclusionMode;
using android::gui::Uid;
using android::gui::WindowInfo;
using android::gui::WindowInfoHandle;
namespace android {
namespace inputdispatcher {
namespace {
// The default pid and uid for windows created by the test.
constexpr gui::Pid WINDOW_PID{999};
constexpr gui::Uid WINDOW_UID{1001};
static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
} // namespace
class FakeInputReceiver {
public:
std::unique_ptr<InputEvent> consumeEvent(std::chrono::milliseconds timeout) {
uint32_t consumeSeq = 0;
std::unique_ptr<InputEvent> event;
std::chrono::time_point start = std::chrono::steady_clock::now();
status_t result = WOULD_BLOCK;
while (result == WOULD_BLOCK) {
InputEvent* rawEventPtr = nullptr;
result = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
&rawEventPtr);
event = std::unique_ptr<InputEvent>(rawEventPtr);
std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
if (elapsed > timeout) {
if (timeout != 0ms) {
LOG(ERROR) << "Waited too long for consumer to produce an event, giving up";
}
break;
}
}
// Events produced by this factory are owned pointers.
if (result != OK) {
if (timeout == 0ms) {
// This is likely expected. No need to log.
} else {
LOG(ERROR) << "Received result = " << result << " from consume";
}
return nullptr;
}
result = mConsumer.sendFinishedSignal(consumeSeq, true);
if (result != OK) {
LOG(ERROR) << "Received result = " << result << " from sendFinishedSignal";
}
return event;
}
explicit FakeInputReceiver(std::unique_ptr<InputChannel> channel, const std::string name)
: mConsumer(std::move(channel)) {}
virtual ~FakeInputReceiver() {}
private:
std::unique_ptr<InputChannel> mClientChannel;
InputConsumer mConsumer;
DynamicInputEventFactory mEventFactory;
};
class FakeWindowHandle : public WindowInfoHandle {
public:
static const int32_t WIDTH = 600;
static const int32_t HEIGHT = 800;
FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
InputDispatcher& dispatcher, const std::string name, int32_t displayId)
: mName(name) {
Result<std::unique_ptr<InputChannel>> channel = dispatcher.createInputChannel(name);
mInfo.token = (*channel)->getConnectionToken();
mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
inputApplicationHandle->updateInfo();
mInfo.applicationInfo = *inputApplicationHandle->getInfo();
mInfo.id = sId++;
mInfo.name = name;
mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
mInfo.alpha = 1.0;
mInfo.frame.left = 0;
mInfo.frame.top = 0;
mInfo.frame.right = WIDTH;
mInfo.frame.bottom = HEIGHT;
mInfo.transform.set(0, 0);
mInfo.globalScaleFactor = 1.0;
mInfo.touchableRegion.clear();
mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
mInfo.ownerPid = WINDOW_PID;
mInfo.ownerUid = WINDOW_UID;
mInfo.displayId = displayId;
mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
}
sp<FakeWindowHandle> clone(int32_t displayId) {
sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
handle->mInfo = mInfo;
handle->mInfo.displayId = displayId;
handle->mInfo.id = sId++;
handle->mInputReceiver = mInputReceiver;
return handle;
}
void setTouchable(bool touchable) {
mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
}
void setFocusable(bool focusable) {
mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
}
void setVisible(bool visible) {
mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
}
void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
mInfo.dispatchingTimeout = timeout;
}
void setPaused(bool paused) {
mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
}
void setPreventSplitting(bool preventSplitting) {
mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
}
void setSlippery(bool slippery) {
mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
}
void setWatchOutsideTouch(bool watchOutside) {
mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
}
void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
void setInterceptsStylus(bool interceptsStylus) {
mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
}
void setDropInput(bool dropInput) {
mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
}
void setDropInputIfObscured(bool dropInputIfObscured) {
mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
}
void setNoInputChannel(bool noInputChannel) {
mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
}
void setDisableUserActivity(bool disableUserActivity) {
mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
}
void setAlpha(float alpha) { mInfo.alpha = alpha; }
void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
mInfo.frame.left = frame.left;
mInfo.frame.top = frame.top;
mInfo.frame.right = frame.right;
mInfo.frame.bottom = frame.bottom;
mInfo.touchableRegion.clear();
mInfo.addTouchableRegion(frame);
const Rect logicalDisplayFrame = displayTransform.transform(frame);
ui::Transform translate;
translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
mInfo.transform = translate * displayTransform;
}
void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
void setIsWallpaper(bool isWallpaper) {
mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
}
void setDupTouchToWallpaper(bool hasWallpaper) {
mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
}
void setTrustedOverlay(bool trustedOverlay) {
mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
}
void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
}
void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout) {
if (mInputReceiver == nullptr) {
return nullptr;
}
return mInputReceiver->consumeEvent(timeout);
}
void consumeMotion() {
std::unique_ptr<InputEvent> event = consume(100ms);
if (event == nullptr) {
LOG(FATAL) << mName << ": expected a MotionEvent, but didn't get one.";
return;
}
if (event->getType() != InputEventType::MOTION) {
LOG(FATAL) << mName << " expected a MotionEvent, got " << *event;
return;
}
}
sp<IBinder> getToken() { return mInfo.token; }
const std::string& getName() { return mName; }
void setOwnerInfo(Pid ownerPid, Uid ownerUid) {
mInfo.ownerPid = ownerPid;
mInfo.ownerUid = ownerUid;
}
Pid getPid() const { return mInfo.ownerPid; }
void destroyReceiver() { mInputReceiver = nullptr; }
private:
FakeWindowHandle(std::string name) : mName(name){};
const std::string mName;
std::shared_ptr<FakeInputReceiver> mInputReceiver;
static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
friend class sp<FakeWindowHandle>;
};
std::atomic<int32_t> FakeWindowHandle::sId{1};
} // namespace inputdispatcher
} // namespace android
|