| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/scoped_nsobject.h" | 5 #include "base/scoped_nsobject.h" |
| 6 #include "base/utf_string_conversions.h" | |
| 7 #include "chrome/browser/profiles/profile.h" | 6 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/search_engines/template_url.h" | 7 #include "chrome/browser/search_engines/template_url.h" |
| 9 #include "chrome/browser/search_engines/template_url_model.h" | 8 #include "chrome/browser/search_engines/template_url_model.h" |
| 10 #include "chrome/browser/ui/cocoa/browser_test_helper.h" | 9 #include "chrome/browser/ui/cocoa/browser_test_helper.h" |
| 11 #import "chrome/browser/ui/cocoa/search_engine_list_model.h" | 10 #import "chrome/browser/ui/cocoa/search_engine_list_model.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #import "testing/gtest_mac.h" | 12 #import "testing/gtest_mac.h" |
| 14 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
| 15 | 14 |
| 16 // A helper for NSNotifications. Makes a note that it's been called back. | 15 // A helper for NSNotifications. Makes a note that it's been called back. |
| 17 @interface SearchEngineListHelper : NSObject { | 16 @interface SearchEngineListHelper : NSObject { |
| 18 @public | 17 @public |
| 19 BOOL sawNotification_; | 18 BOOL sawNotification_; |
| 20 } | 19 } |
| 21 @end | 20 @end |
| 22 | 21 |
| 23 @implementation SearchEngineListHelper | 22 @implementation SearchEngineListHelper |
| 24 - (void)entryChanged:(NSNotification*)notify { | 23 - (void)entryChanged:(NSNotification*)notify { |
| 25 sawNotification_ = YES; | 24 sawNotification_ = YES; |
| 26 } | 25 } |
| 27 @end | 26 @end |
| 28 | 27 |
| 29 class SearchEngineListModelTest : public PlatformTest { | 28 class SearchEngineListModelTest : public PlatformTest { |
| 30 public: | 29 public: |
| 31 SearchEngineListModelTest() { | 30 SearchEngineListModelTest() { |
| 32 // Build a fake set of template urls. | 31 // Build a fake set of template urls. |
| 33 template_model_.reset(new TemplateURLModel(helper_.profile())); | 32 template_model_.reset(new TemplateURLModel(helper_.profile())); |
| 34 TemplateURL* t_url = new TemplateURL(); | 33 TemplateURL* t_url = new TemplateURL(); |
| 35 t_url->SetURL("http://www.google.com/?q={searchTerms}", 0, 0); | 34 t_url->SetURL("http://www.google.com/?q={searchTerms}", 0, 0); |
| 36 t_url->set_keyword(ASCIIToUTF16("keyword")); | 35 t_url->set_keyword(L"keyword"); |
| 37 t_url->set_short_name(ASCIIToUTF16("google")); | 36 t_url->set_short_name(L"google"); |
| 38 t_url->set_show_in_default_list(true); | 37 t_url->set_show_in_default_list(true); |
| 39 template_model_->Add(t_url); | 38 template_model_->Add(t_url); |
| 40 t_url = new TemplateURL(); | 39 t_url = new TemplateURL(); |
| 41 t_url->SetURL("http://www.google2.com/?q={searchTerms}", 0, 0); | 40 t_url->SetURL("http://www.google2.com/?q={searchTerms}", 0, 0); |
| 42 t_url->set_keyword(ASCIIToUTF16("keyword2")); | 41 t_url->set_keyword(L"keyword2"); |
| 43 t_url->set_short_name(ASCIIToUTF16("google2")); | 42 t_url->set_short_name(L"google2"); |
| 44 t_url->set_show_in_default_list(true); | 43 t_url->set_show_in_default_list(true); |
| 45 template_model_->Add(t_url); | 44 template_model_->Add(t_url); |
| 46 EXPECT_EQ(template_model_->GetTemplateURLs().size(), 2U); | 45 EXPECT_EQ(template_model_->GetTemplateURLs().size(), 2U); |
| 47 | 46 |
| 48 model_.reset([[SearchEngineListModel alloc] | 47 model_.reset([[SearchEngineListModel alloc] |
| 49 initWithModel:template_model_.get()]); | 48 initWithModel:template_model_.get()]); |
| 50 notification_helper_.reset([[SearchEngineListHelper alloc] init]); | 49 notification_helper_.reset([[SearchEngineListHelper alloc] init]); |
| 51 [[NSNotificationCenter defaultCenter] | 50 [[NSNotificationCenter defaultCenter] |
| 52 addObserver:notification_helper_.get() | 51 addObserver:notification_helper_.get() |
| 53 selector:@selector(entryChanged:) | 52 selector:@selector(entryChanged:) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 77 | 76 |
| 78 TEST_F(SearchEngineListModelTest, Default) { | 77 TEST_F(SearchEngineListModelTest, Default) { |
| 79 EXPECT_EQ([model_ defaultIndex], -1); | 78 EXPECT_EQ([model_ defaultIndex], -1); |
| 80 | 79 |
| 81 [model_ setDefaultIndex:1]; | 80 [model_ setDefaultIndex:1]; |
| 82 EXPECT_EQ([model_ defaultIndex], 1); | 81 EXPECT_EQ([model_ defaultIndex], 1); |
| 83 | 82 |
| 84 // Add two more URLs, neither of which are shown in the default list. | 83 // Add two more URLs, neither of which are shown in the default list. |
| 85 TemplateURL* t_url = new TemplateURL(); | 84 TemplateURL* t_url = new TemplateURL(); |
| 86 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); | 85 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); |
| 87 t_url->set_keyword(ASCIIToUTF16("keyword3")); | 86 t_url->set_keyword(L"keyword3"); |
| 88 t_url->set_short_name(ASCIIToUTF16("google3 not eligible")); | 87 t_url->set_short_name(L"google3 not eligible"); |
| 89 t_url->set_show_in_default_list(false); | 88 t_url->set_show_in_default_list(false); |
| 90 template_model_->Add(t_url); | 89 template_model_->Add(t_url); |
| 91 t_url = new TemplateURL(); | 90 t_url = new TemplateURL(); |
| 92 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); | 91 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); |
| 93 t_url->set_keyword(ASCIIToUTF16("keyword4")); | 92 t_url->set_keyword(L"keyword4"); |
| 94 t_url->set_short_name(ASCIIToUTF16("google4")); | 93 t_url->set_short_name(L"google4"); |
| 95 t_url->set_show_in_default_list(false); | 94 t_url->set_show_in_default_list(false); |
| 96 template_model_->Add(t_url); | 95 template_model_->Add(t_url); |
| 97 | 96 |
| 98 // Still should only have 2 engines and not these newly added ones. | 97 // Still should only have 2 engines and not these newly added ones. |
| 99 EXPECT_EQ([[model_ searchEngines] count], 2U); | 98 EXPECT_EQ([[model_ searchEngines] count], 2U); |
| 100 | 99 |
| 101 // Since keyword3 is not in the default list, the 2nd index in the default | 100 // Since keyword3 is not in the default list, the 2nd index in the default |
| 102 // keyword list should be keyword4. Test for http://crbug.com/21898. | 101 // keyword list should be keyword4. Test for http://crbug.com/21898. |
| 103 template_model_->SetDefaultSearchProvider(t_url); | 102 template_model_->SetDefaultSearchProvider(t_url); |
| 104 EXPECT_EQ([[model_ searchEngines] count], 3U); | 103 EXPECT_EQ([[model_ searchEngines] count], 3U); |
| 105 EXPECT_EQ([model_ defaultIndex], 2); | 104 EXPECT_EQ([model_ defaultIndex], 2); |
| 106 | 105 |
| 107 NSString* defaultString = [[model_ searchEngines] objectAtIndex:2]; | 106 NSString* defaultString = [[model_ searchEngines] objectAtIndex:2]; |
| 108 EXPECT_NSEQ(@"google4", defaultString); | 107 EXPECT_NSEQ(@"google4", defaultString); |
| 109 } | 108 } |
| 110 | 109 |
| 111 TEST_F(SearchEngineListModelTest, DefaultChosenFromUI) { | 110 TEST_F(SearchEngineListModelTest, DefaultChosenFromUI) { |
| 112 EXPECT_EQ([model_ defaultIndex], -1); | 111 EXPECT_EQ([model_ defaultIndex], -1); |
| 113 | 112 |
| 114 [model_ setDefaultIndex:1]; | 113 [model_ setDefaultIndex:1]; |
| 115 EXPECT_EQ([model_ defaultIndex], 1); | 114 EXPECT_EQ([model_ defaultIndex], 1); |
| 116 | 115 |
| 117 // Add two more URLs, the first one not shown in the default list. | 116 // Add two more URLs, the first one not shown in the default list. |
| 118 TemplateURL* t_url = new TemplateURL(); | 117 TemplateURL* t_url = new TemplateURL(); |
| 119 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); | 118 t_url->SetURL("http://www.google3.com/?q={searchTerms}", 0, 0); |
| 120 t_url->set_keyword(ASCIIToUTF16("keyword3")); | 119 t_url->set_keyword(L"keyword3"); |
| 121 t_url->set_short_name(ASCIIToUTF16("google3 not eligible")); | 120 t_url->set_short_name(L"google3 not eligible"); |
| 122 t_url->set_show_in_default_list(false); | 121 t_url->set_show_in_default_list(false); |
| 123 template_model_->Add(t_url); | 122 template_model_->Add(t_url); |
| 124 t_url = new TemplateURL(); | 123 t_url = new TemplateURL(); |
| 125 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); | 124 t_url->SetURL("http://www.google4.com/?q={searchTerms}", 0, 0); |
| 126 t_url->set_keyword(ASCIIToUTF16("keyword4")); | 125 t_url->set_keyword(L"keyword4"); |
| 127 t_url->set_short_name(ASCIIToUTF16("google4")); | 126 t_url->set_short_name(L"google4"); |
| 128 t_url->set_show_in_default_list(true); | 127 t_url->set_show_in_default_list(true); |
| 129 template_model_->Add(t_url); | 128 template_model_->Add(t_url); |
| 130 | 129 |
| 131 // We should have 3 engines. | 130 // We should have 3 engines. |
| 132 EXPECT_EQ([[model_ searchEngines] count], 3U); | 131 EXPECT_EQ([[model_ searchEngines] count], 3U); |
| 133 | 132 |
| 134 // Simulate the UI setting the default to the third entry. | 133 // Simulate the UI setting the default to the third entry. |
| 135 [model_ setDefaultIndex:2]; | 134 [model_ setDefaultIndex:2]; |
| 136 EXPECT_EQ([model_ defaultIndex], 2); | 135 EXPECT_EQ([model_ defaultIndex], 2); |
| 137 | 136 |
| 138 // The default search provider should be google4. | 137 // The default search provider should be google4. |
| 139 EXPECT_EQ(template_model_->GetDefaultSearchProvider(), t_url); | 138 EXPECT_EQ(template_model_->GetDefaultSearchProvider(), t_url); |
| 140 } | 139 } |
| 141 | 140 |
| 142 // Make sure that when the back-end model changes that we get a notification. | 141 // Make sure that when the back-end model changes that we get a notification. |
| 143 TEST_F(SearchEngineListModelTest, Notification) { | 142 TEST_F(SearchEngineListModelTest, Notification) { |
| 144 // Add one more item to force a notification. | 143 // Add one more item to force a notification. |
| 145 TemplateURL* t_url = new TemplateURL(); | 144 TemplateURL* t_url = new TemplateURL(); |
| 146 t_url->SetURL("http://www.google3.com/foo/bar", 0, 0); | 145 t_url->SetURL("http://www.google3.com/foo/bar", 0, 0); |
| 147 t_url->set_keyword(ASCIIToUTF16("keyword3")); | 146 t_url->set_keyword(L"keyword3"); |
| 148 t_url->set_short_name(ASCIIToUTF16("google3")); | 147 t_url->set_short_name(L"google3"); |
| 149 t_url->set_show_in_default_list(true); | 148 t_url->set_show_in_default_list(true); |
| 150 template_model_->Add(t_url); | 149 template_model_->Add(t_url); |
| 151 | 150 |
| 152 EXPECT_TRUE(notification_helper_.get()->sawNotification_); | 151 EXPECT_TRUE(notification_helper_.get()->sawNotification_); |
| 153 } | 152 } |
| OLD | NEW |