File: feature_tiles_container_view_unittest.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 (499 lines) | stat: -rw-r--r-- 18,051 bytes parent folder | download | duplicates (6)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// 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 "ash/system/unified/feature_tiles_container_view.h"

#include "ash/constants/quick_settings_catalogs.h"
#include "ash/public/cpp/pagination/pagination_model.h"
#include "ash/shell.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/unified/feature_pod_button.h"
#include "ash/system/unified/feature_pod_controller_base.h"
#include "ash/system/unified/feature_tile.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_bubble.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/test/views_test_utils.h"

namespace ash {

namespace {

class MockFeaturePodController : public FeaturePodControllerBase {
 public:
  MockFeaturePodController() = default;
  MockFeaturePodController(const MockFeaturePodController&) = delete;
  MockFeaturePodController& operator=(const MockFeaturePodController&) = delete;
  ~MockFeaturePodController() override = default;

  std::unique_ptr<FeatureTile> CreateTile(bool compact = false) override {
    auto tile = std::make_unique<FeatureTile>(
        base::BindRepeating(&FeaturePodControllerBase::OnIconPressed,
                            weak_ptr_factory_.GetWeakPtr()),
        /*togglable=*/true,
        compact ? FeatureTile::TileType::kCompact
                : FeatureTile::TileType::kPrimary);
    tile->SetVectorIcon(vector_icons::kDogfoodIcon);
    return tile;
  }

  QsFeatureCatalogName GetCatalogName() override {
    return QsFeatureCatalogName::kUnknown;
  }

  void OnIconPressed() override {}
  void OnLabelPressed() override {}

 private:
  base::WeakPtrFactory<MockFeaturePodController> weak_ptr_factory_{this};
};

constexpr int kMaxPrimaryTilesPerRow = 2;

}  // namespace

class FeatureTilesContainerViewTest : public AshTestBase,
                                      public views::ViewObserver {
 public:
  FeatureTilesContainerViewTest() = default;
  FeatureTilesContainerViewTest(const FeatureTilesContainerViewTest&) = delete;
  FeatureTilesContainerViewTest& operator=(
      const FeatureTilesContainerViewTest&) = delete;
  ~FeatureTilesContainerViewTest() override = default;

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();

    tray_model_ =
        base::MakeRefCounted<UnifiedSystemTrayModel>(/*shelf=*/nullptr);
    tray_controller_ =
        std::make_unique<UnifiedSystemTrayController>(tray_model_.get());
    widget_ = CreateFramelessTestWidget();
    widget_->SetFullscreen(true);

    container_ = widget_->SetContentsView(
        std::make_unique<FeatureTilesContainerView>(tray_controller_.get()));
    container_->AddObserver(this);
  }

  void TearDown() override {
    container_->RemoveObserver(this);
    widget_.reset();
    tray_controller_.reset();
    tray_model_.reset();

    AshTestBase::TearDown();
  }

  void PressTab() {
    ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
    generator.PressKey(ui::KeyboardCode::VKEY_TAB, ui::EF_NONE);
  }

  void PressShiftTab() {
    ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
    generator.PressKey(ui::KeyboardCode::VKEY_TAB, ui::EF_SHIFT_DOWN);
  }

  FeatureTilesContainerView* container() { return container_; }

  PaginationModel* pagination_model() { return container()->pagination_model_; }

  void AddTiles(std::vector<std::unique_ptr<FeatureTile>> tiles) {
    container()->AddTiles(std::move(tiles));
  }

  void SetRowsFromHeight(int max_height) {
    return container()->SetRowsFromHeight(max_height);
  }

  int CalculateRowsFromHeight(int height) {
    return container()->CalculateRowsFromHeight(height);
  }

  void AdjustRowsForMediaViewVisibility(int height) {
    container()->AdjustRowsForMediaViewVisibility(true, height);
  }

  int GetRowCount() { return container()->row_count(); }

  int GetPageCount() { return container()->page_count(); }

