File: media_router_dialog_controller_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 (181 lines) | stat: -rw-r--r-- 7,200 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
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
179
180
181
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/media_router/browser/media_router_dialog_controller.h"

#include <memory>

#include "base/functional/bind.h"
#include "build/build_config.h"
#include "components/media_router/browser/media_router_metrics.h"
#include "components/media_router/browser/presentation/start_presentation_context.h"
#include "components/media_router/common/media_route.h"
#include "components/media_router/common/media_source.h"
#include "components/media_router/common/mojom/media_router.mojom.h"
#include "components/media_router/common/route_request_result.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_ANDROID)
#include "components/media_router/browser/android/media_router_dialog_controller_android.h"
#endif

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

namespace media_router {

class TestMediaRouterDialogController : public MediaRouterDialogController {
 public:
  explicit TestMediaRouterDialogController(content::WebContents* initiator)
      : MediaRouterDialogController(initiator) {}
  ~TestMediaRouterDialogController() override = default;

  bool IsShowingMediaRouterDialog() const override { return has_dialog_; }
  void CreateMediaRouterDialog(
      MediaRouterDialogActivationLocation activation_location) override {
    has_dialog_ = true;
  }
  void CloseMediaRouterDialog() override { has_dialog_ = false; }

 private:
  bool has_dialog_ = false;
};

class MockWebContentsDelegate : public content::WebContentsDelegate {
 public:
  MockWebContentsDelegate() = default;
  ~MockWebContentsDelegate() override = default;

  MOCK_METHOD1(ActivateContents, void(content::WebContents* web_contents));
};

class MediaRouterDialogControllerTest
    : public content::RenderViewHostTestHarness {
 public:
  MOCK_METHOD3(RequestSuccess,
               void(const blink::mojom::PresentationInfo&,
                    mojom::RoutePresentationConnectionPtr,
                    const MediaRoute&));
  MOCK_METHOD1(RequestError,
               void(const blink::mojom::PresentationError& error));

 protected:
  MediaRouterDialogControllerTest() = default;
  ~MediaRouterDialogControllerTest() override = default;

  void SetUp() override {
    content::RenderViewHostTestHarness::SetUp();

    web_contents_delegate_ = std::make_unique<MockWebContentsDelegate>();
    web_contents()->SetDelegate(web_contents_delegate_.get());
    dialog_controller_ =
        std::make_unique<TestMediaRouterDialogController>(web_contents());
  }

  void TearDown() override {
    dialog_controller_.reset();
    web_contents_delegate_.reset();
    content::RenderViewHostTestHarness::TearDown();
  }

  bool ShowMediaRouterDialogForPresentation() {
    return dialog_controller_->ShowMediaRouterDialogForPresentation(
        std::make_unique<StartPresentationContext>(
            content::PresentationRequest(
                {1, 2},
                {GURL("http://example.com"), GURL("http://example2.com")},
                url::Origin::Create(GURL("http://google.com"))),
            base::BindOnce(&MediaRouterDialogControllerTest::RequestSuccess,
                           base::Unretained(this)),
            base::BindOnce(&MediaRouterDialogControllerTest::RequestError,
                           base::Unretained(this))));
  }

  std::unique_ptr<TestMediaRouterDialogController> dialog_controller_;
  std::unique_ptr<MockWebContentsDelegate> web_contents_delegate_;
};

#if BUILDFLAG(IS_ANDROID)
// The non-Android implementation is tested in
// MediaRouterDialogControllerViewsTest.
TEST_F(MediaRouterDialogControllerTest, CreateForWebContents) {
  MediaRouterDialogController::SetGetOrCreate(
      base::BindRepeating([](content::WebContents* web_contents) {
        DCHECK(web_contents);
        MediaRouterDialogController* controller = nullptr;
        MediaRouterDialogControllerAndroid::CreateForWebContents(web_contents);
        controller =
            MediaRouterDialogControllerAndroid::FromWebContents(web_contents);
        return controller;
      }));
  MediaRouterDialogController* dialog_controller =
      MediaRouterDialogController::GetOrCreateForWebContents(web_contents());
  ASSERT_NE(dialog_controller, nullptr);
  // Returns the same value the second time.
  ASSERT_EQ(
      dialog_controller,
      MediaRouterDialogController::GetOrCreateForWebContents(web_contents()));
}
#endif

TEST_F(MediaRouterDialogControllerTest, ShowAndHideDialog) {
  EXPECT_CALL(*web_contents_delegate_, ActivateContents(web_contents()));
  EXPECT_TRUE(dialog_controller_->ShowMediaRouterDialog(
      MediaRouterDialogActivationLocation::TOOLBAR));
  EXPECT_TRUE(dialog_controller_->IsShowingMediaRouterDialog());

  // If a dialog is already shown, ShowMediaRouterDialog() should return false.
  EXPECT_CALL(*web_contents_delegate_, ActivateContents(web_contents()));
  EXPECT_FALSE(dialog_controller_->ShowMediaRouterDialog(
      MediaRouterDialogActivationLocation::TOOLBAR));

  dialog_controller_->HideMediaRouterDialog();
  EXPECT_FALSE(dialog_controller_->IsShowingMediaRouterDialog());

  // Once the dialog is hidden, ShowMediaRouterDialog() should return true
  // again.
  EXPECT_CALL(*web_contents_delegate_, ActivateContents(web_contents()));
  EXPECT_TRUE(dialog_controller_->ShowMediaRouterDialog(
      MediaRouterDialogActivationLocation::TOOLBAR));
}

TEST_F(MediaRouterDialogControllerTest, ShowDialogForPresentation) {
  EXPECT_CALL(*web_contents_delegate_, ActivateContents(web_contents()));
  EXPECT_TRUE(ShowMediaRouterDialogForPresentation());
  EXPECT_TRUE(dialog_controller_->IsShowingMediaRouterDialog());

  // If a dialog is already shown, ShowMediaRouterDialogForPresentation() should
  // return false.
  EXPECT_FALSE(ShowMediaRouterDialogForPresentation());

  // The error callback is invoked automatically if StartPresentationContext is
  // destroyed without a response.
  EXPECT_CALL(*this, RequestError(_)).Times(1);
}

TEST_F(MediaRouterDialogControllerTest, StartPresentationContext) {
  auto context = std::make_unique<StartPresentationContext>(
      content::PresentationRequest(
          {1, 2}, {GURL("http://example.com"), GURL("http://example2.com")},
          url::Origin::Create(GURL("http://google.com"))),
      base::BindOnce(&MediaRouterDialogControllerTest::RequestSuccess,
                     base::Unretained(this)),
      base::BindOnce(&MediaRouterDialogControllerTest::RequestError,
                     base::Unretained(this)));

  MediaRoute route("routeId", MediaSource::ForTab(1), "sinkId", "Description",
                   false);
  auto result = RouteRequestResult::FromSuccess(route, "presentationId");

  EXPECT_CALL(*this, RequestSuccess(_, _, _)).Times(1);
  EXPECT_CALL(*this, RequestError(_)).Times(0);
  context->HandleRouteResponse(nullptr, *result);
}

}  // namespace media_router