Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Side by Side Diff: chrome/browser/services/gcm/push_messaging_browsertest.cc

Issue 648623003: PushMessagingBrowserTest for registration success and failure (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Rebase and address Peter's feedback. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/services/gcm/push_messaging_constants.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/infobars/core/confirm_infobar_delegate.h"
19 #include "components/infobars/core/infobar.h"
20 #include "components/infobars/core/infobar_manager.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/test/browser_test_utils.h"
24
25 namespace {
26 // Responds to a confirm infobar by accepting or cancelling it. Responds to at
27 // most one infobar, as it removes itself when it first observes an infobar.
28 class InfoBarResponder : public infobars::InfoBarManager::Observer {
29 public:
30 InfoBarResponder(Browser* browser, bool accept)
31 : infobar_service_(InfoBarService::FromWebContents(
32 browser->tab_strip_model()->GetActiveWebContents())),
33 accept_(accept) {
34 infobar_service_->AddObserver(this);
35 }
36
37 virtual ~InfoBarResponder() { infobar_service_->RemoveObserver(this); }
38
39 // infobars::InfoBarManager::Observer
40 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) override {
41 infobar_service_->RemoveObserver(this);
42 ConfirmInfoBarDelegate* delegate =
43 infobar->delegate()->AsConfirmInfoBarDelegate();
44 DCHECK(delegate);
45 if (accept_)
46 delegate->Accept();
47 else
48 delegate->Cancel();
49 }
50
51 private:
52 InfoBarService* infobar_service_;
53 bool accept_;
54 };
55 } // namespace
56
57 class PushMessagingBrowserTest : public InProcessBrowserTest {
58 public:
59 PushMessagingBrowserTest() : gcm_service_(nullptr) {}
60 virtual ~PushMessagingBrowserTest() {}
61
62 // InProcessBrowserTest:
63 virtual void SetUpCommandLine(base::CommandLine* command_line) override {
64 command_line->AppendSwitch(
65 switches::kEnableExperimentalWebPlatformFeatures);
66
67 InProcessBrowserTest::SetUpCommandLine(command_line);
68 }
69
70 // InProcessBrowserTest:
71 virtual void SetUp() override {
72 https_server_.reset(new net::SpawnedTestServer(
73 net::SpawnedTestServer::TYPE_HTTPS,
74 net::BaseTestServer::SSLOptions(
75 net::BaseTestServer::SSLOptions::CERT_OK),
76 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
77 ASSERT_TRUE(https_server_->Start());
78
79 InProcessBrowserTest::SetUp();
80 }
81
82 // InProcessBrowserTest:
83 virtual void SetUpOnMainThread() override {
84 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
85 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
86 browser()->profile(), &gcm::FakeGCMProfileService::Build));
87 gcm_service_->set_collect(true);
88
89 ui_test_utils::NavigateToURL(
90 browser(), https_server_->GetURL("files/push_messaging/test.html"));
91
92 InProcessBrowserTest::SetUpOnMainThread();
93 }
94
95 bool RunScript(const std::string& script, std::string* result) {
96 return content::ExecuteScriptAndExtractString(
97 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
98 script,
99 result);
100 }
101
102 bool RegisterServiceWorker(std::string* result) {
103 return RunScript("registerServiceWorker()", result);
104 }
105
106 bool RegisterPush(std::string* result) {
107 return RunScript("registerPush('1234567890')", result);
108 }
109
110 protected:
111 scoped_ptr<net::SpawnedTestServer> https_server_;
112 gcm::FakeGCMProfileService* gcm_service_;
113
114 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
115 };
116
117 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) {
118 std::string register_worker_result;
119 ASSERT_TRUE(RegisterServiceWorker(&register_worker_result));
120 ASSERT_EQ("ok", register_worker_result);
121
122 InfoBarResponder accepting_responder(browser(), true);
123
124 std::string register_push_result;
125 ASSERT_TRUE(RegisterPush(&register_push_result));
126 EXPECT_EQ(std::string(gcm::kPushMessagingEndpoint) + " - 1",
127 register_push_result);
128
129 gcm::PushMessagingApplicationId expected_id(https_server_->GetURL(""), 0L);
130 EXPECT_EQ(gcm_service_->last_registered_app_id(), expected_id.ToString());
131 EXPECT_EQ(gcm_service_->last_registered_sender_ids()[0], "1234567890");
132 }
133
134 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) {
135 std::string register_worker_result;
136 ASSERT_TRUE(RegisterServiceWorker(&register_worker_result));
137 ASSERT_EQ("ok", register_worker_result);
138
139 InfoBarResponder cancelling_responder(browser(), false);
140
141 std::string register_push_result;
142 ASSERT_TRUE(RegisterPush(&register_push_result));
143 EXPECT_EQ("AbortError - Registration failed - permission denied",
144 register_push_result);
145 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698