Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/test/scoped_feature_list.h" | |
| 7 #include "chrome/browser/engagement/site_engagement_score.h" | |
| 8 #include "chrome/browser/permissions/permission_request_manager.h" | |
| 9 #include "chrome/browser/ui/browser.h" | |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 11 #include "chrome/browser/ui/website_settings/mock_permission_prompt_factory.h" | |
| 12 #include "chrome/common/chrome_features.h" | |
| 13 #include "chrome/test/base/in_process_browser_test.h" | |
| 14 #include "chrome/test/base/ui_test_utils.h" | |
| 15 #include "content/public/common/content_switches.h" | |
| 16 #include "content/public/test/browser_test_utils.h" | |
| 17 #include "content/public/test/ppapi_test_utils.h" | |
| 18 #include "content/public/test/test_utils.h" | |
| 19 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 20 #include "ppapi/shared_impl/ppapi_switches.h" | |
| 21 | |
| 22 // This is a class which defines how an individual feature that has a permission | |
| 23 // should be tested. Basically there are two functions that should be overriden | |
| 24 // for each feature: (1) TriggerPrompt which should do whatever is needed to | |
| 25 // cause a permission prompt for the feature; and (2) FeatureUsageSucceeds which | |
| 26 // should return true if the feature works (i.e. has permission). Typically | |
| 27 // these functions would call into javascript on the current page to manipulate | |
| 28 // the page in a way which tests these things. | |
| 29 class PermissionTestConfig { | |
|
tommycli
2016/09/26 22:54:49
The abstraction seems good - and I don't know perm
raymes
2016/09/27 04:05:41
Yes we'll definitely reuse it - I have an outstand
| |
| 30 public: | |
| 31 virtual void TriggerPrompt() = 0; | |
| 32 virtual bool FeatureUsageSucceeds() = 0; | |
| 33 | |
| 34 std::string test_name() const { return test_name_; } | |
| 35 std::string test_url() const { return test_url_; } | |
| 36 | |
| 37 void InitBrowser(Browser* browser) { | |
| 38 DCHECK(!browser_); | |
| 39 browser_ = browser; | |
| 40 } | |
| 41 | |
| 42 virtual void SetUpCommandLine(base::CommandLine* command_line) {} | |
| 43 virtual void SetUpOnMainThread() {} | |
| 44 | |
| 45 protected: | |
| 46 explicit PermissionTestConfig(const std::string& test_name, | |
| 47 const std::string& test_url) | |
| 48 : test_name_(test_name), test_url_(test_url), browser_(nullptr) {} | |
| 49 | |
| 50 bool RunScriptReturnBool(const std::string& script) { | |
| 51 bool result; | |
| 52 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
| 53 GetWebContents()->GetMainFrame(), script, &result)); | |
| 54 return result; | |
| 55 } | |
| 56 | |
| 57 Browser* browser() { return browser_; } | |
| 58 | |
| 59 content::WebContents* GetWebContents() { | |
| 60 return browser()->tab_strip_model()->GetActiveWebContents(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 std::string test_name_; | |
| 65 std::string test_url_; | |
| 66 Browser* browser_; | |
| 67 }; | |
| 68 | |
| 69 class FlashPermissionTestConfig : public PermissionTestConfig { | |
| 70 public: | |
| 71 FlashPermissionTestConfig() | |
| 72 : PermissionTestConfig("Flash", "permissions/flash.html") {} | |
| 73 | |
| 74 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 75 command_line->AppendSwitch(switches::kEnablePepperTesting); | |
|
tommycli
2016/09/26 22:54:49
Would recommend inlining kEnablePepperTesting with
raymes
2016/09/27 04:05:41
Done.
| |
| 76 command_line->AppendSwitch(switches::kEnablePluginPlaceholderTesting); | |
| 77 command_line->AppendSwitchASCII( | |
| 78 switches::kOverridePluginPowerSaverForTesting, "ignore-list"); | |
|
tommycli
2016/09/26 22:54:49
ignore-list unneeded if we're registering the plug
raymes
2016/09/27 04:05:41
Done.
| |
| 79 | |
| 80 ASSERT_TRUE(ppapi::RegisterFlashTestPlugin(command_line)); | |
| 81 | |
| 82 feature_list_.InitAndEnableFeature(features::kPreferHtmlOverPlugins); | |
| 83 } | |
| 84 | |
| 85 void SetUpOnMainThread() override { | |
| 86 SiteEngagementScore::SetParamValuesForTesting(); | |
| 87 } | |
| 88 | |
| 89 void TriggerPrompt() override { | |
| 90 EXPECT_TRUE(RunScriptReturnBool("triggerPrompt();")); | |
| 91 } | |
| 92 | |
| 93 bool FeatureUsageSucceeds() override { | |
| 94 // Flash won't be enabled until the page is refreshed. | |
| 95 ui_test_utils::NavigateToURL(browser(), | |
| 96 GetWebContents()->GetLastCommittedURL()); | |
| 97 return RunScriptReturnBool("flashIsEnabled();"); | |
| 98 } | |
| 99 | |
| 100 base::test::ScopedFeatureList feature_list_; | |
| 101 }; | |
| 102 | |
| 103 // This is a generic test for testing features that have permissions. It will | |
| 104 // test cases such as what will happen if a permission prompt is | |
| 105 // allowed/denied/dismissed for a feature. | |
| 106 class PermissionsBrowserTest : public InProcessBrowserTest { | |
| 107 public: | |
| 108 // TODO(raymes): paramaterize this to work with multiple configs. | |
| 109 PermissionsBrowserTest() : current_test_(new FlashPermissionTestConfig) {} | |
| 110 | |
| 111 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 112 current_test_->SetUpCommandLine(command_line); | |
| 113 } | |
| 114 | |
| 115 void SetUpInProcessBrowserTestFixture() override {} | |
|
dominickn
2016/09/26 07:28:59
Nit: remove the do-nothing override?
raymes
2016/09/27 04:05:41
Done.
| |
| 116 | |
| 117 void SetUpOnMainThread() override { | |
| 118 InProcessBrowserTest::SetUpOnMainThread(); | |
| 119 | |
| 120 PermissionRequestManager* manager = | |
| 121 PermissionRequestManager::FromWebContents( | |
| 122 browser()->tab_strip_model()->GetActiveWebContents()); | |
| 123 prompt_factory_.reset(new MockPermissionPromptFactory(manager)); | |
| 124 manager->DisplayPendingRequests(); | |
| 125 | |
| 126 current_test_->InitBrowser(browser()); | |
| 127 current_test_->SetUpOnMainThread(); | |
| 128 | |
| 129 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 130 | |
| 131 ui_test_utils::NavigateToURL( | |
| 132 browser(), embedded_test_server()->GetURL(current_test_->test_url())); | |
| 133 | |
| 134 LOG(INFO) << "Running test for: " << current_test_->test_name(); | |
|
dominickn
2016/09/26 07:28:59
Nit: remove LOG
raymes
2016/09/27 04:05:41
The log will be important when there is a paramate
| |
| 135 } | |
| 136 | |
| 137 void TearDownOnMainThread() override { | |
| 138 prompt_factory_.reset(); | |
| 139 | |
| 140 InProcessBrowserTest::TearDownOnMainThread(); | |
| 141 } | |
| 142 | |
| 143 MockPermissionPromptFactory* prompt_factory() { | |
| 144 return prompt_factory_.get(); | |
| 145 } | |
| 146 | |
| 147 PermissionTestConfig* current_test() { return current_test_.get(); } | |
| 148 | |
| 149 private: | |
| 150 std::unique_ptr<MockPermissionPromptFactory> prompt_factory_; | |
| 151 std::unique_ptr<PermissionTestConfig> current_test_; | |
| 152 }; | |
| 153 | |
| 154 IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest, FailsBeforeRequesting) { | |
| 155 // Dismiss any prompts if they are shown when using the feature. | |
| 156 prompt_factory()->set_response_type(PermissionRequestManager::DISMISS); | |
|
dominickn
2016/09/26 07:28:59
Nit: check that the current count is 0?
raymes
2016/09/27 04:05:41
Done.
| |
| 157 EXPECT_FALSE(current_test()->FeatureUsageSucceeds()); | |
| 158 } | |
| 159 | |
| 160 IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest, FailsIfDismissed) { | |
| 161 prompt_factory()->set_response_type(PermissionRequestManager::DISMISS); | |
| 162 current_test()->TriggerPrompt(); | |
| 163 | |
| 164 EXPECT_EQ(1, prompt_factory()->total_request_count()); | |
| 165 EXPECT_FALSE(current_test()->FeatureUsageSucceeds()); | |
| 166 } | |
| 167 | |
| 168 IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest, FailsIfBlocked) { | |
| 169 prompt_factory()->set_response_type(PermissionRequestManager::DENY_ALL); | |
| 170 current_test()->TriggerPrompt(); | |
| 171 | |
| 172 EXPECT_EQ(1, prompt_factory()->total_request_count()); | |
| 173 EXPECT_FALSE(current_test()->FeatureUsageSucceeds()); | |
| 174 } | |
| 175 | |
| 176 IN_PROC_BROWSER_TEST_F(PermissionsBrowserTest, SucceedsIfAllowed) { | |
| 177 prompt_factory()->set_response_type(PermissionRequestManager::ACCEPT_ALL); | |
| 178 current_test()->TriggerPrompt(); | |
| 179 | |
| 180 EXPECT_EQ(1, prompt_factory()->total_request_count()); | |
| 181 EXPECT_TRUE(current_test()->FeatureUsageSucceeds()); | |
| 182 } | |
| OLD | NEW |