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

ash / webui / common / backend / accelerator_fetcher_unittest.cc [blame]

// 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/webui/common/backend/accelerator_fetcher.h"

#include <memory>
#include <vector>

#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/accelerators/accelerator_lookup.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/accelerators.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/test/ash_test_suite.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/events/ash/event_rewriter_ash.h"
#include "ui/events/devices/device_data_manager_test_api.h"

namespace ash {

namespace {

const std::vector<AcceleratorAction>& actionIds = {
    AcceleratorAction::kSwitchToNextIme};

bool CompareAccelerators(
    const std::vector<AcceleratorLookup::AcceleratorDetails>& expected,
    const std::vector<mojom::StandardAcceleratorPropertiesPtr>& actual) {
  if (expected.size() != actual.size()) {
    return false;
  }

  for (size_t i = 0; i < expected.size(); ++i) {
    const bool accelerators_equal =
        expected[i].accelerator == actual[i]->accelerator;
    const bool key_display_equal =
        expected[i].key_display == actual[i]->key_display;
    if (!accelerators_equal || !key_display_equal) {
      return false;
    }
  }

  return true;
}

}  // namespace

class FakeAcceleratorFetcherObserver
    : public common::mojom::AcceleratorFetcherObserver {
 public:
  void OnAcceleratorsUpdated(
      AcceleratorAction _actionIds,
      std::vector<mojom::StandardAcceleratorPropertiesPtr> accelerators)
      override {
    accelerators_ = std::move(accelerators);
    ++accelerators_updated_called_count_;
  }

  int accelerators_updated_called_count() {
    return accelerators_updated_called_count_;
  }

  void set_accelerators_updated_called_count(int count) {
    accelerators_updated_called_count_ = count;
  }

  std::vector<mojom::StandardAcceleratorPropertiesPtr> accelerators_;
  int accelerators_updated_called_count_ = 0;
  mojo::Receiver<common::mojom::AcceleratorFetcherObserver> receiver{this};
};

class AcceleratorFetcherTest : public AshTestBase {
 public:
  AcceleratorFetcherTest() {
    scoped_feature_list_.InitWithFeatures({features::kPeripheralCustomization,
                                           features::kInputDeviceSettingsSplit},
                                          {});
  }

  void SetUp() override {
    ui::ResourceBundle::CleanupSharedInstance();
    AshTestSuite::LoadTestResources();
    AshTestBase::SetUp();
    accelerator_lookup_ = Shell::Get()->accelerator_lookup();
    accelerator_fetcher_ = std::make_unique<AcceleratorFetcher>();
    observer_ = std::make_unique<FakeAcceleratorFetcherObserver>();
    accelerator_fetcher_->ObserveAcceleratorChanges(
        actionIds, observer_->receiver.BindNewPipeAndPassRemote());
    accelerator_fetcher_->FlushMojoForTesting();
  }

  void TearDown() override {
    accelerator_fetcher_ = nullptr;
    accelerator_lookup_ = nullptr;
    AshTestBase::TearDown();
  }

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<AcceleratorFetcher> accelerator_fetcher_;
  raw_ptr<AcceleratorLookup> accelerator_lookup_;
  std::unique_ptr<FakeAcceleratorFetcherObserver> observer_;
};

TEST_F(AcceleratorFetcherTest, ObserveAcceleratorChanges) {
  observer_->set_accelerators_updated_called_count(0);
  const auto& expected_accelerators =
      accelerator_lookup_->GetAvailableAcceleratorsForAction(
          AcceleratorAction::kSwitchToNextIme);

  accelerator_fetcher_->OnAcceleratorsUpdated();
  accelerator_fetcher_->FlushMojoForTesting();
  EXPECT_EQ(observer_->accelerators_updated_called_count(),
            static_cast<int>(expected_accelerators.size()));
  EXPECT_EQ(observer_->accelerators_.size(), expected_accelerators.size());
  EXPECT_TRUE(
      CompareAccelerators(expected_accelerators, observer_->accelerators_));
}

}  // namespace ash