File: batch_upload_dialog_view_pixel_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (248 lines) | stat: -rw-r--r-- 10,456 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/i18n/number_formatting.h"
#include "chrome/browser/profiles/batch_upload/batch_upload_service.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/profiles/batch_upload_dialog_view.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/signin/public/identity_manager/signin_constants.h"
#include "components/sync/base/data_type.h"
#include "components/sync/service/local_data_description.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "ui/base/ui_base_switches.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/views/widget/any_widget_observer.h"

using signin::constants::kNoHostedDomainFound;

namespace {

const gfx::Image kSignedInImage = gfx::test::CreateImage(20, 20, SK_ColorBLUE);
const char kSignedInImageUrl[] = "SIGNED_IN_IMAGE_URL";

syncer::LocalDataDescription GetFakeLocalData(syncer::DataType type,
                                              int item_count,
                                              bool long_title) {
  syncer::LocalDataDescription data_descriptions;
  data_descriptions.type = type;

  const std::string data_name =
      type == syncer::DataType::PASSWORDS ? "password" : "address";
  // Add arbitrary items.
  for (int i = 0; i < item_count; ++i) {
    syncer::LocalDataItemModel item;
    item.id = syncer::LocalDataItemModel::DataId(base::ToString(i));

    switch (type) {
      case syncer::DataType::PASSWORDS:
        item.icon = syncer::LocalDataItemModel::PageUrlIcon(
            GURL("https://chromium.org"));
        break;
      case syncer::DataType::BOOKMARKS:
        item.icon = syncer::LocalDataItemModel::FolderIcon();
        break;
      case syncer::DataType::CONTACT_INFO:
      case syncer::DataType::THEMES:
        item.icon = syncer::LocalDataItemModel::NoIcon();
        break;
      default:
        NOTREACHED();
    }

    // Theme item has a different title as it is shown in the section title.
    if (type == syncer::DataType::THEMES) {
      item.title = long_title ? "Custom theme name that is very very long long "
                                "long long long long"
                              : "Custom theme name";
    } else {
      item.title =
          data_name + "_title_" + base::UTF16ToUTF8(base::FormatNumber(i));
      item.subtitle =
          data_name + "_subtitle_" + base::UTF16ToUTF8(base::FormatNumber(i));
    }

    data_descriptions.local_data_models.push_back(std::move(item));
  }
  return data_descriptions;
}

struct TestParam {
  std::string test_suffix = "";
  bool use_dark_theme = false;
  std::vector<std::pair<int, syncer::DataType>> section_item_count_type = {
      {2, syncer::DataType::PASSWORDS},
      {1, syncer::DataType::CONTACT_INFO},
  };
  // Currently only relevant for Themes section as the item title is shown in
  // the section title.
  bool long_title = false;
};

// Allows the test to be named like
// `BatchUploadDialogViewPixelTest.InvokeUi_default/{test_suffix}`.
std::string ParamToTestSuffix(const ::testing::TestParamInfo<TestParam>& info) {
  return info.param.test_suffix;
}

// Test configurations
const TestParam kTestParams[] = {
    {.test_suffix = "Regular"},

    {.test_suffix = "DarkTheme", .use_dark_theme = true},

    {.test_suffix = "MultipleSectionsScrollbar",
     // Multiple sections with the same type just for testing purposes.
     .section_item_count_type = {{2, syncer::DataType::PASSWORDS},
                                 {1, syncer::DataType::PASSWORDS},
                                 {10, syncer::DataType::CONTACT_INFO},
                                 {15, syncer::DataType::CONTACT_INFO},
                                 {16, syncer::DataType::CONTACT_INFO},
                                 {10, syncer::DataType::PASSWORDS},
                                 {5, syncer::DataType::PASSWORDS}}},

    // Hero type means the type will be shown in the subtitle of the dialog.
    // Password is a hero type.
    {.test_suffix = "SingleSectionPasswordsHeroTypeWithOneItem",
     .section_item_count_type = {{1, syncer::DataType::PASSWORDS}}},
    {.test_suffix = "SingleSectionPasswordsHeroTypeWithMultipleItems",
     .section_item_count_type = {{5, syncer::DataType::PASSWORDS}}},

    // Hero type with multiple sections. Should show "and other items" in the
    // subtitle of the dialog.
    {.test_suffix = "MultipleSectionsPasswordsHeroTypeWithOneItem",
     .section_item_count_type = {{1, syncer::DataType::PASSWORDS},
                                 {3, syncer::DataType::CONTACT_INFO}}},
    {.test_suffix = "MultipleSectionsPasswordsHeroTypeWithMultipleItems",
     .section_item_count_type = {{5, syncer::DataType::PASSWORDS},
                                 {3, syncer::DataType::CONTACT_INFO}}},

    // Bookmarks hero type.
    {.test_suffix = "SingleSectionBookmarksHeroTypeWithOneItem",
     .section_item_count_type = {{1, syncer::DataType::BOOKMARKS}}},
    {.test_suffix = "SingleSectionBookmarksHeroTypeWithMultipleItems",
     .section_item_count_type = {{5, syncer::DataType::BOOKMARKS}}},
    {.test_suffix = "MultipleSectionsBookmarksHeroTypeWithOneItem",
     .section_item_count_type = {{1, syncer::DataType::BOOKMARKS},
                                 {3, syncer::DataType::CONTACT_INFO}}},
    {.test_suffix = "MultipleSectionsBookmarksHeroTypeWithMultipleItems",
     .section_item_count_type = {{5, syncer::DataType::BOOKMARKS},
                                 {3, syncer::DataType::CONTACT_INFO}}},

    // Addresses is not a hero type. It should not show in the subtitle.
    {.test_suffix = "SingleSectionNonHeroTypeWithOneItem",
     .section_item_count_type = {{1, syncer::DataType::CONTACT_INFO}}},
    {.test_suffix = "SingleSectionNonHeroTypeWithMultipleItems",
     .section_item_count_type = {{5, syncer::DataType::CONTACT_INFO}}},

    // Addresses is not a hero type. It should not show in the subtitle even if
    // other hero types exists.
    {.test_suffix = "MultipleSectionsWithNonHeroTypeAsPrimarySection",
     .section_item_count_type = {{5, syncer::DataType::CONTACT_INFO},
                                 {5, syncer::DataType::PASSWORDS}}},

    // Themes data type section has a specific UI.
    {.test_suffix = "ThemesSectionOnly",
     .section_item_count_type = {{1, syncer::DataType::THEMES}}},
    {.test_suffix = "ThemesSectionOnlyWithLongName",
     .section_item_count_type = {{1, syncer::DataType::THEMES}},
     .long_title = true},
    {.test_suffix = "MultipleSectionsWithThemes",
     .section_item_count_type = {{5, syncer::DataType::CONTACT_INFO},
                                 {5, syncer::DataType::PASSWORDS},
                                 {1, syncer::DataType::THEMES}}},
    {.test_suffix = "MultipleSectionsWithThemesLongName",
     .section_item_count_type = {{5, syncer::DataType::CONTACT_INFO},
                                 {5, syncer::DataType::PASSWORDS},
                                 {1, syncer::DataType::THEMES}},
     .long_title = true},
};

}  // namespace

