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

ash / wm / desks / templates / saved_desk_test_util.cc [blame]

// 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 "ash/wm/desks/templates/saved_desk_test_util.h"

#include "ash/shell.h"
#include "ash/style/icon_button.h"
#include "ash/wm/desks/desks_test_util.h"
#include "ash/wm/desks/overview_desk_bar_view.h"
#include "ash/wm/desks/templates/saved_desk_controller.h"
#include "ash/wm/desks/templates/saved_desk_dialog_controller.h"
#include "ash/wm/desks/templates/saved_desk_icon_container.h"
#include "ash/wm/desks/templates/saved_desk_presenter.h"
#include "ash/wm/desks/templates/saved_desk_save_desk_button.h"
#include "ash/wm/overview/overview_grid.h"
#include "ash/wm/overview/overview_grid_test_api.h"
#include "ash/wm/overview/overview_utils.h"
#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/run_until.h"
#include "ui/compositor/layer.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/animation/bounds_animator_observer.h"
#include "ui/views/widget/widget_delegate.h"

namespace ash {

namespace {

// Gets the overview grid associated with the primary root window. Returns null
// if we aren't in overview.
OverviewGrid* GetPrimaryOverviewGrid() {
  auto* overview_session = GetOverviewSession();
  return overview_session ? overview_session->GetGridWithRootWindow(
                                Shell::GetPrimaryRootWindow())
                          : nullptr;
}

// `BoundsAnimatorWaiter` observes the `BoundsAnimator` and waits for the
// template grid items animations to finish.
class BoundsAnimatorWaiter : public views::BoundsAnimatorObserver {
 public:
  explicit BoundsAnimatorWaiter(views::BoundsAnimator& animator)
      : animator_(animator) {
    animator_->AddObserver(this);
  }

  BoundsAnimatorWaiter(const BoundsAnimatorWaiter&) = delete;
  BoundsAnimatorWaiter& operator=(const BoundsAnimatorWaiter&) = delete;

  ~BoundsAnimatorWaiter() override { animator_->RemoveObserver(this); }

  void Wait() {
    if (!animator_->IsAnimating()) {
      return;
    }

    run_loop_ = std::make_unique<base::RunLoop>();
    run_loop_->Run();
  }

 private:
  // views::BoundsAnimatorObserver:
  void OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) override {}
  void OnBoundsAnimatorDone(views::BoundsAnimator* animator) override {
    if (run_loop_)
      run_loop_->Quit();
  }

  const raw_ref<views::BoundsAnimator> animator_;
  std::unique_ptr<base::RunLoop> run_loop_;
};

}  // namespace

SavedDeskPresenterTestApi::SavedDeskPresenterTestApi(
    SavedDeskPresenter* presenter)
    : presenter_(presenter) {
  DCHECK(presenter_);
}

SavedDeskPresenterTestApi::~SavedDeskPresenterTestApi() = default;

// static
void SavedDeskPresenterTestApi::WaitForSaveAndRecallBlockingDialog() {
  base::RunLoop loop;
  SavedDeskPresenter::SetModalDialogCallbackForTesting(loop.QuitClosure());
  loop.Run();
}

// static
void SavedDeskPresenterTestApi::FireWindowWatcherTimer() {
  SavedDeskPresenter::FireWindowWatcherTimerForTesting();
}

void SavedDeskPresenterTestApi::SetOnUpdateUiClosure(
    base::OnceClosure closure) {
  DCHECK(!presenter_->on_update_ui_closure_for_testing_);
  presenter_->on_update_ui_closure_for_testing_ = std::move(closure);
}

void SavedDeskPresenterTestApi::MaybeWaitForModel() {
  if (presenter_->weak_ptr_factory_.HasWeakPtrs())
    WaitForSavedDeskUI();
}

SavedDeskLibraryViewTestApi::SavedDeskLibraryViewTestApi(
    SavedDeskLibraryView* library_view)
    : library_view_(library_view) {}

void SavedDeskLibraryViewTestApi::WaitForAnimationDone() {
  for (SavedDeskGridView* grid_view : library_view_->grid_views()) {
    SavedDeskGridViewTestApi(grid_view).WaitForItemMoveAnimationDone();
  }
}

SavedDeskGridViewTestApi::SavedDeskGridViewTestApi(SavedDeskGridView* grid_view)
    : grid_view_(grid_view) {
  DCHECK(grid_view_);
}

SavedDeskGridViewTestApi::~SavedDeskGridViewTestApi() = default;

void SavedDeskGridViewTestApi::WaitForItemMoveAnimationDone() {
  BoundsAnimatorWaiter(grid_view_->bounds_animator_).Wait();
}

SavedDeskItemViewTestApi::SavedDeskItemViewTestApi(
    const SavedDeskItemView* item_view)
    : item_view_(item_view) {
  DCHECK(item_view_);
}

SavedDeskItemViewTestApi::~SavedDeskItemViewTestApi() = default;

std::vector<SavedDeskIconView*> SavedDeskItemViewTestApi::GetIconViews() const {
  std::vector<SavedDeskIconView*> casted_icon_views;
  for (views::View* icon_view : item_view_->icon_container_view_->children()) {
    casted_icon_views.push_back(static_cast<SavedDeskIconView*>(icon_view));
  }
  return casted_icon_views;
}

SavedDeskItemHoverState SavedDeskItemViewTestApi::GetHoverState() const {
  float hover_layer_opacity =
      item_view_->hover_container_->layer()->GetTargetOpacity();
  float icon_layer_opacity =
      item_view_->icon_container_view_->layer()->GetTargetOpacity();

  if (hover_layer_opacity == 1.0f && icon_layer_opacity == 0.0f) {
    return SavedDeskItemHoverState::kHover;
  }
  if (hover_layer_opacity == 0.0f && icon_layer_opacity == 1.0f) {
    return SavedDeskItemHoverState::kIcons;
  }
  return SavedDeskItemHoverState::kIndeterminate;
}

SavedDeskIconViewTestApi::SavedDeskIconViewTestApi(
    const SavedDeskIconView* saved_desk_icon_view)
    : saved_desk_icon_view_(saved_desk_icon_view) {
  DCHECK(saved_desk_icon_view_);
}

SavedDeskIconViewTestApi::~SavedDeskIconViewTestApi() = default;

SavedDeskControllerTestApi::SavedDeskControllerTestApi(
    SavedDeskController* saved_desk_controller)
    : saved_desk_controller_(saved_desk_controller) {}

SavedDeskControllerTestApi::~SavedDeskControllerTestApi() = default;

void SavedDeskControllerTestApi::SetAdminTemplate(
    std::unique_ptr<DeskTemplate> admin_template) {
  saved_desk_controller_->SetAdminTemplateForTesting(std::move(admin_template));
}

void SavedDeskControllerTestApi::ResetAutoLaunch() {
  saved_desk_controller_->ResetAutoLaunchForTesting();
}

std::vector<SavedDeskItemView*> GetItemViewsFromDeskLibrary(
    OverviewGrid* overview_grid) {
  SavedDeskLibraryView* saved_desk_library_view =
      overview_grid->GetSavedDeskLibraryView();
  return GetItemViewsFromDeskLibrary(saved_desk_library_view);
}

std::vector<SavedDeskItemView*> GetItemViewsFromDeskLibrary(
    SavedDeskLibraryView* saved_desk_library_view) {
  DCHECK(saved_desk_library_view);
  std::vector<SavedDeskItemView*> grid_items;
  for (SavedDeskGridView* grid_view : saved_desk_library_view->grid_views()) {
    auto& items = grid_view->grid_items();
    grid_items.insert(grid_items.end(), items.begin(), items.end());
  }
  return grid_items;
}

SavedDeskItemView* GetItemViewFromSavedDeskGrid(size_t grid_item_index) {
  auto* overview_grid = GetPrimaryOverviewGrid();
  DCHECK(overview_grid);

  SavedDeskPresenterTestApi(
      overview_grid->overview_session()->saved_desk_presenter())
      .MaybeWaitForModel();

  auto grid_items = GetItemViewsFromDeskLibrary(overview_grid);
  DCHECK_LT(grid_item_index, grid_items.size());

  SavedDeskItemView* item_view = grid_items[grid_item_index];
  DCHECK(item_view);
  return item_view;
}

const views::Button* GetLibraryButton() {
  const auto* overview_grid = GetPrimaryOverviewGrid();
  if (!overview_grid)
    return nullptr;

  // May be null in tablet mode.
  const auto* desks_bar_view = overview_grid->desks_bar_view();
  if (!desks_bar_view) {
    return nullptr;
  }

  return desks_bar_view->library_button();
}

