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

Unified Diff: content/browser/service_worker/service_worker_version_unittest.cc

Issue 1525743002: Allow ServiceWorker events to have custom durations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 5 years 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_version_unittest.cc
diff --git a/content/browser/service_worker/service_worker_version_unittest.cc b/content/browser/service_worker/service_worker_version_unittest.cc
index ba984bffaca3027bbf513c375846568d4d1d28bd..d7cbadf1e14040c28722d1aed692a6ae75bd9ba8 100644
--- a/content/browser/service_worker/service_worker_version_unittest.cc
+++ b/content/browser/service_worker/service_worker_version_unittest.cc
@@ -11,9 +11,11 @@
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/browser/service_worker/service_worker_version.h"
+#include "content/common/background_sync_service.mojom.h"
#include "content/common/service_worker/service_worker_utils.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_thread_bundle.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
#include "testing/gtest/include/gtest/gtest.h"
// IPC messages for testing ---------------------------------------------------
@@ -68,6 +70,25 @@ class MessageReceiver : public EmbeddedWorkerTestHelper {
DISALLOW_COPY_AND_ASSIGN(MessageReceiver);
};
+class MockBackgroundSyncServiceClient : public BackgroundSyncServiceClient {
+ public:
+ MockBackgroundSyncServiceClient(
+ mojo::InterfaceRequest<BackgroundSyncServiceClient> request)
+ : binding_(this, request.Pass()) {}
+
+ private:
+ // BackgroundSyncServiceClient overrides
+ void Sync(int64_t handle_id,
+ content::BackgroundSyncEventLastChance last_chance,
+ const SyncCallback& callback) override {
+ // Wait forever instead of calling the callback.
+ }
+
+ mojo::StrongBinding<BackgroundSyncServiceClient> binding_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockBackgroundSyncServiceClient);
+};
+
void VerifyCalled(bool* called) {
*called = true;
}
@@ -86,6 +107,11 @@ void ReceiveFetchResult(ServiceWorkerStatusCode* status,
*status = actual_status;
}
+void ReceiveSyncStatus(ServiceWorkerStatusCode* status,
+ ServiceWorkerStatusCode actual_status) {
+ *status = actual_status;
+}
+
// A specialized listener class to receive test messages from a worker.
class MessageReceiverFromWorker : public EmbeddedWorkerInstance::Listener {
public:
@@ -180,6 +206,14 @@ class ServiceWorkerVersionTest : public testing::Test {
helper_->mock_render_process_id());
ASSERT_TRUE(helper_->context()->process_manager()
->PatternHasProcessToRun(pattern_));
+
+ // Create a mock BackgroundSyncServiceClient
+ mojo::InterfaceRequest<BackgroundSyncServiceClient> service_request =
Marijn Kruisselbrink 2015/12/16 19:09:57 FYI, after I clean up and land https://codereview.
jkarlin 2015/12/16 20:44:27 Nice!
+ mojo::GetProxy(&version_->background_sync_dispatcher_);
+ // The MocKBackgroundSyncServiceClient is bound to the client, and will be
Marijn Kruisselbrink 2015/12/16 19:09:57 s/MocK/Mock/
jkarlin 2015/12/16 20:44:27 Done.
+ // deleted when the client is deleted.
+ new MockBackgroundSyncServiceClient(service_request.Pass());
+ base::RunLoop().RunUntilIdle();
}
virtual scoped_ptr<MessageReceiver> GetMessageReceiver() {
@@ -753,16 +787,65 @@ TEST_F(ServiceWorkerWaitForeverInFetchTest, RequestTimeout) {
// Simulate timeout.
EXPECT_TRUE(version_->timeout_timer_.IsRunning());
- version_->SetAllRequestTimes(
- base::TimeTicks::Now() -
- base::TimeDelta::FromMinutes(
- ServiceWorkerVersion::kRequestTimeoutMinutes + 1));
+ version_->SetAllRequestExpirations(base::TimeTicks::Now());
+ version_->timeout_timer_.user_task().Run();
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status);
+ EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
+}
+
+TEST_F(ServiceWorkerVersionTest, RequestCustomizedTimeout) {
+ ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
+ version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
+
+ // Create a sync request that should expire Now().
+ version_->DispatchSyncEvent(0 /* sync handle id */,
+ BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE,
+ base::TimeTicks::Now(), /* expiration time */
+ base::Bind(&ReceiveSyncStatus, &status));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(version_->timeout_timer_.IsRunning());
version_->timeout_timer_.user_task().Run();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status);
EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
}
+TEST_F(ServiceWorkerWaitForeverInFetchTest, MixedRequestTimeouts) {
+ ServiceWorkerStatusCode sync_status =
+ SERVICE_WORKER_ERROR_NETWORK; // dummy value
+ ServiceWorkerStatusCode fetch_status =
+ SERVICE_WORKER_ERROR_NETWORK; // dummy value
+ version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
+
+ // Create a fetch request that should expire sometime later.
+ version_->DispatchFetchEvent(ServiceWorkerFetchRequest(),
+ base::Bind(&base::DoNothing),
+ base::Bind(&ReceiveFetchResult, &fetch_status));
+ // Create a sync request that should expire Now().
+ version_->DispatchSyncEvent(0 /* sync handle id */,
+ BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE,
+ base::TimeTicks::Now(), /* expiration time */
+ base::Bind(&ReceiveSyncStatus, &sync_status));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, sync_status);
+
+ // Verify the sync has timed out but not the fetch.
+ EXPECT_TRUE(version_->timeout_timer_.IsRunning());
+ version_->timeout_timer_.user_task().Run();
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, sync_status);
+ EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, fetch_status);
+ EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
+
+ // Verify that the fetch times out later.
+ version_->SetAllRequestExpirations(base::TimeTicks::Now());
+ version_->timeout_timer_.user_task().Run();
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, fetch_status);
+ EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
+}
+
TEST_F(ServiceWorkerFailToStartTest, RendererCrash) {
ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
version_->StartWorker(

Powered by Google App Engine
This is Rietveld 408576698