class BatchUploadDialogViewPixelTest
    : public DialogBrowserTest,
      public testing::WithParamInterface<TestParam> {
 public:
  BatchUploadDialogViewPixelTest() {
    for (const auto& [count, type] : GetParam().section_item_count_type) {
      fake_descriptions_.emplace_back(
          GetFakeLocalData(type, count, GetParam().long_title));
    }

    // The Batch Upload view seems not to be resized properly on changes which
    // causes the view to go out of bounds. This should not happen and needs to
    // be investigated further. As a work around, to have a proper screenshot
    // tests, disable the check.
    // TODO(b/368043624): Make the view resize properly and remove this line as
    // it is not recommended to have per
    // `TestBrowserDialog::should_verify_dialog_bounds_` definition and default
    // value.
    set_should_verify_dialog_bounds(false);
  }

  // DialogBrowserTest:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    if (GetParam().use_dark_theme) {
      command_line->AppendSwitch(switches::kForceDarkMode);
    }
  }

  void SigninWithFullInfo() {
    signin::IdentityManager* identity_manager =
        IdentityManagerFactory::GetForProfile(browser()->profile());
    AccountInfo account_info = signin::MakePrimaryAccountAvailable(
        identity_manager, "test@gmail.com", signin::ConsentLevel::kSignin);
    ASSERT_FALSE(account_info.IsEmpty());

    account_info.full_name = "Joe Testing";
    account_info.given_name = "Joe";
    account_info.picture_url = "SOME_FAKE_URL";
    account_info.hosted_domain = kNoHostedDomainFound;
    account_info.locale = "en";
    ASSERT_TRUE(account_info.IsValid());
    signin::UpdateAccountInfoForAccount(identity_manager, account_info);

    signin::SimulateAccountImageFetch(identity_manager, account_info.account_id,
                                      kSignedInImageUrl, kSignedInImage);
  }

  void ShowUi(const std::string& name) override {
    SigninWithFullInfo();

    content::TestNavigationObserver observer{
        GURL(chrome::kChromeUIBatchUploadURL)};
    observer.StartWatchingNewWebContents();
    views::NamedWidgetShownWaiter widget_waiter(
        views::test::AnyWidgetTestPasskey{}, "BatchUploadDialogView");

    BatchUploadDialogView::CreateBatchUploadDialogView(
        *browser(), fake_descriptions_,
        BatchUploadService::EntryPoint::kPasswordManagerSettings,
        /*complete_callback*/ base::DoNothing());

    widget_waiter.WaitIfNeededAndGet();
    observer.Wait();
  }

 private:
  std::vector<syncer::LocalDataDescription> fake_descriptions_;
};

IN_PROC_BROWSER_TEST_P(BatchUploadDialogViewPixelTest, InvokeUi_default) {
  ShowAndVerifyUi();
}

INSTANTIATE_TEST_SUITE_P(,
                         BatchUploadDialogViewPixelTest,
                         testing::ValuesIn(kTestParams),
                         &ParamToTestSuffix);