OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/views/first_run_search_engine_view.h" | 5 #include "chrome/browser/ui/views/first_run_search_engine_view.h" |
6 | 6 |
7 #include "chrome/browser/search_engines/template_url.h" | 7 #include "chrome/browser/search_engines/template_url.h" |
8 #include "chrome/browser/search_engines/template_url_service.h" | 8 #include "chrome/browser/search_engines/template_url_service.h" |
9 #include "chrome/browser/search_engines/template_url_service_factory.h" | 9 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 10 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/test/base/testing_profile.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" |
11 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "ui/views/focus/accelerator_handler.h" | 17 #include "ui/views/focus/accelerator_handler.h" |
13 #include "ui/views/test/views_test_base.h" | 18 #include "ui/views/test/views_test_base.h" |
14 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
15 | 20 |
16 typedef views::ViewsTestBase FirstRunSearchEngineViewTest; | 21 typedef views::ViewsTestBase FirstRunSearchEngineViewTest; |
17 | 22 |
18 TEST_F(FirstRunSearchEngineViewTest, ClosingSelectsFirstEngine) { | 23 TEST_F(FirstRunSearchEngineViewTest, ClosingSelectsFirstEngine) { |
19 // Create the first run search engine selector, and just close the window. | 24 // Create the first run search engine selector, and just close the window. |
20 // The first engine in the vector returned by GetTemplateURLs should be set as | 25 // The first engine in the vector returned by GetTemplateURLs should be set as |
(...skipping 21 matching lines...) Expand all Loading... |
42 views::Widget* window = views::Widget::CreateWindow(contents); | 47 views::Widget* window = views::Widget::CreateWindow(contents); |
43 window->Show(); | 48 window->Show(); |
44 window->Close(); | 49 window->Close(); |
45 RunPendingMessages(); // Allows the window to be destroyed after Close(); | 50 RunPendingMessages(); // Allows the window to be destroyed after Close(); |
46 | 51 |
47 TemplateURLService::TemplateURLVector template_urls = | 52 TemplateURLService::TemplateURLVector template_urls = |
48 service->GetTemplateURLs(); | 53 service->GetTemplateURLs(); |
49 ASSERT_TRUE(!template_urls.empty()); | 54 ASSERT_TRUE(!template_urls.empty()); |
50 EXPECT_EQ(template_urls.front(), service->GetDefaultSearchProvider()); | 55 EXPECT_EQ(template_urls.front(), service->GetDefaultSearchProvider()); |
51 } | 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 |