Commit 9bdb4805 authored by Jeroen Dhollander's avatar Jeroen Dhollander Committed by Commit Bot

Add unittests for QuickAnswer Consent

If the quick answer consent view is discarded 3 times, we no longer show it
and start showing the quick answers.
This was previously not tested.

Bug: b/169362551
Tests: ash_unittests --gtest_filter="QuickAnswers*"
Change-Id: I7352b45a724980ac169e0d250f06e7b46a7c2440
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2436316Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarYue Li <updowndota@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: Jeroen Dhollander <jeroendh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812296}
parent 70da4f5a
...@@ -45,6 +45,13 @@ base::string16 IntentTypeToString(IntentType intent_type) { ...@@ -45,6 +45,13 @@ base::string16 IntentTypeToString(IntentType intent_type) {
return base::string16(); return base::string16();
} }
} }
// Returns if the request has already been processed (by the text annotator).
bool IsProcessedRequest(const QuickAnswersRequest& request) {
return (request.preprocessed_output.intent_info.intent_type !=
chromeos::quick_answers::IntentType::kUnknown);
}
} // namespace } // namespace
namespace ash { namespace ash {
...@@ -87,13 +94,26 @@ void QuickAnswersControllerImpl::MaybeShowQuickAnswers( ...@@ -87,13 +94,26 @@ void QuickAnswersControllerImpl::MaybeShowQuickAnswers(
// Send the request for preprocessing. Only shows quick answers view if the // Send the request for preprocessing. Only shows quick answers view if the
// predicted intent is not |kUnknown| at |OnRequestPreprocessFinish|. // predicted intent is not |kUnknown| at |OnRequestPreprocessFinish|.
quick_answers_client_->SendRequestForPreprocessing(request); quick_answers_client_->SendRequestForPreprocessing(request);
} else if (!MaybeShowUserConsent(base::string16(), base::string16())) { } else {
// Text annotator is not enabled and consent view is not showing, shows HandleQuickAnswerRequest(request);
// quick answers view with placeholder and send the request. }
}
void QuickAnswersControllerImpl::HandleQuickAnswerRequest(
const chromeos::quick_answers::QuickAnswersRequest& request) {
if (ShouldShowUserConsent()) {
ShowUserConsent(
IntentTypeToString(request.preprocessed_output.intent_info.intent_type),
base::UTF8ToUTF16(request.preprocessed_output.intent_info.intent_text));
} else {
visibility_ = QuickAnswersVisibility::kVisible; visibility_ = QuickAnswersVisibility::kVisible;
quick_answers_ui_controller_->CreateQuickAnswersView(anchor_bounds, title_, quick_answers_ui_controller_->CreateQuickAnswersView(anchor_bounds_, title_,
query_); query_);
quick_answers_client_->SendRequest(request);
if (IsProcessedRequest(request))
quick_answers_client_->FetchQuickAnswers(request);
else
quick_answers_client_->SendRequest(request);
} }
} }
...@@ -172,15 +192,7 @@ void QuickAnswersControllerImpl::OnRequestPreprocessFinished( ...@@ -172,15 +192,7 @@ void QuickAnswersControllerImpl::OnRequestPreprocessFinished(
query_ = processed_request.preprocessed_output.query; query_ = processed_request.preprocessed_output.query;
title_ = processed_request.preprocessed_output.intent_info.intent_text; title_ = processed_request.preprocessed_output.intent_info.intent_text;
if (!MaybeShowUserConsent(IntentTypeToString(intent_type), HandleQuickAnswerRequest(processed_request);
base::UTF8ToUTF16(title_))) {
if (!quick_answers_ui_controller_->is_showing_quick_answers_view()) {
visibility_ = QuickAnswersVisibility::kVisible;
quick_answers_ui_controller_->CreateQuickAnswersView(anchor_bounds_,
title_, query_);
}
quick_answers_client_->FetchQuickAnswers(processed_request);
}
} }
void QuickAnswersControllerImpl::OnRetryQuickAnswersRequest() { void QuickAnswersControllerImpl::OnRetryQuickAnswersRequest() {
...@@ -237,19 +249,19 @@ void QuickAnswersControllerImpl::MaybeDismissQuickAnswersConsent() { ...@@ -237,19 +249,19 @@ void QuickAnswersControllerImpl::MaybeDismissQuickAnswersConsent() {
quick_answers_ui_controller_->CloseUserConsentView(); quick_answers_ui_controller_->CloseUserConsentView();
} }
bool QuickAnswersControllerImpl::MaybeShowUserConsent( bool QuickAnswersControllerImpl::ShouldShowUserConsent() const {
return consent_controller_->ShouldShowConsent();
}
void QuickAnswersControllerImpl::ShowUserConsent(
const base::string16& intent_type, const base::string16& intent_type,
const base::string16& intent_text) { const base::string16& intent_text) {
if (consent_controller_->ShouldShowConsent()) { // Show user-consent notice informing user about the feature if required.
// Show user-consent notice informing user about the feature if required. if (!quick_answers_ui_controller_->is_showing_user_consent_view()) {
if (!quick_answers_ui_controller_->is_showing_user_consent_view()) { quick_answers_ui_controller_->CreateUserConsentView(
quick_answers_ui_controller_->CreateUserConsentView( anchor_bounds_, intent_type, intent_text);
anchor_bounds_, intent_type, intent_text); consent_controller_->StartConsent();
consent_controller_->StartConsent();
}
return true;
} }
return false;
} }
QuickAnswersRequest QuickAnswersControllerImpl::BuildRequest() { QuickAnswersRequest QuickAnswersControllerImpl::BuildRequest() {
......
...@@ -110,9 +110,13 @@ class ASH_EXPORT QuickAnswersControllerImpl ...@@ -110,9 +110,13 @@ class ASH_EXPORT QuickAnswersControllerImpl
private: private:
void MaybeDismissQuickAnswersConsent(); void MaybeDismissQuickAnswersConsent();
// Return true if the user consent view is showing. void HandleQuickAnswerRequest(
bool MaybeShowUserConsent(const base::string16& intent_type, const chromeos::quick_answers::QuickAnswersRequest& request);
const base::string16& intent_text);
bool ShouldShowUserConsent() const;
// Show the user consent view. Does nothing if the view is already visible.
void ShowUserConsent(const base::string16& intent_type,
const base::string16& intent_text);
chromeos::quick_answers::QuickAnswersRequest BuildRequest(); chromeos::quick_answers::QuickAnswersRequest BuildRequest();
......
...@@ -44,6 +44,9 @@ class QuickAnswersControllerTest : public AshTestBase { ...@@ -44,6 +44,9 @@ class QuickAnswersControllerTest : public AshTestBase {
std::make_unique<chromeos::quick_answers::QuickAnswersClient>( std::make_unique<chromeos::quick_answers::QuickAnswersClient>(
&test_url_loader_factory_, ash::AssistantState::Get(), &test_url_loader_factory_, ash::AssistantState::Get(),
controller()->GetQuickAnswersDelegate())); controller()->GetQuickAnswersDelegate()));
controller()->OnEligibilityChanged(true);
controller()->SetVisibilityForTesting(QuickAnswersVisibility::kPending);
} }
QuickAnswersControllerImpl* controller() { QuickAnswersControllerImpl* controller() {
...@@ -51,6 +54,19 @@ class QuickAnswersControllerTest : public AshTestBase { ...@@ -51,6 +54,19 @@ class QuickAnswersControllerTest : public AshTestBase {
QuickAnswersController::Get()); QuickAnswersController::Get());
} }
void ShowQuickAnswers(bool set_visibility = true) {
// To show the quick answers view, its visibility must be set to 'pending'
// first.
if (set_visibility)
controller()->SetPendingShowQuickAnswers();
controller()->MaybeShowQuickAnswers(kDefaultAnchorBoundsInScreen,
kDefaultTitle, {});
}
void DismissQuickAnswers() {
controller()->DismissQuickAnswers(/*is_active=*/true);
}
QuickAnswersUiController* ui_controller() { QuickAnswersUiController* ui_controller() {
return controller()->quick_answers_ui_controller(); return controller()->quick_answers_ui_controller();
} }
...@@ -66,9 +82,7 @@ class QuickAnswersControllerTest : public AshTestBase { ...@@ -66,9 +82,7 @@ class QuickAnswersControllerTest : public AshTestBase {
TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenFeatureNotEligible) { TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenFeatureNotEligible) {
controller()->OnEligibilityChanged(false); controller()->OnEligibilityChanged(false);
controller()->SetVisibilityForTesting(QuickAnswersVisibility::kPending); ShowQuickAnswers();
controller()->MaybeShowQuickAnswers(kDefaultAnchorBoundsInScreen,
kDefaultTitle, {});
// The feature is not eligible, nothing should be shown. // The feature is not eligible, nothing should be shown.
EXPECT_FALSE(ui_controller()->is_showing_user_consent_view()); EXPECT_FALSE(ui_controller()->is_showing_user_consent_view());
...@@ -76,10 +90,8 @@ TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenFeatureNotEligible) { ...@@ -76,10 +90,8 @@ TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenFeatureNotEligible) {
} }
TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenClosed) { TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenClosed) {
controller()->OnEligibilityChanged(true);
controller()->SetVisibilityForTesting(QuickAnswersVisibility::kClosed); controller()->SetVisibilityForTesting(QuickAnswersVisibility::kClosed);
controller()->MaybeShowQuickAnswers(kDefaultAnchorBoundsInScreen, ShowQuickAnswers(/*set_visibility=*/false);
kDefaultTitle, {});
// The UI is closed and session is inactive, nothing should be shown. // The UI is closed and session is inactive, nothing should be shown.
EXPECT_FALSE(ui_controller()->is_showing_user_consent_view()); EXPECT_FALSE(ui_controller()->is_showing_user_consent_view());
...@@ -87,11 +99,9 @@ TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenClosed) { ...@@ -87,11 +99,9 @@ TEST_F(QuickAnswersControllerTest, ShouldNotShowWhenClosed) {
EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kClosed); EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kClosed);
} }
TEST_F(QuickAnswersControllerTest, AcceptUserConsent) { TEST_F(QuickAnswersControllerTest,
controller()->OnEligibilityChanged(true); ShouldShowPendingQueryAfterUserAcceptsConsent) {
controller()->SetVisibilityForTesting(QuickAnswersVisibility::kPending); ShowQuickAnswers();
controller()->MaybeShowQuickAnswers(kDefaultAnchorBoundsInScreen,
kDefaultTitle, {});
// Without user consent, only the user consent view should show. // Without user consent, only the user consent view should show.
EXPECT_TRUE(ui_controller()->is_showing_user_consent_view()); EXPECT_TRUE(ui_controller()->is_showing_user_consent_view());
EXPECT_FALSE(ui_controller()->is_showing_quick_answers_view()); EXPECT_FALSE(ui_controller()->is_showing_quick_answers_view());
...@@ -105,14 +115,11 @@ TEST_F(QuickAnswersControllerTest, AcceptUserConsent) { ...@@ -105,14 +115,11 @@ TEST_F(QuickAnswersControllerTest, AcceptUserConsent) {
EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kVisible); EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kVisible);
} }
TEST_F(QuickAnswersControllerTest, UserConsentAlreadyAccpeted) { TEST_F(QuickAnswersControllerTest, UserConsentAlreadyAccepted) {
consent_controller()->StartConsent(); consent_controller()->StartConsent();
controller()->OnEligibilityChanged(true);
controller()->SetVisibilityForTesting(QuickAnswersVisibility::kPending);
consent_controller()->AcceptConsent( consent_controller()->AcceptConsent(
chromeos::quick_answers::ConsentInteractionType::kAccept); chromeos::quick_answers::ConsentInteractionType::kAccept);
controller()->MaybeShowQuickAnswers(kDefaultAnchorBoundsInScreen, ShowQuickAnswers();
kDefaultTitle, {});
// With user consent already accepted, only the quick answers view should // With user consent already accepted, only the quick answers view should
// show. // show.
...@@ -121,26 +128,38 @@ TEST_F(QuickAnswersControllerTest, UserConsentAlreadyAccpeted) { ...@@ -121,26 +128,38 @@ TEST_F(QuickAnswersControllerTest, UserConsentAlreadyAccpeted) {
EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kVisible); EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kVisible);
} }
TEST_F(QuickAnswersControllerTest,
ShouldShowQuickAnswersIfUserIgnoresConsentViewThreeTimes) {
// Show and dismiss user consent window the first 3 times
for (int i = 0; i < 3; i++) {
ShowQuickAnswers();
EXPECT_TRUE(ui_controller()->is_showing_user_consent_view())
<< "Consent view not shown the " << (i + 1) << " time";
EXPECT_FALSE(ui_controller()->is_showing_quick_answers_view());
DismissQuickAnswers();
}
// The 4th time we should simply show the quick answer.
ShowQuickAnswers();
EXPECT_FALSE(ui_controller()->is_showing_user_consent_view());
EXPECT_TRUE(ui_controller()->is_showing_quick_answers_view());
}
TEST_F(QuickAnswersControllerTest, DismissUserConsentView) { TEST_F(QuickAnswersControllerTest, DismissUserConsentView) {
controller()->OnEligibilityChanged(true); ShowQuickAnswers();
controller()->SetVisibilityForTesting(QuickAnswersVisibility::kPending);
controller()->MaybeShowQuickAnswers(kDefaultAnchorBoundsInScreen,
kDefaultTitle, {});
EXPECT_TRUE(ui_controller()->is_showing_user_consent_view()); EXPECT_TRUE(ui_controller()->is_showing_user_consent_view());
controller()->DismissQuickAnswers(true); DismissQuickAnswers();
EXPECT_FALSE(ui_controller()->is_showing_user_consent_view()); EXPECT_FALSE(ui_controller()->is_showing_user_consent_view());
EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kClosed); EXPECT_EQ(controller()->visibility(), QuickAnswersVisibility::kClosed);
} }
TEST_F(QuickAnswersControllerTest, DismissQuickAnswersView) { TEST_F(QuickAnswersControllerTest, DismissQuickAnswersView) {
consent_controller()->StartConsent(); consent_controller()->StartConsent();
controller()->OnEligibilityChanged(true);
controller()->SetVisibilityForTesting(QuickAnswersVisibility::kPending);
consent_controller()->AcceptConsent( consent_controller()->AcceptConsent(
chromeos::quick_answers::ConsentInteractionType::kAccept); chromeos::quick_answers::ConsentInteractionType::kAccept);
controller()->MaybeShowQuickAnswers(kDefaultAnchorBoundsInScreen, ShowQuickAnswers();
kDefaultTitle, {});
EXPECT_TRUE(ui_controller()->is_showing_quick_answers_view()); EXPECT_TRUE(ui_controller()->is_showing_quick_answers_view());
controller()->DismissQuickAnswers(true); controller()->DismissQuickAnswers(true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment