Home | History | Annotate | Download | only in service_worker
      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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
      6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "ipc/ipc_listener.h"
     13 #include "ipc/ipc_test_sink.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #include "url/gurl.h"
     16 
     17 struct EmbeddedWorkerMsg_StartWorker_Params;
     18 class GURL;
     19 
     20 namespace content {
     21 
     22 class EmbeddedWorkerRegistry;
     23 class EmbeddedWorkerTestHelper;
     24 class ServiceWorkerContextCore;
     25 class ServiceWorkerContextWrapper;
     26 struct ServiceWorkerFetchRequest;
     27 
     28 // In-Process EmbeddedWorker test helper.
     29 //
     30 // Usage: create an instance of this class to test browser-side embedded worker
     31 // code without creating a child process.  This class will create a
     32 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you.
     33 //
     34 // By default this class just notifies back WorkerStarted and WorkerStopped
     35 // for StartWorker and StopWorker requests. The default implementation
     36 // also returns success for event messages (e.g. InstallEvent, FetchEvent).
     37 //
     38 // Alternatively consumers can subclass this helper and override On*()
     39 // methods to add their own logic/verification code.
     40 //
     41 // See embedded_worker_instance_unittest.cc for example usages.
     42 //
     43 class EmbeddedWorkerTestHelper : public IPC::Sender,
     44                                  public IPC::Listener {
     45  public:
     46   // Initialize this helper for |context|, and enable this as an IPC
     47   // sender for |mock_render_process_id|.
     48   explicit EmbeddedWorkerTestHelper(int mock_render_process_id);
     49   virtual ~EmbeddedWorkerTestHelper();
     50 
     51   // Call this to simulate add/associate a process to a pattern.
     52   // This also registers this sender for the process.
     53   void SimulateAddProcessToPattern(const GURL& pattern, int process_id);
     54 
     55   // IPC::Sender implementation.
     56   virtual bool Send(IPC::Message* message) OVERRIDE;
     57 
     58   // IPC::Listener implementation.
     59   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
     60 
     61   // IPC sink for EmbeddedWorker messages.
     62   IPC::TestSink* ipc_sink() { return &sink_; }
     63   // Inner IPC sink for script context messages sent via EmbeddedWorker.
     64   IPC::TestSink* inner_ipc_sink() { return &inner_sink_; }
     65 
     66   ServiceWorkerContextCore* context();
     67   ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); }
     68   void ShutdownContext();
     69 
     70   int mock_render_process_id() const { return mock_render_process_id_;}
     71 
     72  protected:
     73   // Called when StartWorker, StopWorker and SendMessageToWorker message
     74   // is sent to the embedded worker. Override if necessary. By default
     75   // they verify given parameters and:
     76   // - OnStartWorker calls SimulateWorkerStarted
     77   // - OnStopWorker calls SimulateWorkerStoped
     78   // - OnSendMessageToWorker calls the message's respective On*Event handler
     79   virtual void OnStartWorker(int embedded_worker_id,
     80                              int64 service_worker_version_id,
     81                              const GURL& scope,
     82                              const GURL& script_url,
     83                              bool pause_after_download);
     84   virtual void OnResumeAfterDownload(int embedded_worker_id);
     85   virtual void OnStopWorker(int embedded_worker_id);
     86   virtual bool OnMessageToWorker(int thread_id,
     87                                  int embedded_worker_id,
     88                                  const IPC::Message& message);
     89 
     90   // On*Event handlers. Called by the default implementation of
     91   // OnMessageToWorker when events are sent to the embedded
     92   // worker. By default they just return success via
     93   // SimulateSendReplyToBrowser.
     94   virtual void OnActivateEvent(int embedded_worker_id, int request_id);
     95   virtual void OnInstallEvent(int embedded_worker_id,
     96                               int request_id,
     97                               int active_version_id);
     98   virtual void OnFetchEvent(int embedded_worker_id,
     99                             int request_id,
    100                             const ServiceWorkerFetchRequest& request);
    101 
    102   // These functions simulate sending an EmbeddedHostMsg message to the
    103   // browser.
    104   void SimulatePausedAfterDownload(int embedded_worker_id);
    105   void SimulateWorkerReadyForInspection(int embedded_worker_id);
    106   void SimulateWorkerScriptLoaded(int thread_id, int embedded_worker_id);
    107   void SimulateWorkerStarted(int embedded_worker_id);
    108   void SimulateWorkerStopped(int embedded_worker_id);
    109   void SimulateSend(IPC::Message* message);
    110 
    111   EmbeddedWorkerRegistry* registry();
    112 
    113  private:
    114   void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params);
    115   void OnResumeAfterDownloadStub(int embedded_worker_id);
    116   void OnStopWorkerStub(int embedded_worker_id);
    117   void OnMessageToWorkerStub(int thread_id,
    118                              int embedded_worker_id,
    119                              const IPC::Message& message);
    120   void OnActivateEventStub(int request_id);
    121   void OnInstallEventStub(int request_id, int active_version_id);
    122   void OnFetchEventStub(int request_id,
    123                         const ServiceWorkerFetchRequest& request);
    124 
    125   scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
    126 
    127   IPC::TestSink sink_;
    128   IPC::TestSink inner_sink_;
    129 
    130   int next_thread_id_;
    131   int mock_render_process_id_;
    132 
    133   // Updated each time MessageToWorker message is received.
    134   int current_embedded_worker_id_;
    135 
    136   base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_;
    137 
    138   DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper);
    139 };
    140 
    141 }  // namespace content
    142 
    143 #endif  // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
    144