const views::Button* GetSaveDeskAsTemplateButton() {
  auto* overview_grid = GetPrimaryOverviewGrid();
  return overview_grid
             ? OverviewGridTestApi(overview_grid).GetSaveDeskAsTemplateButton()
             : nullptr;
}

const views::Button* GetSaveDeskForLaterButton() {
  auto* overview_grid = GetPrimaryOverviewGrid();
  return overview_grid
             ? OverviewGridTestApi(overview_grid).GetSaveDeskForLaterButton()
             : nullptr;
}

const views::Button* GetSavedDeskItemButton(int index) {
  auto* item = GetItemViewFromSavedDeskGrid(index);
  return item ? static_cast<views::Button*>(item) : nullptr;
}

const views::Button* GetSavedDeskItemDeleteButton(int index) {
  auto* item = GetItemViewFromSavedDeskGrid(index);
  return item ? const_cast<IconButton*>(
                    SavedDeskItemViewTestApi(item).delete_button())
              : nullptr;
}

const views::Button* GetSavedDeskDialogAcceptButton() {
  if (auto* dialog_widget_view = saved_desk_util::GetSavedDeskDialogController()
                                     ->GetSystemDialogViewForTesting()) {
    return dialog_widget_view->GetAcceptButtonForTesting();
  }
  return nullptr;
}

void WaitForSavedDeskUI() {
  auto* overview_session = GetOverviewSession();
  DCHECK(overview_session);

  base::RunLoop run_loop;
  SavedDeskPresenterTestApi(overview_session->saved_desk_presenter())
      .SetOnUpdateUiClosure(run_loop.QuitClosure());
  run_loop.Run();
}

bool WaitForLibraryButtonVisible() {
  return base::test::RunUntil(
      []() { return IsLazyInitViewVisible(GetLibraryButton()); });
}

const app_restore::AppRestoreData* QueryRestoreData(
    const DeskTemplate& saved_desk,
    std::optional<std::string> app_id,
    std::optional<int32_t> window_id) {
  const auto& app_id_to_launch_list =
      saved_desk.desk_restore_data()->app_id_to_launch_list();

  auto app_it = app_id ? app_id_to_launch_list.find(*app_id)
                       : app_id_to_launch_list.begin();
  if (app_it == app_id_to_launch_list.end()) {
    // No matching app found, or the app list is empty.
    return nullptr;
  }

  const auto& launch_list = app_it->second;
  auto window_it =
      window_id ? launch_list.find(*window_id) : launch_list.begin();
  if (window_it == launch_list.end()) {
    // No matching window found, or the window list is is empty.
    return nullptr;
  }

  return window_it->second.get();
}

void AddSavedDeskEntry(desks_storage::DeskModel* desk_model,
                       std::unique_ptr<DeskTemplate> saved_desk) {
  base::RunLoop loop;
  desk_model->AddOrUpdateEntry(
      std::move(saved_desk),
      base::BindLambdaForTesting(
          [&](desks_storage::DeskModel::AddOrUpdateEntryStatus status,
              std::unique_ptr<DeskTemplate> new_entry) {
            CHECK_EQ(desks_storage::DeskModel::AddOrUpdateEntryStatus::kOk,
                     status);
            loop.Quit();
          }));
  loop.Run();
}

void AddSavedDeskEntry(desks_storage::DeskModel* desk_model,
                       const base::Uuid& uuid,
                       const std::string& name,
                       base::Time created_time,
                       DeskTemplateSource source,
                       DeskTemplateType type,
                       std::unique_ptr<app_restore::RestoreData> restore_data) {
  auto saved_desk =
      std::make_unique<DeskTemplate>(uuid, source, name, created_time, type);
  saved_desk->set_desk_restore_data(std::move(restore_data));

  AddSavedDeskEntry(desk_model, std::move(saved_desk));
}

void AddSavedDeskEntry(desks_storage::DeskModel* desk_model,
                       const base::Uuid& uuid,
                       const std::string& name,
                       base::Time created_time,
                       DeskTemplateType type) {
  AddSavedDeskEntry(desk_model, uuid, name, created_time,
                    DeskTemplateSource::kUser, type,
                    std::make_unique<app_restore::RestoreData>());
}

}  // namespace ash