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_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_TEST_UTIL_H_ 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_TEST_UTIL_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/files/scoped_temp_dir.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/message_loop/message_loop.h" 15 #include "base/strings/string16.h" 16 #include "chrome/browser/search_engines/template_url_service_observer.h" 17 #include "chrome/test/base/testing_browser_process.h" 18 #include "content/public/test/test_browser_thread_bundle.h" 19 20 class GURL; 21 class TemplateURLService; 22 class TestingProfile; 23 class TestingTemplateURLService; 24 class TestingProfile; 25 class WebDataService; 26 27 // TemplateURLServiceTestUtilBase contains basic API to ease testing of 28 // TemplateURLService. User should take care of the infrastructure separately. 29 class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver { 30 public: 31 TemplateURLServiceTestUtilBase(); 32 virtual ~TemplateURLServiceTestUtilBase(); 33 34 void CreateTemplateUrlService(); 35 36 // TemplateURLServiceObserver implemementation. 37 virtual void OnTemplateURLServiceChanged() OVERRIDE; 38 39 // Gets the observer count. 40 int GetObserverCount(); 41 42 // Sets the observer count to 0. 43 void ResetObserverCount(); 44 45 // Makes sure the load was successful and sent the correct notification. 46 void VerifyLoad(); 47 48 // Makes the model believe it has been loaded (without actually doing the 49 // load). Since this avoids setting the built-in keyword version, the next 50 // load will do a merge from prepopulated data. 51 void ChangeModelToLoadState(); 52 53 // Deletes the current model (and doesn't create a new one). 54 void ClearModel(); 55 56 // Creates a new TemplateURLService. 57 void ResetModel(bool verify_load); 58 59 // Returns the search term from the last invocation of 60 // TemplateURLService::SetKeywordSearchTermsForURL and clears the search term. 61 base::string16 GetAndClearSearchTerm(); 62 63 // Set the google base url. |base_url| must be valid. 64 void SetGoogleBaseURL(const GURL& base_url) const; 65 66 // Set the managed preferences for the default search provider and trigger 67 // notification. If |alternate_url| is empty, uses an empty list of alternate 68 // URLs, otherwise use a list containing a single entry. 69 void SetManagedDefaultSearchPreferences( 70 bool enabled, 71 const std::string& name, 72 const std::string& keyword, 73 const std::string& search_url, 74 const std::string& suggest_url, 75 const std::string& icon_url, 76 const std::string& encodings, 77 const std::string& alternate_url, 78 const std::string& search_terms_replacement_key); 79 80 // Remove all the managed preferences for the default search provider and 81 // trigger notification. 82 void RemoveManagedDefaultSearchPreferences(); 83 84 // Returns the TemplateURLService. 85 TemplateURLService* model() const; 86 87 // Returns the TestingProfile. 88 virtual TestingProfile* profile() const = 0; 89 90 private: 91 int changed_count_; 92 93 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTestUtilBase); 94 }; 95 96 // TemplateURLServiceTestUtil sets up TestingProfile, TemplateURLService and 97 // required threads. 98 class TemplateURLServiceTestUtil : public TemplateURLServiceTestUtilBase { 99 public: 100 TemplateURLServiceTestUtil(); 101 virtual ~TemplateURLServiceTestUtil(); 102 103 // Sets up the data structures for this class (mirroring gtest standard 104 // methods). 105 void SetUp(); 106 107 // Cleans up data structures for this class (mirroring gtest standard 108 // methods). 109 void TearDown(); 110 111 // Returns the TestingProfile. 112 virtual TestingProfile* profile() const OVERRIDE; 113 114 private: 115 // Needed to make the DeleteOnUIThread trait of WebDataService work 116 // properly. 117 content::TestBrowserThreadBundle thread_bundle_; 118 scoped_ptr<TestingProfile> profile_; 119 base::ScopedTempDir temp_dir_; 120 121 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTestUtil); 122 }; 123 124 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_TEST_UTIL_H_ 125