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