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

ash / webui / os_feedback_ui / resources / fake_feedback_service_provider.ts [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {FakeMethodResolver} from 'chrome://resources/ash/common/fake_method_resolver.js';
import {assert} from 'chrome://resources/js/assert.js';

import {FeedbackAppExitPath, FeedbackAppHelpContentOutcome, FeedbackAppPostSubmitAction, FeedbackAppPreSubmitAction, FeedbackContext, FeedbackServiceProviderInterface, Report, SendReportStatus} from './os_feedback_ui.mojom-webui.js';

/**
 * @fileoverview
 * Implements a fake version of the FeedbackServiceProvider mojo interface.
 */

export class FakeFeedbackServiceProvider implements
    FeedbackServiceProviderInterface {
  private exitPath: FeedbackAppExitPath|null = null;
  private helpContentOutcome: FeedbackAppHelpContentOutcome|null = null;
  private postSubmitAction: FeedbackAppPostSubmitAction|null = null;
  private preSubmitActionMap: Map<FeedbackAppPreSubmitAction, number> =
      new Map();
  private methods: FakeMethodResolver;
  /** Used to track how many times each method is being called.*/
  private callCounts = {
    getFeedbackContext: 0,
    getScreenshotPng: 0,
    sendReport: 0,
    openDiagnosticsApp: 0,
    openExploreApp: 0,
    openMetricsDialog: 0,
    openSystemInfoDialog: 0,
    openAutofillDialog: 0,
    recordHelpContentSearchResultCount: 0,
  };

  constructor() {
    this.methods = new FakeMethodResolver();
    // Setup method resolvers.
    this.methods.register('getFeedbackContext');
    this.methods.register('getScreenshotPng');
    this.methods.register('sendReport');
    // Let sendReport return success by default.
    this.methods.setResult('sendReport', {status: SendReportStatus.kSuccess});
    // Let getScreenshotPng return an empty array by default.
    this.methods.setResult('getScreenshotPng', {pngData: []});
  }

  getFeedbackContextCallCount(): number {
    return this.callCounts.getFeedbackContext;
  }

  getFeedbackContext(): Promise<{feedbackContext: FeedbackContext}> {
    this.callCounts.getFeedbackContext++;
    return this.methods.resolveMethod('getFeedbackContext');
  }

  getSendReportCallCount(): number {
    return this.callCounts.sendReport;
  }

  sendReport(_report: Report): Promise<{status: SendReportStatus}> {
    this.callCounts.sendReport++;
    return this.methods.resolveMethod('sendReport');
  }

  getScreenshotPngCallCount(): number {
    return this.callCounts.getScreenshotPng;
  }

  getScreenshotPng(): Promise<{pngData: number[]}> {
    this.callCounts.getScreenshotPng++;
    return this.methods.resolveMethod('getScreenshotPng');
  }

  /** Sets the value that will be returned when calling getFeedbackContext(). */
  setFakeFeedbackContext(feedbackContext: FeedbackContext) {
    this.methods.setResult('getFeedbackContext', {feedbackContext});
  }

  /**  Sets the value that will be returned when calling sendReport(). */
  setFakeSendFeedbackStatus(status: SendReportStatus) {
    this.methods.setResult('sendReport', {status});
  }

  /**  Sets the value that will be returned when calling getScreenshotPng(). */
  setFakeScreenshotPng(data: number[]) {
    this.methods.setResult('getScreenshotPng', {pngData: data});
  }

  getOpenDiagnosticsAppCallCount(): number {
    return this.callCounts.openDiagnosticsApp;
  }

  openDiagnosticsApp() {
    this.callCounts.openDiagnosticsApp++;
  }

  getOpenExploreAppCallCount(): number {
    return this.callCounts.openExploreApp;
  }

  openExploreApp() {
    this.callCounts.openExploreApp++;
  }

  getOpenMetricsDialogCallCount(): number {
    return this.callCounts.openMetricsDialog;
  }

  openMetricsDialog() {
    this.callCounts.openMetricsDialog++;
  }

  getOpenSystemInfoDialogCallCount(): number {
    return this.callCounts.openSystemInfoDialog;
  }

  openSystemInfoDialog() {
    this.callCounts.openSystemInfoDialog++;
  }

  getOpenAutofillDialogCallCount(): number {
    return this.callCounts.openAutofillDialog;
  }

  openAutofillDialog() {
    this.callCounts.openAutofillDialog++;
  }

  getRecordHelpContentSearchResultCount(): number {
    return this.callCounts.recordHelpContentSearchResultCount;
  }

  recordHelpContentSearchResultCount() {
    this.callCounts.recordHelpContentSearchResultCount++;
  }

  isRecordPostSubmitActionCalled(action: FeedbackAppPostSubmitAction): boolean {
    return this.postSubmitAction !== null && this.postSubmitAction === action;
  }

  recordPostSubmitAction(action: FeedbackAppPostSubmitAction) {
    if (this.postSubmitAction === null) {
      this.postSubmitAction = action;
    }
  }

  isRecordExitPathCalled(exitPath: FeedbackAppExitPath): boolean {
    return this.exitPath !== null && this.exitPath === exitPath;
  }

  recordExitPath(exitPath: FeedbackAppExitPath|null) {
    if (this.exitPath === null) {
      this.exitPath = exitPath;
    }
  }

  getRecordPreSubmitActionCallCount(action: FeedbackAppPreSubmitAction):
      number {
    return this.preSubmitActionMap.get(action) || 0;
  }

  recordPreSubmitAction(action: FeedbackAppPreSubmitAction) {
    this.preSubmitActionMap.set(
        action, this.getRecordPreSubmitActionCallCount(action) + 1);
  }

  isHelpContentOutcomeMetricEmitted(outcome: FeedbackAppHelpContentOutcome):
      boolean {
    return this.helpContentOutcome !== null &&
        this.helpContentOutcome === outcome;
  }

  recordHelpContentOutcome(outcome: FeedbackAppHelpContentOutcome): void {
    assert(this.helpContentOutcome === null);
    this.helpContentOutcome = outcome;
  }
}