OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/path_service.h" | |
7 #include "chrome/browser/extensions/extension_apitest.h" | |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
9 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | |
10 #include "chrome/common/chrome_paths.h" | |
11 #include "chrome/test/base/ui_test_utils.h" | |
12 #include "extensions/test/result_catcher.h" | |
13 #include "media/base/media_switches.h" | |
14 #include "net/dns/mock_host_resolver.h" | |
15 #include "net/test/embedded_test_server/embedded_test_server.h" | |
16 | |
17 namespace extensions { | |
18 | |
19 namespace { | |
20 | |
21 // Used to observe the creation of permission prompt without responding. | |
22 class PermissionRequestObserver : public PermissionBubbleManager::Observer { | |
23 public: | |
24 explicit PermissionRequestObserver(content::WebContents* web_contents) | |
25 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents)), | |
26 request_shown_(false) { | |
27 bubble_manager_->AddObserver(this); | |
28 } | |
29 ~PermissionRequestObserver() override { | |
30 // Safe to remove twice if it happens. | |
31 bubble_manager_->RemoveObserver(this); | |
32 } | |
33 | |
34 bool request_shown() const { return request_shown_; } | |
35 | |
36 private: | |
37 // PermissionBubbleManager::Observer | |
38 void OnBubbleAdded() override { | |
39 request_shown_ = true; | |
40 bubble_manager_->RemoveObserver(this); | |
41 } | |
42 | |
43 PermissionBubbleManager* bubble_manager_; | |
44 bool request_shown_; | |
45 | |
46 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver); | |
47 }; | |
48 | |
49 } // namespace | |
50 | |
51 class WebRtcFromWebAccessibleResourceTest : public ExtensionApiTest { | |
52 public: | |
53 WebRtcFromWebAccessibleResourceTest() {} | |
54 ~WebRtcFromWebAccessibleResourceTest() override {} | |
55 | |
56 // InProcessBrowserTest: | |
57 void SetUpCommandLine(base::CommandLine* command_line) override { | |
58 ExtensionApiTest::SetUpCommandLine(command_line); | |
59 | |
60 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); | |
61 } | |
62 | |
63 protected: | |
64 GURL GetTestServerInsecureUrl(const std::string& path) { | |
65 GURL url = embedded_test_server()->GetURL(path); | |
66 | |
67 GURL::Replacements replace_host_and_scheme; | |
68 replace_host_and_scheme.SetHostStr("a.com"); | |
69 replace_host_and_scheme.SetSchemeStr("http"); | |
70 url = url.ReplaceComponents(replace_host_and_scheme); | |
71 | |
72 return url; | |
73 } | |
74 | |
75 void LoadTestExtension() { | |
76 base::FilePath extension_path; | |
77 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path)); | |
78 extension_path = extension_path.AppendASCII("extensions") | |
79 .AppendASCII("api_test") | |
80 .AppendASCII("webrtc_from_web_accessible_resource"); | |
81 LoadExtension(extension_path); | |
robwu
2015/10/15 22:52:45
The whole function (or its callers) can be replace
jww
2015/10/15 23:06:02
Ah, cool, that's much better :-)
| |
82 } | |
83 | |
84 private: | |
85 DISALLOW_COPY_AND_ASSIGN(WebRtcFromWebAccessibleResourceTest); | |
86 }; | |
87 | |
88 // Verify that a chrome-extension:// web accessible URL can successfully access | |
89 // getUserMedia(), even if it is embedded in an insecure context. | |
90 IN_PROC_BROWSER_TEST_F(WebRtcFromWebAccessibleResourceTest, | |
91 GetUserMediaInWebAccessibleResourceSuccess) { | |
92 host_resolver()->AddRule("a.com", "127.0.0.1"); | |
93 ASSERT_TRUE(StartEmbeddedTestServer()); | |
94 | |
95 LoadTestExtension(); | |
96 GURL url = GetTestServerInsecureUrl("/extensions/test_file.html?succeed"); | |
97 content::WebContents* web_contents = | |
98 browser()->tab_strip_model()->GetActiveWebContents(); | |
99 PermissionBubbleManager* bubble_manager = | |
100 PermissionBubbleManager::FromWebContents(web_contents); | |
101 bubble_manager->set_auto_response_for_test( | |
102 PermissionBubbleManager::ACCEPT_ALL); | |
103 PermissionRequestObserver permission_request_observer(web_contents); | |
104 extensions::ResultCatcher catcher; | |
105 ui_test_utils::NavigateToURL(browser(), url); | |
106 | |
107 ASSERT_TRUE(catcher.GetNextResult()); | |
108 EXPECT_TRUE(permission_request_observer.request_shown()); | |
109 } | |
110 | |
111 // Verify that a chrome-extension:// web accessible URL will fail to access | |
112 // getUserMedia() if it is denied by the permission bubble, even if it is | |
113 // embedded in an insecure context. | |
114 IN_PROC_BROWSER_TEST_F(WebRtcFromWebAccessibleResourceTest, | |
115 GetUserMediaInWebAccessibleResourceFail) { | |
116 host_resolver()->AddRule("a.com", "127.0.0.1"); | |
117 ASSERT_TRUE(StartEmbeddedTestServer()); | |
118 | |
119 LoadTestExtension(); | |
120 GURL url = GetTestServerInsecureUrl("/extensions/test_file.html?fail"); | |
121 content::WebContents* web_contents = | |
122 browser()->tab_strip_model()->GetActiveWebContents(); | |
123 PermissionBubbleManager* bubble_manager = | |
124 PermissionBubbleManager::FromWebContents(web_contents); | |
125 bubble_manager->set_auto_response_for_test(PermissionBubbleManager::DENY_ALL); | |
126 PermissionRequestObserver permission_request_observer(web_contents); | |
127 extensions::ResultCatcher catcher; | |
128 ui_test_utils::NavigateToURL(browser(), url); | |
129 | |
130 ASSERT_TRUE(catcher.GetNextResult()); | |
131 EXPECT_TRUE(permission_request_observer.request_shown()); | |
132 } | |
133 | |
134 } // namespace extensions | |
OLD | NEW |