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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/assistant/test/assistant_ash_test_base.h"
#include <string>
#include <utility>
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/app_list/views/app_list_view.h"
#include "ash/assistant/test/test_assistant_setup.h"
#include "ash/assistant/ui/main_stage/assistant_onboarding_suggestion_view.h"
#include "ash/assistant/ui/main_stage/suggestion_chip_view.h"
#include "ash/constants/ash_features.h"
#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/keyboard/ui/test/keyboard_test_util.h"
#include "ash/public/cpp/assistant/assistant_state.h"
#include "ash/public/cpp/assistant/controller/assistant_ui_controller.h"
#include "ash/public/cpp/test/assistant_test_api.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_helper.h"
#include "ash/test/test_ash_web_view_factory.h"
#include "ash/test/view_drawn_waiter.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/services/assistant/public/cpp/features.h"
#include "chromeos/ash/services/assistant/test_support/scoped_assistant_browser_delegate.h"
#include "ui/views/view_utils.h"
namespace ash {
namespace {
gfx::Point GetPointInside(const views::View* view) {
return view->GetBoundsInScreen().CenterPoint();
}
bool CanProcessEvents(const views::View* view) {
const views::View* ancestor = view;
while (ancestor != nullptr) {
if (!ancestor->GetCanProcessEventsWithinSubtree())
return false;
ancestor = ancestor->parent();
}
return true;
}
void CheckCanProcessEvents(const views::View* view) {
if (!view->IsDrawn()) {
ADD_FAILURE()
<< view->GetClassName()
<< " can not process events because it is not drawn on screen.";
} else if (!CanProcessEvents(view)) {
ADD_FAILURE() << view->GetClassName() << " can not process events.";
}
}
void PressHomeButton() {
Shell::Get()->app_list_controller()->ToggleAppList(
display::Screen::GetScreen()->GetPrimaryDisplay().id(),
AppListShowSource::kShelfButton, base::TimeTicks::Now());
}
// Collects all child views of the given templated type.
// This includes direct and indirect children.
// For this class to work, _ChildView must:
// * Inherit from |views::View|.
// * Implement view metadata (see comments on views::View).
template <class _ChildView>
class ChildViewCollector {
public:
using Views = std::vector<_ChildView*>;
explicit ChildViewCollector(const views::View* parent) : parent_(parent) {}
Views Get() {
Views result;
for (views::View* child : parent_->children())
Get(child, &result);
return result;
}
private:
void Get(views::View* view, Views* result) {
if (views::IsViewClass<_ChildView>(view))
result->push_back(static_cast<_ChildView*>(view));
for (views::View* child : view->children())
Get(child, result);
}
raw_ptr<const views::View> parent_;
};
} // namespace
AssistantAshTestBase::AssistantAshTestBase()
: AssistantAshTestBase(
base::test::TaskEnvironment::TimeSource::SYSTEM_TIME) {}
AssistantAshTestBase::AssistantAshTestBase(
base::test::TaskEnvironment::TimeSource time)
: AshTestBase(time),
test_api_(AssistantTestApi::Create()),
test_setup_(std::make_unique<TestAssistantSetup>()),
test_web_view_factory_(std::make_unique<TestAshWebViewFactory>()),
delegate_(std::make_unique<assistant::ScopedAssistantBrowserDelegate>()) {
}
AssistantAshTestBase::~AssistantAshTestBase() = default;
void AssistantAshTestBase::SetUp() {
AshTestBase::SetUp();
if (ash::assistant::features::IsNewEntryPointEnabled()) {
GTEST_SKIP() << "Assistant is not enabled if new entry point is enabled.";
}
// Make the display big enough to hold the app list.
UpdateDisplay("1024x768");
test_api_->DisableAnimations();
EnableKeyboard();
if (set_up_active_user_in_test_set_up_) {
SetUpActiveUser();
}
}
void AssistantAshTestBase::TearDown() {
windows_.clear();
widgets_.clear();
DisableKeyboard();
AshTestBase::TearDown();
}
void AssistantAshTestBase::CreateAndSwitchActiveUser(
const std::string& display_email,
const std::string& given_name) {
ClearLogin();
SimulateUserLogin({.display_email = display_email, .given_name = given_name});
SetUpActiveUser();
}
void AssistantAshTestBase::ShowAssistantUi(AssistantEntryPoint entry_point) {
if (entry_point == AssistantEntryPoint::kHotword) {
// If the Assistant is triggered via Hotword, the interaction is triggered
// by the Assistant service.
assistant_service()->StartVoiceInteraction();
} else {
// Otherwise, the interaction is triggered by a call to ShowUi().
AssistantUiController::Get()->ShowUi(entry_point);
}
// Send all mojom messages to/from the assistant service.
base::RunLoop().RunUntilIdle();
// Ensure assistant page is visible and has finished layout to non-zero size.
ViewDrawnWaiter().Wait(page_view());
}
void AssistantAshTestBase::CloseAssistantUi(AssistantExitPoint exit_point) {
AssistantUiController::Get()->CloseUi(exit_point);
}
void AssistantAshTestBase::OpenLauncher() {
PressHomeButton();
}
void AssistantAshTestBase::CloseLauncher() {
Shell::Get()->app_list_controller()->DismissAppList();
}
void AssistantAshTestBase::SetTabletMode(bool enable) {
test_api_->SetTabletMode(enable);
}
void AssistantAshTestBase::SetConsentStatus(ConsentStatus consent_status) {
test_api_->SetConsentStatus(consent_status);
}
void AssistantAshTestBase::SetNumberOfSessionsWhereOnboardingShown(
int number_of_sessions) {
test_api_->SetNumberOfSessionsWhereOnboardingShown(number_of_sessions);
}
void AssistantAshTestBase::SetOnboardingMode(
AssistantOnboardingMode onboarding_mode) {
test_api_->SetOnboardingMode(onboarding_mode);
}
void AssistantAshTestBase::SetPreferVoice(bool prefer_voice) {
test_api_->SetPreferVoice(prefer_voice);
}
void AssistantAshTestBase::SetTimeOfLastInteraction(const base::Time& time) {
test_api_->SetTimeOfLastInteraction(time);
}
void AssistantAshTestBase::StartOverview() {
test_api_->StartOverview();
}
bool AssistantAshTestBase::IsVisible() {
return test_api_->IsVisible();
}
views::View* AssistantAshTestBase::page_view() {
return test_api_->page_view();
}
AppListView* AssistantAshTestBase::app_list_view() {
return test_api_->app_list_view();
}
views::View* AssistantAshTestBase::root_view() {
views::View* result = app_list_view();
while (result && result->parent())
result = result->parent();
return result;
}
MockedAssistantInteraction AssistantAshTestBase::MockTextInteraction() {
return MockedAssistantInteraction(test_api_.get(), assistant_service());
}
void AssistantAshTestBase::SendQueryThroughTextField(const std::string& query) {
test_api_->SendTextQuery(query);
}
void AssistantAshTestBase::TapOnAndWait(const views::View* view) {
CheckCanProcessEvents(view);
TapAndWait(GetPointInside(view));
}
void AssistantAshTestBase::TapAndWait(gfx::Point position) {
GetEventGenerator()->GestureTapAt(position);
base::RunLoop().RunUntilIdle();
}
void AssistantAshTestBase::ClickOnAndWait(
const views::View* view,
bool check_if_view_can_process_events) {
if (check_if_view_can_process_events)
CheckCanProcessEvents(view);
GetEventGenerator()->MoveMouseTo(GetPointInside(view));
GetEventGenerator()->ClickLeftButton();
base::RunLoop().RunUntilIdle();
}
std::optional<assistant::AssistantInteractionMetadata>
AssistantAshTestBase::current_interaction() {
return assistant_service()->current_interaction();
}
aura::Window* AssistantAshTestBase::SwitchToNewAppWindow() {
windows_.push_back(CreateAppWindow());
aura::Window* window = windows_.back().get();
window->SetName("<app-window>");
return window;
}
views::Widget* AssistantAshTestBase::SwitchToNewWidget() {
widgets_.push_back(
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET));
views::Widget* result = widgets_.back().get();
// Give the widget a non-zero size, otherwise things like tapping and clicking
// on it do not work.
result->SetBounds(gfx::Rect(500, 100));
return result;
}
aura::Window* AssistantAshTestBase::window() {
return test_api_->window();
}
views::Textfield* AssistantAshTestBase::input_text_field() {
return test_api_->input_text_field();
}
views::View* AssistantAshTestBase::mic_view() {
return test_api_->mic_view();
}
views::View* AssistantAshTestBase::greeting_label() {
return test_api_->greeting_label();
}
views::View* AssistantAshTestBase::voice_input_toggle() {
return test_api_->voice_input_toggle();
}
views::View* AssistantAshTestBase::keyboard_input_toggle() {
return test_api_->keyboard_input_toggle();
}
views::View* AssistantAshTestBase::onboarding_view() {
return test_api_->onboarding_view();
}
views::View* AssistantAshTestBase::opt_in_view() {
return test_api_->opt_in_view();
}
views::View* AssistantAshTestBase::suggestion_chip_container() {
return test_api_->suggestion_chip_container();
}
std::vector<AssistantOnboardingSuggestionView*>
AssistantAshTestBase::GetOnboardingSuggestionViews() {
const views::View* container = onboarding_view();
return ChildViewCollector<AssistantOnboardingSuggestionView>{container}.Get();
}
std::vector<SuggestionChipView*> AssistantAshTestBase::GetSuggestionChips() {
const views::View* container = suggestion_chip_container();
return ChildViewCollector<SuggestionChipView>{container}.Get();
}
void AssistantAshTestBase::ShowKeyboard() {
auto* keyboard_controller = keyboard::KeyboardUIController::Get();
keyboard_controller->ShowKeyboard(/*lock=*/false);
}
void AssistantAshTestBase::DismissKeyboard() {
auto* keyboard_controller = keyboard::KeyboardUIController::Get();
keyboard_controller->HideKeyboardImplicitlyByUser();
EXPECT_FALSE(IsKeyboardShowing());
}
bool AssistantAshTestBase::IsKeyboardShowing() const {
auto* keyboard_controller = keyboard::KeyboardUIController::Get();
return keyboard_controller->IsEnabled() &&
keyboard::test::IsKeyboardShowing();
}
TestAssistantService* AssistantAshTestBase::assistant_service() {
return ash_test_helper()->test_assistant_service();
}
void AssistantAshTestBase::SetUpActiveUser() {
// Enable Assistant in settings.
test_api_->SetAssistantEnabled(true);
// Enable screen context in settings.
test_api_->SetScreenContextEnabled(true);
// Set AssistantAllowedState to ALLOWED.
test_api_->GetAssistantState()->NotifyFeatureAllowed(
assistant::AssistantAllowedState::ALLOWED);
// Set user consent so the suggestion chips are displayed.
SetConsentStatus(ConsentStatus::kActivityControlAccepted);
// At this point our Assistant service is ready for use.
// Indicate this by changing status from NOT_READY to READY.
test_api_->GetAssistantState()->NotifyStatusChanged(
assistant::AssistantStatus::READY);
}
} // namespace ash
|