| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/process_util.h" | 7 #include "base/process_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 static const char kMainWebrtcTestHtmlPage[] = | 34 static const char kMainWebrtcTestHtmlPage[] = |
| 35 "files/webrtc/webrtc_jsep01_test.html"; | 35 "files/webrtc/webrtc_jsep01_test.html"; |
| 36 static const char kFailedWithErrorPermissionDenied[] = | 36 static const char kFailedWithErrorPermissionDenied[] = |
| 37 "failed-with-error-PERMISSION_DENIED"; | 37 "failed-with-error-PERMISSION_DENIED"; |
| 38 | 38 |
| 39 static const char kAudioVideoCallConstraints[] = "'{audio: true, video: true}'"; | 39 static const char kAudioVideoCallConstraints[] = "'{audio: true, video: true}'"; |
| 40 static const char kAudioOnlyCallConstraints[] = "'{audio: true}'"; | 40 static const char kAudioOnlyCallConstraints[] = "'{audio: true}'"; |
| 41 static const char kVideoOnlyCallConstraints[] = "'{video: true}'"; | 41 static const char kVideoOnlyCallConstraints[] = "'{video: true}'"; |
| 42 static const char kOkGotStream[] = "ok-got-stream"; | 42 static const char kOkGotStream[] = "ok-got-stream"; |
| 43 | 43 |
| 44 // Media stream infobar test for WebRTC. | 44 |
| 45 // MediaStreamInfoBarTest ----------------------------------------------------- |
| 46 |
| 45 class MediaStreamInfobarTest : public InProcessBrowserTest { | 47 class MediaStreamInfobarTest : public InProcessBrowserTest { |
| 46 public: | 48 public: |
| 47 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 49 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; |
| 48 // This test expects to run with fake devices but real UI. | 50 |
| 49 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); | |
| 50 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)) | |
| 51 << "Since this test tests the UI we want the real UI!"; | |
| 52 } | |
| 53 protected: | 51 protected: |
| 54 content::WebContents* LoadTestPageInTab() { | 52 content::WebContents* LoadTestPageInTab(); |
| 55 EXPECT_TRUE(test_server()->Start()); | |
| 56 | |
| 57 ui_test_utils::NavigateToURL( | |
| 58 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage)); | |
| 59 return browser()->tab_strip_model()->GetActiveWebContents(); | |
| 60 } | |
| 61 | 53 |
| 62 // TODO(phoglund): upstream and reuse in other browser tests. | 54 // TODO(phoglund): upstream and reuse in other browser tests. |
| 63 MediaStreamInfoBarDelegate* GetUserMediaAndWaitForInfobar( | 55 MediaStreamInfoBarDelegate* GetUserMediaAndWaitForInfobar( |
| 64 content::WebContents* tab_contents, | 56 content::WebContents* tab_contents, |
| 65 const std::string& constraints) { | 57 const std::string& constraints); |
| 66 content::WindowedNotificationObserver infobar_added( | |
| 67 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
| 68 content::NotificationService::AllSources()); | |
| 69 | |
| 70 // Request user media: this will launch the media stream info bar. | |
| 71 GetUserMedia(constraints, tab_contents); | |
| 72 | |
| 73 // Wait for the bar to pop up, then return it | |
| 74 infobar_added.Wait(); | |
| 75 content::Details<InfoBarAddedDetails> details(infobar_added.details()); | |
| 76 MediaStreamInfoBarDelegate* media_infobar = | |
| 77 details.ptr()->AsMediaStreamInfoBarDelegate(); | |
| 78 return media_infobar; | |
| 79 } | |
| 80 | 58 |
| 81 void CloseInfobarInTab(content::WebContents* tab_contents, | 59 void CloseInfobarInTab(content::WebContents* tab_contents, |
| 82 MediaStreamInfoBarDelegate* infobar) { | 60 MediaStreamInfoBarDelegate* infobar); |
| 83 content::WindowedNotificationObserver infobar_removed( | |
| 84 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 85 content::NotificationService::AllSources()); | |
| 86 | |
| 87 InfoBarService* infobar_service = | |
| 88 InfoBarService::FromWebContents(tab_contents); | |
| 89 infobar_service->RemoveInfoBar(infobar); | |
| 90 | |
| 91 infobar_removed.Wait(); | |
| 92 } | |
| 93 | 61 |
| 94 // Convenience method which executes the provided javascript in the context | 62 // Convenience method which executes the provided javascript in the context |
| 95 // of the provided web contents and returns what it evaluated to. | 63 // of the provided web contents and returns what it evaluated to. |
| 96 std::string ExecuteJavascript(const std::string& javascript, | 64 std::string ExecuteJavascript(const std::string& javascript, |
| 97 content::WebContents* tab_contents) { | 65 content::WebContents* tab_contents); |
| 98 std::string result; | |
| 99 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 100 tab_contents, javascript, &result)); | |
| 101 return result; | |
| 102 } | |
| 103 | 66 |
| 104 void TestAcceptOnInfobar(content::WebContents* tab_contents) { | 67 void TestAcceptOnInfobar(content::WebContents* tab_contents); |
| 105 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, | |
| 106 kAudioVideoCallConstraints); | |
| 107 } | |
| 108 | |
| 109 void TestAcceptOnInfobarWithSpecificConstraints( | 68 void TestAcceptOnInfobarWithSpecificConstraints( |
| 110 content::WebContents* tab_contents, const std::string& constraints) { | 69 content::WebContents* tab_contents, const std::string& constraints); |
| 111 MediaStreamInfoBarDelegate* media_infobar = | 70 void TestDenyOnInfobar(content::WebContents* tab_contents); |
| 112 GetUserMediaAndWaitForInfobar(tab_contents, constraints); | |
| 113 | |
| 114 media_infobar->Accept(); | |
| 115 | |
| 116 CloseInfobarInTab(tab_contents, media_infobar); | |
| 117 | |
| 118 // Wait for WebRTC to call the success callback. | |
| 119 EXPECT_TRUE(PollingWaitUntil( | |
| 120 "obtainGetUserMediaResult()", kOkGotStream, tab_contents)); | |
| 121 } | |
| 122 | |
| 123 void TestDenyOnInfobar(content::WebContents* tab_contents) { | |
| 124 return TestDenyWithSpecificConstraints(tab_contents, | |
| 125 kAudioVideoCallConstraints); | |
| 126 } | |
| 127 | |
| 128 void TestDenyWithSpecificConstraints(content::WebContents* tab_contents, | 71 void TestDenyWithSpecificConstraints(content::WebContents* tab_contents, |
| 129 const std::string& constraints) { | 72 const std::string& constraints); |
| 130 MediaStreamInfoBarDelegate* media_infobar = | 73 void TestDismissOnInfobar(content::WebContents* tab_contents); |
| 131 GetUserMediaAndWaitForInfobar(tab_contents, constraints); | |
| 132 | |
| 133 media_infobar->Cancel(); | |
| 134 | |
| 135 CloseInfobarInTab(tab_contents, media_infobar); | |
| 136 | |
| 137 // Wait for WebRTC to call the fail callback. | |
| 138 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", | |
| 139 kFailedWithErrorPermissionDenied, | |
| 140 tab_contents)); | |
| 141 } | |
| 142 | |
| 143 void TestDismissOnInfobar(content::WebContents* tab_contents) { | |
| 144 MediaStreamInfoBarDelegate* media_infobar = | |
| 145 GetUserMediaAndWaitForInfobar(tab_contents, kAudioVideoCallConstraints); | |
| 146 | |
| 147 media_infobar->InfoBarDismissed(); | |
| 148 | |
| 149 CloseInfobarInTab(tab_contents, media_infobar); | |
| 150 | |
| 151 // A dismiss should be treated like a deny. | |
| 152 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", | |
| 153 kFailedWithErrorPermissionDenied, | |
| 154 tab_contents)); | |
| 155 } | |
| 156 | 74 |
| 157 void GetUserMedia(const std::string& constraints, | 75 void GetUserMedia(const std::string& constraints, |
| 158 content::WebContents* tab_contents) { | 76 content::WebContents* tab_contents); |
| 159 // Request user media: this will launch the media stream info bar. | |
| 160 EXPECT_EQ("ok-requested", | |
| 161 ExecuteJavascript( | |
| 162 base::StringPrintf("getUserMedia(%s);", constraints.c_str()), | |
| 163 tab_contents)); | |
| 164 } | |
| 165 }; | 77 }; |
| 166 | 78 |
| 79 void MediaStreamInfobarTest::SetUpCommandLine(CommandLine* command_line) { |
| 80 // This test expects to run with fake devices but real UI. |
| 81 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); |
| 82 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)) |
| 83 << "Since this test tests the UI we want the real UI!"; |
| 84 } |
| 85 |
| 86 content::WebContents* MediaStreamInfobarTest::LoadTestPageInTab() { |
| 87 EXPECT_TRUE(test_server()->Start()); |
| 88 |
| 89 ui_test_utils::NavigateToURL( |
| 90 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage)); |
| 91 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 92 } |
| 93 |
| 94 MediaStreamInfoBarDelegate* |
| 95 MediaStreamInfobarTest::GetUserMediaAndWaitForInfobar( |
| 96 content::WebContents* tab_contents, |
| 97 const std::string& constraints) { |
| 98 content::WindowedNotificationObserver infobar_added( |
| 99 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 100 content::NotificationService::AllSources()); |
| 101 |
| 102 // Request user media: this will launch the media stream info bar. |
| 103 GetUserMedia(constraints, tab_contents); |
| 104 |
| 105 // Wait for the bar to pop up, then return it |
| 106 infobar_added.Wait(); |
| 107 content::Details<InfoBarAddedDetails> details(infobar_added.details()); |
| 108 MediaStreamInfoBarDelegate* media_infobar = |
| 109 details.ptr()->AsMediaStreamInfoBarDelegate(); |
| 110 return media_infobar; |
| 111 } |
| 112 |
| 113 void MediaStreamInfobarTest::CloseInfobarInTab( |
| 114 content::WebContents* tab_contents, |
| 115 MediaStreamInfoBarDelegate* infobar) { |
| 116 content::WindowedNotificationObserver infobar_removed( |
| 117 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 118 content::NotificationService::AllSources()); |
| 119 |
| 120 InfoBarService* infobar_service = |
| 121 InfoBarService::FromWebContents(tab_contents); |
| 122 infobar_service->RemoveInfoBar(infobar); |
| 123 |
| 124 infobar_removed.Wait(); |
| 125 } |
| 126 |
| 127 std::string MediaStreamInfobarTest::ExecuteJavascript( |
| 128 const std::string& javascript, |
| 129 content::WebContents* tab_contents) { |
| 130 std::string result; |
| 131 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 132 tab_contents, javascript, &result)); |
| 133 return result; |
| 134 } |
| 135 |
| 136 void MediaStreamInfobarTest::TestAcceptOnInfobar( |
| 137 content::WebContents* tab_contents) { |
| 138 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, |
| 139 kAudioVideoCallConstraints); |
| 140 } |
| 141 |
| 142 void MediaStreamInfobarTest::TestAcceptOnInfobarWithSpecificConstraints( |
| 143 content::WebContents* tab_contents, |
| 144 const std::string& constraints) { |
| 145 MediaStreamInfoBarDelegate* media_infobar = |
| 146 GetUserMediaAndWaitForInfobar(tab_contents, constraints); |
| 147 |
| 148 media_infobar->Accept(); |
| 149 |
| 150 CloseInfobarInTab(tab_contents, media_infobar); |
| 151 |
| 152 // Wait for WebRTC to call the success callback. |
| 153 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", kOkGotStream, |
| 154 tab_contents)); |
| 155 } |
| 156 |
| 157 void MediaStreamInfobarTest::TestDenyOnInfobar( |
| 158 content::WebContents* tab_contents) { |
| 159 return TestDenyWithSpecificConstraints(tab_contents, |
| 160 kAudioVideoCallConstraints); |
| 161 } |
| 162 |
| 163 void MediaStreamInfobarTest::TestDenyWithSpecificConstraints( |
| 164 content::WebContents* tab_contents, |
| 165 const std::string& constraints) { |
| 166 MediaStreamInfoBarDelegate* media_infobar = |
| 167 GetUserMediaAndWaitForInfobar(tab_contents, constraints); |
| 168 |
| 169 media_infobar->Cancel(); |
| 170 |
| 171 CloseInfobarInTab(tab_contents, media_infobar); |
| 172 |
| 173 // Wait for WebRTC to call the fail callback. |
| 174 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", |
| 175 kFailedWithErrorPermissionDenied, tab_contents)); |
| 176 } |
| 177 |
| 178 void MediaStreamInfobarTest::TestDismissOnInfobar( |
| 179 content::WebContents* tab_contents) { |
| 180 MediaStreamInfoBarDelegate* media_infobar = |
| 181 GetUserMediaAndWaitForInfobar(tab_contents, kAudioVideoCallConstraints); |
| 182 |
| 183 media_infobar->InfoBarDismissed(); |
| 184 |
| 185 CloseInfobarInTab(tab_contents, media_infobar); |
| 186 |
| 187 // A dismiss should be treated like a deny. |
| 188 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", |
| 189 kFailedWithErrorPermissionDenied, tab_contents)); |
| 190 } |
| 191 |
| 192 void MediaStreamInfobarTest::GetUserMedia(const std::string& constraints, |
| 193 content::WebContents* tab_contents) { |
| 194 // Request user media: this will launch the media stream info bar. |
| 195 EXPECT_EQ("ok-requested", |
| 196 ExecuteJavascript( |
| 197 base::StringPrintf("getUserMedia(%s);", constraints.c_str()), |
| 198 tab_contents)); |
| 199 } |
| 200 |
| 201 |
| 202 // Actual tests --------------------------------------------------------------- |
| 203 |
| 167 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestAllowingUserMedia) { | 204 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestAllowingUserMedia) { |
| 168 content::WebContents* tab_contents = LoadTestPageInTab(); | 205 content::WebContents* tab_contents = LoadTestPageInTab(); |
| 169 TestAcceptOnInfobar(tab_contents); | 206 TestAcceptOnInfobar(tab_contents); |
| 170 } | 207 } |
| 171 | 208 |
| 172 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDenyingUserMedia) { | 209 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDenyingUserMedia) { |
| 173 content::WebContents* tab_contents = LoadTestPageInTab(); | 210 content::WebContents* tab_contents = LoadTestPageInTab(); |
| 174 TestDenyOnInfobar(tab_contents); | 211 TestDenyOnInfobar(tab_contents); |
| 175 } | 212 } |
| 176 | 213 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 content::WebContents* tab_contents = LoadTestPageInTab(); | 300 content::WebContents* tab_contents = LoadTestPageInTab(); |
| 264 | 301 |
| 265 // If microphone blocking also blocked a AV call, the second call here | 302 // If microphone blocking also blocked a AV call, the second call here |
| 266 // would hang. The requester should only be granted access to the cam though. | 303 // would hang. The requester should only be granted access to the cam though. |
| 267 TestDenyWithSpecificConstraints(tab_contents, kAudioOnlyCallConstraints); | 304 TestDenyWithSpecificConstraints(tab_contents, kAudioOnlyCallConstraints); |
| 268 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, | 305 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, |
| 269 kAudioVideoCallConstraints); | 306 kAudioVideoCallConstraints); |
| 270 | 307 |
| 271 // TODO(phoglund): verify the requester actually only gets video tracks. | 308 // TODO(phoglund): verify the requester actually only gets video tracks. |
| 272 } | 309 } |
| OLD | NEW |