File: save_update_address_profile_message_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (363 lines) | stat: -rw-r--r-- 16,248 bytes parent folder | download | duplicates (4)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/autofill/android/save_update_address_profile_message_controller.h"

#include "base/android/jni_android.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/android/resource_mapper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_testing_helper.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile_test_api.h"
#include "components/autofill/core/browser/foundations/autofill_client.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/messages/android/mock_message_dispatcher_bridge.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/strings/grit/components_strings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

namespace autofill {

using testing::_;
using profile_ref = base::optional_ref<const AutofillProfile>;
using ::testing::Property;

class SaveUpdateAddressProfileMessageControllerTest
    : public ChromeRenderViewHostTestHarness {
 public:
  SaveUpdateAddressProfileMessageControllerTest() = default;

 protected:
  void SetUp() override;
  void TearDown() override;
  TestingProfile::TestingFactories GetTestingFactories() const override;

  void SigninUser(const std::string& email, signin::ConsentLevel consent_level);
  void EnqueueSaveMessage(
      const AutofillProfile& profile,
      bool is_migration_to_account,
      AutofillClient::AddressProfileSavePromptCallback save_callback,
      SaveUpdateAddressProfileMessageController::PrimaryActionCallback
          action_callback) {
    EnqueueMessage(profile, nullptr, is_migration_to_account,
                   std::move(save_callback), std::move(action_callback));
  }
  void EnqueueUpdateMessage(
      const AutofillProfile& profile,
      const AutofillProfile* original_profile,
      AutofillClient::AddressProfileSavePromptCallback save_callback,
      SaveUpdateAddressProfileMessageController::PrimaryActionCallback
          action_callback) {
    EnqueueMessage(profile, original_profile, /*is_migration_to_account=*/false,
                   std::move(save_callback), std::move(action_callback));
  }
  void ExpectDismissMessageCall();

  void TriggerActionClick();
  void TriggerMessageDismissedCallback(messages::DismissReason dismiss_reason);

  messages::MessageWrapper* GetMessageWrapper();

  std::unique_ptr<AutofillProfile> profile_;
  std::unique_ptr<AutofillProfile> original_profile_;
  base::MockCallback<AutofillClient::AddressProfileSavePromptCallback>
      save_callback_;
  base::MockCallback<
      SaveUpdateAddressProfileMessageController::PrimaryActionCallback>
      action_callback_;

 private:
  void EnqueueMessage(
      const AutofillProfile& profile,
      const AutofillProfile* original_profile,
      bool is_migration_to_account,
      AutofillClient::AddressProfileSavePromptCallback save_callback,
      SaveUpdateAddressProfileMessageController::PrimaryActionCallback
          action_callback);

  std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
      identity_test_env_adaptor_;
  SaveUpdateAddressProfileMessageController controller_;
  messages::MockMessageDispatcherBridge message_dispatcher_bridge_;
  base::test::ScopedFeatureList feature_list_;
};

void SaveUpdateAddressProfileMessageControllerTest::SetUp() {
  ChromeRenderViewHostTestHarness::SetUp();
  messages::MessageDispatcherBridge::SetInstanceForTesting(
      &message_dispatcher_bridge_);
  identity_test_env_adaptor_ =
      std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile());

  profile_ = std::make_unique<AutofillProfile>(test::GetFullProfile());
  original_profile_ =
      std::make_unique<AutofillProfile>(test::GetFullProfile2());
}

void SaveUpdateAddressProfileMessageControllerTest::TearDown() {
  messages::MessageDispatcherBridge::SetInstanceForTesting(nullptr);
  ChromeRenderViewHostTestHarness::TearDown();
}

TestingProfile::TestingFactories
SaveUpdateAddressProfileMessageControllerTest::GetTestingFactories() const {
  return IdentityTestEnvironmentProfileAdaptor::
      GetIdentityTestEnvironmentFactories();
}

