| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/base_switches.h" | 5 #include "base/base_switches.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "chrome/browser/permissions/permissions_browsertest.h" | 8 #include "chrome/browser/permissions/permissions_browsertest.h" |
| 9 #include "chrome/browser/ui/website_settings/mock_permission_prompt_factory.h" | 9 #include "chrome/browser/ui/website_settings/mock_permission_prompt_factory.h" |
| 10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
| 11 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
| 12 #include "components/variations/variations_switches.h" | 12 #include "components/variations/variations_switches.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/test/browser_test_utils.h" | 15 #include "content/public/test/browser_test_utils.h" |
| 16 #include "content/public/test/ppapi_test_utils.h" | 16 #include "content/public/test/ppapi_test_utils.h" |
| 17 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 17 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace { |
| 21 |
| 22 class PageReloadWaiter { |
| 23 public: |
| 24 explicit PageReloadWaiter(content::WebContents* web_contents) |
| 25 : web_contents_(web_contents), |
| 26 navigation_observer_(web_contents, |
| 27 web_contents->GetLastCommittedURL()) {} |
| 28 |
| 29 bool Wait() { |
| 30 navigation_observer_.WaitForNavigationFinished(); |
| 31 return content::WaitForLoadStop(web_contents_); |
| 32 } |
| 33 |
| 34 private: |
| 35 content::WebContents* web_contents_; |
| 36 content::TestNavigationManager navigation_observer_; |
| 37 }; |
| 38 |
| 39 } // namespace |
| 40 |
| 20 class FlashPermissionBrowserTest : public PermissionsBrowserTest { | 41 class FlashPermissionBrowserTest : public PermissionsBrowserTest { |
| 21 public: | 42 public: |
| 22 FlashPermissionBrowserTest() | 43 FlashPermissionBrowserTest() |
| 23 : PermissionsBrowserTest("/permissions/flash.html") {} | 44 : PermissionsBrowserTest("/permissions/flash.html") {} |
| 24 ~FlashPermissionBrowserTest() override {} | 45 ~FlashPermissionBrowserTest() override {} |
| 25 | 46 |
| 26 // PermissionsBrowserTest | 47 // PermissionsBrowserTest |
| 27 void SetUpCommandLine(base::CommandLine* command_line) override { | 48 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 28 PermissionsBrowserTest::SetUpCommandLine(command_line); | 49 PermissionsBrowserTest::SetUpCommandLine(command_line); |
| 29 | 50 |
| 30 ASSERT_TRUE(ppapi::RegisterFlashTestPlugin(command_line)); | 51 ASSERT_TRUE(ppapi::RegisterFlashTestPlugin(command_line)); |
| 31 | 52 |
| 32 // Set a high engagement threshhold so it doesn't interfere with testing the | 53 // Set a high engagement threshhold so it doesn't interfere with testing the |
| 33 // permission. | 54 // permission. |
| 34 command_line->AppendSwitchASCII(switches::kEnableFeatures, | 55 command_line->AppendSwitchASCII(switches::kEnableFeatures, |
| 35 "PreferHtmlOverPlugins<Study1"); | 56 "PreferHtmlOverPlugins<Study1"); |
| 36 command_line->AppendSwitchASCII(switches::kForceFieldTrials, | 57 command_line->AppendSwitchASCII(switches::kForceFieldTrials, |
| 37 "Study1/Enabled/"); | 58 "Study1/Enabled/"); |
| 38 command_line->AppendSwitchASCII( | 59 command_line->AppendSwitchASCII( |
| 39 variations::switches::kForceFieldTrialParams, | 60 variations::switches::kForceFieldTrialParams, |
| 40 "Study1.Enabled:engagement_threshold_for_flash/100"); | 61 "Study1.Enabled:engagement_threshold_for_flash/100"); |
| 41 } | 62 } |
| 42 | 63 |
| 43 void TriggerPrompt() override { | 64 void TriggerPrompt() override { |
| 44 if (prompt_factory()->response_type() == | 65 if (prompt_factory()->response_type() == |
| 45 PermissionRequestManager::ACCEPT_ALL) { | 66 PermissionRequestManager::ACCEPT_ALL) { |
| 46 // If the prompt will be allowed, we need to wait for the page to refresh. | 67 // If the prompt will be allowed, we need to wait for the page to refresh. |
| 47 content::TestNavigationManager observer( | 68 PageReloadWaiter reload_waiter(GetWebContents()); |
| 48 GetWebContents(), GetWebContents()->GetLastCommittedURL()); | |
| 49 EXPECT_TRUE(RunScriptReturnBool("triggerPrompt();")); | 69 EXPECT_TRUE(RunScriptReturnBool("triggerPrompt();")); |
| 50 observer.WaitForNavigationFinished(); | 70 EXPECT_TRUE(reload_waiter.Wait()); |
| 51 } else { | 71 } else { |
| 52 EXPECT_TRUE(RunScriptReturnBool("triggerPrompt();")); | 72 EXPECT_TRUE(RunScriptReturnBool("triggerPrompt();")); |
| 53 } | 73 } |
| 54 } | 74 } |
| 55 | 75 |
| 56 bool FeatureUsageSucceeds() override { | 76 bool FeatureUsageSucceeds() override { |
| 57 // Wait until the page is refreshed before testing whether flash is enabled | 77 // If flash should have been blocked, reload the page to be sure that it is |
| 58 // or disabled. | 78 // blocked. |
| 79 // |
| 80 // NB: In cases where flash is allowed the page reloads automatically, |
| 81 // and tests should always wait for that reload to finish before calling |
| 82 // this method. |
| 59 ui_test_utils::NavigateToURL(browser(), | 83 ui_test_utils::NavigateToURL(browser(), |
| 60 GetWebContents()->GetLastCommittedURL()); | 84 GetWebContents()->GetLastCommittedURL()); |
| 61 // If either flash with or without fallback content runs successfully it | 85 // If either flash with or without fallback content runs successfully it |
| 62 // indicates the feature is at least partly working, which could imply a | 86 // indicates the feature is at least partly working, which could imply a |
| 63 // faulty permission. | 87 // faulty permission. |
| 64 return RunScriptReturnBool("flashIsEnabled();") || | 88 return RunScriptReturnBool("flashIsEnabled();") || |
| 65 RunScriptReturnBool("flashIsEnabledForPluginWithoutFallback();"); | 89 RunScriptReturnBool("flashIsEnabledForPluginWithoutFallback();"); |
| 66 } | 90 } |
| 67 }; | 91 }; |
| 68 | 92 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 79 CommonFailsIfBlocked(); | 103 CommonFailsIfBlocked(); |
| 80 } | 104 } |
| 81 | 105 |
| 82 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, CommonSucceedsIfAllowed) { | 106 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, CommonSucceedsIfAllowed) { |
| 83 CommonSucceedsIfAllowed(); | 107 CommonSucceedsIfAllowed(); |
| 84 } | 108 } |
| 85 | 109 |
| 86 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, TriggerPromptViaNewWindow) { | 110 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, TriggerPromptViaNewWindow) { |
| 87 EXPECT_EQ(0, prompt_factory()->total_request_count()); | 111 EXPECT_EQ(0, prompt_factory()->total_request_count()); |
| 88 prompt_factory()->set_response_type(PermissionRequestManager::ACCEPT_ALL); | 112 prompt_factory()->set_response_type(PermissionRequestManager::ACCEPT_ALL); |
| 113 // FlashPermissionContext::UpdateTabContext will reload the page, we'll have |
| 114 // to wait until it is ready. |
| 115 PageReloadWaiter reload_waiter(GetWebContents()); |
| 89 EXPECT_TRUE(RunScriptReturnBool("triggerPromptViaNewWindow();")); | 116 EXPECT_TRUE(RunScriptReturnBool("triggerPromptViaNewWindow();")); |
| 117 EXPECT_TRUE(reload_waiter.Wait()); |
| 90 | 118 |
| 91 EXPECT_TRUE(FeatureUsageSucceeds()); | 119 EXPECT_TRUE(FeatureUsageSucceeds()); |
| 92 EXPECT_EQ(1, prompt_factory()->total_request_count()); | 120 EXPECT_EQ(1, prompt_factory()->total_request_count()); |
| 93 } | 121 } |
| 94 | 122 |
| 95 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, | 123 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, |
| 96 TriggerPromptViaPluginPlaceholder) { | 124 TriggerPromptViaPluginPlaceholder) { |
| 97 EXPECT_EQ(0, prompt_factory()->total_request_count()); | 125 EXPECT_EQ(0, prompt_factory()->total_request_count()); |
| 98 EXPECT_FALSE(FeatureUsageSucceeds()); | 126 EXPECT_FALSE(FeatureUsageSucceeds()); |
| 99 prompt_factory()->set_response_type(PermissionRequestManager::ACCEPT_ALL); | 127 prompt_factory()->set_response_type(PermissionRequestManager::ACCEPT_ALL); |
| 100 // We need to simulate a mouse click to trigger the placeholder to prompt. | 128 // We need to simulate a mouse click to trigger the placeholder to prompt. |
| 101 content::TestNavigationManager observer( | 129 // When the prompt is auto-accepted, the page will be reloaded. |
| 102 GetWebContents(), GetWebContents()->GetLastCommittedURL()); | 130 PageReloadWaiter reload_waiter(GetWebContents()); |
| 103 content::SimulateMouseClickAt(GetWebContents(), 0 /* modifiers */, | 131 content::SimulateMouseClickAt(GetWebContents(), 0 /* modifiers */, |
| 104 blink::WebMouseEvent::Button::Left, | 132 blink::WebMouseEvent::Button::Left, |
| 105 gfx::Point(50, 50)); | 133 gfx::Point(50, 50)); |
| 106 observer.WaitForNavigationFinished(); | 134 EXPECT_TRUE(reload_waiter.Wait()); |
| 107 | 135 |
| 108 EXPECT_TRUE(FeatureUsageSucceeds()); | 136 EXPECT_TRUE(FeatureUsageSucceeds()); |
| 109 EXPECT_EQ(1, prompt_factory()->total_request_count()); | 137 EXPECT_EQ(1, prompt_factory()->total_request_count()); |
| 110 } | 138 } |
| 111 | 139 |
| 112 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, AllowFileURL) { | 140 IN_PROC_BROWSER_TEST_F(FlashPermissionBrowserTest, AllowFileURL) { |
| 113 base::FilePath test_path; | 141 base::FilePath test_path; |
| 114 PathService::Get(chrome::DIR_TEST_DATA, &test_path); | 142 PathService::Get(chrome::DIR_TEST_DATA, &test_path); |
| 115 ui_test_utils::NavigateToURL( | 143 ui_test_utils::NavigateToURL( |
| 116 browser(), GURL("file://" + test_path.AsUTF8Unsafe() + test_url())); | 144 browser(), GURL("file://" + test_path.AsUTF8Unsafe() + test_url())); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 131 browser(), GURL("file://" + test_path.AsUTF8Unsafe() + test_url())); | 159 browser(), GURL("file://" + test_path.AsUTF8Unsafe() + test_url())); |
| 132 CommonFailsIfBlocked(); | 160 CommonFailsIfBlocked(); |
| 133 EXPECT_EQ(1, prompt_factory()->total_request_count()); | 161 EXPECT_EQ(1, prompt_factory()->total_request_count()); |
| 134 | 162 |
| 135 // Navigate to a second URL to verify it's blocked on all file: URLs. | 163 // Navigate to a second URL to verify it's blocked on all file: URLs. |
| 136 ui_test_utils::NavigateToURL( | 164 ui_test_utils::NavigateToURL( |
| 137 browser(), | 165 browser(), |
| 138 GURL("file://" + test_path.AsUTF8Unsafe() + "/permissions/flash2.html")); | 166 GURL("file://" + test_path.AsUTF8Unsafe() + "/permissions/flash2.html")); |
| 139 EXPECT_FALSE(FeatureUsageSucceeds()); | 167 EXPECT_FALSE(FeatureUsageSucceeds()); |
| 140 } | 168 } |
| OLD | NEW |