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 // This file declares the C++ side of PyAuto, the python interface to 6 // Chromium automation. It access Chromium's internals using Automation Proxy. 7 8 #ifndef CHROME_TEST_PYAUTOLIB_PYAUTOLIB_H_ 9 #define CHROME_TEST_PYAUTOLIB_PYAUTOLIB_H_ 10 11 #include "base/compiler_specific.h" 12 #include "base/message_loop/message_loop.h" 13 #include "base/test/test_timeouts.h" 14 #include "base/time/time.h" 15 #include "chrome/test/ui/ui_test.h" 16 #include "chrome/test/ui/ui_test_suite.h" 17 18 #if defined(OS_MACOSX) 19 #include "base/mac/scoped_nsautorelease_pool.h" 20 #endif 21 22 class AutomationProxy; 23 24 // The C++ style guide forbids using default arguments but I'm taking the 25 // liberty of allowing it in this file. The sole purpose of this (and the 26 // .cc) is to support the python interface, and default args are allowed in 27 // python. Strictly adhering to the guide here would mean having to re-define 28 // all methods in python just for the sake of providing default args. This 29 // seems cumbersome and unwanted. 30 31 // Test Suite for Pyauto tests. All one-time initializations go here. 32 class PyUITestSuiteBase : public UITestSuite { 33 public: 34 PyUITestSuiteBase(int argc, char** argv); 35 virtual ~PyUITestSuiteBase(); 36 37 void InitializeWithPath(const base::FilePath& browser_dir); 38 39 void SetCrSourceRoot(const base::FilePath& path); 40 41 private: 42 #if defined(OS_MACOSX) 43 base::mac::ScopedNSAutoreleasePool pool_; 44 #endif 45 }; 46 47 // The primary class that interfaces with Automation Proxy. 48 // This class is accessed from python using swig. 49 class PyUITestBase : public UITestBase { 50 public: 51 // Only public methods are accessible from swig. 52 53 // Constructor. Lookup pyauto.py for doc on these args. 54 PyUITestBase(bool clear_profile, std::wstring homepage); 55 virtual ~PyUITestBase(); 56 57 // Initialize the setup. Should be called before launching the browser. 58 // |browser_dir| is the path to dir containing chromium binaries. 59 void Initialize(const base::FilePath& browser_dir); 60 UseNamedChannelID(const std::string & named_channel_id)61 void UseNamedChannelID(const std::string& named_channel_id) { 62 named_channel_id_ = named_channel_id; 63 launcher_.reset(CreateProxyLauncher()); 64 } 65 66 virtual ProxyLauncher* CreateProxyLauncher() OVERRIDE; 67 68 // SetUp,TearDown is redeclared as public to make it accessible from swig. 69 virtual void SetUp() OVERRIDE; 70 virtual void TearDown() OVERRIDE; 71 72 // AutomationProxy methods 73 74 // Meta-methods. Generic pattern of passing args and response as 75 // JSON dict to avoid future use of the SWIG interface and 76 // automation proxy additions. Returns response as JSON dict. 77 // Use -ve window_index for automation calls not targetted at a browser 78 // window. Example: Login call for chromeos. 79 std::string _SendJSONRequest(int window_index, 80 const std::string& request, 81 int timeout); 82 83 // Sets a cookie value for a url. Returns true on success. 84 bool SetCookie(const GURL& cookie_url, const std::string& value, 85 int window_index = 0, int tab_index = 0); 86 // Gets a cookie value for the given url. 87 std::string GetCookie(const GURL& cookie_url, int window_index = 0, 88 int tab_index = 0); 89 90 protected: 91 // Gets the automation proxy and checks that it exists. 92 virtual AutomationProxy* automation() const OVERRIDE; 93 94 virtual void SetLaunchSwitches() OVERRIDE; 95 96 private: 97 // Create JSON error responses. 98 void ErrorResponse(const std::string& error_string, 99 const std::string& request, 100 bool is_timeout, 101 std::string* response); 102 void RequestFailureResponse( 103 const std::string& request, 104 const base::TimeDelta& duration, 105 const base::TimeDelta& timeout, 106 std::string* response); 107 108 // Enables PostTask to main thread. 109 // Should be shared across multiple instances of PyUITestBase so that this 110 // class is re-entrant and multiple instances can be created. 111 // This is necessary since python's unittest module creates instances of 112 // TestCase at load time itself. 113 static base::MessageLoop* GetSharedMessageLoop( 114 base::MessageLoop::Type msg_loop_type); 115 static base::MessageLoop* message_loop_; 116 117 // Path to named channel id. 118 std::string named_channel_id_; 119 }; 120 121 #endif // CHROME_TEST_PYAUTOLIB_PYAUTOLIB_H_ 122