summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/media/webrtc/fake_desktop_media_picker_factory.cc
blob: 99a474ce3244978937fae0e709e4959e55ce0a12 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media/webrtc/fake_desktop_media_picker_factory.h"

#include <utility>

#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/media/webrtc/fake_desktop_media_list.h"
#include "testing/gtest/include/gtest/gtest.h"

class FakeDesktopMediaPicker : public DesktopMediaPicker {
 public:
  explicit FakeDesktopMediaPicker(
      FakeDesktopMediaPickerFactory::TestFlags* expectation)
      : expectation_(expectation) {
    expectation_->picker_created = true;
  }
  ~FakeDesktopMediaPicker() override { expectation_->picker_deleted = true; }

  // DesktopMediaPicker interface.
  void Show(const DesktopMediaPicker::Params& params,
            std::vector<std::unique_ptr<DesktopMediaList>> source_lists,
            DoneCallback done_callback) override {
    bool show_screens = false;
    bool show_windows = false;
    bool show_tabs = false;

    for (auto& source_list : source_lists) {
      switch (source_list->GetMediaListType()) {
        case content::DesktopMediaID::TYPE_NONE:
          break;
        case content::DesktopMediaID::TYPE_SCREEN:
          show_screens = true;
          break;
        case content::DesktopMediaID::TYPE_WINDOW:
          show_windows = true;
          break;
        case content::DesktopMediaID::TYPE_WEB_CONTENTS:
          show_tabs = true;
          break;
      }
    }
    EXPECT_EQ(expectation_->expect_screens, show_screens);
    EXPECT_EQ(expectation_->expect_windows, show_windows);
    EXPECT_EQ(expectation_->expect_tabs, show_tabs);
    EXPECT_EQ(expectation_->expect_audio, params.request_audio);
    EXPECT_EQ(params.modality, ui::ModalType::MODAL_TYPE_CHILD);

    if (!expectation_->cancelled) {
      // Post a task to call the callback asynchronously.
      base::ThreadTaskRunnerHandle::Get()->PostTask(
          FROM_HERE,
          base::BindOnce(&FakeDesktopMediaPicker::CallCallback,
                         weak_factory_.GetWeakPtr(), std::move(done_callback)));
    } else {
      // If we expect the dialog to be cancelled then store the callback to
      // retain reference to the callback handler.
      done_callback_ = std::move(done_callback);
    }
  }

 private:
  void CallCallback(DoneCallback done_callback) {
    std::move(done_callback).Run(expectation_->selected_source);
  }

  FakeDesktopMediaPickerFactory::TestFlags* expectation_;
  DoneCallback done_callback_;

  base::WeakPtrFactory<FakeDesktopMediaPicker> weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(FakeDesktopMediaPicker);
};

FakeDesktopMediaPickerFactory::FakeDesktopMediaPickerFactory() = default;

FakeDesktopMediaPickerFactory::~FakeDesktopMediaPickerFactory() = default;

void FakeDesktopMediaPickerFactory::SetTestFlags(TestFlags* test_flags,
                                                 int tests_count) {
  test_flags_ = test_flags;
  tests_count_ = tests_count;
  current_test_ = 0;
}

std::unique_ptr<DesktopMediaPicker>
FakeDesktopMediaPickerFactory::CreatePicker() {
  EXPECT_LE(current_test_, tests_count_);
  if (current_test_ >= tests_count_)
    return std::unique_ptr<DesktopMediaPicker>();
  ++current_test_;
  return std::unique_ptr<DesktopMediaPicker>(
      new FakeDesktopMediaPicker(test_flags_ + current_test_ - 1));
}

std::vector<std::unique_ptr<DesktopMediaList>>
FakeDesktopMediaPickerFactory::CreateMediaList(
    const std::vector<content::DesktopMediaID::Type>& types) {
  EXPECT_LE(current_test_, tests_count_);
  std::vector<std::unique_ptr<DesktopMediaList>> media_lists;
  for (auto source_type : types)
    media_lists.emplace_back(new FakeDesktopMediaList(source_type));
  return media_lists;
}