Chromium Code Reviews| Index: chrome/browser/services/gcm/push_messaging_browsertest.cc |
| diff --git a/chrome/browser/services/gcm/push_messaging_browsertest.cc b/chrome/browser/services/gcm/push_messaging_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5f2ce80a02553c959ac7b7d546d75eac3a8cce99 |
| --- /dev/null |
| +++ b/chrome/browser/services/gcm/push_messaging_browsertest.cc |
| @@ -0,0 +1,145 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| + |
| +#include "base/command_line.h" |
| +#include "chrome/browser/infobars/infobar_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| +#include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| +#include "chrome/browser/services/gcm/push_messaging_application_id.h" |
| +#include "chrome/browser/services/gcm/push_messaging_constants.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "components/infobars/core/confirm_infobar_delegate.h" |
| +#include "components/infobars/core/infobar.h" |
| +#include "components/infobars/core/infobar_manager.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "content/public/test/browser_test_utils.h" |
| + |
| +namespace { |
| +// Responds to a confirm infobar by accepting or cancelling it. Responds to at |
| +// most one infobar, as it removes itself when it first observes an infobar. |
| +class InfoBarResponder : public infobars::InfoBarManager::Observer { |
| + public: |
| + InfoBarResponder(Browser* browser, bool accept) |
| + : infobar_service_(InfoBarService::FromWebContents( |
| + browser->tab_strip_model()->GetActiveWebContents())), |
| + accept_(accept) { |
| + infobar_service_->AddObserver(this); |
| + } |
| + |
| + virtual ~InfoBarResponder() { infobar_service_->RemoveObserver(this); } |
| + |
| + // infobars::InfoBarManager::Observer |
| + virtual void OnInfoBarAdded(infobars::InfoBar* infobar) override { |
| + infobar_service_->RemoveObserver(this); |
| + ConfirmInfoBarDelegate* delegate = |
| + infobar->delegate()->AsConfirmInfoBarDelegate(); |
| + DCHECK(delegate); |
| + if (accept_) |
| + delegate->Accept(); |
| + else |
| + delegate->Cancel(); |
| + } |
| + |
| + private: |
| + InfoBarService* infobar_service_; |
| + bool accept_; |
| +}; |
| +} // namespace |
| + |
| +class PushMessagingBrowserTest : public InProcessBrowserTest { |
|
fgorski
2014/10/20 22:29:07
Don't you want your tests to be in ::gcm namespace
Michael van Ouwerkerk
2014/10/21 10:32:01
No reason not to. Done!
|
| + public: |
| + PushMessagingBrowserTest() : gcm_service_(nullptr) {} |
| + virtual ~PushMessagingBrowserTest() {} |
| + |
| + // InProcessBrowserTest: |
| + virtual void SetUpCommandLine(base::CommandLine* command_line) override { |
| + command_line->AppendSwitch( |
| + switches::kEnableExperimentalWebPlatformFeatures); |
| + |
| + InProcessBrowserTest::SetUpCommandLine(command_line); |
| + } |
| + |
| + // InProcessBrowserTest: |
| + virtual void SetUp() override { |
| + https_server_.reset(new net::SpawnedTestServer( |
| + net::SpawnedTestServer::TYPE_HTTPS, |
| + net::BaseTestServer::SSLOptions( |
| + net::BaseTestServer::SSLOptions::CERT_OK), |
| + base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); |
| + ASSERT_TRUE(https_server_->Start()); |
| + |
| + InProcessBrowserTest::SetUp(); |
| + } |
| + |
| + // InProcessBrowserTest: |
| + virtual void SetUpOnMainThread() override { |
| + gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( |
| + gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| + browser()->profile(), &gcm::FakeGCMProfileService::Build)); |
| + gcm_service_->set_collect(true); |
| + |
| + ui_test_utils::NavigateToURL( |
| + browser(), https_server_->GetURL("files/push_messaging/test.html")); |
| + |
| + InProcessBrowserTest::SetUpOnMainThread(); |
| + } |
| + |
| + bool RunScript(const std::string& script, std::string* result) { |
| + return content::ExecuteScriptAndExtractString( |
| + browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), |
| + script, |
| + result); |
| + } |
| + |
| + bool RegisterServiceWorker(std::string* result) { |
| + return RunScript("registerServiceWorker()", result); |
| + } |
| + |
| + bool RegisterPush(std::string* result) { |
| + return RunScript("registerPush('1234567890')", result); |
| + } |
| + |
| + protected: |
| + scoped_ptr<net::SpawnedTestServer> https_server_; |
| + gcm::FakeGCMProfileService* gcm_service_; |
|
fgorski
2014/10/20 22:29:08
nit: make both private and expose through getters.
Michael van Ouwerkerk
2014/10/21 10:32:01
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { |
| + std::string register_worker_result; |
| + ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); |
| + ASSERT_EQ("ok", register_worker_result); |
| + |
| + InfoBarResponder accepting_responder(browser(), true); |
| + |
| + std::string register_push_result; |
| + ASSERT_TRUE(RegisterPush(®ister_push_result)); |
| + EXPECT_EQ(std::string(gcm::kPushMessagingEndpoint) + " - 1", |
| + register_push_result); |
| + |
| + gcm::PushMessagingApplicationId expected_id(https_server_->GetURL(""), 0L); |
| + EXPECT_EQ(gcm_service_->last_registered_app_id(), expected_id.ToString()); |
|
fgorski
2014/10/20 22:29:08
expected goes first, actual follows.
Please fix e
Michael van Ouwerkerk
2014/10/21 10:32:01
Done.
|
| + EXPECT_EQ(gcm_service_->last_registered_sender_ids()[0], "1234567890"); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) { |
| + std::string register_worker_result; |
| + ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); |
| + ASSERT_EQ("ok", register_worker_result); |
| + |
| + InfoBarResponder cancelling_responder(browser(), false); |
| + |
| + std::string register_push_result; |
| + ASSERT_TRUE(RegisterPush(®ister_push_result)); |
| + EXPECT_EQ("AbortError - Registration failed - permission denied", |
| + register_push_result); |
| +} |