1 // Copyright 2013 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_BROWSER_UI_SEARCH_INSTANT_TEST_UTILS_H_ 6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_TEST_UTILS_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/run_loop.h" 13 #include "base/strings/string16.h" 14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser_instant_controller.h" 16 #include "chrome/browser/ui/browser_window.h" 17 #include "chrome/browser/ui/omnibox/location_bar.h" 18 #include "chrome/browser/ui/search/instant_controller.h" 19 #include "chrome/common/search_types.h" 20 #include "net/test/spawned_test_server/spawned_test_server.h" 21 #include "url/gurl.h" 22 23 class BrowserInstantController; 24 class InstantController; 25 class InstantModel; 26 class OmniboxView; 27 28 namespace content { 29 class WebContents; 30 }; 31 32 // This utility class is meant to be used in a "mix-in" fashion, giving the 33 // derived test class additional Instant-related functionality. 34 class InstantTestBase { 35 protected: 36 InstantTestBase() 37 : https_test_server_( 38 net::SpawnedTestServer::TYPE_HTTPS, 39 net::BaseTestServer::SSLOptions(), 40 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) { 41 } 42 virtual ~InstantTestBase() {} 43 44 protected: 45 void SetupInstant(Browser* browser); 46 void Init(const GURL& instant_url); 47 48 void SetInstantURL(const std::string& url); 49 50 void set_browser(Browser* browser) { 51 browser_ = browser; 52 } 53 54 BrowserInstantController* browser_instant() { 55 return browser_->instant_controller(); 56 } 57 58 InstantController* instant() { 59 return browser_->instant_controller()->instant(); 60 } 61 62 OmniboxView* omnibox() { 63 return browser_->window()->GetLocationBar()->GetLocationEntry(); 64 } 65 66 const GURL& instant_url() const { return instant_url_; } 67 68 net::SpawnedTestServer& https_test_server() { return https_test_server_; } 69 70 void KillInstantRenderView(); 71 72 void FocusOmnibox(); 73 void FocusOmniboxAndWaitForInstantNTPSupport(); 74 75 void SetOmniboxText(const std::string& text); 76 77 void PressEnterAndWaitForNavigation(); 78 79 bool GetBoolFromJS(content::WebContents* contents, 80 const std::string& script, 81 bool* result) WARN_UNUSED_RESULT; 82 bool GetIntFromJS(content::WebContents* contents, 83 const std::string& script, 84 int* result) WARN_UNUSED_RESULT; 85 bool GetStringFromJS(content::WebContents* contents, 86 const std::string& script, 87 std::string* result) WARN_UNUSED_RESULT; 88 bool ExecuteScript(const std::string& script) WARN_UNUSED_RESULT; 89 bool CheckVisibilityIs(content::WebContents* contents, 90 bool expected) WARN_UNUSED_RESULT; 91 92 std::string GetOmniboxText(); 93 94 // Loads a named image from url |image| from the given |rvh| host. |loaded| 95 // returns whether the image was able to load without error. 96 // The method returns true if the JavaScript executed cleanly. 97 bool LoadImage(content::RenderViewHost* rvh, 98 const std::string& image, 99 bool* loaded); 100 101 // Returns the omnibox's inline autocompletion (shown in blue highlight). 102 string16 GetBlueText(); 103 104 private: 105 GURL instant_url_; 106 107 Browser* browser_; 108 109 // HTTPS Testing server, started on demand. 110 net::SpawnedTestServer https_test_server_; 111 112 DISALLOW_COPY_AND_ASSIGN(InstantTestBase); 113 }; 114 115 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_TEST_UTILS_H_ 116