- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search_engines / keyword_editor_controller_unittest.cc
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 #include "base/message_loop/message_loop.h"
6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search_engines/template_url.h"
11 #include "chrome/browser/search_engines/template_url_service.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h"
14 #include "chrome/browser/ui/search_engines/template_url_table_model.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "content/public/browser/notification_service.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/base/models/table_model_observer.h"
21
22 static const string16 kA(ASCIIToUTF16("a"));
23 static const string16 kA1(ASCIIToUTF16("a1"));
24 static const string16 kB(ASCIIToUTF16("b"));
25 static const string16 kB1(ASCIIToUTF16("b1"));
26
27 // Base class for keyword editor tests. Creates a profile containing an
28 // empty TemplateURLService.
29 class KeywordEditorControllerTest : public testing::Test,
30                                     public ui::TableModelObserver {
31  public:
32   // Initializes all of the state.
33   void Init(bool simulate_load_failure);
34
35   virtual void SetUp() {
36     Init(false);
37   }
38
39   virtual void OnModelChanged() OVERRIDE {
40     model_changed_count_++;
41   }
42
43   virtual void OnItemsChanged(int start, int length) OVERRIDE {
44     items_changed_count_++;
45   }
46
47   virtual void OnItemsAdded(int start, int length) OVERRIDE {
48     added_count_++;
49   }
50
51   virtual void OnItemsRemoved(int start, int length) OVERRIDE {
52     removed_count_++;
53   }
54
55   void VerifyChangeCount(int model_changed_count, int item_changed_count,
56                          int added_count, int removed_count) {
57     ASSERT_EQ(model_changed_count, model_changed_count_);
58     ASSERT_EQ(item_changed_count, items_changed_count_);
59     ASSERT_EQ(added_count, added_count_);
60     ASSERT_EQ(removed_count, removed_count_);
61     ClearChangeCount();
62   }
63
64   void ClearChangeCount() {
65     model_changed_count_ = items_changed_count_ = added_count_ =
66         removed_count_ = 0;
67   }
68
69   void SimulateDefaultSearchIsManaged(const std::string& url) {
70     ASSERT_FALSE(url.empty());
71     TestingPrefServiceSyncable* service = profile_->GetTestingPrefService();
72     service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
73                             new base::FundamentalValue(true));
74     service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
75                             new base::StringValue(url));
76     service->SetManagedPref(prefs::kDefaultSearchProviderName,
77                             new base::StringValue("managed"));
78     // Clear the IDs that are not specified via policy.
79     service->SetManagedPref(prefs::kDefaultSearchProviderID,
80                             new base::StringValue(std::string()));
81     service->SetManagedPref(prefs::kDefaultSearchProviderPrepopulateID,
82                             new base::StringValue(std::string()));
83     model_->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
84                     content::NotificationService::AllSources(),
85                     content::NotificationService::NoDetails());
86   }
87
88   TemplateURLTableModel* table_model() const {
89     return controller_->table_model();
90   }
91
92  protected:
93   base::MessageLoopForUI message_loop_;
94   scoped_ptr<TestingProfile> profile_;
95   scoped_ptr<KeywordEditorController> controller_;
96   TemplateURLService* model_;
97
98   int model_changed_count_;
99   int items_changed_count_;
100   int added_count_;
101   int removed_count_;
102 };
103
104 void KeywordEditorControllerTest::Init(bool simulate_load_failure) {
105   ClearChangeCount();
106
107   // If init is called twice, make sure that the controller is destroyed before
108   // the profile is.
109   controller_.reset();
110   profile_.reset(new TestingProfile());
111   TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
112       profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor);
113
114   model_ = TemplateURLServiceFactory::GetForProfile(profile_.get());
115   if (simulate_load_failure)
116     model_->OnWebDataServiceRequestDone(0, NULL);
117
118   controller_.reset(new KeywordEditorController(profile_.get()));
119   controller_->table_model()->SetObserver(this);
120 }
121
122 // Tests adding a TemplateURL.
123 TEST_F(KeywordEditorControllerTest, Add) {
124   controller_->AddTemplateURL(kA, kB, "http://c");
125
126   // Verify the observer was notified.
127   VerifyChangeCount(0, 0, 1, 0);
128   if (HasFatalFailure())
129     return;
130
131   // Verify the TableModel has the new data.
132   ASSERT_EQ(1, table_model()->RowCount());
133
134   // Verify the TemplateURLService has the new entry.
135   ASSERT_EQ(1U, model_->GetTemplateURLs().size());
136
137   // Verify the entry is what we added.
138   const TemplateURL* turl = model_->GetTemplateURLs()[0];
139   EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name());
140   EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword());
141   EXPECT_EQ("http://c", turl->url());
142 }
143
144 // Tests modifying a TemplateURL.
145 TEST_F(KeywordEditorControllerTest, Modify) {
146   controller_->AddTemplateURL(kA, kB, "http://c");
147   ClearChangeCount();
148
149   // Modify the entry.
150   TemplateURL* turl = model_->GetTemplateURLs()[0];
151   controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1");
152
153   // Make sure it was updated appropriately.
154   VerifyChangeCount(0, 1, 0, 0);
155   EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name());
156   EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword());
157   EXPECT_EQ("http://c1", turl->url());
158 }
159
160 // Tests making a TemplateURL the default search provider.
161 TEST_F(KeywordEditorControllerTest, MakeDefault) {
162   controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
163   ClearChangeCount();
164
165   const TemplateURL* turl = model_->GetTemplateURLs()[0];
166   int new_default = controller_->MakeDefaultTemplateURL(0);
167   EXPECT_EQ(0, new_default);
168   // Making an item the default sends a handful of changes. Which are sent isn't
169   // important, what is important is 'something' is sent.
170   ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 ||
171               removed_count_ > 0);
172   ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl);
173
174   // Making it default a second time should fail.
175   new_default = controller_->MakeDefaultTemplateURL(0);
176   EXPECT_EQ(-1, new_default);
177 }
178
179 // Tests that a TemplateURL can't be made the default if the default search
180 // provider is managed via policy.
181 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) {
182   controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
183   controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}");
184   ClearChangeCount();
185
186   const TemplateURL* turl1 =
187       model_->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
188   ASSERT_TRUE(turl1 != NULL);
189   const TemplateURL* turl2 =
190       model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1"));
191   ASSERT_TRUE(turl2 != NULL);
192
193   EXPECT_TRUE(controller_->CanMakeDefault(turl1));
194   EXPECT_TRUE(controller_->CanMakeDefault(turl2));
195
196   SimulateDefaultSearchIsManaged(turl2->url());
197   EXPECT_TRUE(model_->is_default_search_managed());
198
199   EXPECT_FALSE(controller_->CanMakeDefault(turl1));
200   EXPECT_FALSE(controller_->CanMakeDefault(turl2));
201 }
202
203 // Tests that a TemplateURL can't be edited if it is the managed default search
204 // provider.
205 TEST_F(KeywordEditorControllerTest, EditManagedDefault) {
206   controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
207   controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}");
208   ClearChangeCount();
209
210   const TemplateURL* turl1 =
211       model_->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
212   ASSERT_TRUE(turl1 != NULL);
213   const TemplateURL* turl2 =
214       model_->GetTemplateURLForKeyword(ASCIIToUTF16("b1"));
215   ASSERT_TRUE(turl2 != NULL);
216
217   EXPECT_TRUE(controller_->CanEdit(turl1));
218   EXPECT_TRUE(controller_->CanEdit(turl2));
219
220   // Simulate setting a managed default.  This will add another template URL to
221   // the model.
222   SimulateDefaultSearchIsManaged(turl2->url());
223   EXPECT_TRUE(model_->is_default_search_managed());
224   EXPECT_TRUE(controller_->CanEdit(turl1));
225   EXPECT_TRUE(controller_->CanEdit(turl2));
226   EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider()));
227 }
228
229 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) {
230   // Simulate a failure to load Web Data.
231   Init(true);
232
233   controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}");
234   ClearChangeCount();
235
236   // This should not result in a crash.
237   int new_default = controller_->MakeDefaultTemplateURL(0);
238   EXPECT_EQ(0, new_default);
239 }
240
241 // Mutates the TemplateURLService and make sure table model is updating
242 // appropriately.
243 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) {
244   TemplateURLData data;
245   data.short_name = ASCIIToUTF16("b");
246   data.SetKeyword(ASCIIToUTF16("a"));
247   TemplateURL* turl = new TemplateURL(profile_.get(), data);
248   model_->Add(turl);
249
250   // Table model should have updated.
251   VerifyChangeCount(1, 0, 0, 0);
252
253   // And should contain the newly added TemplateURL.
254   ASSERT_EQ(1, table_model()->RowCount());
255   ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl));
256 }