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

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. 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 class InfoBarObserver : public infobars::InfoBarManager::Observer {
27 public:
28 InfoBarObserver(Browser* browser, bool accept)
Peter Beverloo 2014/10/20 12:45:22 I don't really like the name of InfoBarObserver gi
Peter Beverloo 2014/10/20 12:45:22 Is it much more noise to pass in a WebContents* ra
Michael van Ouwerkerk 2014/10/20 15:30:37 InfoBarResponder then, as it can deny as well as g
Michael van Ouwerkerk 2014/10/20 15:30:37 I think so.
Peter Beverloo 2014/10/20 16:35:52 Acknowledged, that sounds better :-).
Peter Beverloo 2014/10/20 16:35:52 Did you try this? The following does not look that
Michael van Ouwerkerk 2014/10/20 17:06:49 I didn't mean to dismiss a serious concern, this l
29 : infobar_service_(InfoBarService::FromWebContents(
30 browser->tab_strip_model()->GetActiveWebContents())),
31 accept_(accept) {
32 infobar_service_->AddObserver(this);
33 }
34
35 virtual ~InfoBarObserver() { infobar_service_->RemoveObserver(this); }
36
37 // infobars::InfoBarManager::Observer
38 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) override {
39 ConfirmInfoBarDelegate* delegate =
40 infobar->delegate()->AsConfirmInfoBarDelegate();
Peter Beverloo 2014/10/20 12:45:22 Can we check somehow if the infobar which you're d
Michael van Ouwerkerk 2014/10/20 15:30:37 That seems to require Run-Time Type Information. B
Peter Beverloo 2014/10/20 16:35:52 I was hoping there'd be some way to get the owner
Michael van Ouwerkerk 2014/10/20 17:06:49 Acknowledged.
41 if (accept_)
42 delegate->Accept();
43 else
44 delegate->Cancel();
45 }
46
47 private:
48 InfoBarService* infobar_service_;
49 bool accept_;
50 };
51 } // namespace
52
53 class PushMessagingBrowserTest : public InProcessBrowserTest {
54 public:
55 PushMessagingBrowserTest() : gcm_service_(nullptr) {}
56 virtual ~PushMessagingBrowserTest() {}
57
58 // InProcessBrowserTest:
59 virtual void SetUpCommandLine(base::CommandLine* command_line) override {
60 command_line->AppendSwitch(
61 switches::kEnableExperimentalWebPlatformFeatures);
Peter Beverloo 2014/10/20 12:45:22 + InProcessBrowserTest::SetUpCommandLine()
Michael van Ouwerkerk 2014/10/20 15:30:37 Done.
62 }
63
64 // InProcessBrowserTest:
65 virtual void SetUp() override {
66 https_server_.reset(new net::SpawnedTestServer(
67 net::SpawnedTestServer::TYPE_HTTPS,
68 net::BaseTestServer::SSLOptions(
69 net::BaseTestServer::SSLOptions::CERT_OK),
70 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/"))));
71 ASSERT_TRUE(https_server_->Start());
72
73 InProcessBrowserTest::SetUp();
74 }
75
76 // InProcessBrowserTest:
77 virtual void SetUpOnMainThread() override {
78 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
79 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
80 browser()->profile(), &gcm::FakeGCMProfileService::Build));
81 gcm_service_->set_collect(true);
82
83 ui_test_utils::NavigateToURL(
84 browser(), https_server_->GetURL("files/push_messaging/test.html"));
Peter Beverloo 2014/10/20 12:45:22 + InProcessBrowserTest::SetUpOnMainThread()
Michael van Ouwerkerk 2014/10/20 15:30:37 Done.
85 }
86
87 void RunScript(const std::string& script, std::string* result) {
88 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
Peter Beverloo 2014/10/20 12:45:22 This still has the problem that an ASSERT_*() in a
Michael van Ouwerkerk 2014/10/20 15:30:37 It should be an ASSERT, as it would not make sense
Peter Beverloo 2014/10/20 16:35:52 My point is that ASSERTs don't work in called meth
Michael van Ouwerkerk 2014/10/20 17:06:49 Acknowledged.
89 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
90 script,
91 result));
92 }
93
94 void RegisterServiceWorker(std::string* result) {
95 RunScript("registerServiceWorker()", result);
96 }
97
98 void RegisterPush(std::string* result) {
99 RunScript("registerPush('1234567890')", result);
100 }
101
102 protected:
103 scoped_ptr<net::SpawnedTestServer> https_server_;
104 gcm::FakeGCMProfileService* gcm_service_;
105
106 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
107 };
108
109 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) {
110 std::string register_worker_result;
111 RegisterServiceWorker(&register_worker_result);
112 EXPECT_EQ("ok", register_worker_result);
113
114 InfoBarObserver accepting_observer(browser(), true);
115
116 std::string register_push_result;
117 RegisterPush(&register_push_result);
118 EXPECT_EQ(std::string(gcm::kPushMessagingEndpoint) + " - 1",
119 register_push_result);
120
121 gcm::PushMessagingApplicationId expected_id(https_server_->GetURL(""), 0L);
122 EXPECT_EQ(gcm_service_->last_registered_app_id(), expected_id.ToString());
123 EXPECT_EQ(gcm_service_->last_registered_sender_ids()[0], "1234567890");
124 }
125
126 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) {
127 std::string register_worker_result;
128 RegisterServiceWorker(&register_worker_result);
129 EXPECT_EQ("ok", register_worker_result);
130
131 InfoBarObserver cancelling_observer(browser(), false);
132
133 std::string register_push_result;
134 RegisterPush(&register_push_result);
135 EXPECT_EQ("AbortError - Registration failed - permission denied",
136 register_push_result);
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698