File: media_stream_focus_delegate_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 5,207 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 The Chromium Authors
// 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/media_stream_focus_delegate.h"

#include <memory>

#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "testing/gmock/include/gmock/gmock.h"

using testing::_;
using testing::Return;

namespace {

const content::DesktopMediaID kDesktopMediaIDWindow =
    content::DesktopMediaID(content::DesktopMediaID::TYPE_WINDOW,
                            content::DesktopMediaID::kNullId);

class MockWindowCapturer : public webrtc::DesktopCapturer {
 public:
  MockWindowCapturer() = default;
  ~MockWindowCapturer() override = default;

  MOCK_METHOD0(FocusOnSelectedSource, bool());
  MOCK_METHOD1(SelectSource, bool(webrtc::DesktopCapturer::SourceId));

  void Start(Callback* callback) override {}
  void CaptureFrame() override {}
};

}  // namespace

class MediaStreamFocusDelegateTest : public BrowserWithTestWindowTest {
 public:
  ~MediaStreamFocusDelegateTest() override = default;

  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();

    // Tabs are added at index 0, so add them in reverse order,
    // leading to a simple mapping of tab-N pointing at foo/N.
    AddTab(browser(), GURL("http://foo/2"));
    AddTab(browser(), GURL("http://foo/1"));
    AddTab(browser(), GURL("http://foo/0"));

    delegate_ = std::make_unique<MediaStreamFocusDelegate>(
        browser()->tab_strip_model()->GetWebContentsAt(0));
  }

  content::DesktopMediaID DesktopMediaIDForTabAt(int index) const {
    content::WebContents* const tab =
        browser()->tab_strip_model()->GetWebContentsAt(index);
    return content::DesktopMediaID(
        content::DesktopMediaID::TYPE_WEB_CONTENTS,
        content::DesktopMediaID::kNullId,
        content::WebContentsMediaCaptureId(
            tab->GetPrimaryMainFrame()->GetProcess()->GetDeprecatedID(),
            tab->GetPrimaryMainFrame()->GetRoutingID()));
  }

  void SetFocus(const content::DesktopMediaID& media_id,
                bool focus,
                bool is_from_microtask = false,
                bool is_from_timer = false) {
    delegate_->SetFocus(media_id, focus, is_from_microtask, is_from_timer);
  }

  void SetWindowCapturer(MockWindowCapturer* window_capturer) {
    delegate_->SetWindowCapturerForTesting(
        base::WrapUnique<MockWindowCapturer>(window_capturer));
  }

 protected:
  std::unique_ptr<MediaStreamFocusDelegate> delegate_;  // Unit-under-test.
};

TEST_F(MediaStreamFocusDelegateTest, FirstSetFocusTrueFocusesTab) {
  ASSERT_EQ(browser()->tab_strip_model()->active_index(), 0);
  SetFocus(DesktopMediaIDForTabAt(1), true);
  EXPECT_EQ(browser()->tab_strip_model()->active_index(), 1);
}

TEST_F(MediaStreamFocusDelegateTest, SecondSetFocusTrueHasNoEffect) {
  // Setup - repeated from FirstSetFocusTrueFocusesTab, but as an assumption.
  ASSERT_EQ(browser()->tab_strip_model()->active_index(), 0);
  SetFocus(DesktopMediaIDForTabAt(1), true);
  ASSERT_EQ(browser()->tab_strip_model()->active_index(), 1);

  // Test - focus unchanged.
  SetFocus(DesktopMediaIDForTabAt(0), true);
  EXPECT_EQ(browser()->tab_strip_model()->active_index(), 1);
}

TEST_F(MediaStreamFocusDelegateTest, SetFocusFalseClosesFocusWindow) {
  ASSERT_EQ(browser()->tab_strip_model()->active_index(), 0);

  // Calling SetFocus(false) does not change focus.
  SetFocus(DesktopMediaIDForTabAt(1), false);
  EXPECT_EQ(browser()->tab_strip_model()->active_index(), 0);

  // Focus can no longer change by new calls to SetFocus(true).
  SetFocus(DesktopMediaIDForTabAt(1), true);
  EXPECT_EQ(browser()->tab_strip_model()->active_index(), 0);
}

TEST_F(MediaStreamFocusDelegateTest, ChangeOfTabClosesFocusWindow) {
  // Setup.
  ASSERT_EQ(browser()->tab_strip_model()->active_index(), 0);
  browser()->tab_strip_model()->ActivateTabAt(2);
  ASSERT_EQ(browser()->tab_strip_model()->active_index(), 2);

  // Test - SetFocus has had no effect.
  SetFocus(DesktopMediaIDForTabAt(1), true);
  EXPECT_EQ(browser()->tab_strip_model()->active_index(), 2);
}

TEST_F(MediaStreamFocusDelegateTest, SelectedSourceFocusesWindow) {
  // Setup.
  raw_ptr<MockWindowCapturer, DanglingUntriaged> window_capturer =
      new MockWindowCapturer();
  SetWindowCapturer(window_capturer);

  ON_CALL(*window_capturer, SelectSource(_)).WillByDefault(Return(true));
  EXPECT_CALL(*window_capturer, SelectSource(_)).Times(1);
  EXPECT_CALL(*window_capturer, FocusOnSelectedSource()).Times(1);
  SetFocus(kDesktopMediaIDWindow, true);
}

TEST_F(MediaStreamFocusDelegateTest, NotSelectedSourceDoesNotFocusWindow) {
  // Setup.
  raw_ptr<MockWindowCapturer, DanglingUntriaged> window_capturer =
      new MockWindowCapturer();
  SetWindowCapturer(window_capturer);

  ON_CALL(*window_capturer, SelectSource(_)).WillByDefault(Return(false));
  EXPECT_CALL(*window_capturer, SelectSource(_)).Times(1);
  EXPECT_CALL(*window_capturer, FocusOnSelectedSource()).Times(0);
  SetFocus(kDesktopMediaIDWindow, true);
}