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

Side by Side Diff: content/browser/service_worker/embedded_worker_test_helper.h

Issue 238043002: Teach EmbeddedWorkerInstance to create a process when it needs one. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Handle 2 places where context_ could be NULL. Created 6 years, 7 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 | Annotate | Revision Log
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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "ipc/ipc_listener.h" 12 #include "ipc/ipc_listener.h"
13 #include "ipc/ipc_test_sink.h" 13 #include "ipc/ipc_test_sink.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h" 15 #include "url/gurl.h"
16 16
17 class GURL; 17 class GURL;
18 18
19 namespace content { 19 namespace content {
20 20
21 class EmbeddedWorkerRegistry; 21 class EmbeddedWorkerRegistry;
22 class EmbeddedWorkerTestHelper; 22 class EmbeddedWorkerTestHelper;
23 class ServiceWorkerContextCore; 23 class ServiceWorkerContextCore;
24 class ServiceWorkerContextWrapper;
24 struct ServiceWorkerFetchRequest; 25 struct ServiceWorkerFetchRequest;
25 26
26 // In-Process EmbeddedWorker test helper. 27 // In-Process EmbeddedWorker test helper.
27 // 28 //
28 // Usage: create an instance of this class for a ServiceWorkerContextCore 29 // Usage: create an instance of this class to test browser-side embedded worker
29 // to test browser-side embedded worker code without creating a child process. 30 // code without creating a child process. This class will create a
31 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you.
30 // 32 //
31 // By default this class just notifies back WorkerStarted and WorkerStopped 33 // By default this class just notifies back WorkerStarted and WorkerStopped
32 // for StartWorker and StopWorker requests. The default implementation 34 // for StartWorker and StopWorker requests. The default implementation
33 // also returns success for event messages (e.g. InstallEvent, FetchEvent). 35 // also returns success for event messages (e.g. InstallEvent, FetchEvent).
34 // 36 //
35 // Alternatively consumers can subclass this helper and override On*() 37 // Alternatively consumers can subclass this helper and override On*()
36 // methods to add their own logic/verification code. 38 // methods to add their own logic/verification code.
37 // 39 //
38 // See embedded_worker_instance_unittest.cc for example usages. 40 // See embedded_worker_instance_unittest.cc for example usages.
39 // 41 //
40 class EmbeddedWorkerTestHelper : public IPC::Sender, 42 class EmbeddedWorkerTestHelper : public IPC::Sender,
41 public IPC::Listener { 43 public IPC::Listener {
42 public: 44 public:
43 // Initialize this helper for |context|, and enable this as an IPC 45 // Initialize this helper for |context|, and enable this as an IPC
44 // sender for |mock_render_process_id|. 46 // sender for |mock_render_process_id|.
45 EmbeddedWorkerTestHelper(ServiceWorkerContextCore* context, 47 EmbeddedWorkerTestHelper(int mock_render_process_id);
46 int mock_render_process_id);
47 virtual ~EmbeddedWorkerTestHelper(); 48 virtual ~EmbeddedWorkerTestHelper();
48 49
49 // Call this to simulate add/associate a process to a worker. 50 // Call this to simulate add/associate a process to a worker.
50 // This also registers this sender for the process. 51 // This also registers this sender for the process.
51 void SimulateAddProcessToWorker(int embedded_worker_id, int process_id); 52 void SimulateAddProcessToWorker(int embedded_worker_id, int process_id);
52 53
53 // IPC::Sender implementation. 54 // IPC::Sender implementation.
54 virtual bool Send(IPC::Message* message) OVERRIDE; 55 virtual bool Send(IPC::Message* message) OVERRIDE;
55 56
56 // IPC::Listener implementation. 57 // IPC::Listener implementation.
57 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 58 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
58 59
59 // IPC sink for EmbeddedWorker messages. 60 // IPC sink for EmbeddedWorker messages.
60 IPC::TestSink* ipc_sink() { return &sink_; } 61 IPC::TestSink* ipc_sink() { return &sink_; }
61 // Inner IPC sink for script context messages sent via EmbeddedWorker. 62 // Inner IPC sink for script context messages sent via EmbeddedWorker.
62 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } 63 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; }
63 64
65 ServiceWorkerContextCore* context();
66 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); }
67 void ShutdownContext();
68
64 protected: 69 protected:
65 // Called when StartWorker, StopWorker and SendMessageToWorker message 70 // Called when StartWorker, StopWorker and SendMessageToWorker message
66 // is sent to the embedded worker. Override if necessary. By default 71 // is sent to the embedded worker. Override if necessary. By default
67 // they verify given parameters and: 72 // they verify given parameters and:
68 // - OnStartWorker calls SimulateWorkerStarted 73 // - OnStartWorker calls SimulateWorkerStarted
69 // - OnStopWorker calls SimulateWorkerStoped 74 // - OnStopWorker calls SimulateWorkerStoped
70 // - OnSendMessageToWorker calls the message's respective On*Event handler 75 // - OnSendMessageToWorker calls the message's respective On*Event handler
71 virtual void OnStartWorker(int embedded_worker_id, 76 virtual void OnStartWorker(int embedded_worker_id,
72 int64 service_worker_version_id, 77 int64 service_worker_version_id,
73 const GURL& scope, 78 const GURL& scope,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 const GURL& script_url); 110 const GURL& script_url);
106 void OnStopWorkerStub(int embedded_worker_id); 111 void OnStopWorkerStub(int embedded_worker_id);
107 void OnMessageToWorkerStub(int thread_id, 112 void OnMessageToWorkerStub(int thread_id,
108 int embedded_worker_id, 113 int embedded_worker_id,
109 const IPC::Message& message); 114 const IPC::Message& message);
110 void OnActivateEventStub(int request_id); 115 void OnActivateEventStub(int request_id);
111 void OnInstallEventStub(int request_id, int active_version_id); 116 void OnInstallEventStub(int request_id, int active_version_id);
112 void OnFetchEventStub(int request_id, 117 void OnFetchEventStub(int request_id,
113 const ServiceWorkerFetchRequest& request); 118 const ServiceWorkerFetchRequest& request);
114 119
115 base::WeakPtr<ServiceWorkerContextCore> context_; 120 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
116 121
117 IPC::TestSink sink_; 122 IPC::TestSink sink_;
118 IPC::TestSink inner_sink_; 123 IPC::TestSink inner_sink_;
119 124
120 int next_thread_id_; 125 int next_thread_id_;
121 126
122 // Updated each time MessageToWorker message is received. 127 // Updated each time MessageToWorker message is received.
123 int current_embedded_worker_id_; 128 int current_embedded_worker_id_;
124 129
125 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; 130 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_;
126 131
127 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); 132 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper);
128 }; 133 };
129 134
130 } // namespace content 135 } // namespace content
131 136
132 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ 137 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698