Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1364)

Unified Diff: content/browser/renderer_host/media/audio_renderer_host_unittest.cc

Issue 2424163004: Factor out authorization from AudioRendererHost. (Closed)
Patch Set: Dale's comments. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/renderer_host/media/audio_renderer_host_unittest.cc
diff --git a/content/browser/renderer_host/media/audio_renderer_host_unittest.cc b/content/browser/renderer_host/media/audio_renderer_host_unittest.cc
index 1fe9f76f8b2b4025693e9e5980bc7d92c5b7878c..ba629db852ebb3b30bca9685f0247b301b54e19d 100644
--- a/content/browser/renderer_host/media/audio_renderer_host_unittest.cc
+++ b/content/browser/renderer_host/media/audio_renderer_host_unittest.cc
@@ -19,6 +19,8 @@
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/common/media/audio_messages.h"
#include "content/public/common/content_switches.h"
+#include "content/public/test/mock_render_process_host.h"
+#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "ipc/ipc_message_utils.h"
#include "media/audio/audio_manager.h"
@@ -36,7 +38,6 @@ using ::testing::NotNull;
namespace content {
namespace {
-const int kRenderProcessId = 1;
const int kRenderFrameId = 5;
const int kStreamId = 50;
const char kSecurityOrigin[] = "http://localhost";
@@ -52,8 +53,7 @@ void ValidateRenderFrameId(int render_process_id,
int render_frame_id,
const base::Callback<void(bool)>& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
- const bool frame_exists = (render_process_id == kRenderProcessId &&
- render_frame_id == kRenderFrameId);
+ const bool frame_exists = (render_frame_id == kRenderFrameId);
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(callback, frame_exists));
}
@@ -78,12 +78,13 @@ class MockAudioMirroringManager : public AudioMirroringManager {
class MockAudioRendererHost : public AudioRendererHost {
public:
MockAudioRendererHost(base::RunLoop* auth_run_loop,
+ int render_process_id,
media::AudioManager* audio_manager,
AudioMirroringManager* mirroring_manager,
MediaInternals* media_internals,
MediaStreamManager* media_stream_manager,
const std::string& salt)
- : AudioRendererHost(kRenderProcessId,
+ : AudioRendererHost(render_process_id,
audio_manager,
mirroring_manager,
media_internals,
@@ -95,7 +96,6 @@ class MockAudioRendererHost : public AudioRendererHost {
}
// A list of mock methods.
- MOCK_METHOD0(ShutdownForBadMessage, void());
MOCK_METHOD4(OnDeviceAuthorized,
void(int stream_id,
media::OutputDeviceStatus device_status,
@@ -104,6 +104,10 @@ class MockAudioRendererHost : public AudioRendererHost {
MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length));
MOCK_METHOD1(OnStreamError, void(int stream_id));
+ void ShutdownForBadMessage() override { bad_msg_count++; }
+
+ int bad_msg_count = 0;
+
private:
virtual ~MockAudioRendererHost() {
// Make sure all audio streams have been deleted.
@@ -175,20 +179,34 @@ class MockAudioRendererHost : public AudioRendererHost {
DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
};
+class MockRenderProcessHostWithSignaling : public MockRenderProcessHost {
+ public:
+ MockRenderProcessHostWithSignaling(BrowserContext* context,
+ base::RunLoop* auth_run_loop)
+ : MockRenderProcessHost(context), auth_run_loop_(auth_run_loop) {}
+
+ void ShutdownForBadMessage(CrashReportMode crash_report_mode) override {
+ MockRenderProcessHost::ShutdownForBadMessage(crash_report_mode);
+ auth_run_loop_->Quit();
+ }
+
+ private:
+ base::RunLoop* auth_run_loop_; // Used to wait for authorization.
+};
+
class AudioRendererHostTest : public testing::Test {
public:
- AudioRendererHostTest() {
+ AudioRendererHostTest()
+ : render_process_host_(&browser_context_, &auth_run_loop_) {
audio_manager_ = media::AudioManager::CreateForTesting(
base::ThreadTaskRunnerHandle::Get());
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kUseFakeDeviceForMediaStream);
media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
host_ = new MockAudioRendererHost(
- &auth_run_loop_, audio_manager_.get(), &mirroring_manager_,
- MediaInternals::GetInstance(), media_stream_manager_.get(),
- std::string());
-
- EXPECT_CALL(*host_, ShutdownForBadMessage()).Times(0);
+ &auth_run_loop_, render_process_host_.GetID(), audio_manager_.get(),
+ &mirroring_manager_, MediaInternals::GetInstance(),
+ media_stream_manager_.get(), std::string());
// Simulate IPC channel connected.
host_->set_peer_process_for_testing(base::Process::Current());
@@ -207,13 +225,15 @@ class AudioRendererHostTest : public testing::Test {
protected:
void Create() {
- Create(false, kDefaultDeviceId, url::Origin(GURL(kSecurityOrigin)), true);
+ Create(false, kDefaultDeviceId, url::Origin(GURL(kSecurityOrigin)), true,
+ true);
}
void Create(bool unified_stream,
const std::string& device_id,
const url::Origin& security_origin,
- bool wait_for_auth) {
+ bool wait_for_auth,
+ bool expect_onauthorized) {
media::OutputDeviceStatus expected_device_status =
device_id == kDefaultDeviceId
? media::OUTPUT_DEVICE_STATUS_OK
@@ -221,13 +241,14 @@ class AudioRendererHostTest : public testing::Test {
? media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED
: media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND;
- EXPECT_CALL(*host_.get(),
- OnDeviceAuthorized(kStreamId, expected_device_status, _, _));
+ if (expect_onauthorized)
+ EXPECT_CALL(*host_.get(),
+ OnDeviceAuthorized(kStreamId, expected_device_status, _, _));
if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) {
EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _));
- EXPECT_CALL(mirroring_manager_,
- AddDiverter(kRenderProcessId, kRenderFrameId, NotNull()))
+ EXPECT_CALL(mirroring_manager_, AddDiverter(render_process_host_.GetID(),
+ kRenderFrameId, NotNull()))
.RetiresOnSaturation();
}
@@ -269,7 +290,7 @@ class AudioRendererHostTest : public testing::Test {
}
void CreateWithoutWaitingForAuth(const std::string& device_id) {
- Create(false, device_id, url::Origin(GURL(kSecurityOrigin)), false);
+ Create(false, device_id, url::Origin(GURL(kSecurityOrigin)), false, true);
}
void CreateWithInvalidRenderFrameId() {
@@ -344,8 +365,10 @@ class AudioRendererHostTest : public testing::Test {
run_loop.Run();
}
- void ExpectShutdown() {
- EXPECT_CALL(*host_, ShutdownForBadMessage()).Times(1);
+ void AssertBadMsgReported() {
+ // Bad messages can be reported either directly to the RPH or through the
+ // ARH, so we check both of them.
+ EXPECT_EQ(render_process_host_.bad_msg_count() + host_->bad_msg_count, 1);
}
private:
@@ -355,6 +378,8 @@ class AudioRendererHostTest : public testing::Test {
TestBrowserThreadBundle thread_bundle_;
media::ScopedAudioManagerPtr audio_manager_;
MockAudioMirroringManager mirroring_manager_;
+ TestBrowserContext browser_context_;
+ MockRenderProcessHostWithSignaling render_process_host_;
scoped_refptr<MockAudioRendererHost> host_;
base::RunLoop auth_run_loop_;
@@ -422,30 +447,33 @@ TEST_F(AudioRendererHostTest, SimulateErrorAndClose) {
}
TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) {
- Create(true, kDefaultDeviceId, url::Origin(GURL(kSecurityOrigin)), true);
+ Create(true, kDefaultDeviceId, url::Origin(GURL(kSecurityOrigin)), true,
+ true);
Close();
}
TEST_F(AudioRendererHostTest, CreateUnauthorizedDevice) {
- Create(false, kBadDeviceId, url::Origin(GURL(kSecurityOrigin)), true);
+ Create(false, kBadDeviceId, url::Origin(GURL(kSecurityOrigin)), true, true);
Close();
}
TEST_F(AudioRendererHostTest, CreateDeviceWithAuthorizationPendingIsError) {
- ExpectShutdown();
CreateWithoutWaitingForAuth(kBadDeviceId);
Close();
+ AssertBadMsgReported();
}
TEST_F(AudioRendererHostTest, CreateDeviceWithBadSecurityOrigin) {
- ExpectShutdown();
RequestDeviceAuthorizationWithBadOrigin(kNondefaultDeviceId);
Close();
+ AssertBadMsgReported();
}
TEST_F(AudioRendererHostTest, CreateInvalidDevice) {
- Create(false, kInvalidDeviceId, url::Origin(GURL(kSecurityOrigin)), true);
+ Create(false, kInvalidDeviceId, url::Origin(GURL(kSecurityOrigin)), true,
+ false);
Close();
+ AssertBadMsgReported();
}
TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) {

Powered by Google App Engine
This is Rietveld 408576698