void SaveUpdateAddressProfileMessageControllerTest::SigninUser(
    const std::string& email,
    signin::ConsentLevel consent_level) {
  identity_test_env_adaptor_->identity_test_env()->MakePrimaryAccountAvailable(
      email, consent_level);
}

void SaveUpdateAddressProfileMessageControllerTest::EnqueueMessage(
    const AutofillProfile& profile,
    const AutofillProfile* original_profile,
    bool is_migration_to_account,
    AutofillClient::AddressProfileSavePromptCallback save_callback,
    SaveUpdateAddressProfileMessageController::PrimaryActionCallback
        action_callback) {
  EXPECT_CALL(message_dispatcher_bridge_, EnqueueMessage);
  controller_.DisplayMessage(web_contents(), profile, original_profile,
                             is_migration_to_account, std::move(save_callback),
                             std::move(action_callback));
  EXPECT_TRUE(controller_.IsMessageDisplayed());
}

void SaveUpdateAddressProfileMessageControllerTest::ExpectDismissMessageCall() {
  EXPECT_CALL(message_dispatcher_bridge_, DismissMessage)
      .WillOnce([](messages::MessageWrapper* message,
                   messages::DismissReason dismiss_reason) {
        message->HandleDismissCallback(base::android::AttachCurrentThread(),
                                       static_cast<int>(dismiss_reason));
      });
}

void SaveUpdateAddressProfileMessageControllerTest::TriggerActionClick() {
  GetMessageWrapper()->HandleActionClick(base::android::AttachCurrentThread());
  EXPECT_TRUE(controller_.IsMessageDisplayed());
}

void SaveUpdateAddressProfileMessageControllerTest::
    TriggerMessageDismissedCallback(messages::DismissReason dismiss_reason) {
  GetMessageWrapper()->HandleDismissCallback(
      base::android::AttachCurrentThread(), static_cast<int>(dismiss_reason));
  EXPECT_FALSE(controller_.IsMessageDisplayed());
}

messages::MessageWrapper*
SaveUpdateAddressProfileMessageControllerTest::GetMessageWrapper() {
  return controller_.message_.get();
}

// Tests that the save message properties (title, description with profile
// details, primary button text, icon) are set correctly during local or sync
// address profile saving process.
TEST_F(SaveUpdateAddressProfileMessageControllerTest,
       SaveMessageContent_LocalOrSyncAddressProfile) {
  EnqueueSaveMessage(*profile_, /*is_migration_to_account=*/false,
                     save_callback_.Get(), action_callback_.Get());

  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_SAVE_ADDRESS_PROMPT_TITLE),
            GetMessageWrapper()->GetTitle());
  EXPECT_EQ(u"John H. Doe, 666 Erebus St.",
            GetMessageWrapper()->GetDescription());

  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_AUTOFILL_SAVE_ADDRESS_PROMPT_OK_BUTTON_LABEL),
            GetMessageWrapper()->GetPrimaryButtonText());
  EXPECT_EQ(1, GetMessageWrapper()->GetPrimaryButtonTextMaxLines());
  EXPECT_EQ(SaveUpdateAddressProfileMessageController::kDescriptionMaxLines,
            GetMessageWrapper()->GetDescriptionMaxLines());
  EXPECT_EQ(ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_AUTOFILL_ADDRESS),
            GetMessageWrapper()->GetIconResourceId());

  TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
}

