File: apps_collections_controller_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 (410 lines) | stat: -rw-r--r-- 15,620 bytes parent folder | download | duplicates (3)
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
// 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 "ash/app_list/apps_collections_controller.h"

#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <utility>

#include "ash/app_list/test/app_list_test_helper.h"
#include "ash/app_list/views/app_list_bubble_apps_collections_page.h"
#include "ash/app_list/views/app_list_bubble_apps_page.h"
#include "ash/app_list/views/apps_collections_dismiss_dialog.h"
#include "ash/app_list/views/apps_grid_context_menu.h"
#include "ash/app_list/views/paged_apps_grid_view.h"
#include "ash/app_list/views/search_result_page_anchored_dialog.h"
#include "ash/app_menu/app_menu_model_adapter.h"
#include "ash/public/cpp/app_list/app_list_features.h"
#include "ash/public/cpp/ash_prefs.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/test/widget_test.h"

namespace ash {
namespace {

class AppsCollectionsControllerTest : public NoSessionAshTestBase {
 public:
  AppsCollectionsControllerTest() {
    scoped_feature_list_.InitWithFeatures({app_list_features::kAppsCollections},
                                          {});
  }

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

    GetTestAppListClient()->set_is_new_user(true);
    SimulateNewUserFirstLogin("primary@test");
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(AppsCollectionsControllerTest,
       ShowAppsPageOnFirstShowAfterDismissingNudge) {
  auto* helper = GetAppListTestHelper();
  helper->ShowAppList();
  base::HistogramTester histograms;

  histograms.ExpectBucketCount(
      "Apps.AppList.AppsCollections.DismissedReason",
      AppsCollectionsController::DismissReason::kExitNudge, 0);

  auto* apps_collections_page = helper->GetBubbleAppsCollectionsPage();
  AppListToastContainerView* toast_container =
      apps_collections_page->GetToastContainerViewForTest();
  EXPECT_TRUE(toast_container->IsToastVisible());

  // Click on close button to dismiss the toast.
  LeftClickOn(toast_container->GetToastButton());
  EXPECT_FALSE(toast_container->IsToastVisible());

  // Apps page is not visible.
  EXPECT_FALSE(apps_collections_page->GetVisible());

  histograms.ExpectBucketCount(
      "Apps.AppList.AppsCollections.DismissedReason",
      AppsCollectionsController::DismissReason::kExitNudge, 1);

  helper->Dismiss();
  helper->ShowAppList();

  // Apps page is not visible.
  EXPECT_FALSE(apps_collections_page->GetVisible());
  EXPECT_TRUE(helper->GetBubbleAppsPage()->GetVisible());

  histograms.ExpectBucketCount(
      "Apps.AppList.AppsCollections.DismissedReason",
      AppsCollectionsController::DismissReason::kExitNudge, 1);
}

TEST_F(AppsCollectionsControllerTest, ShowAppsPageAfterSortingGrid) {
  auto* helper = GetAppListTestHelper();
  helper->ShowAppList();
  base::HistogramTester histograms;

  histograms.ExpectBucketCount(
      "Apps.AppList.AppsCollections.DismissedReason",
      AppsCollectionsController::DismissReason::kSorting, 0);

  auto* apps_collections_page = helper->GetBubbleAppsCollectionsPage();
  AppsGridContextMenu* context_menu =
      apps_collections_page->context_menu_for_test();
  EXPECT_FALSE(context_menu->IsMenuShowing());

  // Get a point in `apps_collections_page` that doesn't have an item on it.
  const gfx::Point empty_space =
      apps_collections_page->GetBoundsInScreen().CenterPoint();

  // Open the menu to test the alphabetical sort option.
  GetEventGenerator()->MoveMouseTo(empty_space);
  GetEventGenerator()->ClickRightButton();
  EXPECT_TRUE(context_menu->IsMenuShowing());

  // Click on any reorder option and accept the dialog.
  views::MenuItemView* reorder_option =
      context_menu->root_menu_item_view()->GetSubmenu()->GetMenuItemAt(1);
  LeftClickOn(reorder_option);
  ASSERT_TRUE(helper->GetBubbleSearchPageDialog());

  views::Widget* widget = helper->GetBubbleSearchPageDialog()->widget();
  views::WidgetDelegate* widget_delegate = widget->widget_delegate();
  views::test::WidgetDestroyedWaiter widget_waiter(widget);
  LeftClickOn(static_cast<AppsCollectionsDismissDialog*>(widget_delegate)
                  ->accept_button_for_test());
  widget_waiter.Wait();

  // Apps collections page is not visible.
  EXPECT_FALSE(apps_collections_page->GetVisible());
  EXPECT_EQ(AppListSortOrder::kNameAlphabetical,
            helper->model()->requested_sort_order());

  // Apps page is not visible.
  EXPECT_FALSE(apps_collections_page->GetVisible());
  EXPECT_TRUE(helper->GetBubbleAppsPage()->GetVisible());

  histograms.ExpectBucketCount(
      "Apps.AppList.AppsCollections.DismissedReason",
      AppsCollectionsController::DismissReason::kSorting, 1);
}

// Tests that sorting on tablet mode updates apps collections.
TEST_F(AppsCollectionsControllerTest,
       AppListSortOnTabletModeUpdatesAppsCollections) {
  auto* helper = GetAppListTestHelper();
  helper->AddAppListItemsWithCollection(AppCollection::kEntertainment, 2);
  helper->ShowAppList();
  EXPECT_EQ(AppListSortOrder::kCustom, helper->model()->requested_sort_order());

  // Apps collections page is visible.
  EXPECT_TRUE(helper->GetBubbleAppsCollectionsPage()->GetVisible());
  EXPECT_FALSE(helper->GetBubbleAppsPage()->GetVisible());

  // Enter tablet mode.
  TabletModeControllerTestApi().EnterTabletMode();

  auto* apps_grid_view = helper->GetRootPagedAppsGridView();
  // Get a point in `apps_grid` that doesn't have an item on it.
  const gfx::Point empty_space =
      apps_grid_view->GetBoundsInScreen().CenterPoint();

  // Open the menu to test the alphabetical sort option.
  ui::test::EventGenerator* generator = GetEventGenerator();
  ui::GestureEvent long_press(
      empty_space.x(), empty_space.y(), 0, base::TimeTicks(),
      ui::GestureEventDetails(ui::EventType::kGestureLongPress));
  generator->Dispatch(&long_press);
  GetAppListTestHelper()->WaitUntilIdle();

  AppMenuModelAdapter* context_menu =
      Shell::GetPrimaryRootWindowController()->menu_model_adapter_for_testing();
  ASSERT_TRUE(context_menu);
  EXPECT_TRUE(context_menu->IsShowingMenu());

  // Cache the current context menu view.
  views::MenuItemView* reorder_submenu =
      context_menu->root_for_testing()->GetSubmenu()->GetMenuItemAt(2);
  ASSERT_EQ(reorder_submenu->title(), u"Sort by");
  GetEventGenerator()->GestureTapAt(
      reorder_submenu->GetBoundsInScreen().CenterPoint());

  // Click on any reorder option and accept the dialog.
  views::MenuItemView* reorder_option =
      reorder_submenu->GetSubmenu()->GetMenuItemAt(0);
  ASSERT_EQ(reorder_option->title(), u"Name");
  GetEventGenerator()->GestureTapAt(
      reorder_option->GetBoundsInScreen().CenterPoint());
  helper->WaitUntilIdle();
  EXPECT_EQ(AppListSortOrder::kNameAlphabetical,
            helper->model()->requested_sort_order());

  helper->model()->RequestCommitTemporarySortOrder();

  // Leave tablet mode.
  TabletModeControllerTestApi().LeaveTabletMode();

  helper->ShowAppList();

  // Apps collections page is visible.
  EXPECT_FALSE(helper->GetBubbleAppsCollectionsPage()->GetVisible());
  EXPECT_TRUE(helper->GetBubbleAppsPage()->GetVisible());
}

// Verifies that the apps collections is not shown after the user logs back in
// again.
TEST_F(AppsCollectionsControllerTest, AppsCollectionsDismissedAfterRestart) {
  auto* helper = GetAppListTestHelper();
  helper->ShowAppList();

  EXPECT_TRUE(helper->GetBubbleAppsCollectionsPage()->GetVisible());

  // Logout and simulate that the user logs back in again.
  helper->Dismiss();
  ClearLogin();
  SimulateUserLogin({"primary@test"});

  // The bubble should not be shown.
  helper->ShowAppList();
  EXPECT_FALSE(helper->GetBubbleAppsCollectionsPage()->GetVisible());
}

// Class for tests of the `AppsCollectionsController` which are
// concerned with user eligibility, parameterized by:
// (a) whether the user should be considered "new" locally
// (b) whether the user is managed
// (c) the user type.
class AppsCollectionsControllerUserElegibilityTest
    : public NoSessionAshTestBase,
      public ::testing::WithParamInterface<std::tuple<
          /*is_new_user_locally=*/bool,
          /*is_managed_user=*/bool,
          user_manager::UserType,
          /*is_user_first_login_to_chromeos=*/std::optional<bool>>> {
 public:
  AppsCollectionsControllerUserElegibilityTest() {
    scoped_feature_list_.InitWithFeatures({app_list_features::kAppsCollections},
                                          {});
  }

  // NoSessionAshTestBase:
  void SetUp() override {
    NoSessionAshTestBase::SetUp();
    GetTestAppListClient()->set_is_new_user(IsUserFirstLogInToChromeOS());
  }

  // Returns the user type based on test parameterization.
  user_manager::UserType GetUserType() const { return std::get<2>(GetParam()); }

  // Returns whether the user is managed based on test parameterization.
  bool IsManagedUser() const { return std::get<1>(GetParam()); }

  // Returns whether the user should be considered "new" locally based on test
  // parameterization.
  bool IsNewUserLocally() const { return std::get<0>(GetParam()); }

  // Returns whether the user should be considered "new" across all devices
  // based on test parameterization.
  std::optional<bool> IsUserFirstLogInToChromeOS() const {
    return std::get<3>(GetParam());
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(
    All,
    AppsCollectionsControllerUserElegibilityTest,
    ::testing::Combine(
        /*is_new_user_locally=*/::testing::Bool(),
        /*is_managed_user=*/::testing::Bool(),
        ::testing::Values(user_manager::UserType::kChild,
                          user_manager::UserType::kGuest,
                          user_manager::UserType::kKioskChromeApp,
                          user_manager::UserType::kPublicAccount,
                          user_manager::UserType::kRegular,
                          user_manager::UserType::kKioskWebApp),
        /*is_user_first_login_to_chromeos=*/
        ::testing::Values(std::make_optional(true),
                          std::make_optional(false),
                          std::nullopt)));

TEST_P(AppsCollectionsControllerUserElegibilityTest, EnforcesUserEligibility) {
  // A user is eligible for showing AppsCollections if and only if the user
  // satisfies the following conditions:
  // (1) known to be "new" locally, and
  // (2) not a managed user, and
  // (3) a regular user.
  // (4) a known to be 'new' user across the ChromeOS ecosystem.
  const bool is_user_eligibility_expected =
      IsNewUserLocally() && !IsManagedUser() &&
      GetUserType() == user_manager::UserType::kRegular &&
      IsUserFirstLogInToChromeOS().value_or(false);

  // Login based on test parameterization.
  SimulateUserLogin({.display_email = "primary@test",
                     .user_type = GetUserType(),
                     .is_new_profile = IsNewUserLocally(),
                     .is_account_managed = IsManagedUser()});
  auto* helper = GetAppListTestHelper();
  helper->ShowAppList();

  auto* apps_collections_page = helper->GetBubbleAppsCollectionsPage();
  EXPECT_EQ(apps_collections_page->GetVisible(), is_user_eligibility_expected);
}

// Verifies that regardless of the user elegibility parameters, secondary users
// are not presented with apps collections. This is a self-imposed restriction.
TEST_P(AppsCollectionsControllerUserElegibilityTest, SecondaryUserNotElegible) {
  SimulateNewUserFirstLogin("primary@test");
  // ogin a user based on test parameterization.
  SimulateUserLogin({.display_email = "secondary@test",
                     .user_type = GetUserType(),
                     .is_new_profile = IsNewUserLocally(),
                     .is_account_managed = IsManagedUser()});

  auto* helper = GetAppListTestHelper();
  helper->ShowAppList();

  auto* apps_collections_page = helper->GetBubbleAppsCollectionsPage();
  EXPECT_FALSE(apps_collections_page->GetVisible());
}

// Class for tests of the `AppsCollectionsController` which are
// concerned with the experiment prefs.
class AppsCollectionsControllerPrefTest
    : public NoSessionAshTestBase,
      public ::testing::WithParamInterface<std::tuple<
          /*is_apps_collections_active=*/bool,
          /*is_counterfactual=*/bool,
          /*is_modified_order=*/bool>> {
 public:
  AppsCollectionsControllerPrefTest() {
    if (IsAppsCollectionsEnabled()) {
      scoped_feature_list_.InitAndEnableFeatureWithParameters(
          app_list_features::kAppsCollections,
          {{"is-counterfactual",
            IsAppsCollectionsEnabledCounterfactually() ? "true" : "false"},
           {"is-modified-order",
            IsAppsCollectionsEnabledWithModifiedOrder() ? "true" : "false"}});
    } else {
      scoped_feature_list_.InitAndDisableFeature(
          app_list_features::kAppsCollections);
    }
  }

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

    GetTestAppListClient()->set_is_new_user(true);
    SimulateUserLogin(
        {.display_email = "primary@test", .is_new_profile = true});
  }

  // Returns whether apps collectionns feature is enabled.
  bool IsAppsCollectionsEnabled() const { return std::get<0>(GetParam()); }

  // Returns whether apps collections feature is enabled counterfactrually.
  bool IsAppsCollectionsEnabledCounterfactually() const {
    return IsAppsCollectionsEnabled() && std::get<1>(GetParam());
  }
  // Returns whether apps collections feature is enabled counterfactrually.
  bool IsAppsCollectionsEnabledWithModifiedOrder() const {
    return IsAppsCollectionsEnabled() && std::get<2>(GetParam());
  }

  AppsCollectionsController::ExperimentalArm GetExpectedExperimentalArm() {
    if (!IsAppsCollectionsEnabled()) {
      return AppsCollectionsController::ExperimentalArm::kControl;
    }

    if (IsAppsCollectionsEnabledCounterfactually()) {
      return AppsCollectionsController::ExperimentalArm::kCounterfactual;
    }

    return IsAppsCollectionsEnabledWithModifiedOrder()
               ? AppsCollectionsController::ExperimentalArm::kModifiedOrder
               : AppsCollectionsController::ExperimentalArm::kEnabled;
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         AppsCollectionsControllerPrefTest,
                         ::testing::Combine(
                             /*is_apps_collections_active=*/::testing::Bool(),
                             /*is_counterfactual=*/::testing::Bool(),
                             /*is_modified_order=*/::testing::Bool()));

// Verifies that the experimental arm for the user is calculated and stored
// correctly.
TEST_P(AppsCollectionsControllerPrefTest, GetExperimentalArm) {
  EXPECT_EQ(AppsCollectionsController::Get()->GetUserExperimentalArm(),
            AppsCollectionsController::ExperimentalArm::kDefaultValue);

  auto* helper = GetAppListTestHelper();
  helper->ShowAppList();

  EXPECT_EQ(AppsCollectionsController::Get()->GetUserExperimentalArm(),
            GetExpectedExperimentalArm());
}

}  // namespace
}  // namespace ash