File: app_list_item_view_pixeltest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (348 lines) | stat: -rw-r--r-- 12,847 bytes parent folder | download
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>
#include <tuple>
#include <vector>

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/app_list/model/app_list_item.h"
#include "ash/app_list/model/app_list_test_model.h"
#include "ash/app_list/model/search/search_model.h"
#include "ash/app_list/test/app_list_test_helper.h"
#include "ash/app_list/views/app_list_item_view.h"
#include "ash/app_list/views/apps_grid_view_test_api.h"
#include "ash/app_list/views/paged_apps_grid_view.h"
#include "ash/app_list/views/scrollable_apps_grid_view.h"
#include "ash/drag_drop/drag_drop_controller.h"
#include "ash/drag_drop/drag_drop_controller_test_api.h"
#include "ash/public/cpp/app_list/app_list_features.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/pixel/ash_pixel_differ.h"
#include "ash/test/pixel/ash_pixel_test_init_params.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/constants/chromeos_features.h"

namespace ash {

class AppListItemViewPixelTest
    : public AshTestBase,
      public testing::WithParamInterface<
          std::tuple</*use_drag_drop_refactor=*/bool,
                     /*use_folder_icon_refresh=*/bool,
                     /*use_tablet_mode=*/bool,
                     /*use_dense_ui=*/bool,
                     /*use_rtl=*/bool,
                     /*is_new_install=*/bool,
                     /*has_notification=*/bool,
                     /*jelly_enabled=*/bool>> {
 public:
  // AshTestBase:
  absl::optional<pixel_test::InitParams> CreatePixelTestInitParams()
      const override {
    pixel_test::InitParams init_params;
    init_params.under_rtl = use_rtl();
    return init_params;
  }

  // AshTestBase:
  void SetUp() override {
    scoped_feature_list_.InitWithFeatureStates(
        {{app_list_features::kDragAndDropRefactor, use_drag_drop_refactor()},
         {chromeos::features::kJelly, jelly_enabled()}});

    AshTestBase::SetUp();

    // As per `app_list_config_provider.cc`, dense values are used for screens
    // with width OR height <= 675.
    UpdateDisplay(use_dense_ui() ? "800x600" : "1200x800");
    if (use_drag_drop_refactor()) {
      auto* drag_controller = ShellTestApi().drag_drop_controller();
      drag_drop_controller_test_api_ =
          std::make_unique<DragDropControllerTestApi>(drag_controller);
      drag_controller->SetDisableNestedLoopForTesting(true);
    }
  }

  void TearDown() override {
    drag_drop_controller_test_api_.reset();
    AshTestBase::TearDown();
  }

  // Creates multiple folders that contain from 1 app to `max_items` apps
  // respectively.
  void CreateFoldersContainingDifferentNumOfItems(int max_items) {
    AppListFolderItem* folder_item;
    for (int i = 1; i <= max_items; ++i) {
      if (i == 1) {
        folder_item = GetAppListTestHelper()->model()->CreateSingleItemFolder(
            "folder_id", "item_id");
      } else {
        folder_item =
            GetAppListTestHelper()->model()->CreateAndPopulateFolderWithApps(i);
      }
      // Update the notification state of the first app in the folder to
      // simulate that there exists an app with notifications in the folder.
      folder_item->item_list()->item_at(0)->UpdateNotificationBadge(
          has_notification());
    }
  }

  void CreateAppListItem(const std::string& name) {
    AppListItem* item =
        GetAppListTestHelper()->model()->CreateAndAddItem(name + "_id");
    item->SetName(name);
    item->SetIsNewInstall(is_new_install());
    item->UpdateNotificationBadge(has_notification());
  }

  void ShowAppList() {
    if (use_tablet_mode()) {
      Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
    } else {
      GetAppListTestHelper()->ShowAppList();
    }
  }

  AppsGridView* GetAppsGridView() {
    if (use_tablet_mode()) {
      return GetAppListTestHelper()->GetRootPagedAppsGridView();
    }

    return GetAppListTestHelper()->GetScrollableAppsGridView();
  }

  AppListItemView* GetItemViewAt(size_t index) {
    return GetAppsGridView()->GetItemViewAt(index);
  }

  std::string GenerateScreenshotName() {
    std::vector<std::string> parameters = {
        use_tablet_mode() ? "tablet_mode" : "clamshell_mode",
        use_dense_ui() ? "dense_ui" : "regular_ui", use_rtl() ? "rtl" : "ltr",
        is_new_install() ? "new_install=true" : "new_install=false",
        has_notification() ? "has_notification=true"
                           : "has_notification=false"};
    if (jelly_enabled()) {
      parameters.push_back("jelly_enabled");
    }
    std::string stringified_params = base::JoinString(parameters, "|");
    return base::JoinString({"app_list_item_view", stringified_params}, ".");
  }

  views::Widget* GetDraggedWidget() {
    return use_drag_drop_refactor()
               ? drag_drop_controller_test_api_->drag_image_widget()
               : GetAppsGridView()
                     ->app_drag_icon_proxy_for_test()
                     ->GetWidgetForTesting();
  }

  size_t GetRevisionNumber() {
    if (jelly_enabled()) {
      // Revision numbers reset with Jelly.
      return 5;
    }

    size_t base_revision_number = 8;

    if (use_drag_drop_refactor()) {
      ++base_revision_number;
    }

    return base_revision_number;
  }

  bool use_drag_drop_refactor() const { return std::get<0>(GetParam()); }
  bool use_folder_icon_refresh() const { return std::get<1>(GetParam()); }
  bool use_tablet_mode() const { return std::get<2>(GetParam()); }
  bool use_dense_ui() const { return std::get<3>(GetParam()); }
  bool use_rtl() const { return std::get<4>(GetParam()); }
  bool is_new_install() const { return std::get<5>(GetParam()); }
  bool has_notification() const { return std::get<6>(GetParam()); }
  bool jelly_enabled() const { return std::get<7>(GetParam()); }

 private:
  std::unique_ptr<DragDropControllerTestApi> drag_drop_controller_test_api_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(
    All,
    AppListItemViewPixelTest,
    testing::Combine(/*use_drag_drop_refactor=*/testing::Bool(),
                     /*use_folder_icon_refresh=*/testing::Bool(),
                     /*use_tablet_mode=*/testing::Bool(),
                     /*use_dense_ui=*/testing::Bool(),
                     /*use_rtl=*/testing::Bool(),
                     /*is_new_install=*/testing::Bool(),
                     /*has_notification=*/testing::Bool(),
                     /*jelly_enabled=*/testing::Bool()));

TEST_P(AppListItemViewPixelTest, AppListItemView) {
  CreateAppListItem("App");
  CreateAppListItem("App with a loooooooong name");

  ShowAppList();
  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      GenerateScreenshotName(), /*revision_number=*/3, GetItemViewAt(0),
      GetItemViewAt(1)));
}