  int GetVisibleCount() { return container()->GetVisibleFeatureTileCount(); }

  std::vector<raw_ptr<views::View, VectorExperimental>> pages() {
    return container()->children();
  }

  // Fills the container with a number of `pages` given the max amount of
  // displayable primary tiles per page.
  void FillContainerWithPrimaryTiles(int pages) {
    auto mock_controller = std::make_unique<MockFeaturePodController>();
    std::vector<std::unique_ptr<FeatureTile>> tiles;

    size_t number_of_tiles =
        pages * container()->displayable_rows() * kMaxPrimaryTilesPerRow;

    while (tiles.size() < number_of_tiles) {
      tiles.push_back(mock_controller->CreateTile());
    }
    AddTiles(std::move(tiles));

    EXPECT_EQ(pages, GetPageCount());
    EXPECT_EQ(pages, pagination_model()->total_pages());
  }

 private:
  std::unique_ptr<views::Widget> widget_;
  std::unique_ptr<UnifiedSystemTrayController> tray_controller_;
  scoped_refptr<UnifiedSystemTrayModel> tray_model_;
  raw_ptr<FeatureTilesContainerView, DanglingUntriaged> container_;
};

// Tests `CalculateRowsFromHeight()` which returns the number of max displayable
// feature tile rows given the available height.
TEST_F(FeatureTilesContainerViewTest, DisplayableRows) {
  int row_height = kFeatureTileHeight;

  // Expect max number of rows even if available height could fit another row.
  EXPECT_EQ(kFeatureTileMaxRows,
            CalculateRowsFromHeight((kFeatureTileMaxRows + 1) * row_height));

  // Expect appropriate number of rows with available height.
  EXPECT_EQ(3, CalculateRowsFromHeight(3 * row_height));

  // Expect min number of rows even with zero height.
  EXPECT_EQ(kFeatureTileMinRows, CalculateRowsFromHeight(0));
}

TEST_F(FeatureTilesContainerViewTest,
       DisplayableRowsIsLessWhenMediaViewIsShowing) {
  int row_height = kFeatureTileHeight;
  // Set height to equivalent of max+1 rows.
  const int max_height = (kFeatureTileMaxRows + 1) * row_height;

  // Expect default to cap at `kFeatureTileMaxRows`.
  EXPECT_EQ(kFeatureTileMaxRows, CalculateRowsFromHeight(max_height));

  AdjustRowsForMediaViewVisibility(max_height);

  // Expect height to be capped at `kFeatureTileMaxRowsWhenMediaViewIsShowing`
  // when media view is showing.
  EXPECT_EQ(kFeatureTileMaxRowsWhenMediaViewIsShowing,
            CalculateRowsFromHeight(max_height));
}

// Tests that rows are dynamically added by adding `FeatureTile` elements to the
// container.
TEST_F(FeatureTilesContainerViewTest, FeatureTileRows) {
  auto mock_controller = std::make_unique<MockFeaturePodController>();

  // Expect one row by adding two primary tiles.
  std::vector<std::unique_ptr<FeatureTile>> two_primary_tiles;
  two_primary_tiles.push_back(mock_controller->CreateTile());
  two_primary_tiles.push_back(mock_controller->CreateTile());
  container()->AddTiles(std::move(two_primary_tiles));
  EXPECT_EQ(1, GetRowCount());
  EXPECT_EQ(2, GetVisibleCount());

  // Add one primary, and two compact tiles. This should create a second row.
  std::vector<std::unique_ptr<FeatureTile>> one_primary_two_compact_tiles;
  one_primary_two_compact_tiles.push_back(mock_controller->CreateTile());
  one_primary_two_compact_tiles.push_back(
      mock_controller->CreateTile(/*compact=*/true));
  one_primary_two_compact_tiles.push_back(
      mock_controller->CreateTile(/*compact=*/true));
  container()->AddTiles(std::move(one_primary_two_compact_tiles));
  EXPECT_EQ(2, GetRowCount());
  EXPECT_EQ(5, GetVisibleCount());

  // Add one primary tile, this should result in a third row.
  std::vector<std::unique_ptr<FeatureTile>> one_primary_tile;
  one_primary_tile.push_back(mock_controller->CreateTile());
  container()->AddTiles(std::move(one_primary_tile));
  EXPECT_EQ(3, GetRowCount());
  EXPECT_EQ(6, GetVisibleCount());
}

