| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/strings/string16.h" | 6 #include "base/strings/string16.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search_engines/template_url.h" | 10 #include "chrome/browser/search_engines/template_url.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get()); | 118 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get()); |
| 119 if (simulate_load_failure) | 119 if (simulate_load_failure) |
| 120 model_->OnWebDataServiceRequestDone(0, NULL); | 120 model_->OnWebDataServiceRequestDone(0, NULL); |
| 121 | 121 |
| 122 controller_.reset(new KeywordEditorController(profile_.get())); | 122 controller_.reset(new KeywordEditorController(profile_.get())); |
| 123 controller_->table_model()->SetObserver(this); | 123 controller_->table_model()->SetObserver(this); |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Tests adding a TemplateURL. | 126 // Tests adding a TemplateURL. |
| 127 TEST_F(KeywordEditorControllerTest, Add) { | 127 TEST_F(KeywordEditorControllerTest, Add) { |
| 128 int original_row_count = table_model()->RowCount(); |
| 128 controller_->AddTemplateURL(kA, kB, "http://c"); | 129 controller_->AddTemplateURL(kA, kB, "http://c"); |
| 129 | 130 |
| 130 // Verify the observer was notified. | 131 // Verify the observer was notified. |
| 131 VerifyChangeCount(0, 0, 1, 0); | 132 VerifyChangeCount(0, 0, 1, 0); |
| 132 if (HasFatalFailure()) | 133 if (HasFatalFailure()) |
| 133 return; | 134 return; |
| 134 | 135 |
| 135 // Verify the TableModel has the new data. | 136 // Verify the TableModel has the new data. |
| 136 ASSERT_EQ(1, table_model()->RowCount()); | 137 ASSERT_EQ(original_row_count + 1, table_model()->RowCount()); |
| 137 | 138 |
| 138 // Verify the TemplateURLService has the new entry. | 139 // Verify the TemplateURLService has the new data. |
| 139 ASSERT_EQ(1U, model_->GetTemplateURLs().size()); | 140 const TemplateURL* turl = model_->GetTemplateURLForKeyword(kB); |
| 140 | 141 ASSERT_TRUE(turl); |
| 141 // Verify the entry is what we added. | |
| 142 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | |
| 143 EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name()); | 142 EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name()); |
| 144 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); | 143 EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); |
| 145 EXPECT_EQ("http://c", turl->url()); | 144 EXPECT_EQ("http://c", turl->url()); |
| 146 } | 145 } |
| 147 | 146 |
| 148 // Tests modifying a TemplateURL. | 147 // Tests modifying a TemplateURL. |
| 149 TEST_F(KeywordEditorControllerTest, Modify) { | 148 TEST_F(KeywordEditorControllerTest, Modify) { |
| 150 controller_->AddTemplateURL(kA, kB, "http://c"); | 149 controller_->AddTemplateURL(kA, kB, "http://c"); |
| 151 ClearChangeCount(); | 150 ClearChangeCount(); |
| 152 | 151 |
| 153 // Modify the entry. | 152 // Modify the entry. |
| 154 TemplateURL* turl = model_->GetTemplateURLs()[0]; | 153 TemplateURL* turl = model_->GetTemplateURLs()[0]; |
| 155 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1"); | 154 controller_->ModifyTemplateURL(turl, kA1, kB1, "http://c1"); |
| 156 | 155 |
| 157 // Make sure it was updated appropriately. | 156 // Make sure it was updated appropriately. |
| 158 VerifyChangeCount(0, 1, 0, 0); | 157 VerifyChangeCount(0, 1, 0, 0); |
| 159 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); | 158 EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); |
| 160 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); | 159 EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); |
| 161 EXPECT_EQ("http://c1", turl->url()); | 160 EXPECT_EQ("http://c1", turl->url()); |
| 162 } | 161 } |
| 163 | 162 |
| 164 // Tests making a TemplateURL the default search provider. | 163 // Tests making a TemplateURL the default search provider. |
| 165 TEST_F(KeywordEditorControllerTest, MakeDefault) { | 164 TEST_F(KeywordEditorControllerTest, MakeDefault) { |
| 166 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 165 int index = controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 167 ClearChangeCount(); | 166 ClearChangeCount(); |
| 168 | 167 |
| 169 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | 168 const TemplateURL* turl = model_->GetTemplateURLForKeyword(kB); |
| 170 int new_default = controller_->MakeDefaultTemplateURL(0); | 169 int new_default = controller_->MakeDefaultTemplateURL(index); |
| 171 EXPECT_EQ(0, new_default); | 170 EXPECT_EQ(index, new_default); |
| 172 // Making an item the default sends a handful of changes. Which are sent isn't | 171 // Making an item the default sends a handful of changes. Which are sent isn't |
| 173 // important, what is important is 'something' is sent. | 172 // important, what is important is 'something' is sent. |
| 174 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || | 173 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || |
| 175 removed_count_ > 0); | 174 removed_count_ > 0); |
| 176 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); | 175 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); |
| 177 | 176 |
| 178 // Making it default a second time should fail. | 177 // Making it default a second time should fail. |
| 179 new_default = controller_->MakeDefaultTemplateURL(0); | 178 new_default = controller_->MakeDefaultTemplateURL(index); |
| 180 EXPECT_EQ(-1, new_default); | 179 EXPECT_EQ(-1, new_default); |
| 181 } | 180 } |
| 182 | 181 |
| 183 // Tests that a TemplateURL can't be made the default if the default search | 182 // Tests that a TemplateURL can't be made the default if the default search |
| 184 // provider is managed via policy. | 183 // provider is managed via policy. |
| 185 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { | 184 TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { |
| 186 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 185 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 187 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); | 186 controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); |
| 188 ClearChangeCount(); | 187 ClearChangeCount(); |
| 189 | 188 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 EXPECT_TRUE(model_->is_default_search_managed()); | 226 EXPECT_TRUE(model_->is_default_search_managed()); |
| 228 EXPECT_TRUE(controller_->CanEdit(turl1)); | 227 EXPECT_TRUE(controller_->CanEdit(turl1)); |
| 229 EXPECT_TRUE(controller_->CanEdit(turl2)); | 228 EXPECT_TRUE(controller_->CanEdit(turl2)); |
| 230 EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider())); | 229 EXPECT_FALSE(controller_->CanEdit(model_->GetDefaultSearchProvider())); |
| 231 } | 230 } |
| 232 | 231 |
| 233 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) { | 232 TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) { |
| 234 // Simulate a failure to load Web Data. | 233 // Simulate a failure to load Web Data. |
| 235 Init(true); | 234 Init(true); |
| 236 | 235 |
| 237 controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); | 236 int index = controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); |
| 238 ClearChangeCount(); | 237 ClearChangeCount(); |
| 239 | 238 |
| 240 // This should not result in a crash. | 239 // This should not result in a crash. |
| 241 int new_default = controller_->MakeDefaultTemplateURL(0); | 240 int new_default = controller_->MakeDefaultTemplateURL(index); |
| 242 EXPECT_EQ(0, new_default); | 241 EXPECT_EQ(index, new_default); |
| 243 } | 242 } |
| 244 | 243 |
| 245 // Mutates the TemplateURLService and make sure table model is updating | 244 // Mutates the TemplateURLService and make sure table model is updating |
| 246 // appropriately. | 245 // appropriately. |
| 247 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) { | 246 TEST_F(KeywordEditorControllerTest, MutateTemplateURLService) { |
| 247 int original_row_count = table_model()->RowCount(); |
| 248 |
| 248 TemplateURLData data; | 249 TemplateURLData data; |
| 249 data.short_name = ASCIIToUTF16("b"); | 250 data.short_name = ASCIIToUTF16("b"); |
| 250 data.SetKeyword(ASCIIToUTF16("a")); | 251 data.SetKeyword(ASCIIToUTF16("a")); |
| 251 TemplateURL* turl = new TemplateURL(profile_.get(), data); | 252 TemplateURL* turl = new TemplateURL(profile_.get(), data); |
| 252 model_->Add(turl); | 253 model_->Add(turl); |
| 253 | 254 |
| 254 // Table model should have updated. | 255 // Table model should have updated. |
| 255 VerifyChangeCount(1, 0, 0, 0); | 256 VerifyChangeCount(1, 0, 0, 0); |
| 256 | 257 |
| 257 // And should contain the newly added TemplateURL. | 258 // And should contain the newly added TemplateURL. |
| 258 ASSERT_EQ(1, table_model()->RowCount()); | 259 ASSERT_EQ(original_row_count + 1, table_model()->RowCount()); |
| 259 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); | 260 ASSERT_GE(table_model()->IndexOfTemplateURL(turl), 0); |
| 260 } | 261 } |
| OLD | NEW |