0

FaceGaze: Consolidate test setup methods

Currently three methods need to be called to initialize FaceGaze for
tests. This change consolidates these methods so that devs only need
to call one method, EnableFaceGaze(config), to setup FaceGaze.

Bug: b/309121742
Change-Id: I26caab3829c20be11e9cf75f4b7bf0111cb5aa46
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5455258
Reviewed-by: Katie Dektar <katie@chromium.org>
Commit-Queue: Akihiro Ota <akihiroota@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1288900}
This commit is contained in:
Akihiro Ota
2024-04-17 19:21:18 +00:00
committed by Chromium LUCI CQ
parent 5387458c09
commit b24e0ef791
3 changed files with 63 additions and 68 deletions

@ -9,7 +9,6 @@
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/events/event.h"
#include "ui/events/event_handler.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
@ -26,8 +25,6 @@ using MockFaceLandmarkerResult = FaceGazeTestUtils::MockFaceLandmarkerResult;
namespace {
const char* kDefaultDisplaySize = "1200x800";
aura::Window* GetRootWindow() {
auto* root_window = Shell::GetRootWindowForNewWindows();
if (!root_window) {
@ -94,12 +91,6 @@ class FaceGazeIntegrationTest : public AccessibilityFeatureBrowserTest {
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
GetRootWindow()->AddPreTargetHandler(&event_handler_);
display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
.UpdateDisplay(kDefaultDisplaySize);
// Initialize FaceGaze.
utils_->EnableFaceGaze();
utils_->CreateFaceLandmarker();
}
void TearDownOnMainThread() override {
@ -129,7 +120,7 @@ class FaceGazeIntegrationTest : public AccessibilityFeatureBrowserTest {
};
IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest, UpdateCursorLocation) {
utils()->ConfigureFaceGaze(Config().Default());
utils()->EnableFaceGaze(Config().Default());
event_handler().ClearEvents();
utils()->ProcessFaceLandmarkerResult(
@ -151,7 +142,7 @@ IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest, UpdateCursorLocation) {
}
IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest, ResetCursor) {
utils()->ConfigureFaceGaze(
utils()->EnableFaceGaze(
Config()
.Default()
.WithGesturesToMacros(
@ -183,7 +174,7 @@ IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest, ResetCursor) {
IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest,
IgnoreGesturesWithLowConfidence) {
utils()->ConfigureFaceGaze(
utils()->EnableFaceGaze(
Config()
.Default()
.WithGesturesToMacros(
@ -208,7 +199,7 @@ IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest,
IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest,
UpdateCursorLocationWithSpeed1) {
utils()->ConfigureFaceGaze(Config().Default().WithCursorSpeeds(
utils()->EnableFaceGaze(Config().Default().WithCursorSpeeds(
{/*up=*/1, /*down=*/1, /*left=*/1, /*right=*/1}));
// With cursor acceleration off and buffer size 1, one-pixel head movements
@ -225,7 +216,7 @@ IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest,
}
IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest, SpaceKeyEvents) {
utils()->ConfigureFaceGaze(
utils()->EnableFaceGaze(
Config()
.Default()
.WithGesturesToMacros(
@ -257,7 +248,7 @@ IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest, SpaceKeyEvents) {
// ensures that the associated action is performed if either of the gestures is
// detected.
IN_PROC_BROWSER_TEST_F(FaceGazeIntegrationTest, BrowsDownGesture) {
utils()->ConfigureFaceGaze(
utils()->EnableFaceGaze(
Config()
.Default()
.WithCursorLocation(gfx::Point(0, 0))

@ -21,6 +21,7 @@
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/extension_host_test_helper.h"
#include "ui/display/screen.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/gfx/geometry/point.h"
namespace ash {
@ -30,6 +31,7 @@ using MediapipeGesture = FaceGazeTestUtils::MediapipeGesture;
namespace {
const char* kDefaultDisplaySize = "1200x800";
constexpr char kMediapipeTestFilePath[] =
"resources/chromeos/accessibility/accessibility_common/third_party/"
"mediapipe_task_vision";
@ -220,7 +222,11 @@ FaceGazeTestUtils::MockFaceLandmarkerResult::WithGesture(
FaceGazeTestUtils::FaceGazeTestUtils() = default;
FaceGazeTestUtils::~FaceGazeTestUtils() = default;
void FaceGazeTestUtils::EnableFaceGaze() {
void FaceGazeTestUtils::EnableFaceGaze(const Config& config) {
// TODO(b/309121742): Add display size to Config so that tests can configure
// it.
display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
.UpdateDisplay(kDefaultDisplaySize);
event_generator_ = std::make_unique<ui::test::EventGenerator>(
Shell::Get()->GetPrimaryRootWindow());
@ -238,6 +244,49 @@ void FaceGazeTestUtils::EnableFaceGaze() {
WaitForJSReady();
SetUpJSTestSupport();
CancelMouseControllerInterval();
CreateFaceLandmarker();
ConfigureFaceGaze(config);
}
void FaceGazeTestUtils::WaitForCursorPosition(const gfx::Point& location) {
std::string script =
base::StringPrintf("faceGazeTestSupport.waitForCursorLocation(%d, %d);",
location.x(), location.y());
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::ProcessFaceLandmarkerResult(
const MockFaceLandmarkerResult& result) {
std::string forehead_location_json =
base::WriteJson(result.forehead_location()).value();
std::string recognized_gestures_json =
base::WriteJson(result.recognized_gestures()).value();
std::string script = base::StringPrintf(
"faceGazeTestSupport.processFaceLandmarkerResult(%s, %s)",
forehead_location_json.c_str(), recognized_gestures_json.c_str());
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::TriggerMouseControllerInterval() {
std::string script = "faceGazeTestSupport.triggerMouseControllerInterval();";
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::MoveMouseTo(const gfx::Point& location) {
event_generator_->MoveMouseTo(location.x(), location.y());
}
void FaceGazeTestUtils::AssertCursorAt(const gfx::Point& location) {
WaitForCursorPosition(location);
ASSERT_EQ(location, display::Screen::GetScreen()->GetCursorScreenPoint());
}
void FaceGazeTestUtils::ExecuteAccessibilityCommonScript(
const std::string& script) {
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
/*context=*/AccessibilityManager::Get()->profile(),
/*extension_id=*/extension_misc::kAccessibilityCommonExtensionId,
/*script=*/script);
}
void FaceGazeTestUtils::SetUpMediapipeDir() {
@ -263,14 +312,6 @@ void FaceGazeTestUtils::WaitForJSReady() {
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::ExecuteAccessibilityCommonScript(
const std::string& script) {
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
/*context=*/AccessibilityManager::Get()->profile(),
/*extension_id=*/extension_misc::kAccessibilityCommonExtensionId,
/*script=*/script);
}
void FaceGazeTestUtils::SetUpJSTestSupport() {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath source_dir;
@ -332,13 +373,6 @@ void FaceGazeTestUtils::ConfigureFaceGaze(const Config& config) {
AssertCursorAt(config.cursor_location());
}
void FaceGazeTestUtils::WaitForCursorPosition(const gfx::Point& location) {
std::string script =
base::StringPrintf("faceGazeTestSupport.waitForCursorLocation(%d, %d);",
location.x(), location.y());
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::SetCursorSpeeds(const CursorSpeeds& speeds) {
GetPrefs()->SetInteger(prefs::kAccessibilityFaceGazeCursorSpeedUp, speeds.up);
GetPrefs()->SetInteger(prefs::kAccessibilityFaceGazeCursorSpeedDown,
@ -393,30 +427,4 @@ void FaceGazeTestUtils::SetGestureRepeatDelayMs(int delay) {
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::ProcessFaceLandmarkerResult(
const MockFaceLandmarkerResult& result) {
std::string forehead_location_json =
base::WriteJson(result.forehead_location()).value();
std::string recognized_gestures_json =
base::WriteJson(result.recognized_gestures()).value();
std::string script = base::StringPrintf(
"faceGazeTestSupport.processFaceLandmarkerResult(%s, %s)",
forehead_location_json.c_str(), recognized_gestures_json.c_str());
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::TriggerMouseControllerInterval() {
std::string script = "faceGazeTestSupport.triggerMouseControllerInterval();";
ExecuteAccessibilityCommonScript(script);
}
void FaceGazeTestUtils::MoveMouseTo(const gfx::Point& location) {
event_generator_->MoveMouseTo(location.x(), location.y());
}
void FaceGazeTestUtils::AssertCursorAt(const gfx::Point& location) {
WaitForCursorPosition(location);
ASSERT_EQ(location, display::Screen::GetScreen()->GetCursorScreenPoint());
}
} // namespace ash

@ -216,12 +216,8 @@ class FaceGazeTestUtils {
FaceGazeTestUtils(const FaceGazeTestUtils&) = delete;
FaceGazeTestUtils& operator=(const FaceGazeTestUtils&) = delete;
// Enables and sets up FaceGaze.
void EnableFaceGaze();
// Creates and initializes the FaceLandmarker API within the extension.
void CreateFaceLandmarker();
// Configures FaceGaze with the given configuration.
void ConfigureFaceGaze(const Config& config);
// Enables, sets up, and configures FaceGaze with the given configuration.
void EnableFaceGaze(const Config& config);
// Waits for the cursor location to propagate to the FaceGaze MouseController.
void WaitForCursorPosition(const gfx::Point& location);
// Forces FaceGaze to process `result`, since tests don't have access to real
@ -243,19 +239,19 @@ class FaceGazeTestUtils {
void WaitForJSReady();
void SetUpJSTestSupport();
void CancelMouseControllerInterval();
// Creates and initializes the FaceLandmarker API within the extension.
void CreateFaceLandmarker();
void ConfigureFaceGaze(const Config& config);
// Sets cursor speed prefs.
// Preference-related methods.
void SetCursorSpeeds(const CursorSpeeds& speeds);
// Sets the buffer size pref.
void SetBufferSize(int size);
// Sets the cursor acceleration pref.
void SetCursorAcceleration(bool use_acceleration);
// Sets the gesture to macro mapping pref.
void SetGesturesToMacros(
const base::flat_map<FaceGazeGesture, MacroName>& gestures_to_macros);
// Sets the gesture confidences mapping pref.
void SetGestureConfidences(
const base::flat_map<FaceGazeGesture, int>& gesture_confidences);
// Sets the gesture repeat delay threshold.
void SetGestureRepeatDelayMs(int delay);