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 CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_
      6 #define CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_
      7 
      8 #include <string>
      9 
     10 #include "base/files/file_path.h"
     11 #include "base/path_service.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "base/test/test_process_killer_win.h"
     14 #include "chrome/common/chrome_switches.h"
     15 #include "chrome_frame/chrome_frame_automation.h"
     16 #include "chrome_frame/chrome_frame_plugin.h"
     17 #include "chrome_frame/navigation_constraints.h"
     18 #include "chrome_frame/test/test_scrubber.h"
     19 #include "chrome_frame/test/test_with_web_server.h"
     20 #include "chrome_frame/utils.h"
     21 
     22 template <typename T>
     23 class AutomationMockDelegate
     24     : public CWindowImpl<T>,
     25       public ChromeFramePlugin<T> {
     26  public:
     27   AutomationMockDelegate(base::MessageLoop* caller_message_loop,
     28       int launch_timeout, bool perform_version_check,
     29       const std::wstring& profile_name,
     30       const std::wstring& language,
     31       bool incognito, bool is_widget_mode)
     32       : caller_message_loop_(caller_message_loop), is_connected_(false),
     33         navigation_result_(false),
     34         mock_server_(1337, L"127.0.0.1",
     35             chrome_frame_test::GetTestDataFolder()) {
     36 
     37     // Endeavour to only kill off Chrome Frame derived Chrome processes.
     38     base::KillAllNamedProcessesWithArgument(
     39         UTF8ToWide(chrome_frame_test::kChromeImageName),
     40         UTF8ToWide(switches::kChromeFrame));
     41 
     42     mock_server_.ExpectAndServeAnyRequests(CFInvocation(CFInvocation::NONE));
     43 
     44     base::FilePath profile_path;
     45     GetChromeFrameProfilePath(profile_name, &profile_path);
     46     chrome_frame_test::OverrideDataDirectoryForThisTest(profile_path.value());
     47 
     48     automation_client_ = new ChromeFrameAutomationClient;
     49     GURL empty;
     50     scoped_refptr<ChromeFrameLaunchParams> clp(
     51         new ChromeFrameLaunchParams(empty, empty, profile_path, profile_name,
     52             language, incognito, is_widget_mode, false));
     53     clp->set_launch_timeout(launch_timeout);
     54     clp->set_version_check(perform_version_check);
     55     automation_client_->Initialize(this, clp);
     56   }
     57   ~AutomationMockDelegate() {
     58     if (automation_client_.get()) {
     59       automation_client_->Uninitialize();
     60       automation_client_ = NULL;
     61     }
     62     if (IsWindow())
     63       DestroyWindow();
     64   }
     65 
     66   // Navigate external tab to the specified url through automation
     67   bool Navigate(const std::string& url) {
     68     NavigationConstraintsImpl navigation_constraints;
     69     url_ = GURL(url);
     70     bool result = automation_client_->InitiateNavigation(
     71         url, std::string(), &navigation_constraints);
     72     if (!result)
     73       OnLoadFailed(0, url);
     74     return result;
     75   }
     76 
     77   // Navigate the external to a 'file://' url for unit test files
     78   bool NavigateRelativeFile(const std::wstring& file) {
     79     base::FilePath cf_source_path;
     80     PathService::Get(base::DIR_SOURCE_ROOT, &cf_source_path);
     81     std::wstring file_url(L"file://");
     82     file_url.append(cf_source_path.Append(
     83         FILE_PATH_LITERAL("chrome_frame")).Append(
     84             FILE_PATH_LITERAL("test")).Append(
     85                 FILE_PATH_LITERAL("data")).Append(file).value());
     86     return Navigate(WideToUTF8(file_url));
     87   }
     88 
     89   bool NavigateRelative(const std::wstring& relative_url) {
     90     return Navigate(WideToUTF8(mock_server_.Resolve(relative_url.c_str())));
     91   }
     92 
     93   virtual void OnAutomationServerReady() {
     94     if (automation_client_.get()) {
     95       Create(NULL, 0, NULL, WS_OVERLAPPEDWINDOW);
     96       DCHECK(IsWindow());
     97       is_connected_ = true;
     98     } else {
     99       NOTREACHED();
    100     }
    101   }
    102 
    103   virtual void OnAutomationServerLaunchFailed() {
    104     QuitMessageLoop();
    105   }
    106 
    107   virtual void OnLoad(const GURL& url) {
    108     if (url_ == url) {
    109       navigation_result_ = true;
    110     } else {
    111       QuitMessageLoop();
    112     }
    113   }
    114 
    115   virtual void OnLoadFailed(int error_code, const std::string& url) {
    116     navigation_result_ = false;
    117     QuitMessageLoop();
    118   }
    119 
    120   ChromeFrameAutomationClient* automation() {
    121     return automation_client_.get();
    122   }
    123   const GURL& url() const {
    124     return url_;
    125   }
    126   bool is_connected() const {
    127     return is_connected_;
    128   }
    129   bool navigation_result() const {
    130     return navigation_result_;
    131   }
    132 
    133   BEGIN_MSG_MAP(AutomationMockDelegate)
    134   END_MSG_MAP()
    135 
    136  protected:
    137   void QuitMessageLoop() {
    138     // Quit on the caller message loop has to be called on the caller
    139     // thread.
    140     caller_message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
    141   }
    142 
    143  private:
    144   testing::StrictMock<MockWebServer> mock_server_;
    145   base::MessageLoop* caller_message_loop_;
    146   GURL url_;
    147   bool is_connected_;
    148   bool navigation_result_;
    149 };
    150 
    151 class AutomationMockLaunch
    152     : public AutomationMockDelegate<AutomationMockLaunch> {
    153  public:
    154   typedef AutomationMockDelegate<AutomationMockLaunch> Base;
    155   AutomationMockLaunch(base::MessageLoop* caller_message_loop,
    156                        int launch_timeout)
    157       : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
    158              false) {
    159   }
    160   virtual void OnAutomationServerReady() {
    161     Base::OnAutomationServerReady();
    162     QuitMessageLoop();
    163   }
    164   bool launch_result() const {
    165     return is_connected();
    166   }
    167 };
    168 
    169 class AutomationMockNavigate
    170     : public AutomationMockDelegate<AutomationMockNavigate> {
    171  public:
    172   typedef AutomationMockDelegate<AutomationMockNavigate> Base;
    173   AutomationMockNavigate(base::MessageLoop* caller_message_loop,
    174                          int launch_timeout)
    175       : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
    176              false) {
    177   }
    178   virtual void OnLoad(const GURL& url) {
    179     Base::OnLoad(url);
    180     QuitMessageLoop();
    181   }
    182 };
    183 
    184 class AutomationMockPostMessage
    185     : public AutomationMockDelegate<AutomationMockPostMessage> {
    186  public:
    187   typedef AutomationMockDelegate<AutomationMockPostMessage> Base;
    188   AutomationMockPostMessage(base::MessageLoop* caller_message_loop,
    189                             int launch_timeout)
    190       : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
    191              false),
    192         postmessage_result_(false) {}
    193   bool postmessage_result() const {
    194     return postmessage_result_;
    195   }
    196   virtual void OnLoad(const GURL& url) {
    197     Base::OnLoad(url);
    198     if (navigation_result()) {
    199       automation()->ForwardMessageFromExternalHost("Test", "null", "*");
    200     }
    201   }
    202   virtual void OnMessageFromChromeFrame(const std::string& message,
    203                                         const std::string& origin,
    204                                         const std::string& target) {
    205     postmessage_result_ = true;
    206     QuitMessageLoop();
    207   }
    208  private:
    209   bool postmessage_result_;
    210 };
    211 
    212 class AutomationMockHostNetworkRequestStart
    213     : public AutomationMockDelegate<AutomationMockHostNetworkRequestStart> {
    214  public:
    215   typedef AutomationMockDelegate<AutomationMockHostNetworkRequestStart> Base;
    216   AutomationMockHostNetworkRequestStart(base::MessageLoop* caller_message_loop,
    217       int launch_timeout)
    218       : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
    219              false),
    220         request_start_result_(false) {
    221     if (automation()) {
    222       automation()->set_use_chrome_network(false);
    223     }
    224   }
    225   bool request_start_result() const {
    226     return request_start_result_;
    227   }
    228   virtual void OnRequestStart(int request_id,
    229                               const AutomationURLRequest& request) {
    230     request_start_result_ = true;
    231     QuitMessageLoop();
    232   }
    233   virtual void OnLoad(const GURL& url) {
    234     Base::OnLoad(url);
    235   }
    236  private:
    237   bool request_start_result_;
    238 };
    239 
    240 #endif  // CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_
    241