1 // Copyright 2013 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 "base/command_line.h" 6 #include "base/file_util.h" 7 #include "base/path_service.h" 8 #include "base/strings/stringprintf.h" 9 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/content_settings/host_content_settings_map.h" 11 #include "chrome/browser/infobars/infobar.h" 12 #include "chrome/browser/infobars/infobar_service.h" 13 #include "chrome/browser/media/webrtc_browsertest_base.h" 14 #include "chrome/browser/media/webrtc_browsertest_common.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser_tabstrip.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/content_settings_types.h" 21 #include "chrome/test/base/in_process_browser_test.h" 22 #include "chrome/test/base/test_switches.h" 23 #include "chrome/test/base/ui_test_utils.h" 24 #include "chrome/test/ui/ui_test.h" 25 #include "content/public/browser/notification_service.h" 26 #include "content/public/test/browser_test_utils.h" 27 #include "net/test/spawned_test_server/spawned_test_server.h" 28 29 30 // MediaStreamInfoBarTest ----------------------------------------------------- 31 32 class MediaStreamInfoBarTest : public WebRtcTestBase { 33 public: 34 MediaStreamInfoBarTest() {} 35 virtual ~MediaStreamInfoBarTest() {} 36 37 // InProcessBrowserTest: 38 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 39 // This test expects to run with fake devices but real UI. 40 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); 41 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)) 42 << "Since this test tests the UI we want the real UI!"; 43 } 44 45 protected: 46 content::WebContents* LoadTestPageInTab() { 47 EXPECT_TRUE(test_server()->Start()); 48 49 const char kMainWebrtcTestHtmlPage[] = 50 "files/webrtc/webrtc_jsep01_test.html"; 51 ui_test_utils::NavigateToURL( 52 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage)); 53 return browser()->tab_strip_model()->GetActiveWebContents(); 54 } 55 56 private: 57 DISALLOW_COPY_AND_ASSIGN(MediaStreamInfoBarTest); 58 }; 59 60 61 // Actual tests --------------------------------------------------------------- 62 63 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestAllowingUserMedia) { 64 content::WebContents* tab_contents = LoadTestPageInTab(); 65 GetUserMediaAndAccept(tab_contents); 66 } 67 68 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestDenyingUserMedia) { 69 content::WebContents* tab_contents = LoadTestPageInTab(); 70 GetUserMediaAndDeny(tab_contents); 71 } 72 73 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestDismissingInfobar) { 74 content::WebContents* tab_contents = LoadTestPageInTab(); 75 GetUserMediaAndDismiss(tab_contents); 76 } 77 78 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, 79 TestAcceptThenDenyWhichShouldBeSticky) { 80 #if defined(OS_WIN) && defined(USE_ASH) 81 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 82 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 83 return; 84 #endif 85 86 content::WebContents* tab_contents = LoadTestPageInTab(); 87 88 GetUserMediaAndAccept(tab_contents); 89 GetUserMediaAndDeny(tab_contents); 90 91 // Should fail with permission denied right away with no infobar popping up. 92 GetUserMedia(tab_contents, kAudioVideoCallConstraints); 93 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", 94 kFailedWithErrorPermissionDenied, 95 tab_contents)); 96 InfoBarService* infobar_service = 97 InfoBarService::FromWebContents(tab_contents); 98 EXPECT_EQ(0u, infobar_service->infobar_count()); 99 } 100 101 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestAcceptIsNotSticky) { 102 content::WebContents* tab_contents = LoadTestPageInTab(); 103 104 // If accept were sticky the second call would hang because it hangs if an 105 // infobar does not pop up. 106 GetUserMediaAndAccept(tab_contents); 107 GetUserMediaAndAccept(tab_contents); 108 } 109 110 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestDismissIsNotSticky) { 111 content::WebContents* tab_contents = LoadTestPageInTab(); 112 113 // If dismiss were sticky the second call would hang because it hangs if an 114 // infobar does not pop up. 115 GetUserMediaAndDismiss(tab_contents); 116 GetUserMediaAndDismiss(tab_contents); 117 } 118 119 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, 120 TestDenyingThenClearingStickyException) { 121 content::WebContents* tab_contents = LoadTestPageInTab(); 122 123 GetUserMediaAndDeny(tab_contents); 124 125 HostContentSettingsMap* settings_map = 126 browser()->profile()->GetHostContentSettingsMap(); 127 128 settings_map->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); 129 settings_map->ClearSettingsForOneType( 130 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); 131 132 // If an infobar is not launched now, this will hang. 133 GetUserMediaAndDeny(tab_contents); 134 } 135 136 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, 137 DenyingMicDoesNotCauseStickyDenyForCameras) { 138 content::WebContents* tab_contents = LoadTestPageInTab(); 139 140 // If mic blocking also blocked cameras, the second call here would hang. 141 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, 142 kAudioOnlyCallConstraints); 143 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents, 144 kVideoOnlyCallConstraints); 145 } 146 147 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, 148 DenyingCameraDoesNotCauseStickyDenyForMics) { 149 content::WebContents* tab_contents = LoadTestPageInTab(); 150 151 // If camera blocking also blocked mics, the second call here would hang. 152 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, 153 kVideoOnlyCallConstraints); 154 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents, 155 kAudioOnlyCallConstraints); 156 } 157 158 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, 159 DenyingMicStillSucceedsWithCameraForAudioVideoCalls) { 160 content::WebContents* tab_contents = LoadTestPageInTab(); 161 162 // If microphone blocking also blocked a AV call, the second call here 163 // would hang. The requester should only be granted access to the cam though. 164 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, 165 kAudioOnlyCallConstraints); 166 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents, 167 kAudioVideoCallConstraints); 168 169 // TODO(phoglund): verify the requester actually only gets video tracks. 170 } 171