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

ash / components / arc / test / arc_payment_app_bridge_test_support.h [blame]

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

#ifndef ASH_COMPONENTS_ARC_TEST_ARC_PAYMENT_APP_BRIDGE_TEST_SUPPORT_H_
#define ASH_COMPONENTS_ARC_TEST_ARC_PAYMENT_APP_BRIDGE_TEST_SUPPORT_H_

#include <memory>
#include <string>

#include "ash/components/arc/pay/arc_payment_app_bridge.h"
#include "ash/components/arc/session/arc_service_manager.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/components/payments/mock_payment_app_instance.h"
#include "chromeos/components/payments/mojom/payment_app.mojom.h"
#include "chromeos/components/payments/mojom/payment_app_types.mojom.h"
#include "components/user_prefs/test/test_browser_context_with_prefs.h"
#include "content/public/test/browser_task_environment.h"

namespace content {
class BrowserContext;
}  // namespace content

namespace arc {

// Common support utility for tests of payment_app.mojom interface.
class ArcPaymentAppBridgeTestSupport {
 public:
  // Sets up the payment_app.mojom connection in the constructor and disconnects
  // in the destructor.
  class ScopedSetInstance {
   public:
    ScopedSetInstance(ArcServiceManager* manager,
                      chromeos::payments::mojom::PaymentAppInstance* instance);
    ~ScopedSetInstance();

    ScopedSetInstance(const ScopedSetInstance& other) = delete;
    ScopedSetInstance& operator=(const ScopedSetInstance& other) = delete;

   private:
    raw_ptr<ArcServiceManager> manager_;
    raw_ptr<chromeos::payments::mojom::PaymentAppInstance> instance_;
  };

  ArcPaymentAppBridgeTestSupport();
  ~ArcPaymentAppBridgeTestSupport();

  ArcPaymentAppBridgeTestSupport(const ArcPaymentAppBridgeTestSupport& other) =
      delete;
  ArcPaymentAppBridgeTestSupport& operator=(
      const ArcPaymentAppBridgeTestSupport& other) = delete;

  // Creates a ScopedSetInstance to be placed on the stack, so that
  // payment_app.mojom connection is available in the test and is cleaned up
  // when the returned value goes out of scope.
  std::unique_ptr<ScopedSetInstance> CreateScopedSetInstance();

  // The ARC service manager.
  ArcServiceManager* manager() { return ArcServiceManager::Get(); }

  // The mock payment_app.mojom connection.
  payments::MockPaymentAppInstance* instance() { return &instance_; }

  // The browser context that should be used in the test.
  content::BrowserContext* context() { return &context_; }

 private:
  // Required for the TestBrowserContext and ArcServiceManager.
  content::BrowserTaskEnvironment task_environment_;

  // Used for retrieving an instance of ArcPaymentAppBridge owned by a
  // BrowserContext.
  user_prefs::TestBrowserContextWithPrefs context_;

  // The unit test must create an instance of ArcServiceManager for
  // ArcServiceManager::Get() to work correctly.
  ArcServiceManager manager_;

  payments::MockPaymentAppInstance instance_;
};

}  // namespace arc

#endif  // ASH_COMPONENTS_ARC_TEST_ARC_PAYMENT_APP_BRIDGE_TEST_SUPPORT_H_