1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_FAKE_DESKTOP_MEDIA_PICKER_FACTORY_H_
6 #define CHROME_BROWSER_MEDIA_WEBRTC_FAKE_DESKTOP_MEDIA_PICKER_FACTORY_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/optional.h"
12 #include "chrome/browser/media/webrtc/desktop_media_list.h"
13 #include "chrome/browser/media/webrtc/desktop_media_picker.h"
14 #include "chrome/browser/media/webrtc/desktop_media_picker_factory.h"
15 #include "content/public/browser/desktop_media_id.h"
16 
17 class FakeDesktopMediaPicker;
18 
19 // Used in tests to supply fake picker.
20 class FakeDesktopMediaPickerFactory : public DesktopMediaPickerFactory {
21  public:
22   struct TestFlags {
23     bool expect_screens = false;
24     bool expect_windows = false;
25     bool expect_tabs = false;
26     bool expect_audio = false;
27     content::DesktopMediaID selected_source;
28     bool cancelled = false;
29 
30     // Following flags are set by FakeDesktopMediaPicker when it's created and
31     // deleted.
32     bool picker_created = false;
33     bool picker_deleted = false;
34   };
35 
36   FakeDesktopMediaPickerFactory();
37   ~FakeDesktopMediaPickerFactory() override;
38 
39   //  |test_flags| are expected to outlive the factory.
40   void SetTestFlags(TestFlags* test_flags, int tests_count);
picker()41   FakeDesktopMediaPicker* picker() const { return picker_; }
42   // DesktopMediaPickerFactory implementation
43   std::unique_ptr<DesktopMediaPicker> CreatePicker() override;
44   std::vector<std::unique_ptr<DesktopMediaList>> CreateMediaList(
45       const std::vector<content::DesktopMediaID::Type>& types) override;
46 
47  private:
48   FakeDesktopMediaPicker* picker_;
49   TestFlags* test_flags_;
50   int tests_count_;
51   int current_test_;
52 
53   DISALLOW_COPY_AND_ASSIGN(FakeDesktopMediaPickerFactory);
54 };
55 
56 class FakeDesktopMediaPicker : public DesktopMediaPicker {
57  public:
58   explicit FakeDesktopMediaPicker(
59       FakeDesktopMediaPickerFactory::TestFlags* expectation);
60   ~FakeDesktopMediaPicker() override;
61 
62   // DesktopMediaPicker interface.
63   void Show(const DesktopMediaPicker::Params& params,
64             std::vector<std::unique_ptr<DesktopMediaList>> source_lists,
65             DoneCallback done_callback) override;
66 
67   DesktopMediaPicker::Params GetParams();
68 
69  private:
70   void CallCallback(DoneCallback done_callback);
71 
72   FakeDesktopMediaPickerFactory::TestFlags* expectation_;
73   DoneCallback done_callback_;
74   DesktopMediaPicker::Params picker_params_;
75 
76   base::WeakPtrFactory<FakeDesktopMediaPicker> weak_factory_{this};
77 
78   DISALLOW_COPY_AND_ASSIGN(FakeDesktopMediaPicker);
79 };
80 
81 #endif  // CHROME_BROWSER_MEDIA_WEBRTC_FAKE_DESKTOP_MEDIA_PICKER_FACTORY_H_
82