Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 <string> | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "chrome/browser/infobars/infobar_service.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | |
| 11 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | |
| 12 #include "chrome/browser/services/gcm/push_messaging_application_id.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "chrome/test/base/ui_test_utils.h" | |
| 17 #include "components/infobars/core/confirm_infobar_delegate.h" | |
| 18 #include "components/infobars/core/infobar.h" | |
| 19 #include "components/infobars/core/infobar_manager.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 #include "content/public/common/content_switches.h" | |
| 22 #include "content/public/test/browser_test_utils.h" | |
| 23 | |
| 24 namespace { | |
| 25 class InfoBarObserver : public infobars::InfoBarManager::Observer { | |
| 26 public: | |
| 27 InfoBarObserver(Browser* browser, bool accept) | |
| 28 : infobar_service_(InfoBarService::FromWebContents( | |
| 29 browser->tab_strip_model()->GetActiveWebContents())), | |
|
Peter Beverloo
2014/10/17 14:04:07
nit: I'd initialize this in the constructor's body
Michael van Ouwerkerk
2014/10/17 15:28:01
I don't mind strongly one way or the other. It's n
| |
| 30 accept_(accept) { | |
| 31 infobar_service_->AddObserver(this); | |
| 32 } | |
| 33 | |
| 34 virtual ~InfoBarObserver() { infobar_service_->RemoveObserver(this); } | |
| 35 | |
| 36 // infobars::InfoBarManager::Observer | |
| 37 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) override { | |
| 38 ConfirmInfoBarDelegate* delegate = | |
| 39 infobar->delegate()->AsConfirmInfoBarDelegate(); | |
| 40 if (accept_) | |
| 41 delegate->Accept(); | |
| 42 else | |
| 43 delegate->Cancel(); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 InfoBarService* infobar_service_; | |
| 48 bool accept_; | |
| 49 }; | |
| 50 } // namespace | |
| 51 | |
| 52 class PushMessagingBrowserTest : public InProcessBrowserTest { | |
| 53 public: | |
| 54 PushMessagingBrowserTest() : gcm_service_(nullptr) {} | |
| 55 virtual ~PushMessagingBrowserTest() {} | |
| 56 | |
| 57 // InProcessBrowserTest: | |
|
Peter Beverloo
2014/10/17 14:04:08
micro nit: Perhaps group the three "InProcessBrows
Michael van Ouwerkerk
2014/10/17 15:28:01
I find the current pattern easier to read. If you
| |
| 58 virtual void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 59 command_line->AppendSwitch( | |
| 60 switches::kEnableExperimentalWebPlatformFeatures); | |
| 61 } | |
| 62 | |
| 63 // InProcessBrowserTest: | |
| 64 virtual void SetUp() override { | |
| 65 https_server_.reset(new net::SpawnedTestServer( | |
| 66 net::SpawnedTestServer::TYPE_HTTPS, | |
| 67 net::BaseTestServer::SSLOptions( | |
| 68 net::BaseTestServer::SSLOptions::CERT_OK), | |
| 69 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); | |
| 70 ASSERT_TRUE(https_server_->Start()); | |
| 71 | |
| 72 InProcessBrowserTest::SetUp(); | |
| 73 } | |
| 74 | |
| 75 // InProcessBrowserTest: | |
| 76 virtual void SetUpOnMainThread() override { | |
| 77 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>( | |
| 78 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 79 browser()->profile(), &gcm::FakeGCMProfileService::Build)); | |
| 80 gcm_service_->set_collect(true); | |
| 81 | |
| 82 ui_test_utils::NavigateToURL( | |
| 83 browser(), https_server_->GetURL("files/push_messaging/test.html")); | |
| 84 } | |
| 85 | |
| 86 void RunScript(const std::string& script, const std::string& expected) { | |
|
Peter Beverloo
2014/10/17 14:04:08
I would prefer if we could factor |expected| out o
Michael van Ouwerkerk
2014/10/17 15:28:01
Done.
| |
| 87 std::string result; | |
| 88 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | |
| 89 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), | |
| 90 script, | |
| 91 &result)); | |
| 92 ASSERT_EQ(expected, result); | |
| 93 } | |
| 94 | |
| 95 void RegisterServiceWorkerAndExpect(const std::string& expected) { | |
| 96 return RunScript("registerServiceWorker()", expected); | |
| 97 } | |
| 98 | |
| 99 void RegisterPushAndExpect(const std::string& expected) { | |
| 100 return RunScript("registerPush('1234567890')", expected); | |
|
Peter Beverloo
2014/10/17 14:04:07
nit: Will we always pass 1234567890 for tests, or
Michael van Ouwerkerk
2014/10/17 15:28:01
The sender id is opaque from the browser perspecti
| |
| 101 } | |
| 102 | |
| 103 protected: | |
| 104 scoped_ptr<net::SpawnedTestServer> https_server_; | |
| 105 gcm::FakeGCMProfileService* gcm_service_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); | |
| 108 }; | |
| 109 | |
| 110 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { | |
| 111 RegisterServiceWorkerAndExpect("ok"); | |
| 112 InfoBarObserver accepting_observer(browser(), true); | |
| 113 RegisterPushAndExpect("https://android.googleapis.com/gcm/send - 1"); | |
|
Peter Beverloo
2014/10/17 14:04:07
Is there some kind of constant or method we can us
Michael van Ouwerkerk
2014/10/17 15:28:01
Done.
| |
| 114 gcm::PushMessagingApplicationId expected_id(https_server_->GetURL(""), 0L); | |
| 115 EXPECT_EQ(gcm_service_->last_registered_app_id(), expected_id.ToString()); | |
| 116 EXPECT_EQ(gcm_service_->last_registered_sender_ids()[0], "1234567890"); | |
| 117 } | |
| 118 | |
| 119 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) { | |
| 120 RegisterServiceWorkerAndExpect("ok"); | |
| 121 InfoBarObserver cancelling_observer(browser(), false); | |
| 122 RegisterPushAndExpect("AbortError - Registration failed - permission denied"); | |
| 123 } | |
| OLD | NEW |