• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_MODEL_TEST_UTIL_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_TEST_UTIL_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop.h"
15 #include "base/string16.h"
16 #include "chrome/browser/search_engines/template_url_model_observer.h"
17 #include "content/browser/browser_thread.h"
18 
19 class TemplateURLModel;
20 class TemplateURLModelTestingProfile;
21 class TestingTemplateURLModel;
22 class TestingProfile;
23 class WebDataService;
24 
25 // Implements functionality to make it easier to test TemplateURLModel and
26 // make changes to it.
27 class TemplateURLModelTestUtil : public TemplateURLModelObserver {
28  public:
29   TemplateURLModelTestUtil();
30 
31   virtual ~TemplateURLModelTestUtil();
32 
33   // Sets up the data structures for this class (mirroring gtest standard
34   // methods).
35   void SetUp();
36 
37   // Cleans up data structures for this class  (mirroring gtest standard
38   // methods).
39   void TearDown();
40 
41   // TemplateURLModelObserver implemementation.
42   virtual void OnTemplateURLModelChanged();
43 
44   // Gets the observer count.
45   int GetObserverCount();
46 
47   // Sets the observer count to 0.
48   void ResetObserverCount();
49 
50   // Blocks the caller until the service has finished servicing all pending
51   // requests.
52   static void BlockTillServiceProcessesRequests();
53 
54   // Blocks the caller until the I/O thread has finished servicing all pending
55   // requests.
56   static void BlockTillIOThreadProcessesRequests();
57 
58   // Makes sure the load was successful and sent the correct notification.
59   void VerifyLoad();
60 
61   // Makes the model believe it has been loaded (without actually doing the
62   // load). Since this avoids setting the built-in keyword version, the next
63   // load will do a merge from prepopulated data.
64   void ChangeModelToLoadState();
65 
66   // Deletes the current model (and doesn't create a new one).
67   void ClearModel();
68 
69   // Creates a new TemplateURLModel.
70   void ResetModel(bool verify_load);
71 
72   // Returns the search term from the last invocation of
73   // TemplateURLModel::SetKeywordSearchTermsForURL and clears the search term.
74   string16 GetAndClearSearchTerm();
75 
76   // Set the google base url.
77   void SetGoogleBaseURL(const std::string& base_url) const;
78 
79   // Returns the WebDataService.
80   WebDataService* GetWebDataService();
81 
82   // Returns the TemplateURLModel.
83   TemplateURLModel* model() const;
84 
85   // Returns the TestingProfile.
86   TestingProfile* profile() const;
87 
88   // Starts an I/O thread.
89   void StartIOThread();
90 
91  private:
92   MessageLoopForUI message_loop_;
93   // Needed to make the DeleteOnUIThread trait of WebDataService work
94   // properly.
95   BrowserThread ui_thread_;
96   scoped_ptr<TemplateURLModelTestingProfile> profile_;
97   scoped_ptr<TestingTemplateURLModel> model_;
98   int changed_count_;
99 
100   DISALLOW_COPY_AND_ASSIGN(TemplateURLModelTestUtil);
101 };
102 
103 #endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_MODEL_TEST_UTIL_H_
104