#ifndef ASH_TEST_ASH_TEST_BASE_H_
#define ASH_TEST_ASH_TEST_BASE_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <utility>
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/system/privacy_hub/sensor_disabled_notification_delegate.h"
#include "ash/test/ash_test_helper.h"
#include "ash/test/login_info.h"
#include "ash/test/pixel/ash_pixel_test_init_params.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/overview/overview_types.h"
#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/traits_bag.h"
#include "chromeos/ui/base/app_types.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/user_type.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/aura/client/window_types.h"
#include "ui/aura/env.h"
#include "ui/aura/test/test_windows.h"
#include "ui/compositor/test/test_context_factories.h"
#include "ui/display/display.h"
#include "ui/events/event_constants.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/widget/widget.h"
namespace aura {
class Window;
}
namespace base {
namespace test {
class TaskEnvironment;
}
}
namespace chromeos {
class FakePowerManagerClient;
}
namespace display {
class Display;
class DisplayManager;
namespace test {
class DisplayManagerTestApi;
}
}
namespace gfx {
class Rect;
}
namespace ash {
class AmbientAshTestHelper;
class AppListTestHelper;
class AshPixelDiffer;
class AshPixelTestHelper;
class AshTestHelper;
class NotificationCenterTray;
class Shelf;
class TestAppListClient;
class TestSystemTrayClient;
class UnifiedSystemTray;
class WorkAreaInsets;
class AshTestBase : public testing::Test {
public:
template <typename... TaskEnvironmentTraits>
NOINLINE explicit AshTestBase(TaskEnvironmentTraits&&... traits)
: AshTestBase(std::make_unique<base::test::TaskEnvironment>(
base::test::TaskEnvironment::MainThreadType::UI,
std::forward<TaskEnvironmentTraits>(traits)...)) {}
explicit AshTestBase(
std::unique_ptr<base::test::TaskEnvironment> task_environment);
AshTestBase(std::unique_ptr<base::test::TaskEnvironment> task_environment,
TestingPrefServiceSimple* local_state);
AshTestBase(const AshTestBase&) = delete;
AshTestBase& operator=(const AshTestBase&) = delete;
~AshTestBase() override;
void SetUp() override;
void TearDown() override;
static NotificationCenterTray* GetPrimaryNotificationCenterTray();
static Shelf* GetPrimaryShelf();
static UnifiedSystemTray* GetPrimaryUnifiedSystemTray();
static WorkAreaInsets* GetPrimaryWorkAreaInsets();
void UpdateDisplay(const std::string& display_specs,
bool from_native_platform = false,
bool generate_new_ids = false);
aura::Window* GetContext();
static std::unique_ptr<views::Widget> CreateTestWidget(
views::Widget::InitParams::Ownership ownership,
views::WidgetDelegate* delegate = nullptr,
int container_id = desks_util::GetActiveDeskContainerId(),
const gfx::Rect& bounds = gfx::Rect(),
bool show = true);
static std::unique_ptr<views::Widget> CreateFramelessTestWidget(
views::Widget::InitParams::Ownership ownership =
views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
std::unique_ptr<aura::Window> CreateAppWindow(
const gfx::Rect& bounds_in_screen = gfx::Rect(),
chromeos::AppType app_type = chromeos::AppType::SYSTEM_APP,
int shell_window_id = kShellWindowId_Invalid,
views::WidgetDelegate* delegate = nullptr,
bool show = true);
std::unique_ptr<aura::Window> CreateTestWindow(
const gfx::Rect& bounds_in_screen = gfx::Rect(),
aura::client::WindowType type = aura::client::WINDOW_TYPE_NORMAL,
int shell_window_id = kShellWindowId_Invalid);
std::unique_ptr<aura::Window> CreateToplevelTestWindow(
const gfx::Rect& bounds_in_screen = gfx::Rect(),
int shell_window_id = kShellWindowId_Invalid);
aura::Window* CreateTestWindowInShell(aura::test::WindowBuilderParams params);
void ParentWindowInPrimaryRootWindow(aura::Window* window);
AshPixelDiffer* GetPixelDiffer();
ui::test::EventGenerator* GetEventGenerator();
display::DisplayManager* display_manager();
chromeos::FakePowerManagerClient* power_manager_client() const;
bool TestIfMouseWarpsAt(ui::test::EventGenerator* event_generator,
const gfx::Point& point_in_screen);
void PressAndReleaseKey(ui::KeyboardCode key_code, int flags = ui::EF_NONE);
void LeftClickOn(const views::View* view);
void RightClickOn(const views::View* view);
void GestureTapOn(const views::View* view);
bool EnterOverview(
OverviewEnterExitType type = OverviewEnterExitType::kNormal);
bool ExitOverview(
OverviewEnterExitType type = OverviewEnterExitType::kNormal);
void SetShelfAnimationDuration(base::TimeDelta duration);
void WaitForShelfAnimation();
void MaybeRunDragAndDropSequenceForAppList(
std::list<base::OnceClosure>* tasks,
bool is_touch);
virtual void OnHelperWillBeDestroyed() {}
protected:
enum UserSessionBlockReason {
FIRST_BLOCK_REASON,
BLOCKED_BY_LOCK_SCREEN = FIRST_BLOCK_REASON,
BLOCKED_BY_LOGIN_SCREEN,
BLOCKED_BY_USER_ADDING_SCREEN,
NUMBER_OF_BLOCK_REASONS
};
static display::Display::Rotation GetActiveDisplayRotation(int64_t id);
static display::Display::Rotation GetCurrentInternalDisplayRotation();
virtual std::optional<pixel_test::InitParams> CreatePixelTestInitParams()
const;
virtual std::string GenerateScreenshotName(const std::string& title);
void set_start_session(bool start_session) {
CHECK(init_params_) << "start_session must set before calling SetUp()";
init_params_->start_session = start_session;
}
void set_create_signin_pref_service(bool create_signin_pref_service) {
CHECK(init_params_)
<< "create_create_signin_pref_service must set before calling SetUp()";
init_params_->create_signin_pref_service = create_signin_pref_service;
}
void set_create_global_cras_audio_handler(
bool create_global_cras_audio_handler) {
CHECK(init_params_)
<< "create_global_cras_audio_handler must set before calling SetUp()";
init_params_->create_global_cras_audio_handler =
create_global_cras_audio_handler;
}
void set_create_quick_pair_mediator(bool create_quick_pair_mediator) {
CHECK(init_params_)
<< "create_quick_pair_mediator must set before calling SetUp()";
init_params_->create_quick_pair_mediator = create_quick_pair_mediator;
}
void set_shell_delegate(std::unique_ptr<ShellDelegate> shell_delegate) {
CHECK(init_params_) << "shell_delegate mustset before calling SetUp()";
CHECK(!init_params_->delegate);
init_params_->delegate = std::move(shell_delegate);
}
base::test::TaskEnvironment* task_environment() {
return task_environment_.get();
}
TestingPrefServiceSimple* local_state() { return local_state_.get(); }
AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
AshPixelTestHelper* pixel_test_helper() { return pixel_test_helper_.get(); }
ui::InProcessContextFactory* GetContextFactory() {
return test_context_factories_
? test_context_factories_->GetContextFactory()
: nullptr;
}
void SetUserPref(const std::string& user_email,
const std::string& path,
const base::Value& value);
TestSessionControllerClient* GetSessionControllerClient();
TestSystemTrayClient* GetSystemTrayClient();
AppListTestHelper* GetAppListTestHelper();
TestAppListClient* GetTestAppListClient();
AmbientAshTestHelper* GetAmbientAshTestHelper();
AccountId SimulateUserLogin(
LoginInfo login_info,
std::optional<AccountId> opt_account_id = std::nullopt,
std::unique_ptr<PrefService> pref_service = nullptr);
void SimulateUserLogin(const AccountId& account_id);
AccountId SimulateNewUserFirstLogin(const std::string& user_email);
AccountId SimulateGuestLogin();
AccountId SimulateKioskMode(user_manager::UserType user_type);
void SwitchActiveUser(const AccountId& account_id);
bool IsInSessionState(session_manager::SessionState state) const;
void SetAccessibilityPanelHeight(int panel_height);
void ClearLogin();
void SetCanLockScreen(bool can_lock);
void SetShouldLockScreenAutomatically(bool should_lock);
void SetUserAddingScreenRunning(bool user_adding_screen_running);
void BlockUserSession(UserSessionBlockReason block_reason);
void UnblockUserSession();
void SetVirtualKeyboardEnabled(bool enabled);
void DisableIME();
void SwapPrimaryDisplay();
display::Display GetPrimaryDisplay() const;
display::Display GetSecondaryDisplay() const;
private:
void CreateWindowTreeIfNecessary();
void PrepareForPixelDiffTest();
bool setup_called_ = false;
bool teardown_called_ = false;
std::unique_ptr<AshTestHelper::InitParams> init_params_ =
std::make_unique<AshTestHelper::InitParams>();
std::unique_ptr<base::test::TaskEnvironment> task_environment_;
std::unique_ptr<TestingPrefServiceSimple> owned_local_state_;
raw_ptr<TestingPrefServiceSimple> local_state_;
std::unique_ptr<AshPixelDiffer> pixel_differ_;
std::unique_ptr<ui::TestContextFactories> test_context_factories_;
std::unique_ptr<AshTestHelper> ash_test_helper_;
std::unique_ptr<AshPixelTestHelper> pixel_test_helper_;
std::unique_ptr<ui::test::EventGenerator> event_generator_;
std::unique_ptr<ScopedSensorDisabledNotificationDelegateForTest>
scoped_disabled_notification_delegate_;
};
class NoSessionAshTestBase : public AshTestBase {
public:
NoSessionAshTestBase();
explicit NoSessionAshTestBase(
base::test::TaskEnvironment::TimeSource time_source);
NoSessionAshTestBase(const NoSessionAshTestBase&) = delete;
NoSessionAshTestBase& operator=(const NoSessionAshTestBase&) = delete;
~NoSessionAshTestBase() override;
};
}
#endif