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