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

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

Issue 2703153002: ServiceWorker: ignore AddMessageToConsole before process allocation (Closed)
Patch Set: Created 3 years, 10 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/browser/service_worker/embedded_worker_instance.h" 5 #include "content/browser/service_worker/embedded_worker_instance.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 base::RunLoop().RunUntilIdle(); 739 base::RunLoop().RunUntilIdle();
740 740
741 // Worker should handle the sudden shutdown as detach. 741 // Worker should handle the sudden shutdown as detach.
742 ASSERT_EQ(3u, events_.size()); 742 ASSERT_EQ(3u, events_.size());
743 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); 743 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
744 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type); 744 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
745 EXPECT_EQ(DETACHED, events_[2].type); 745 EXPECT_EQ(DETACHED, events_[2].type);
746 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status); 746 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[2].status);
747 } 747 }
748 748
749 class StoreMessageInstanceClient
750 : public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient {
751 public:
752 explicit StoreMessageInstanceClient(
753 base::WeakPtr<EmbeddedWorkerTestHelper> helper)
754 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {}
755
756 const std::vector<std::pair<blink::WebConsoleMessage::Level, std::string>>&
757 message() {
758 return messages_;
759 }
760
761 private:
762 void AddMessageToConsole(blink::WebConsoleMessage::Level level,
763 const std::string& message) override {
764 messages_.push_back(std::make_pair(level, message));
765 }
766
767 std::vector<std::pair<blink::WebConsoleMessage::Level, std::string>>
768 messages_;
769 };
770
771 TEST_F(EmbeddedWorkerInstanceTest, AddMessageToConsole) {
772 const int64_t version_id = 55L;
773 const GURL pattern("http://example.com/");
774 const GURL url("http://example.com/worker.js");
775 // Let StartWorker fail; binding is discarded in the middle of IPC
falken 2017/02/20 08:07:13 I don't understand this comment: where is StartWor
shimazu 2017/02/21 03:13:53 Sorry, it's just my copy-and-paste failure. Fixed.
776 std::unique_ptr<StoreMessageInstanceClient> instance_client =
777 base::MakeUnique<StoreMessageInstanceClient>(helper_->AsWeakPtr());
778 StoreMessageInstanceClient* instance_client_rawptr = instance_client.get();
779 helper_->RegisterMockInstanceClient(std::move(instance_client));
780 ASSERT_EQ(mock_instance_clients()->size(), 1UL);
781
782 std::unique_ptr<EmbeddedWorkerInstance> worker =
783 embedded_worker_registry()->CreateWorker();
784 helper_->SimulateAddProcessToPattern(pattern,
785 helper_->mock_render_process_id());
786 worker->AddListener(this);
787
788 // Attempt to start the worker and immediate AddMessageToConsole should not
789 // cause a crash.
790 std::pair<blink::WebConsoleMessage::Level, std::string> test_message =
791 std::make_pair(blink::WebConsoleMessage::LevelVerbose, "");
792 std::unique_ptr<EmbeddedWorkerStartParams> params =
793 CreateStartParams(version_id, pattern, url);
794 worker->Start(std::move(params), CreateEventDispatcher(),
795 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
796 worker->AddMessageToConsole(test_message.first, test_message.second);
797 base::RunLoop().RunUntilIdle();
798
799 // Messages sent before sending StartWorker message won't be dispatched.
800 ASSERT_EQ(0UL, instance_client_rawptr->message().size());
801 ASSERT_EQ(3UL, events_.size());
802 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
803 EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
804 EXPECT_EQ(STARTED, events_[2].type);
805 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status());
806
807 worker->AddMessageToConsole(test_message.first, test_message.second);
808 base::RunLoop().RunUntilIdle();
809
810 // Messages sent after sending StartWorker message should be reached to
811 // the renderer.
812 ASSERT_EQ(1UL, instance_client_rawptr->message().size());
813 EXPECT_EQ(test_message, instance_client_rawptr->message()[0]);
814
815 // Ensure the worker is stopped.
816 worker->Stop();
817 base::RunLoop().RunUntilIdle();
818 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status());
819 }
820
749 } // namespace content 821 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698