Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2012 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_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
      6 #define CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
      7 
      8 #include "base/memory/shared_memory.h"
      9 #include "base/observer_list.h"
     10 #include "base/strings/string16.h"
     11 #include "content/public/renderer/render_thread.h"
     12 #include "ipc/ipc_test_sink.h"
     13 #include "ipc/message_filter.h"
     14 #include "third_party/WebKit/public/web/WebPopupType.h"
     15 
     16 struct ViewHostMsg_CreateWindow_Params;
     17 
     18 namespace IPC {
     19 class MessageFilter;
     20 class MessageReplyDeserializer;
     21 }
     22 
     23 namespace content {
     24 
     25 // This class is a very simple mock of RenderThread. It simulates an IPC channel
     26 // which supports only three messages:
     27 // ViewHostMsg_CreateWidget : sync message sent by the Widget.
     28 // ViewHostMsg_CreateWindow : sync message sent by the Widget.
     29 // ViewMsg_Close : async, send to the Widget.
     30 class MockRenderThread : public RenderThread {
     31  public:
     32   MockRenderThread();
     33   virtual ~MockRenderThread();
     34 
     35   // Provides access to the messages that have been received by this thread.
     36   IPC::TestSink& sink() { return sink_; }
     37 
     38   // RenderThread implementation:
     39   virtual bool Send(IPC::Message* msg) OVERRIDE;
     40   virtual base::MessageLoop* GetMessageLoop() OVERRIDE;
     41   virtual IPC::SyncChannel* GetChannel() OVERRIDE;
     42   virtual std::string GetLocale() OVERRIDE;
     43   virtual IPC::SyncMessageFilter* GetSyncMessageFilter() OVERRIDE;
     44   virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy()
     45       OVERRIDE;
     46   virtual void AddRoute(int32 routing_id, IPC::Listener* listener) OVERRIDE;
     47   virtual void RemoveRoute(int32 routing_id) OVERRIDE;
     48   virtual int GenerateRoutingID() OVERRIDE;
     49   virtual void AddFilter(IPC::MessageFilter* filter) OVERRIDE;
     50   virtual void RemoveFilter(IPC::MessageFilter* filter) OVERRIDE;
     51   virtual void AddObserver(RenderProcessObserver* observer) OVERRIDE;
     52   virtual void RemoveObserver(RenderProcessObserver* observer) OVERRIDE;
     53   virtual void SetResourceDispatcherDelegate(
     54       ResourceDispatcherDelegate* delegate) OVERRIDE;
     55   virtual void EnsureWebKitInitialized() OVERRIDE;
     56   virtual void RecordAction(const base::UserMetricsAction& action) OVERRIDE;
     57   virtual void RecordComputedAction(const std::string& action) OVERRIDE;
     58   virtual scoped_ptr<base::SharedMemory> HostAllocateSharedMemoryBuffer(
     59       size_t buffer_size) OVERRIDE;
     60   virtual void RegisterExtension(v8::Extension* extension) OVERRIDE;
     61   virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE;
     62   virtual void IdleHandler() OVERRIDE;
     63   virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE;
     64   virtual void SetIdleNotificationDelayInMs(
     65       int64 idle_notification_delay_in_ms) OVERRIDE;
     66   virtual void UpdateHistograms(int sequence_number) OVERRIDE;
     67   virtual int PostTaskToAllWebWorkers(const base::Closure& closure) OVERRIDE;
     68   virtual bool ResolveProxy(const GURL& url, std::string* proxy_list) OVERRIDE;
     69   virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
     70 #if defined(OS_WIN)
     71   virtual void PreCacheFont(const LOGFONT& log_font) OVERRIDE;
     72   virtual void ReleaseCachedFonts() OVERRIDE;
     73 #endif
     74   virtual ServiceRegistry* GetServiceRegistry() OVERRIDE;
     75 
     76   //////////////////////////////////////////////////////////////////////////
     77   // The following functions are called by the test itself.
     78 
     79   void set_routing_id(int32 id) {
     80     routing_id_ = id;
     81   }
     82 
     83   void set_surface_id(int32 id) {
     84     surface_id_ = id;
     85   }
     86 
     87   int32 opener_id() const {
     88     return opener_id_;
     89   }
     90 
     91   void set_new_window_routing_id(int32 id) {
     92     new_window_routing_id_ = id;
     93   }
     94 
     95   void set_new_frame_routing_id(int32 id) {
     96     new_frame_routing_id_ = id;
     97   }
     98 
     99   // Simulates the Widget receiving a close message. This should result
    100   // on releasing the internal reference counts and destroying the internal
    101   // state.
    102   void SendCloseMessage();
    103 
    104   // Dispatches control messages to observers.
    105   bool OnControlMessageReceived(const IPC::Message& msg);
    106 
    107   ObserverList<RenderProcessObserver>& observers() {
    108     return observers_;
    109   }
    110 
    111  protected:
    112   // This function operates as a regular IPC listener. Subclasses
    113   // overriding this should first delegate to this implementation.
    114   virtual bool OnMessageReceived(const IPC::Message& msg);
    115 
    116   // The Widget expects to be returned valid route_id.
    117   void OnCreateWidget(int opener_id,
    118                       blink::WebPopupType popup_type,
    119                       int* route_id,
    120                       int* surface_id);
    121 
    122   // The View expects to be returned a valid route_id different from its own.
    123   // We do not keep track of the newly created widget in MockRenderThread,
    124   // so it must be cleaned up on its own.
    125   void OnCreateWindow(
    126     const ViewHostMsg_CreateWindow_Params& params,
    127     int* route_id,
    128     int* main_frame_route_id,
    129     int* surface_id,
    130     int64* cloned_session_storage_namespace_id);
    131 
    132   // The Frame expects to be returned a valid route_id different from its own.
    133   void OnCreateChildFrame(int new_frame_routing_id,
    134                           const std::string& frame_name,
    135                           int* new_render_frame_id);
    136 
    137 #if defined(OS_WIN)
    138   void OnDuplicateSection(base::SharedMemoryHandle renderer_handle,
    139                           base::SharedMemoryHandle* browser_handle);
    140 #endif
    141 
    142   IPC::TestSink sink_;
    143 
    144   // Routing id what will be assigned to the Widget.
    145   int32 routing_id_;
    146 
    147   // Surface id what will be assigned to the Widget.
    148   int32 surface_id_;
    149 
    150   // Opener id reported by the Widget.
    151   int32 opener_id_;
    152 
    153   // Routing id that will be assigned to a CreateWindow Widget.
    154   int32 new_window_routing_id_;
    155   int32 new_window_main_frame_routing_id_;
    156   int32 new_frame_routing_id_;
    157 
    158   // The last known good deserializer for sync messages.
    159   scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_;
    160 
    161   // A list of message filters added to this thread.
    162   std::vector<scoped_refptr<IPC::MessageFilter> > filters_;
    163 
    164   // Observers to notify.
    165   ObserverList<RenderProcessObserver> observers_;
    166 };
    167 
    168 }  // namespace content
    169 
    170 #endif  // CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
    171