// Verifies the layout of the item icons inside a folder.
TEST_P(AppListItemViewPixelTest, AppListFolderItemsLayoutInIcon) {
  // Skip the case where the apps are newly installed as it doesn't change the
  // folder icons.
  if (!use_folder_icon_refresh() || is_new_install()) {
    return;
  }

  // Reset any configs set by previous tests so that
  // ItemIconInFolderIconMargin() in app_list_config.cc is correctly
  // initialized. Can be removed if folder icon refresh is set as default.
  AppListConfigProvider::Get().ResetForTesting();

  // To test the item counter on folder icons, set the maximum number of the
  // items in a folder to 5.
  const int max_items_in_folder = 5;
  CreateFoldersContainingDifferentNumOfItems(max_items_in_folder);
  ShowAppList();

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      GenerateScreenshotName(), /*revision_number=*/8, GetItemViewAt(0),
      GetItemViewAt(1), GetItemViewAt(2), GetItemViewAt(3), GetItemViewAt(4)));
}

// Verifies the folder icon is extended when an app is dragged upon it.
TEST_P(AppListItemViewPixelTest, AppListFolderIconExtendedState) {
  // Skip the case where the apps are newly installed as it doesn't change the
  // folder icons.
  if (!use_folder_icon_refresh() || is_new_install()) {
    return;
  }

  // Reset any configs set by previous tests so that
  // ItemIconInFolderIconMargin() in app_list_config.cc is correctly
  // initialized. Can be removed if folder icon refresh is set as default.
  AppListConfigProvider::Get().ResetForTesting();

  // To test the item counter on folder icons, set the maximum number of the
  // items in a folder to 5.
  const int max_items_in_folder = 5;
  CreateFoldersContainingDifferentNumOfItems(max_items_in_folder);
  CreateAppListItem("App");
  ShowAppList();

  // For tablet mode, simulate that a drag starts and enter the cardified state.
  if (use_tablet_mode()) {
    GetAppListTestHelper()
        ->GetRootPagedAppsGridView()
        ->MaybeStartCardifiedView();
  }

  // Simulate that there is an app dragged onto it for each folder.
  for (int i = 0; i < max_items_in_folder; ++i) {
    GetItemViewAt(i)->OnDraggedViewEnter();
  }

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      GenerateScreenshotName(), /*revision_number=*/8, GetItemViewAt(0),
      GetItemViewAt(1), GetItemViewAt(2), GetItemViewAt(3), GetItemViewAt(4)));

  // Reset the states.
  for (int i = 0; i < max_items_in_folder; ++i) {
    GetItemViewAt(i)->OnDraggedViewExit();
  }
  if (use_tablet_mode()) {
    GetAppListTestHelper()->GetRootPagedAppsGridView()->MaybeEndCardifiedView();
  }
}

