#include "ui/views/focus/focus_manager.h"
#include <stddef.h>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "build/build_config.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/test_accelerator_target.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/accessible_pane_view.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/buildflags.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/focus/focus_manager_delegate.h"
#include "ui/views/focus/focus_manager_factory.h"
#include "ui/views/focus/widget_focus_manager.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/test/focus_manager_test.h"
#include "ui/views/test/native_widget_factory.h"
#include "ui/views/test/test_platform_native_widget.h"
#include "ui/views/test/test_views.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"
#if defined(USE_AURA)
#include "ui/aura/client/focus_client.h"
#include "ui/views/widget/native_widget_aura.h"
#endif
namespace views {
enum FocusTestEventType { … };
struct FocusTestEvent { … };
class FocusTestEventList : public base::RefCounted<FocusTestEventList> { … };
class SimpleTestView : public View { … };
BEGIN_METADATA(…)
TEST_F(FocusManagerTest, ViewFocusCallbacks) { … }
TEST_F(FocusManagerTest, FocusChangeListener) { … }
TEST_F(FocusManagerTest, WidgetFocusChangeListener) { … }
TEST_F(FocusManagerTest, CallsNormalAcceleratorTarget) { … }
TEST_F(FocusManagerTest, HighPriorityHandlers) { … }
TEST_F(FocusManagerTest, CallsEnabledAcceleratorTargetsOnly) { … }
class SelfUnregisteringAcceleratorTarget : public ui::TestAcceleratorTarget { … };
TEST_F(FocusManagerTest, CallsSelfDeletingAcceleratorTarget) { … }
TEST_F(FocusManagerTest, SuspendAccelerators) { … }
namespace {
class FocusInAboutToRequestFocusFromTabTraversalView : public View { … };
BEGIN_METADATA(…)
}
TEST_F(FocusManagerTest, FocusInAboutToRequestFocusFromTabTraversal) { … }
TEST_F(FocusManagerTest, RotateFocus) { … }
TEST_F(FocusManagerTest, ImplicitlyStoresFocus) { … }
namespace {
class FocusManagerArrowKeyTraversalTest
: public FocusManagerTest,
public testing::WithParamInterface<bool> { … };
INSTANTIATE_TEST_SUITE_P(…);
}
TEST_P(FocusManagerArrowKeyTraversalTest, ArrowKeyTraversal) { … }
TEST_F(FocusManagerTest, SkipViewsInArrowKeyTraversal) { … }
TEST_F(FocusManagerTest, StoreFocusedView) { … }
#if BUILDFLAG(IS_MAC)
TEST_F(FocusManagerTest, StoreFocusedViewFullKeyboardAccess) {
View* view1 = new View;
View* view2 = new View;
View* view3 = new View;
view1->SetFocusBehavior(View::FocusBehavior::ACCESSIBLE_ONLY);
view2->SetFocusBehavior(View::FocusBehavior::NEVER);
view3->SetFocusBehavior(View::FocusBehavior::ALWAYS);
GetWidget()->GetRootView()->AddChildView(view1);
GetWidget()->GetRootView()->AddChildView(view2);
GetWidget()->GetRootView()->AddChildView(view3);
view1->RequestFocus();
EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
GetFocusManager()->StoreFocusedView(true);
EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView());
GetFocusManager()->SetKeyboardAccessible(false);
EXPECT_FALSE(GetFocusManager()->RestoreFocusedView());
EXPECT_EQ(view3, GetFocusManager()->GetFocusedView());
GetFocusManager()->StoreFocusedView(false);
EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView());
GetFocusManager()->SetKeyboardAccessible(true);
EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
EXPECT_EQ(view3, GetFocusManager()->GetFocusedView());
}
TEST_F(FocusManagerTest, RequestFocus) {
View* view1 = new View();
View* view2 = new View();
view1->SetFocusBehavior(View::FocusBehavior::ALWAYS);
view2->SetFocusBehavior(View::FocusBehavior::ACCESSIBLE_ONLY);
GetWidget()->GetRootView()->AddChildView(view1);
GetWidget()->GetRootView()->AddChildView(view2);
EXPECT_TRUE(GetFocusManager()->keyboard_accessible());
view1->RequestFocus();
EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
view2->RequestFocus();
EXPECT_EQ(view2, GetFocusManager()->GetFocusedView());
GetFocusManager()->SetKeyboardAccessible(false);
GetFocusManager()->ClearFocus();
EXPECT_NE(view1, GetFocusManager()->GetFocusedView());
view1->RequestFocus();
EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
view2->RequestFocus();
EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
}
#endif
namespace {
class AdvanceFocusWidgetDelegate : public WidgetDelegate { … };
class TestBubbleDialogDelegateView : public BubbleDialogDelegateView { … };
}
TEST_F(FocusManagerTest, AdvanceFocusStaysInWidget) { … }
TEST_F(FocusManagerTest, NavigateIntoAnchoredDialog) { … }
TEST_F(FocusManagerTest, AnchoredDialogOnContainerView) { … }
TEST_F(FocusManagerTest, AnchoredDialogInPane) { … }
TEST_F(FocusManagerTest, FocusRing) { … }
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
class DesktopWidgetFocusManagerTest : public FocusManagerTest { … };
TEST_F(DesktopWidgetFocusManagerTest, AnchoredDialogInDesktopNativeWidgetAura) { … }
#endif
#if defined(USE_AURA)
class RedirectToParentFocusManagerTest : public FocusManagerTest { … };
TEST_F(RedirectToParentFocusManagerTest, ParentHandlesAcceleratorFromBubble) { … }
TEST_F(RedirectToParentFocusManagerTest, BubbleHandlesRegisteredAccelerators) { … }
TEST_F(RedirectToParentFocusManagerTest, NotProcessedAccelerator) { … }
#endif
}