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