// Vefifies the dragged folder icon proxy is correctly created.
TEST_P(AppListItemViewPixelTest, DraggedAppListFolderIcon) {
  // Skip the case where the apps are newly installed or have notifications as
  // they don't change the folder icons.
  if (!use_folder_icon_refresh() || is_new_install() || has_notification()) {
    return;
  }

  // Reset any configs set by previous tests so that
  // ItemIconInFolderIconMargin() in app_list_config.cc is correctly
  // initialized. Can be removed if folder icon refresh is set as default.
  AppListConfigProvider::Get().ResetForTesting();

  // Set the maximum number of the items in a folder that we want to test to 4.
  const int max_items_in_folder = 4;
  CreateFoldersContainingDifferentNumOfItems(max_items_in_folder);
  ShowAppList();

  auto* event_generator = GetEventGenerator();
  AppsGridView* apps_grid_view = GetAppsGridView();
  gfx::Point grid_center = apps_grid_view->GetBoundsInScreen().CenterPoint();

  // Create a folder item view list for folders with different number of items.
  // This is used instead of GetItemViewAt() to prevent reordering while
  // dragging each folder.
  std::vector<AppListItemView*> folder_list;
  for (int i = 0; i < max_items_in_folder; ++i) {
    folder_list.push_back(GetItemViewAt(i));
  }

  const size_t revision_number = GetRevisionNumber();

  auto verify_folder_widget =
      [&](int number_of_items) {
        std::string filename =
            base::NumberToString(number_of_items) + "_items_folder";
        EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
            base::JoinString({GenerateScreenshotName(), filename}, "."),
            revision_number, GetDraggedWidget()));
      };

  for (size_t i = 0; i < max_items_in_folder; ++i) {
    gfx::Point folder_icon_center =
        folder_list[i]->GetIconBoundsInScreen().CenterPoint();

    std::list<base::OnceClosure> tasks;
    tasks.push_back(base::BindLambdaForTesting([&]() {
      if (use_tablet_mode()) {
        event_generator->PressTouch(folder_icon_center);
        folder_list[i]->FireTouchDragTimerForTest();
      } else {
        event_generator->MoveMouseTo(folder_icon_center);
        event_generator->PressLeftButton();
        folder_list[i]->FireMouseDragTimerForTest();
      }
    }));
    tasks.push_back(base::BindLambdaForTesting([&]() {
      if (use_tablet_mode()) {
        event_generator->MoveTouch(grid_center);
      } else {
        event_generator->MoveMouseTo(grid_center);
      }
      test::AppsGridViewTestApi(apps_grid_view).WaitForItemMoveAnimationDone();
    }));
    tasks.push_back(base::BindLambdaForTesting(
        [&]() { verify_folder_widget(/*number_of_items=*/i + 1); }));
    tasks.push_back(base::BindLambdaForTesting([&]() {
      if (use_tablet_mode()) {
        event_generator->ReleaseTouch();
      } else {
        event_generator->ReleaseLeftButton();
      }
    }));

    MaybeRunDragAndDropSequenceForAppList(&tasks,
                                          /*is_touch=*/use_tablet_mode());
  }
}

}  // namespace ash