// Tests that the save message properties (title, description with profile
// details, primary button text, icon) are set correctly during address profile
// migration flow.
TEST_F(SaveUpdateAddressProfileMessageControllerTest,
       SaveMessageContent_AddressProfileMigrationFlow) {
  test_api(*profile_).set_record_type(AutofillProfile::RecordType::kAccount);
  test_api(*original_profile_)
      .set_record_type(AutofillProfile::RecordType::kAccount);
  SigninUser(TestingProfile::kDefaultProfileUserName,
             signin::ConsentLevel::kSignin);
  EnqueueSaveMessage(*profile_, /*is_migration_to_account=*/true,
                     save_callback_.Get(), action_callback_.Get());

  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_AUTOFILL_ACCOUNT_MIGRATE_ADDRESS_PROMPT_TITLE),
            GetMessageWrapper()->GetTitle());
  EXPECT_EQ(
      l10n_util::GetStringUTF16(
          IDS_AUTOFILL_SAVE_IN_ACCOUNT_MESSAGE_ADDRESS_MIGRATION_RECORD_TYPE_NOTICE),
      GetMessageWrapper()->GetDescription());

  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_AUTOFILL_SAVE_ADDRESS_PROMPT_OK_BUTTON_LABEL),
            GetMessageWrapper()->GetPrimaryButtonText());
  EXPECT_EQ(1, GetMessageWrapper()->GetPrimaryButtonTextMaxLines());
  EXPECT_EQ(SaveUpdateAddressProfileMessageController::kDescriptionMaxLines,
            GetMessageWrapper()->GetDescriptionMaxLines());
  EXPECT_EQ(
      ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_AUTOFILL_UPLOAD_ADDRESS),
      GetMessageWrapper()->GetIconResourceId());

  TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
}

// Tests that the save message properties (title, description with profile
// details, primary button text, icon) are set correctly when a new address
// profile is saved in account.
TEST_F(SaveUpdateAddressProfileMessageControllerTest,
       SaveMessageContent_AccountAddressProfile) {
  test_api(*profile_).set_record_type(AutofillProfile::RecordType::kAccount);
  test_api(*original_profile_)
      .set_record_type(AutofillProfile::RecordType::kAccount);
  SigninUser(TestingProfile::kDefaultProfileUserName,
             signin::ConsentLevel::kSignin);
  EnqueueSaveMessage(*profile_, /*is_migration_to_account=*/false,
                     save_callback_.Get(), action_callback_.Get());

  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_SAVE_ADDRESS_PROMPT_TITLE),
            GetMessageWrapper()->GetTitle());
  EXPECT_EQ(l10n_util::GetStringFUTF16(
                IDS_AUTOFILL_SAVE_IN_ACCOUNT_MESSAGE_ADDRESS_RECORD_TYPE_NOTICE,
                base::ASCIIToUTF16(TestingProfile::kDefaultProfileUserName)),
            GetMessageWrapper()->GetDescription());

  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_AUTOFILL_SAVE_ADDRESS_PROMPT_OK_BUTTON_LABEL),
            GetMessageWrapper()->GetPrimaryButtonText());
  EXPECT_EQ(1, GetMessageWrapper()->GetPrimaryButtonTextMaxLines());
  EXPECT_EQ(SaveUpdateAddressProfileMessageController::kDescriptionMaxLines,
            GetMessageWrapper()->GetDescriptionMaxLines());
  EXPECT_EQ(ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_AUTOFILL_ADDRESS),
            GetMessageWrapper()->GetIconResourceId());

  TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
}

// Tests that the update message properties (title, description with original
// profile details, primary button text, icon) are set correctly.
TEST_F(SaveUpdateAddressProfileMessageControllerTest, UpdateMessageContent) {
  EnqueueUpdateMessage(*profile_, original_profile_.get(), save_callback_.Get(),
                       action_callback_.Get());

  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_UPDATE_ADDRESS_PROMPT_TITLE),
            GetMessageWrapper()->GetTitle());
  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_AUTOFILL_UPDATE_ADDRESS_PROMPT_OK_BUTTON_LABEL),
            GetMessageWrapper()->GetPrimaryButtonText());
  EXPECT_EQ(1, GetMessageWrapper()->GetPrimaryButtonTextMaxLines());
  EXPECT_EQ(u"Jane A. Smith, 123 Main Street",
            GetMessageWrapper()->GetDescription());
  EXPECT_EQ(SaveUpdateAddressProfileMessageController::kDescriptionMaxLines,
            GetMessageWrapper()->GetDescriptionMaxLines());
  EXPECT_EQ(ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_AUTOFILL_ADDRESS),
            GetMessageWrapper()->GetIconResourceId());

  TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
}

