test_app_list_client.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/app_list/test_app_list_client.h"
  5. #include <utility>
  6. #include "ash/app_list/app_list_model_provider.h"
  7. #include "ash/app_list/model/app_list_item.h"
  8. #include "ash/public/cpp/app_list/app_list_controller.h"
  9. #include "base/bind.h"
  10. #include "base/threading/sequenced_task_runner_handle.h"
  11. #include "base/time/time.h"
  12. #include "ui/base/models/simple_menu_model.h"
  13. namespace ash {
  14. TestAppListClient::TestAppListClient() = default;
  15. TestAppListClient::~TestAppListClient() = default;
  16. void TestAppListClient::StartZeroStateSearch(base::OnceClosure on_done,
  17. base::TimeDelta timeout) {
  18. start_zero_state_search_count_++;
  19. if (run_zero_state_callback_immediately_) {
  20. // Most unit tests generally expect the launcher to open immediately, so run
  21. // the callback synchronously.
  22. std::move(on_done).Run();
  23. } else {
  24. // Simulate production behavior, which collects the results asynchronously.
  25. // Bounce through OnZeroStateSearchDone() to count calls, so that tests can
  26. // assert that the callback happened.
  27. base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
  28. FROM_HERE,
  29. base::BindOnce(&TestAppListClient::OnZeroStateSearchDone,
  30. weak_factory_.GetWeakPtr(), std::move(on_done)),
  31. base::Milliseconds(1));
  32. }
  33. }
  34. void TestAppListClient::StartSearch(const std::u16string& trimmed_query) {
  35. search_queries_.push_back(trimmed_query);
  36. }
  37. void TestAppListClient::OpenSearchResult(int profile_id,
  38. const std::string& result_id,
  39. int event_flags,
  40. AppListLaunchedFrom launched_from,
  41. AppListLaunchType launch_type,
  42. int suggestion_index,
  43. bool launch_as_default) {
  44. last_opened_search_result_ = result_id;
  45. }
  46. void TestAppListClient::InvokeSearchResultAction(
  47. const std::string& result_id,
  48. SearchResultActionType action) {
  49. invoked_result_actions_.emplace_back(result_id, action);
  50. }
  51. void TestAppListClient::GetSearchResultContextMenuModel(
  52. const std::string& result_id,
  53. GetContextMenuModelCallback callback) {
  54. std::move(callback).Run(nullptr);
  55. }
  56. void TestAppListClient::ActivateItem(int profile_id,
  57. const std::string& id,
  58. int event_flags,
  59. ash::AppListLaunchedFrom launched_from) {
  60. activate_item_count_++;
  61. activate_item_last_id_ = id;
  62. }
  63. void TestAppListClient::GetContextMenuModel(
  64. int profile_id,
  65. const std::string& id,
  66. AppListItemContext item_context,
  67. GetContextMenuModelCallback callback) {
  68. auto model = std::make_unique<ui::SimpleMenuModel>(/*delegate=*/nullptr);
  69. model->AddItem(/*command_id=*/0, u"Menu item");
  70. std::move(callback).Run(std::move(model));
  71. }
  72. AppListNotifier* TestAppListClient::GetNotifier() {
  73. return nullptr;
  74. }
  75. std::vector<TestAppListClient::SearchResultActionId>
  76. TestAppListClient::GetAndResetInvokedResultActions() {
  77. std::vector<SearchResultActionId> result;
  78. result.swap(invoked_result_actions_);
  79. return result;
  80. }
  81. std::vector<std::u16string> TestAppListClient::GetAndResetPastSearchQueries() {
  82. std::vector<std::u16string> result;
  83. result.swap(search_queries_);
  84. return result;
  85. }
  86. ash::AppListSortOrder TestAppListClient::GetPermanentSortingOrder() const {
  87. NOTIMPLEMENTED();
  88. return ash::AppListSortOrder::kCustom;
  89. }
  90. void TestAppListClient::CommitTemporarySortOrder() {
  91. // Committing the temporary sort order should not introduce item reorder so
  92. // reset the sort order without reorder animation.
  93. AppListController::Get()->UpdateAppListWithNewTemporarySortOrder(
  94. /*new_order=*/absl::nullopt, /*animate=*/false, base::NullCallback());
  95. }
  96. void TestAppListClient::OnZeroStateSearchDone(base::OnceClosure on_done) {
  97. zero_state_search_done_count_++;
  98. std::move(on_done).Run();
  99. }
  100. } // namespace ash