| OLD | NEW |
| (Empty) |
| 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 #include "chrome/browser/ui/views/first_run_search_engine_view.h" | |
| 6 | |
| 7 #include "chrome/browser/search_engines/template_url.h" | |
| 8 #include "chrome/browser/search_engines/template_url_service.h" | |
| 9 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 10 #include "chrome/common/chrome_notification_types.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | |
| 12 #include "chrome/test/base/ui_test_utils.h" | |
| 13 #include "content/browser/browser_process_sub_thread.h" | |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 #include "content/test/test_browser_thread.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "ui/views/focus/accelerator_handler.h" | |
| 18 #include "ui/views/test/views_test_base.h" | |
| 19 #include "ui/views/widget/widget.h" | |
| 20 | |
| 21 typedef views::ViewsTestBase FirstRunSearchEngineViewTest; | |
| 22 | |
| 23 TEST_F(FirstRunSearchEngineViewTest, ClosingSelectsFirstEngine) { | |
| 24 // Create the first run search engine selector, and just close the window. | |
| 25 // The first engine in the vector returned by GetTemplateURLs should be set as | |
| 26 // the default engine. | |
| 27 TestingProfile profile; | |
| 28 profile.CreateTemplateURLService(); | |
| 29 profile.BlockUntilTemplateURLServiceLoaded(); | |
| 30 | |
| 31 // Set a dummy provider as the default so we can verify something changed. | |
| 32 TemplateURLService* service = | |
| 33 TemplateURLServiceFactory::GetForProfile(&profile); | |
| 34 ASSERT_TRUE(service != NULL); | |
| 35 EXPECT_EQ(NULL, service->GetDefaultSearchProvider()); | |
| 36 TemplateURL* d1 = new TemplateURL; | |
| 37 TemplateURL* d2 = new TemplateURL; | |
| 38 TemplateURL* d3 = new TemplateURL; | |
| 39 service->Add(d1); | |
| 40 service->Add(d2); | |
| 41 service->Add(d3); | |
| 42 service->SetDefaultSearchProvider(d3); | |
| 43 | |
| 44 FirstRunSearchEngineView* contents = | |
| 45 new FirstRunSearchEngineView(&profile, false); | |
| 46 contents->set_quit_on_closing(false); | |
| 47 views::Widget* window = views::Widget::CreateWindow(contents); | |
| 48 window->Show(); | |
| 49 window->Close(); | |
| 50 RunPendingMessages(); // Allows the window to be destroyed after Close(); | |
| 51 | |
| 52 TemplateURLService::TemplateURLVector template_urls = | |
| 53 service->GetTemplateURLs(); | |
| 54 ASSERT_TRUE(!template_urls.empty()); | |
| 55 EXPECT_EQ(template_urls.front(), service->GetDefaultSearchProvider()); | |
| 56 } | |
| 57 | |
| 58 TEST_F(FirstRunSearchEngineViewTest, ClosingBeforeServiceLoadedAbortsClose) { | |
| 59 // This ensures the current thread is named the UI thread, so code that checks | |
| 60 // that this is the UI thread doesn't assert. | |
| 61 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 62 message_loop()); | |
| 63 content::BrowserProcessSubThread db_thread(content::BrowserThread::DB); | |
| 64 db_thread.StartWithOptions(base::Thread::Options()); | |
| 65 | |
| 66 TestingProfile profile; | |
| 67 // We need to initialize the web database before accessing the template url | |
| 68 // service otherwise the template url service will init itself synchronously | |
| 69 // and appear to be loaded. | |
| 70 profile.CreateWebDataService(false); | |
| 71 profile.CreateTemplateURLService(); | |
| 72 | |
| 73 // Instead of giving the template url service a chance to load, try and close | |
| 74 // the window immediately. | |
| 75 FirstRunSearchEngineView* contents = | |
| 76 new FirstRunSearchEngineView(&profile, false); | |
| 77 contents->set_quit_on_closing(false); | |
| 78 views::Widget* window = views::Widget::CreateWindow(contents); | |
| 79 window->Show(); | |
| 80 EXPECT_TRUE(window->IsVisible()); | |
| 81 window->Close(); | |
| 82 // The window wouldn't actually be closed until a return to the message loop, | |
| 83 // which we don't want to spin here because the window shouldn't have closed | |
| 84 // in the correct case. The window is however actually hidden immediately when | |
| 85 // the window is allowed to close, so we can test for visibility to make sure | |
| 86 // it hasn't. | |
| 87 EXPECT_TRUE(window->IsVisible()); | |
| 88 | |
| 89 // Now let the template url service a chance to load. | |
| 90 ui_test_utils::WindowedNotificationObserver service_load_observer( | |
| 91 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | |
| 92 content::NotificationService::AllSources()); | |
| 93 service_load_observer.Wait(); | |
| 94 | |
| 95 // .. and try and close the window. It should be allowed to now. | |
| 96 window->Close(); | |
| 97 EXPECT_FALSE(window->IsVisible()); | |
| 98 | |
| 99 // Allow the window to actually close. | |
| 100 RunPendingMessages(); | |
| 101 | |
| 102 // Verify goodness. Because we actually went to the trouble of starting the | |
| 103 // WebDB, we will have real data in the model, so we can verify a choice was | |
| 104 // made without having to seed the model with dummy data. | |
| 105 TemplateURLService* service = | |
| 106 TemplateURLServiceFactory::GetForProfile(&profile); | |
| 107 ASSERT_TRUE(service != NULL); | |
| 108 EXPECT_TRUE(service->GetDefaultSearchProvider() != NULL); | |
| 109 } | |
| OLD | NEW |