TEST_F(FeatureTilesContainerViewTest, ChangeTileVisibility) {
  // Create 3 full-size tiles. Normally they would require 2 rows.
  auto mock_controller = std::make_unique<MockFeaturePodController>();
  std::unique_ptr<FeatureTile> tile1 = mock_controller->CreateTile();
  std::unique_ptr<FeatureTile> tile2 = mock_controller->CreateTile();
  std::unique_ptr<FeatureTile> tile3 = mock_controller->CreateTile();

  // Make the first tile invisible.
  FeatureTile* tile1_ptr = tile1.get();
  tile1_ptr->SetVisible(false);

  // Add the tiles to the container.
  std::vector<std::unique_ptr<FeatureTile>> tiles;
  tiles.push_back(std::move(tile1));
  tiles.push_back(std::move(tile2));
  tiles.push_back(std::move(tile3));
  AddTiles(std::move(tiles));

  // Only one row is created because the first tile is not visible.
  EXPECT_EQ(1, GetRowCount());
  EXPECT_EQ(2, GetVisibleCount());

  // Making the tile visible causes a second row to be created.
  tile1_ptr->SetVisible(true);
  EXPECT_EQ(2, GetRowCount());
  EXPECT_EQ(3, GetVisibleCount());

  // Making the tile invisible causes the second row to be removed.
  tile1_ptr->SetVisible(false);
  EXPECT_EQ(1, GetRowCount());
  EXPECT_EQ(2, GetVisibleCount());
}

TEST_F(FeatureTilesContainerViewTest, PageCountUpdated) {
  auto mock_controller = std::make_unique<MockFeaturePodController>();

  // Set the container height to have two displayable rows per page.
  SetRowsFromHeight(kFeatureTileHeight * 2);

  std::vector<std::unique_ptr<FeatureTile>> tiles;

  // Get pointer of one tile so we can make invisible later.
  std::unique_ptr<FeatureTile> tile1 = mock_controller->CreateTile();
  FeatureTile* tile1_ptr = tile1.get();
  tiles.push_back(std::move(tile1));

  // Add a total of five tiles to the container.
  while (tiles.size() < 5) {
    tiles.push_back(mock_controller->CreateTile());
  }

  // Since a row fits two primary tiles, expect two pages for five primary
  // tiles.
  AddTiles(std::move(tiles));
  EXPECT_EQ(2, GetPageCount());
  EXPECT_EQ(5, GetVisibleCount());

  // Expect change in page count after updating visibility of a tile.
  tile1_ptr->SetVisible(false);
  EXPECT_EQ(1, GetPageCount());
  EXPECT_EQ(4, GetVisibleCount());

  // Expect change in page count after updating max displayable rows by updating
  // the available height.
  SetRowsFromHeight(kFeatureTileHeight);
  EXPECT_EQ(2, GetPageCount());
  EXPECT_EQ(4, GetVisibleCount());
}

