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

Side by Side Diff: content/browser/service_worker/service_worker_browsertest.cc

Issue 1081953004: Add tests for console messages of waitUntil() and respondWith() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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
« no previous file with comments | « no previous file | content/test/data/service_worker/fetch_event_rejected.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/callback.h" 6 #include "base/callback.h"
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "content/browser/fileapi/chrome_blob_storage_context.h" 10 #include "content/browser/fileapi/chrome_blob_storage_context.h"
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 DONT_PAUSE, 420 DONT_PAUSE,
421 PAUSE_THEN_RESUME, 421 PAUSE_THEN_RESUME,
422 PAUSE_THEN_STOP, 422 PAUSE_THEN_STOP,
423 } pause_mode_; 423 } pause_mode_;
424 424
425 // Called by EmbeddedWorkerInstance::Observer overrides so that 425 // Called by EmbeddedWorkerInstance::Observer overrides so that
426 // test code can wait for the worker status notifications. 426 // test code can wait for the worker status notifications.
427 base::Closure done_closure_; 427 base::Closure done_closure_;
428 }; 428 };
429 429
430 class ConsoleListener : public EmbeddedWorkerInstance::Listener {
431 public:
432 void OnReportConsoleMessage(int source_identifier,
433 int message_level,
434 const base::string16& message,
435 int line_number,
436 const GURL& source_url) override {
437 messages_.push_back(message);
438 if (!quit_.is_null() && messages_.size() == expected_message_count_) {
439 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_);
440 quit_.Reset();
441 }
442 }
443
444 void WaitForConsoleMessages(size_t expected_message_count) {
445 if (messages_.size() >= expected_message_count)
446 return;
447
448 expected_message_count_ = expected_message_count;
449 base::RunLoop console_run_loop;
450 quit_ = console_run_loop.QuitClosure();
451 console_run_loop.Run();
452
453 ASSERT_EQ(messages_.size(), expected_message_count);
454 }
455
456 bool OnMessageReceived(const IPC::Message& message) override { return false; }
457 const std::vector<base::string16>& messages() const { return messages_; }
458
459 private:
460 std::vector<base::string16> messages_;
461 size_t expected_message_count_;
462 base::Closure quit_;
463 };
464
430 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest { 465 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest {
431 public: 466 public:
432 using self = ServiceWorkerVersionBrowserTest; 467 using self = ServiceWorkerVersionBrowserTest;
433 468
434 ~ServiceWorkerVersionBrowserTest() override {} 469 ~ServiceWorkerVersionBrowserTest() override {}
435 470
436 void TearDownOnIOThread() override { 471 void TearDownOnIOThread() override {
437 registration_ = NULL; 472 registration_ = NULL;
438 version_ = NULL; 473 version_ = NULL;
439 } 474 }
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 ActivateTestHelper("/service_worker/worker_activate_rejected.js", 761 ActivateTestHelper("/service_worker/worker_activate_rejected.js",
727 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED); 762 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED);
728 } 763 }
729 764
730 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 765 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
731 InstallWithWaitUntil_Rejected) { 766 InstallWithWaitUntil_Rejected) {
732 InstallTestHelper("/service_worker/worker_install_rejected.js", 767 InstallTestHelper("/service_worker/worker_install_rejected.js",
733 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); 768 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED);
734 } 769 }
735 770
771 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
772 InstallWithWaitUntil_RejectConsoleMessage) {
773 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
774 "/service_worker/worker_install_rejected.js"));
775
776 ConsoleListener console_listener;
777 version_->embedded_worker()->AddListener(&console_listener);
778
779 // Dispatch install on a worker.
780 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
781 base::RunLoop install_run_loop;
782 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
783 base::Bind(&self::InstallOnIOThread, this,
784 install_run_loop.QuitClosure(), &status));
785 install_run_loop.Run();
786 ASSERT_EQ(SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED, status);
787
788 const base::string16 expected =
789 base::ASCIIToUTF16("Rejecting oninstall event");
790 console_listener.WaitForConsoleMessages(1);
791 ASSERT_NE(base::string16::npos,
792 console_listener.messages()[0].find(expected));
793 version_->embedded_worker()->RemoveListener(&console_listener);
794 }
795
736 class WaitForLoaded : public EmbeddedWorkerInstance::Listener { 796 class WaitForLoaded : public EmbeddedWorkerInstance::Listener {
737 public: 797 public:
738 WaitForLoaded(const base::Closure& quit) : quit_(quit) {} 798 WaitForLoaded(const base::Closure& quit) : quit_(quit) {}
739 799
740 void OnScriptLoaded() override { 800 void OnScriptLoaded() override {
741 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_); 801 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_);
742 } 802 }
743 bool OnMessageReceived(const IPC::Message& message) override { return false; } 803 bool OnMessageReceived(const IPC::Message& message) override { return false; }
744 804
745 private: 805 private:
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 EXPECT_EQ(expected_headers, response.headers); 880 EXPECT_EQ(expected_headers, response.headers);
821 881
822 std::string body; 882 std::string body;
823 RunOnIOThread( 883 RunOnIOThread(
824 base::Bind(&ReadResponseBody, 884 base::Bind(&ReadResponseBody,
825 &body, base::Owned(blob_data_handle.release()))); 885 &body, base::Owned(blob_data_handle.release())));
826 EXPECT_EQ("This resource is gone. Gone, gone, gone.", body); 886 EXPECT_EQ("This resource is gone. Gone, gone, gone.", body);
827 } 887 }
828 888
829 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 889 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
890 FetchEvent_respondWithRejection) {
891 ServiceWorkerFetchEventResult result;
892 ServiceWorkerResponse response;
893 scoped_ptr<storage::BlobDataHandle> blob_data_handle;
894
895 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
896 "/service_worker/fetch_event_rejected.js"));
897
898 ConsoleListener console_listener;
899 version_->embedded_worker()->AddListener(&console_listener);
900
901 FetchOnRegisteredWorker(&result, &response, &blob_data_handle);
902 const base::string16 expected =
903 base::ASCIIToUTF16("Rejecting respondWith promise");
904 console_listener.WaitForConsoleMessages(1);
905 ASSERT_NE(base::string16::npos,
906 console_listener.messages()[0].find(expected));
907 version_->embedded_worker()->RemoveListener(&console_listener);
908
909 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result);
910 EXPECT_EQ(0, response.status_code);
911
912 ASSERT_FALSE(blob_data_handle);
913 }
914
915 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
830 SyncAbortedWithoutFlag) { 916 SyncAbortedWithoutFlag) {
831 RunOnIOThread(base::Bind( 917 RunOnIOThread(base::Bind(
832 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js")); 918 &self::SetUpRegistrationOnIOThread, this, "/service_worker/sync.js"));
833 919
834 // Run the sync event. 920 // Run the sync event.
835 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 921 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
836 base::RunLoop sync_run_loop; 922 base::RunLoop sync_run_loop;
837 BrowserThread::PostTask(BrowserThread::IO, 923 BrowserThread::PostTask(BrowserThread::IO,
838 FROM_HERE, 924 FROM_HERE,
839 base::Bind(&self::SyncEventOnIOThread, 925 base::Bind(&self::SyncEventOnIOThread,
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1204 ASSERT_EQ(SERVICE_WORKER_OK, status); 1290 ASSERT_EQ(SERVICE_WORKER_OK, status);
1205 // Stop the worker. 1291 // Stop the worker.
1206 StopWorker(SERVICE_WORKER_OK); 1292 StopWorker(SERVICE_WORKER_OK);
1207 // Restart the worker. 1293 // Restart the worker.
1208 StartWorker(SERVICE_WORKER_OK); 1294 StartWorker(SERVICE_WORKER_OK);
1209 // Stop the worker. 1295 // Stop the worker.
1210 StopWorker(SERVICE_WORKER_OK); 1296 StopWorker(SERVICE_WORKER_OK);
1211 } 1297 }
1212 1298
1213 } // namespace content 1299 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/test/data/service_worker/fetch_event_rejected.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698