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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/interaction/interactive_test.h"
#include <functional>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
#include <variant>
#include "base/functional/callback_helpers.h"
#include "base/functional/overloaded.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/interaction_sequence.h"
#include "ui/base/interaction/interaction_test_util.h"
#include "ui/base/interaction/interactive_test_internal.h"
namespace ui::test {
using internal::kInteractiveTestPivotElementId;
InteractiveTestApi::InteractiveTestApi(
std::unique_ptr<internal::InteractiveTestPrivate> private_test_impl)
: private_test_impl_(std::move(private_test_impl)) {}
InteractiveTestApi::~InteractiveTestApi() = default;
InteractionSequence::StepBuilder InteractiveTestApi::PressButton(
ElementSpecifier button,
InputType input_type) {
StepBuilder builder;
builder.SetDescription("PressButton()");
internal::SpecifyElement(builder, button);
builder.SetMustRemainVisible(false);
builder.SetStartCallback(base::BindOnce(
[](InputType input_type, InteractiveTestApi* test,
InteractionSequence* seq, TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "PressButton",
test->test_util().PressButton(el, input_type));
},
input_type, base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::SelectMenuItem(
ElementSpecifier menu_item,
InputType input_type) {
RequireInteractiveTest();
StepBuilder builder;
builder.SetDescription("SelectMenuItem()");
internal::SpecifyElement(builder, menu_item);
builder.SetMustRemainVisible(false);
builder.SetStartCallback(base::BindOnce(
[](InputType input_type, InteractiveTestApi* test,
InteractionSequence* seq, TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "SelectMenuItem",
test->test_util().SelectMenuItem(el, input_type));
},
input_type, base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::DoDefaultAction(
ElementSpecifier element,
InputType input_type) {
StepBuilder builder;
builder.SetDescription("DoDefaultAction()");
internal::SpecifyElement(builder, element);
builder.SetMustRemainVisible(false);
builder.SetStartCallback(base::BindOnce(
[](InputType input_type, InteractiveTestApi* test,
InteractionSequence* seq, TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "DoDefaultAction",
test->test_util().DoDefaultAction(el, input_type));
},
input_type, base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::SelectTab(
ElementSpecifier tab_collection,
size_t tab_index,
InputType input_type,
std::optional<size_t> expected_index_after_selection) {
StepBuilder builder;
builder.SetDescription(base::StringPrintf("SelectTab( %zu )", tab_index));
internal::SpecifyElement(builder, tab_collection);
builder.SetStartCallback(base::BindOnce(
[](size_t index, InputType input_type,
std::optional<size_t> expected_index_after_selection,
InteractiveTestApi* test, InteractionSequence* seq,
TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "SelectTab",
test->test_util().SelectTab(el, index, input_type,
expected_index_after_selection));
},
tab_index, input_type, expected_index_after_selection,
base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::SelectDropdownItem(
ElementSpecifier collection,
size_t item,
InputType input_type) {
// "Don't care" option directly sets the value; the other actually require
// popping out the dropdown menu and selecting an item which is not reliable
// in non-interactive tests.
if (input_type != InputType::kDontCare) {
RequireInteractiveTest();
}
StepBuilder builder;
builder.SetDescription(base::StringPrintf("SelectDropdownItem( %zu )", item));
internal::SpecifyElement(builder, collection);
builder.SetStartCallback(base::BindOnce(
[](size_t item, InputType input_type, InteractiveTestApi* test,
InteractionSequence* seq, TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "SelectDropdownItem",
test->test_util().SelectDropdownItem(el, item, input_type));
},
item, input_type, base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::EnterText(
ElementSpecifier element,
std::u16string text,
TextEntryMode mode) {
StepBuilder builder;
builder.SetDescription(base::StringPrintf("EnterText( \"%s\" )",
base::UTF16ToUTF8(text).c_str()));
internal::SpecifyElement(builder, element);
builder.SetStartCallback(base::BindOnce(
[](std::u16string text, TextEntryMode mode, InteractiveTestApi* test,
InteractionSequence* seq, TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "EnterText",
test->test_util().EnterText(el, std::move(text), mode));
},
std::move(text), mode, base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::ActivateSurface(
ElementSpecifier element) {
RequireInteractiveTest();
StepBuilder builder;
builder.SetDescription("ActivateSurface()");
internal::SpecifyElement(builder, element);
builder.SetStartCallback(base::BindOnce(
[](InteractiveTestApi* test, InteractionSequence* seq,
TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "ActivateSurface", test->test_util().ActivateSurface(el));
},
base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::FocusElement(
ElementSpecifier element) {
RequireInteractiveTest();
StepBuilder builder;
builder.SetDescription("FocusElement()");
internal::SpecifyElement(builder, element);
builder.SetStartCallback(base::BindOnce(
[](InteractiveTestApi* test, InteractionSequence* seq,
TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "FocusElement", test->test_util().FocusElement(el));
},
base::Unretained(this)));
return builder;
}
#if !BUILDFLAG(IS_IOS)
InteractionSequence::StepBuilder InteractiveTestApi::SendAccelerator(
ElementSpecifier element,
Accelerator accelerator) {
StepBuilder builder;
builder.SetDescription(base::StringPrintf(
"SendAccelerator( %s )",
base::UTF16ToUTF8(accelerator.GetShortcutText()).c_str()));
internal::SpecifyElement(builder, element);
builder.SetStartCallback(base::BindOnce(
[](Accelerator accelerator, InteractiveTestApi* test,
InteractionSequence* seq, TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "SendAccelerator",
test->test_util().SendAccelerator(el, accelerator));
},
accelerator, base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::SendKeyPress(
ElementSpecifier element,
KeyboardCode key,
int flags) {
StepBuilder builder;
std::ostringstream oss;
oss << "SendKeyPress( " << key << ", " << flags << " )";
builder.SetDescription(oss.str());
internal::SpecifyElement(builder, element);
builder.SetStartCallback(base::BindOnce(
[](KeyboardCode key, int flags, InteractiveTestApi* test,
InteractionSequence* seq, TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "SendKeyPress",
test->test_util().SendKeyPress(el, key, flags));
},
key, flags, base::Unretained(this)));
return builder;
}
#endif // !BUILDFLAG(IS_IOS)
InteractionSequence::StepBuilder InteractiveTestApi::Confirm(
ElementSpecifier element) {
StepBuilder builder;
builder.SetDescription("Confirm()");
internal::SpecifyElement(builder, element);
builder.SetStartCallback(base::BindOnce(
[](InteractiveTestApi* test, InteractionSequence* seq,
TrackedElement* el) {
test->private_test_impl().HandleActionResult(
seq, el, "Confirm", test->test_util().Confirm(el));
},
base::Unretained(this)));
return builder;
}
InteractionSequence::StepBuilder InteractiveTestApi::DumpElements() {
return WithElement(kInteractiveTestPivotElementId,
[this](ui::TrackedElement* el) {
private_test_impl()
.DebugDumpElements(el->context())
.PrintTo(COMPACT_GOOGLE_LOG_INFO.stream());
});
}
InteractionSequence::StepBuilder InteractiveTestApi::DumpElementsInContext() {
return WithElement(kInteractiveTestPivotElementId,
[this](ui::TrackedElement* el) {
private_test_impl()
.DebugDumpContext(el->context())
.PrintTo(COMPACT_GOOGLE_LOG_INFO.stream());
});
}
// static
InteractionSequence::StepBuilder InteractiveTestApi::WaitForShow(
ElementSpecifier element,
bool transition_only_on_event) {
StepBuilder step;
step.SetDescription("WaitForShow()");
internal::SpecifyElement(step, element);
step.SetTransitionOnlyOnEvent(transition_only_on_event);
return step;
}
// static
InteractionSequence::StepBuilder InteractiveTestApi::WaitForHide(
ElementSpecifier element,
bool transition_only_on_event) {
StepBuilder step;
step.SetDescription("WaitForHide()");
internal::SpecifyElement(step, element);
step.SetType(InteractionSequence::StepType::kHidden);
step.SetTransitionOnlyOnEvent(transition_only_on_event);
return step;
}
// static
InteractionSequence::StepBuilder InteractiveTestApi::WaitForEvent(
ElementSpecifier element,
CustomElementEventType event) {
StepBuilder step;
step.SetDescription(
base::StringPrintf("WaitForEvent( %s )", event.GetName().c_str()));
internal::SpecifyElement(step, element);
step.SetType(InteractionSequence::StepType::kCustomEvent, event);
return step;
}
// static
InteractiveTestApi::StepBuilder InteractiveTestApi::EnsureNotPresent(
ElementIdentifier element_to_check) {
return std::move(
WithElement(kInteractiveTestPivotElementId,
[element_to_check](InteractionSequence* seq,
TrackedElement* reference) {
auto* const tracker = ElementTracker::GetElementTracker();
auto* const element =
seq->IsCurrentStepInAnyContextForTesting()
? tracker->GetElementInAnyContext(element_to_check)
: tracker->GetFirstMatchingElement(
element_to_check, reference->context());
if (element) {
LOG(ERROR) << "Expected element " << element->ToString()
<< " not to be present but it was present.";
seq->FailForTesting();
}
})
.SetDescription(base::StringPrintf(
"EnsureNotPresent( %s )", element_to_check.GetName().c_str())));
}
// static
InteractiveTestApi::StepBuilder InteractiveTestApi::EnsurePresent(
ElementSpecifier element_to_check) {
return std::move(
WithElement(element_to_check, base::DoNothing())
.SetDescription(base::StringPrintf(
"EnsurePresent( %s )",
internal::DescribeElement(element_to_check).c_str())));
}
InteractionSequence::StepBuilder InteractiveTestApi::NameElement(
std::string_view name,
AbsoluteElementSpecifier spec) {
return NameElementRelative(kInteractiveTestPivotElementId, name,
GetFindElementCallback(std::move(spec)));
}
InteractiveTestApi::StepBuilder InteractiveTestApi::SetOnIncompatibleAction(
OnIncompatibleAction action,
const char* reason) {
return Do(base::BindOnce(
[](InteractiveTestApi* test, OnIncompatibleAction action,
std::string reason) {
DCHECK(action == OnIncompatibleAction::kFailTest || !reason.empty());
test->private_test_impl().on_incompatible_action_ = action;
test->private_test_impl().on_incompatible_action_reason_ = reason;
},
base::Unretained(this), action, std::string(reason)));
}
constexpr char kInteractiveErrorMessage[] = R"(
The test verb you are trying to use requires an interactive test environment.
This is one in which the test can safely control things like mouse movement and
window activation, without having to worry about other processes making changes
that can cause flakiness.
Q: But I was just selecting a menu item!
A: In tests where process exclusivity is not guaranteed, if the test application
loses focus, the menu could unexpectedly close, leading to flakiness. We want
to preemptively avoid these flakes.
Solutions:
- Use PressButton() instead of MoveMouseTo() + ClickMouse()
- Move your browser test from browser_tests to interactive_ui_tests
- Move your Ash test into chromeos_integration_tests
How to make a browser test interactive:
- Rename your test file from *_browsertest.cc to *_interactive_uitest.cc
* e.g. my_system_browsertest.cc -> my_system_interactive_uitest.cc
- Rename your test class from *Browsertest to *UiTest
* e.g. MySystemBrowserTest -> MySystemUiTest
- Edit chrome/test/BUILD.gn to move your test from the "browser_tests" target
to the "interactive_ui_tests" target.
* Ensure that if your test only ran in certain configurations, in
browser_tests, it is gated by the same conditions in interactive_ui_tests.
)";
void InteractiveTestApi::RequireInteractiveTest() {
CHECK(internal::InteractiveTestPrivate::allow_interactive_test_verbs_)
<< kInteractiveErrorMessage;
}
bool InteractiveTestApi::RunTestSequenceImpl(
ElementContext context,
InteractionSequence::Builder builder) {
builder.SetContext(context);
private_test_impl_->Init(context);
builder.SetCompletedCallback(
base::BindOnce(&internal::InteractiveTestPrivate::OnSequenceComplete,
base::Unretained(private_test_impl_.get())));
builder.SetAbortedCallback(
base::BindOnce(&internal::InteractiveTestPrivate::OnSequenceAborted,
base::Unretained(private_test_impl_.get())));
auto sequence = builder.Build();
{
base::test::ScopedRunLoopTimeout timeout(
FROM_HERE, std::nullopt,
base::BindRepeating(
[](base::WeakPtr<InteractionSequence> sequence,
base::WeakPtr<internal::InteractiveTestPrivate> impl) {
std::ostringstream oss;
ui::ElementContext context;
if (sequence) {
const auto data = sequence->BuildAbortedData(
InteractionSequence::AbortedReason::kSequenceTimedOut);
oss << internal::kInteractiveTestFailedMessagePrefix << data;
context = data.context;
if (data.step_type != InteractionSequence::StepType::kHidden &&
context && !data.element && data.element_id) {
const size_t elements_in_context =
ui::ElementTracker::GetElementTracker()
->GetAllMatchingElements(data.element_id, context)
.size();
const size_t total_elements =
ui::ElementTracker::GetElementTracker()
->GetAllMatchingElementsInAnyContext(data.element_id)
.size();
if (elements_in_context == 0U && total_elements > 0U) {
oss << "\nNote that there were matching elements in other "
"contexts; did you forget InSameContext() or "
"InAnyContext()?";
}
}
} else {
oss << "Interactive test: timeout after test sequence "
"destroyed; a failure message may already have been "
"logged.";
}
if (impl) {
const auto additional_context = impl->GetAdditionalContext();
if (!additional_context.empty()) {
oss << "\nAdditional test context:";
for (const auto& ctx : additional_context) {
oss << "\n * " << ctx;
}
}
impl->DebugDumpElements(context).PrintTo(oss);
}
return oss.str();
},
sequence->AsWeakPtr(), private_test_impl().GetAsWeakPtr()));
sequence->RunSynchronouslyForTesting();
}
private_test_impl_->Cleanup();
return private_test_impl_->success_;
}
// static
InteractiveTestApi::FindElementCallback
InteractiveTestApi::GetFindElementCallback(AbsoluteElementSpecifier spec) {
using ContextCallback = base::OnceCallback<TrackedElement*(ElementContext)>;
return std::visit(
base::Overloaded{
[](TrackedElement* el) {
CHECK(el) << "NameView(TrackedElement*): view must be set.";
return base::BindOnce(
[](const SafeElementReference& ref, TrackedElement*) {
LOG_IF(ERROR, !ref.get()) << "NameElement(TrackedElement*): "
"element ceased to be valid "
"before step was executed.";
return ref.get();
},
SafeElementReference(el));
},
[](std::reference_wrapper<TrackedElement*> ref) {
return base::BindOnce(
[](std::reference_wrapper<TrackedElement*> ref,
TrackedElement*) {
LOG_IF(ERROR, !ref.get()) << "NameElement(TrackedElement*): "
"element ceased to be valid "
"before step was executed.";
return ref.get();
},
ref);
},
[](ContextCallback& callback) {
return base::BindOnce(
[](ContextCallback callback, TrackedElement* relative_to) {
return std::move(callback).Run(relative_to->context());
},
std::move(callback));
},
[](base::OnceCallback<TrackedElement*()>& callback) {
return base::RectifyCallback<FindElementCallback>(
std::move(callback));
}},
spec);
}
// static
void InteractiveTestApi::AddStep(InteractionSequence::Builder& builder,
MultiStep multi_step) {
for (auto& step : multi_step)
builder.AddStep(step);
}
// static
void InteractiveTestApi::AddStep(MultiStep& dest, StepBuilder src) {
dest.emplace_back(std::move(src));
}
// static
void InteractiveTestApi::AddStep(MultiStep& dest, MultiStep src) {
for (auto& step : src)
dest.emplace_back(std::move(step));
}
// static
void InteractiveTestApi::AddDescriptionPrefix(MultiStep& steps,
std::string_view prefix) {
for (auto& step : steps) {
step.AddDescriptionPrefix(prefix);
}
}
} // namespace ui::test
|