// TODO(b/263185068): Use EventGenerator.
TEST_F(FeatureTilesContainerViewTest, PaginationGesture) {
  constexpr int kNumberOfPages = 4;
  FillContainerWithPrimaryTiles(kNumberOfPages);

  gfx::Point container_origin = container()->GetBoundsInScreen().origin();
  ui::GestureEvent swipe_left_begin(
      container_origin.x(), container_origin.y(), 0, base::TimeTicks(),
      ui::GestureEventDetails(ui::EventType::kGestureScrollBegin, -1, 0));
  ui::GestureEvent swipe_left_update(
      container_origin.x(), container_origin.y(), 0, base::TimeTicks(),
      ui::GestureEventDetails(ui::EventType::kGestureScrollUpdate, -1000, 0));
  ui::GestureEvent swipe_right_begin(
      container_origin.x(), container_origin.y(), 0, base::TimeTicks(),
      ui::GestureEventDetails(ui::EventType::kGestureScrollBegin, 1, 0));
  ui::GestureEvent swipe_right_update(
      container_origin.x(), container_origin.y(), 0, base::TimeTicks(),
      ui::GestureEventDetails(ui::EventType::kGestureScrollUpdate, 1000, 0));
  ui::GestureEvent swipe_end(
      container_origin.x(), container_origin.y(), 0, base::TimeTicks(),
      ui::GestureEventDetails(ui::EventType::kGestureEnd));

  int previous_page = pagination_model()->selected_page();

  // Swipe left takes to next page
  for (int i = 0; i < kNumberOfPages - 1; i++) {
    // Simulate swipe left
    container()->OnGestureEvent(&swipe_left_begin);
    container()->OnGestureEvent(&swipe_left_update);
    container()->OnGestureEvent(&swipe_end);

    int current_page = pagination_model()->selected_page();
    // Expect next page
    EXPECT_EQ(previous_page + 1, current_page);
    previous_page = current_page;
  }

  // Swipe left on last page does nothing
  container()->OnGestureEvent(&swipe_left_begin);
  container()->OnGestureEvent(&swipe_left_update);
  container()->OnGestureEvent(&swipe_end);

  EXPECT_EQ(previous_page, pagination_model()->selected_page());

  // Swipe right takes to previous page
  for (int i = 0; i < kNumberOfPages - 1; i++) {
    // Simulate swipe right
    container()->OnGestureEvent(&swipe_right_begin);
    container()->OnGestureEvent(&swipe_right_update);
    container()->OnGestureEvent(&swipe_end);

    int current_page = pagination_model()->selected_page();
    // Expect previous page
    EXPECT_EQ(previous_page - 1, current_page);
    previous_page = current_page;
  }

  // Swipe right on first page does nothing
  container()->OnGestureEvent(&swipe_right_begin);
  container()->OnGestureEvent(&swipe_right_update);
  container()->OnGestureEvent(&swipe_end);

  EXPECT_EQ(previous_page, pagination_model()->selected_page());
}

// TODO(b/263185068): Use EventGenerator.
TEST_F(FeatureTilesContainerViewTest, PaginationScroll) {
  constexpr int kNumberOfFingers = 2;
  constexpr int kNumberOfPages = 4;
  FillContainerWithPrimaryTiles(kNumberOfPages);

  gfx::Point container_origin = container()->GetBoundsInScreen().origin();

  ui::ScrollEvent fling_up_start(ui::EventType::kScrollFlingStart,
                                 container_origin, base::TimeTicks(), 0, 0, 100,
                                 0, 10, kNumberOfFingers);

  ui::ScrollEvent fling_down_start(ui::EventType::kScrollFlingStart,
                                   container_origin, base::TimeTicks(), 0, 0,
                                   -100, 0, 10, kNumberOfFingers);

  ui::ScrollEvent fling_cancel(ui::EventType::kScrollFlingCancel,
                               container_origin, base::TimeTicks(), 0, 0, 0, 0,
                               0, kNumberOfFingers);

  int previous_page = pagination_model()->selected_page();

  // Scroll down takes to next page
  for (int i = 0; i < kNumberOfPages - 1; i++) {
    // Simulate Scroll left
    container()->OnScrollEvent(&fling_down_start);
    container()->OnScrollEvent(&fling_cancel);
    pagination_model()->FinishAnimation();

    int current_page = pagination_model()->selected_page();
    // Expect next page
    EXPECT_EQ(previous_page + 1, current_page);
    previous_page = current_page;
  }

  // Scroll up takes to previous page
  for (int i = 0; i < kNumberOfPages - 1; i++) {
    // Simulate Scroll up
    container()->OnScrollEvent(&fling_up_start);
    container()->OnScrollEvent(&fling_cancel);
    pagination_model()->FinishAnimation();

    int current_page = pagination_model()->selected_page();
    // Expect previous page
    EXPECT_EQ(previous_page - 1, current_page);
    previous_page = current_page;
  }
}

