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 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/accessibility/scoped_mode_collection.h"
#include <memory>
#include <optional>
#include "base/functional/callback_helpers.h"
#include "base/test/mock_callback.h"
#include "content/public/browser/scoped_accessibility_mode.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_mode.h"
namespace content {
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::Return;
using ::testing::ReturnArg;
using ::testing::WithArg;
using ::testing::WithoutArgs;
class MockDelegate : public ScopedModeCollection::Delegate {
public:
MOCK_METHOD(void, OnModeChanged, (ui::AXMode, ui::AXMode), (override));
MOCK_METHOD(ui::AXMode, FilterModeFlags, (ui::AXMode), (override));
using ScopedModeCollection::Delegate::MakePassKey;
};
// An action that evaluates an expectation that `collection` is or is not
// empty.
ACTION_P(ExpectCollectionModeEqualsArg, collection) {
EXPECT_EQ(collection->accessibility_mode(), arg0);
}
class ScopedModeCollectionTest : public ::testing::Test {
protected:
ScopedModeCollectionTest() {
// Set a default action on OnModeChanged to check the invariant that the
// `new_mode` given to the callback equals the collection's notion of the
// effective mode.
ON_CALL(delegate_, OnModeChanged(_, _))
.WillByDefault(
WithArg<1>(ExpectCollectionModeEqualsArg(&collection())));
// Set a default action on FilterModeFlags to perform no filtering.
ON_CALL(delegate_, FilterModeFlags(_)).WillByDefault(ReturnArg<0>());
}
void SetUp() override {
// The mode must be empty at construction.
ASSERT_EQ(collection_->accessibility_mode(), ui::AXMode());
}
bool HasCollection() const { return collection_.has_value(); }
void PrematurelyDestroyCollection() { collection_.reset(); }
MockDelegate& delegate() { return delegate_; }
ScopedModeCollection& collection() { return collection_.value(); }
std::unique_ptr<ScopedAccessibilityMode>& lazy_scoped_mode() {
return lazy_scoped_mode_;
}
private:
// Must precede `collection_`, which holds its delegate.
MockDelegate delegate_;
// Must precede `collection_` so that it is destructed after it; see
// `OutstandingScoper` below.
std::unique_ptr<ScopedAccessibilityMode> lazy_scoped_mode_;
std::optional<ScopedModeCollection> collection_{delegate_};
};
// Tests the most basic use of adding/removing a scoper.
TEST_F(ScopedModeCollectionTest, SimpleAddRemove) {
// OnModeChanged should be called twice: once when a scoper is added and again
// when it is destroyed. FilterModeFlags should be called once to filter the
// one scoper's value.
{
::testing::InSequence sequence;
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete));
EXPECT_CALL(delegate(), OnModeChanged(ui::AXMode(), ui::kAXModeComplete));
EXPECT_CALL(delegate(), OnModeChanged(ui::kAXModeComplete, ui::AXMode()));
}
auto scoped_mode = collection().Add(ui::kAXModeComplete);
}
// Tests multiple scopers perfectly nested.
TEST_F(ScopedModeCollectionTest, MultipleNested) {
// OnModeChanged should be called for each addition/removal for nested
// scopers, with appropriate filtering.
{
::testing::InSequence sequence;
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(), OnModeChanged(ui::AXMode(), ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete));
EXPECT_CALL(delegate(),
OnModeChanged(ui::kAXModeBasic, ui::kAXModeComplete));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(),
OnModeChanged(ui::kAXModeComplete, ui::kAXModeBasic));
EXPECT_CALL(delegate(), OnModeChanged(ui::kAXModeBasic, ui::AXMode()));
}
auto outer_scoped_mode = collection().Add(ui::kAXModeBasic);
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeBasic);
auto inner_scoped_mode = collection().Add(ui::kAXModeComplete);
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeComplete);
inner_scoped_mode.reset();
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeBasic);
outer_scoped_mode.reset();
ASSERT_EQ(collection().accessibility_mode(), ui::AXMode());
}
// Tests multiple scopers deleted out of order.
TEST_F(ScopedModeCollectionTest, MultipleNotNested) {
// The callback should not be run when the first scoper is deleted since its
// mode flags are a subset of the second.
{
::testing::InSequence sequence;
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(), OnModeChanged(ui::AXMode(), ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete));
EXPECT_CALL(delegate(),
OnModeChanged(ui::kAXModeBasic, ui::kAXModeComplete));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete));
EXPECT_CALL(delegate(), OnModeChanged(ui::kAXModeComplete, ui::AXMode()));
}
auto first_scoped_mode = collection().Add(ui::kAXModeBasic);
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeBasic);
auto second_scoped_mode = collection().Add(ui::kAXModeComplete);
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeComplete);
first_scoped_mode.reset();
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeComplete);
second_scoped_mode.reset();
ASSERT_EQ(collection().accessibility_mode(), ui::AXMode());
}
// Tests that deleting the collection while it holds a scoper should neither
// crash nor run the callback when the scoper is destroyed. (Note: by
// "outstanding", we mean that there remains a scoper alive. We are not making a
// judgement about this particular scoper being better in any way than another
// scoper. All scopers are equal in the transistors of the CPU.)
TEST_F(ScopedModeCollectionTest, OutstandingScoper) {
// The callback should be run once when the scoper is added but not when it
// is destroyed.
{
::testing::InSequence sequence;
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete));
EXPECT_CALL(delegate(), OnModeChanged(ui::AXMode(), ui::kAXModeComplete));
}
// Make sure that `scoped_mode` outlives `collection`.
lazy_scoped_mode() = collection().Add(ui::kAXModeComplete);
}
// An action that evaluates an expectation that `collection` is empty.
ACTION_P(ExpectCollectionIsEmpty, collection) {
EXPECT_TRUE(collection->empty());
}
// An action that evaluates an expectation that `collection` is not empty.
ACTION_P(ExpectCollectionIsNotEmpty, collection) {
EXPECT_FALSE(collection->empty());
}
// Tests that `empty()` works.
TEST_F(ScopedModeCollectionTest, Empty) {
EXPECT_CALL(delegate(), FilterModeFlags(_)).Times(AnyNumber());
// Expect that `empty()` will return the right thing from within the callback.
{
::testing::InSequence sequence;
EXPECT_CALL(delegate(), OnModeChanged(_, _))
.WillOnce(
DoAll(WithArg<1>(ExpectCollectionModeEqualsArg(&collection())),
WithoutArgs(ExpectCollectionIsNotEmpty(&collection()))));
EXPECT_CALL(delegate(), OnModeChanged(_, _))
.WillOnce(
DoAll(WithArg<1>(ExpectCollectionModeEqualsArg(&collection())),
WithoutArgs(ExpectCollectionIsEmpty(&collection()))));
}
ASSERT_TRUE(collection().empty());
auto scoped_mode = collection().Add(ui::kAXModeComplete);
ASSERT_FALSE(collection().empty());
scoped_mode.reset();
ASSERT_TRUE(collection().empty());
}
// Tests that destroying a collection from within its callback does not crash,
// even if scopers are still alive.
TEST_F(ScopedModeCollectionTest, DestroyFromCallback) {
EXPECT_CALL(delegate(), FilterModeFlags(_)).Times(AnyNumber());
{
::testing::InSequence sequence;
EXPECT_CALL(delegate(), OnModeChanged(_, _));
EXPECT_CALL(delegate(), OnModeChanged(_, _));
EXPECT_CALL(delegate(), OnModeChanged(_, _)).WillOnce(WithoutArgs([this]() {
PrematurelyDestroyCollection();
}));
}
ASSERT_TRUE(collection().empty());
auto first_scoped_mode = collection().Add(ui::kAXModeBasic);
auto second_scoped_mode = collection().Add(ui::kAXModeComplete);
// The collection will be destroyed by the callback when the mode drops back
// down to `kAXModeBasic`.
second_scoped_mode.reset();
ASSERT_FALSE(HasCollection());
}
TEST_F(ScopedModeCollectionTest, Filtering) {
// The delegate's filtering wishes are obeyed.
static constexpr ui::AXMode kCompleteNoInline =
ui::kAXModeComplete & ~ui::AXMode(ui::AXMode::kInlineTextBoxes);
{
::testing::InSequence sequence;
// Two scoped are created with a pass-through filter.
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(), OnModeChanged(ui::AXMode(), ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete));
EXPECT_CALL(delegate(),
OnModeChanged(ui::kAXModeBasic, ui::kAXModeComplete));
// Recalculation is forced with a filter that strips one mode flag.
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic))
.WillOnce(Return(ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete))
.WillOnce(Return(kCompleteNoInline));
EXPECT_CALL(delegate(),
OnModeChanged(ui::kAXModeComplete, kCompleteNoInline));
// Recalculation is forced with the pass-through filter again.
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeComplete));
EXPECT_CALL(delegate(),
OnModeChanged(kCompleteNoInline, ui::kAXModeComplete));
// The scopers are destroyed.
EXPECT_CALL(delegate(), FilterModeFlags(ui::kAXModeBasic));
EXPECT_CALL(delegate(),
OnModeChanged(ui::kAXModeComplete, ui::kAXModeBasic));
EXPECT_CALL(delegate(), OnModeChanged(ui::kAXModeBasic, ui::AXMode()));
}
auto outer_scoped_mode = collection().Add(ui::kAXModeBasic);
auto inner_scoped_mode = collection().Add(ui::kAXModeComplete);
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeComplete);
collection().Recompute(delegate().MakePassKey());
ASSERT_EQ(collection().accessibility_mode(), kCompleteNoInline);
collection().Recompute(delegate().MakePassKey());
ASSERT_EQ(collection().accessibility_mode(), ui::kAXModeComplete);
}
} // namespace content
|