OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 #include "chrome/browser/media/router/mojo/media_route_controller.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/run_loop.h" | |
11 #include "chrome/browser/media/router/mojo/media_router_mojo_test.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using ::testing::StrictMock; | |
16 | |
17 namespace media_router { | |
18 | |
19 class MockMediaController : public mojom::MediaController { | |
20 public: | |
21 MOCK_METHOD0(Play, void()); | |
22 MOCK_METHOD0(Pause, void()); | |
23 MOCK_METHOD1(SetMute, void(bool mute)); | |
24 MOCK_METHOD1(SetVolume, void(float volume)); | |
25 MOCK_METHOD1(Seek, void(base::TimeDelta time)); | |
26 }; | |
27 | |
28 class MockMediaRouteControllerObserver : public MediaRouteController::Observer { | |
29 public: | |
30 MockMediaRouteControllerObserver( | |
31 scoped_refptr<MediaRouteController> controller) | |
32 : MediaRouteController::Observer(controller) {} | |
33 | |
34 MOCK_METHOD1(OnMediaStatusUpdated, void(const MediaStatus& status)); | |
35 }; | |
36 | |
37 class MediaRouteControllerUnitTest : public ::testing::Test { | |
mark a. foltz
2017/03/17 23:55:04
Nit: drop "Unit"
takumif
2017/03/20 22:10:45
Done.
| |
38 public: | |
39 MediaRouteControllerUnitTest() {} | |
40 ~MediaRouteControllerUnitTest() override {} | |
41 | |
42 void SetUp() override { | |
43 mojom::MediaControllerPtr media_controller_ptr; | |
44 mojom::MediaControllerRequest media_controller_request = | |
45 mojo::MakeRequest(&media_controller_ptr); | |
46 mock_media_controller_ = base::MakeUnique<MockMediaController>(); | |
mark a. foltz
2017/03/17 23:55:04
This can be a class member.
takumif
2017/03/20 22:10:45
Done.
| |
47 media_controller_binding_ = | |
48 base::MakeUnique<mojo::Binding<mojom::MediaController>>( | |
49 mock_media_controller_.get(), std::move(media_controller_request)); | |
50 | |
51 observer_ = base::MakeUnique<MockMediaRouteControllerObserver>( | |
52 new MediaRouteController( | |
53 "routeId", std::move(media_controller_ptr), | |
54 base::Bind(&MediaRouteControllerUnitTest::OnControllerDestroyed, | |
55 base::Unretained(this)))); | |
56 } | |
57 | |
58 MediaRouteController* GetController() { | |
59 return observer_ ? observer_->controller() : nullptr; | |
mark a. foltz
2017/03/17 23:55:05
I think this will be non-null while the test cases
takumif
2017/03/20 22:10:45
In DestroyControllerOnNoObservers we reset |observ
| |
60 } | |
61 | |
62 MOCK_METHOD0(OnControllerDestroyed, void()); | |
63 | |
64 protected: | |
65 std::unique_ptr<MockMediaController> mock_media_controller_; | |
66 std::unique_ptr<mojo::Binding<mojom::MediaController>> | |
67 media_controller_binding_; | |
68 std::unique_ptr<MockMediaRouteControllerObserver> observer_; | |
69 | |
70 content::TestBrowserThreadBundle test_thread_bundle_; | |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(MediaRouteControllerUnitTest); | |
74 }; | |
75 | |
76 TEST_F(MediaRouteControllerUnitTest, ForwardControllerCommands) { | |
77 const float volume = 0.5; | |
78 const base::TimeDelta time = base::TimeDelta::FromSeconds(42); | |
79 | |
80 EXPECT_CALL(*mock_media_controller_, Play()); | |
81 GetController()->Play(); | |
82 EXPECT_CALL(*mock_media_controller_, Pause()); | |
83 GetController()->Pause(); | |
84 EXPECT_CALL(*mock_media_controller_, SetMute(true)); | |
85 GetController()->SetMute(true); | |
86 EXPECT_CALL(*mock_media_controller_, SetVolume(volume)); | |
87 GetController()->SetVolume(volume); | |
88 EXPECT_CALL(*mock_media_controller_, Seek(time)); | |
89 GetController()->Seek(time); | |
90 } | |
91 | |
92 TEST_F(MediaRouteControllerUnitTest, NotifyMediaRouteControllerObservers) { | |
93 std::unique_ptr<StrictMock<MockMediaRouteControllerObserver>> observer1 = | |
94 base::MakeUnique<StrictMock<MockMediaRouteControllerObserver>>( | |
95 GetController()); | |
96 std::unique_ptr<StrictMock<MockMediaRouteControllerObserver>> observer2 = | |
97 base::MakeUnique<StrictMock<MockMediaRouteControllerObserver>>( | |
98 GetController()); | |
99 | |
100 MediaStatus status; | |
101 status.title = "test media status"; | |
102 | |
103 EXPECT_CALL(*observer1, OnMediaStatusUpdated(status)); | |
104 EXPECT_CALL(*observer2, OnMediaStatusUpdated(status)); | |
105 // TODO(takumif): Use the controller's mojo interface pointer here. | |
mark a. foltz
2017/03/17 23:55:04
Not sure I follow this comment? I didn't see a cod
takumif
2017/03/20 22:10:45
As seen in the patch before this one, the controll
| |
106 GetController()->OnMediaStatusUpdated(status); | |
107 | |
108 observer1.reset(); | |
109 std::unique_ptr<StrictMock<MockMediaRouteControllerObserver>> observer3 = | |
110 base::MakeUnique<StrictMock<MockMediaRouteControllerObserver>>( | |
111 GetController()); | |
112 | |
113 EXPECT_CALL(*observer2, OnMediaStatusUpdated(status)); | |
114 EXPECT_CALL(*observer3, OnMediaStatusUpdated(status)); | |
115 // TODO(takumif): Use the controller's mojo interface pointer here. | |
116 GetController()->OnMediaStatusUpdated(status); | |
117 } | |
118 | |
119 TEST_F(MediaRouteControllerUnitTest, DestroyControllerOnDisconnect) { | |
120 // OnControllerDestroyed() should be called when the connection to | |
121 // |mock_media_controller_| is invalidated. | |
122 EXPECT_CALL(*this, OnControllerDestroyed()).Times(1); | |
123 media_controller_binding_.reset(); | |
124 } | |
125 | |
126 TEST_F(MediaRouteControllerUnitTest, DestroyControllerOnNoObservers) { | |
127 std::unique_ptr<MockMediaRouteControllerObserver> observer1 = | |
mark a. foltz
2017/03/17 23:55:05
IMO, this would be less boilerplate with:
std::un
takumif
2017/03/20 22:10:45
Is this tied to your other comment that the Observ
mark a. foltz
2017/03/21 00:05:21
Yeah, if we are constructing the Observers separat
| |
128 base::MakeUnique<MockMediaRouteControllerObserver>(GetController()); | |
129 std::unique_ptr<MockMediaRouteControllerObserver> observer2 = | |
130 base::MakeUnique<MockMediaRouteControllerObserver>(GetController()); | |
131 // Get rid of |observer_| and its reference to the controller. | |
132 observer_.reset(); | |
133 | |
134 EXPECT_CALL(*this, OnControllerDestroyed()).Times(0); | |
135 observer1.reset(); | |
136 | |
137 // OnControllerDestroyed() should be called when the controller no longer has | |
138 // any observers. | |
139 EXPECT_CALL(*this, OnControllerDestroyed()).Times(1); | |
140 observer2.reset(); | |
141 } | |
142 | |
143 } // namespace media_router | |
OLD | NEW |