// TODO(b/263185068): Use EventGenerator.
TEST_F(FeatureTilesContainerViewTest, PaginationMouseWheel) {
  constexpr int kNumberOfPages = 4;
  FillContainerWithPrimaryTiles(kNumberOfPages);

  gfx::Point container_origin = container()->GetBoundsInScreen().origin();
  ui::MouseWheelEvent wheel_up(gfx::Vector2d(0, 1000), container_origin,
                               container_origin, base::TimeTicks(), 0, 0);

  ui::MouseWheelEvent wheel_down(gfx::Vector2d(0, -1000), container_origin,
                                 container_origin, base::TimeTicks(), 0, 0);

  int previous_page = pagination_model()->selected_page();

  // Mouse wheel down takes to next page
  for (int i = 0; i < kNumberOfPages - 1; i++) {
    // Simulate mouse wheel down
    container()->OnMouseWheel(wheel_down);
    pagination_model()->FinishAnimation();

    int current_page = pagination_model()->selected_page();
    // Expect next page
    EXPECT_EQ(previous_page + 1, current_page);
    previous_page = current_page;
  }

  // Mouse wheel up takes to previous page
  for (int i = 0; i < kNumberOfPages - 1; i++) {
    // Simulate mouse wheel up
    container()->OnMouseWheel(wheel_up);
    pagination_model()->FinishAnimation();

    int current_page = pagination_model()->selected_page();
    // Expect previous page
    EXPECT_EQ(previous_page - 1, current_page);
    previous_page = current_page;
  }
}

TEST_F(FeatureTilesContainerViewTest, SwitchPageWithFocus) {
  FillContainerWithPrimaryTiles(/*pages=*/2);

  // View starts at page with index zero.
  EXPECT_EQ(0, pagination_model()->selected_page());

  // Tab until the `selected_page` index changes to 1.
  while (pagination_model()->selected_page() == 0) {
    PressTab();
  }
  EXPECT_EQ(1, pagination_model()->selected_page());

  // Pressing shift tab returns to the previous page.
  PressShiftTab();
  EXPECT_EQ(0, pagination_model()->selected_page());
}

TEST_F(FeatureTilesContainerViewTest, PaginationTransition) {
  FillContainerWithPrimaryTiles(/*pages=*/3);
  views::test::RunScheduledLayout(container());

  gfx::Rect initial_bounds = pages()[0]->bounds();
  gfx::Rect current_bounds;
  gfx::Rect previous_bounds = initial_bounds;

  // Page bounds should slide to the left during a transition to the next page.
  PaginationModel::Transition transition(
      pagination_model()->selected_page() + 1, 0);

  for (double i = 0.1; i <= 1.0; i += 0.1) {
    transition.progress = i;
    pagination_model()->SetTransition(transition);

    current_bounds = pages()[0]->bounds();

    EXPECT_LT(current_bounds.x(), previous_bounds.x());
    EXPECT_EQ(current_bounds.y(), previous_bounds.y());

    previous_bounds = current_bounds;
  }

  // Page position after the transition ends should be a page offset to the
  // left.
  int page_offset = kWideTrayMenuWidth;
  gfx::Rect final_bounds =
      gfx::Rect(initial_bounds.x() - page_offset, initial_bounds.y(),
                initial_bounds.width(), initial_bounds.height());
  pagination_model()->SelectPage(1, false);
  views::test::RunScheduledLayout(container());
  EXPECT_EQ(final_bounds, pages()[0]->bounds());
}

}  // namespace ash