// Tests that the action callback is triggered when the user clicks on the
// primary action button of the save message.
TEST_F(SaveUpdateAddressProfileMessageControllerTest,
       ProceedOnActionClickWhenSave) {
  EnqueueSaveMessage(*profile_, /*is_migration_to_account=*/false,
                     save_callback_.Get(), action_callback_.Get());

  EXPECT_CALL(action_callback_, Run(_, *profile_, nullptr, false, _));
  TriggerActionClick();

  EXPECT_CALL(save_callback_, Run(_, Property(&profile_ref::has_value, false)))
      .Times(0);
  TriggerMessageDismissedCallback(messages::DismissReason::PRIMARY_ACTION);
}

// Tests that the action callback is triggered when the user clicks on the
// primary action button of the update message.
TEST_F(SaveUpdateAddressProfileMessageControllerTest,
       ProceedOnActionClickWhenUpdate) {
  EnqueueUpdateMessage(*profile_, original_profile_.get(), save_callback_.Get(),
                       action_callback_.Get());

  EXPECT_CALL(action_callback_,
              Run(_, *profile_, original_profile_.get(), _, _));
  TriggerActionClick();

  EXPECT_CALL(save_callback_, Run(_, Property(&profile_ref::has_value, false)))
      .Times(0);
  TriggerMessageDismissedCallback(messages::DismissReason::PRIMARY_ACTION);
}

// Tests that the save callback is triggered with
// `AddressPromptUserDecision::kMessageDeclined` when the user
// dismisses the message via gesture.
TEST_F(SaveUpdateAddressProfileMessageControllerTest,
       DecisionIsMessageDeclinedOnGestureDismiss) {
  EnqueueSaveMessage(*profile_, /*is_migration_to_account=*/false,
                     save_callback_.Get(), action_callback_.Get());

  EXPECT_CALL(save_callback_,
              Run(AutofillClient::AddressPromptUserDecision::kMessageDeclined,
                  Property(&profile_ref::has_value, false)));
  TriggerMessageDismissedCallback(messages::DismissReason::GESTURE);
}

// Tests that the save callback is triggered with
// `AddressPromptUserDecision::kMessageTimeout` when the message is
// auto-dismissed after a timeout.
TEST_F(SaveUpdateAddressProfileMessageControllerTest,
       DecisionIsMessageTimeoutOnTimerAutodismiss) {
  EnqueueSaveMessage(*profile_, /*is_migration_to_account=*/false,
                     save_callback_.Get(), action_callback_.Get());

  EXPECT_CALL(save_callback_,
              Run(AutofillClient::AddressPromptUserDecision::kMessageTimeout,
                  Property(&profile_ref::has_value, false)));
  TriggerMessageDismissedCallback(messages::DismissReason::TIMER);
}

// Tests that the previous prompt gets dismissed when the new one is enqueued.
TEST_F(SaveUpdateAddressProfileMessageControllerTest, OnlyOnePromptAtATime) {
  EnqueueUpdateMessage(*profile_, original_profile_.get(), save_callback_.Get(),
                       action_callback_.Get());

  AutofillProfile another_profile = test::GetFullProfile();
  base::MockCallback<AutofillClient::AddressProfileSavePromptCallback>
      another_save_callback;
  base::MockCallback<
      SaveUpdateAddressProfileMessageController::PrimaryActionCallback>
      another_action_callback;
  EXPECT_CALL(save_callback_,
              Run(AutofillClient::AddressPromptUserDecision::kIgnored,
                  Property(&profile_ref::has_value, false)));
  ExpectDismissMessageCall();
  EnqueueSaveMessage(another_profile, /*is_migration_to_account=*/false,
                     another_save_callback.Get(),
                     another_action_callback.Get());

  TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
}

}  // namespace autofill