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

ash / webui / print_management / backend / print_management_handler_unittest.cc [blame]

// Copyright 2023 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/print_management/backend/print_management_handler.h"

#include <memory>

#include "ash/webui/print_management/backend/print_management_delegate.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::printing::printing_manager {

namespace {

constexpr char kRecordUserActionMetric[] =
    "ChromeOS.PrintManagement.PrinterSettingsLaunchSource";

using chromeos::printing::printing_manager::mojom::LaunchSource;

class FakePrintManagementDelegate : public PrintManagementDelegate {
 public:
  FakePrintManagementDelegate() = default;
  ~FakePrintManagementDelegate() override = default;

  // PrintManagementDelegate:
  void LaunchPrinterSettings() override { launch_printer_settings_count_++; }

  int launch_printer_settings_count() const {
    return launch_printer_settings_count_;
  }

 private:
  int launch_printer_settings_count_ = 0;
};

}  // namespace

class PrintManagementHandlerTest : public testing::Test {
 public:
  PrintManagementHandlerTest() {
    auto delegate = std::make_unique<FakePrintManagementDelegate>();
    delegate_ = delegate.get();
    handler_ = std::make_unique<PrintManagementHandler>(std::move(delegate));
  }
  ~PrintManagementHandlerTest() override = default;

  FakePrintManagementDelegate* delegate() { return delegate_; }
  PrintManagementHandler* handler() { return handler_.get(); }

 private:
  raw_ptr<FakePrintManagementDelegate, DanglingUntriaged> delegate_;
  std::unique_ptr<PrintManagementHandler> handler_;
};

// Verifies handler uses delegate to attempt to open printer settings.
TEST_F(PrintManagementHandlerTest, LaunchPrinterSettingsCallsDelegate) {
  EXPECT_EQ(0, delegate()->launch_printer_settings_count());

  handler()->LaunchPrinterSettings(LaunchSource::kEmptyStateButton);

  EXPECT_EQ(1, delegate()->launch_printer_settings_count());
}

// Verifies handler records expected metrics when launch printer settings
// called.
TEST_F(PrintManagementHandlerTest, LaunchPrinterSettingsTriggersMetric) {
  base::HistogramTester tester;
  tester.ExpectBucketCount(kRecordUserActionMetric,
                           LaunchSource::kEmptyStateButton,
                           /*expected_count=*/0);
  tester.ExpectBucketCount(kRecordUserActionMetric, LaunchSource::kHeaderButton,
                           /*expected_count=*/0);

  // Simulate launch from empty state 'manage printers' button.
  handler()->LaunchPrinterSettings(LaunchSource::kEmptyStateButton);

  tester.ExpectBucketCount(kRecordUserActionMetric,
                           LaunchSource::kEmptyStateButton,
                           /*expected_count=*/1);
  tester.ExpectBucketCount(kRecordUserActionMetric, LaunchSource::kHeaderButton,
                           /*expected_count=*/0);

  // Simulate launch from header 'manage printers' button.
  handler()->LaunchPrinterSettings(LaunchSource::kHeaderButton);

  tester.ExpectBucketCount(kRecordUserActionMetric,
                           LaunchSource::kEmptyStateButton,
                           /*expected_count=*/1);
  tester.ExpectBucketCount(kRecordUserActionMetric, LaunchSource::kHeaderButton,
                           /*expected_count=*/1);
}

}  // namespace ash::printing::printing_manager