| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "ui/app_list/views/app_list_view.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "ui/app_list/app_list_switches.h" | |
| 12 #include "ui/app_list/pagination_model.h" | |
| 13 #include "ui/app_list/test/app_list_test_model.h" | |
| 14 #include "ui/app_list/test/app_list_test_view_delegate.h" | |
| 15 #include "ui/app_list/views/app_list_main_view.h" | |
| 16 #include "ui/app_list/views/contents_view.h" | |
| 17 #include "ui/app_list/views/search_box_view.h" | |
| 18 #include "ui/aura/test/aura_test_base.h" | |
| 19 #include "ui/aura/window.h" | |
| 20 #include "ui/views/test/views_test_base.h" | |
| 21 #include "ui/views/views_delegate.h" | |
| 22 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" | |
| 23 | |
| 24 namespace app_list { | |
| 25 namespace test { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // Choose a set that is 3 regular app list pages and 2 landscape app list pages. | |
| 30 const int kInitialItems = 34; | |
| 31 | |
| 32 const char* TestName() { | |
| 33 return ::testing::UnitTest::GetInstance()->current_test_info()->name(); | |
| 34 } | |
| 35 | |
| 36 // Allows the same tests to run with different contexts: either an Ash-style | |
| 37 // root window or a desktop window tree host. | |
| 38 class AppListViewTestContext { | |
| 39 public: | |
| 40 AppListViewTestContext(aura::Window* parent); | |
| 41 ~AppListViewTestContext(); | |
| 42 | |
| 43 // Test displaying the app list and performs a standard set of checks on its | |
| 44 // top level views. Then closes the window. | |
| 45 void RunDisplayTest(); | |
| 46 | |
| 47 // A standard set of checks on a view, e.g., ensuring it is drawn and visible. | |
| 48 static void CheckView(views::View* subview); | |
| 49 | |
| 50 // Invoked when the Widget is closing, and the view it contains is about to | |
| 51 // be torn down. This only occurs in a run loop and will be used as a signal | |
| 52 // to quit. | |
| 53 void NativeWidgetClosing() { | |
| 54 view_ = NULL; | |
| 55 run_loop_->Quit(); | |
| 56 } | |
| 57 | |
| 58 // Whether the experimental "landscape" app launcher UI is being tested. | |
| 59 bool is_landscape() const { return is_landscape_; } | |
| 60 | |
| 61 private: | |
| 62 const bool is_landscape_; | |
| 63 scoped_ptr<base::RunLoop> run_loop_; | |
| 64 PaginationModel pagination_model_; | |
| 65 app_list::AppListView* view_; // Owned by native widget. | |
| 66 app_list::test::AppListTestViewDelegate* delegate_; // Owned by |view_|; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(AppListViewTestContext); | |
| 69 }; | |
| 70 | |
| 71 // Extend the regular AppListTestViewDelegate to communicate back to the test | |
| 72 // context. Note the test context doesn't simply inherit this, because the | |
| 73 // delegate is owned by the view. | |
| 74 class UnitTestViewDelegate : public app_list::test::AppListTestViewDelegate { | |
| 75 public: | |
| 76 UnitTestViewDelegate(AppListViewTestContext* parent) : parent_(parent) {} | |
| 77 | |
| 78 // Overridden from app_list::test::AppListTestViewDelegate: | |
| 79 virtual void ViewClosing() OVERRIDE { parent_->NativeWidgetClosing(); } | |
| 80 | |
| 81 private: | |
| 82 AppListViewTestContext* parent_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(UnitTestViewDelegate); | |
| 85 }; | |
| 86 | |
| 87 AppListViewTestContext::AppListViewTestContext(aura::Window* parent) | |
| 88 : is_landscape_(EndsWith(TestName(), "Landscape", true)) { | |
| 89 if (is_landscape_) { | |
| 90 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 91 switches::kEnableExperimentalAppListPosition); | |
| 92 } | |
| 93 | |
| 94 delegate_ = new UnitTestViewDelegate(this); | |
| 95 view_ = new app_list::AppListView(delegate_); | |
| 96 | |
| 97 // Initialize centered around a point that ensures the window is wholly shown. | |
| 98 view_->InitAsBubbleAtFixedLocation(parent, | |
| 99 &pagination_model_, | |
| 100 gfx::Point(300, 300), | |
| 101 views::BubbleBorder::FLOAT, | |
| 102 false /* border_accepts_events */); | |
| 103 } | |
| 104 | |
| 105 AppListViewTestContext::~AppListViewTestContext() { | |
| 106 // The view observes the PaginationModel which is about to get destroyed, so | |
| 107 // if the view is not already deleted by the time this destructor is called, | |
| 108 // there will be problems. | |
| 109 EXPECT_FALSE(view_); | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 void AppListViewTestContext::CheckView(views::View* subview) { | |
| 114 ASSERT_TRUE(subview); | |
| 115 EXPECT_TRUE(subview->parent()); | |
| 116 EXPECT_TRUE(subview->visible()); | |
| 117 EXPECT_TRUE(subview->IsDrawn()); | |
| 118 } | |
| 119 | |
| 120 void AppListViewTestContext::RunDisplayTest() { | |
| 121 EXPECT_FALSE(view_->GetWidget()->IsVisible()); | |
| 122 EXPECT_EQ(-1, pagination_model_.total_pages()); | |
| 123 view_->GetWidget()->Show(); | |
| 124 delegate_->GetTestModel()->PopulateApps(kInitialItems); | |
| 125 | |
| 126 run_loop_.reset(new base::RunLoop); | |
| 127 AppListView::SetNextPaintCallback(run_loop_->QuitClosure()); | |
| 128 run_loop_->Run(); | |
| 129 | |
| 130 EXPECT_TRUE(view_->GetWidget()->IsVisible()); | |
| 131 | |
| 132 if (is_landscape_) | |
| 133 EXPECT_EQ(2, pagination_model_.total_pages()); | |
| 134 else | |
| 135 EXPECT_EQ(3, pagination_model_.total_pages()); | |
| 136 EXPECT_EQ(0, pagination_model_.selected_page()); | |
| 137 | |
| 138 // Checks on the main view. | |
| 139 AppListMainView* main_view = view_->app_list_main_view(); | |
| 140 EXPECT_NO_FATAL_FAILURE(CheckView(main_view)); | |
| 141 EXPECT_NO_FATAL_FAILURE(CheckView(main_view->search_box_view())); | |
| 142 EXPECT_NO_FATAL_FAILURE(CheckView(main_view->contents_view())); | |
| 143 | |
| 144 view_->GetWidget()->Close(); | |
| 145 run_loop_.reset(new base::RunLoop); | |
| 146 run_loop_->Run(); | |
| 147 | |
| 148 // |view_| should have been deleted and set to NULL via ViewClosing(). | |
| 149 EXPECT_FALSE(view_); | |
| 150 } | |
| 151 | |
| 152 class AppListViewTestAura : public views::ViewsTestBase { | |
| 153 public: | |
| 154 AppListViewTestAura() {} | |
| 155 virtual ~AppListViewTestAura() {} | |
| 156 | |
| 157 // testing::Test overrides: | |
| 158 virtual void SetUp() OVERRIDE { | |
| 159 views::ViewsTestBase::SetUp(); | |
| 160 test_context_.reset(new AppListViewTestContext(GetContext())); | |
| 161 } | |
| 162 | |
| 163 virtual void TearDown() OVERRIDE { | |
| 164 test_context_.reset(); | |
| 165 views::ViewsTestBase::TearDown(); | |
| 166 } | |
| 167 | |
| 168 protected: | |
| 169 scoped_ptr<AppListViewTestContext> test_context_; | |
| 170 | |
| 171 private: | |
| 172 DISALLOW_COPY_AND_ASSIGN(AppListViewTestAura); | |
| 173 }; | |
| 174 | |
| 175 class AppListViewTestDesktop : public views::ViewsTestBase { | |
| 176 public: | |
| 177 AppListViewTestDesktop() {} | |
| 178 virtual ~AppListViewTestDesktop() {} | |
| 179 | |
| 180 // testing::Test overrides: | |
| 181 virtual void SetUp() OVERRIDE { | |
| 182 set_views_delegate(new AppListViewTestViewsDelegate(this)); | |
| 183 views::ViewsTestBase::SetUp(); | |
| 184 test_context_.reset(new AppListViewTestContext(NULL)); | |
| 185 } | |
| 186 | |
| 187 virtual void TearDown() OVERRIDE { | |
| 188 test_context_.reset(); | |
| 189 views::ViewsTestBase::TearDown(); | |
| 190 } | |
| 191 | |
| 192 protected: | |
| 193 scoped_ptr<AppListViewTestContext> test_context_; | |
| 194 | |
| 195 private: | |
| 196 class AppListViewTestViewsDelegate : public views::TestViewsDelegate { | |
| 197 public: | |
| 198 AppListViewTestViewsDelegate(AppListViewTestDesktop* parent) | |
| 199 : parent_(parent) {} | |
| 200 | |
| 201 // Overridden from views::ViewsDelegate: | |
| 202 virtual void OnBeforeWidgetInit( | |
| 203 views::Widget::InitParams* params, | |
| 204 views::internal::NativeWidgetDelegate* delegate) OVERRIDE; | |
| 205 | |
| 206 private: | |
| 207 AppListViewTestDesktop* parent_; | |
| 208 | |
| 209 DISALLOW_COPY_AND_ASSIGN(AppListViewTestViewsDelegate); | |
| 210 }; | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(AppListViewTestDesktop); | |
| 213 }; | |
| 214 | |
| 215 void AppListViewTestDesktop::AppListViewTestViewsDelegate::OnBeforeWidgetInit( | |
| 216 views::Widget::InitParams* params, | |
| 217 views::internal::NativeWidgetDelegate* delegate) { | |
| 218 // Mimic the logic in ChromeViewsDelegate::OnBeforeWidgetInit(). Except, for | |
| 219 // ChromeOS, use the root window from the AuraTestHelper rather than depending | |
| 220 // on ash::Shell:GetPrimaryRootWindow(). Also assume non-ChromeOS is never the | |
| 221 // Ash desktop, as that is covered by AppListViewTestAura. | |
| 222 #if defined(OS_CHROMEOS) | |
| 223 if (!params->parent && !params->context) | |
| 224 params->context = parent_->GetContext(); | |
| 225 #elif defined(USE_AURA) | |
| 226 if (params->parent == NULL && params->context == NULL && params->top_level) | |
| 227 params->native_widget = new views::DesktopNativeWidgetAura(delegate); | |
| 228 #endif | |
| 229 } | |
| 230 | |
| 231 } // namespace | |
| 232 | |
| 233 // Tests showing the app list with basic test model in an ash-style root window. | |
| 234 TEST_F(AppListViewTestAura, Display) { | |
| 235 EXPECT_FALSE(test_context_->is_landscape()); // Sanity check. | |
| 236 host()->Show(); | |
| 237 EXPECT_NO_FATAL_FAILURE(test_context_->RunDisplayTest()); | |
| 238 } | |
| 239 | |
| 240 // Tests showing the app list on the desktop. Note on ChromeOS, this will still | |
| 241 // use the regular root window. | |
| 242 TEST_F(AppListViewTestDesktop, Display) { | |
| 243 EXPECT_FALSE(test_context_->is_landscape()); | |
| 244 EXPECT_NO_FATAL_FAILURE(test_context_->RunDisplayTest()); | |
| 245 } | |
| 246 | |
| 247 // Landscape versions of the above. | |
| 248 | |
| 249 TEST_F(AppListViewTestAura, DisplayLandscape) { | |
| 250 EXPECT_TRUE(test_context_->is_landscape()); | |
| 251 host()->Show(); | |
| 252 EXPECT_NO_FATAL_FAILURE(test_context_->RunDisplayTest()); | |
| 253 } | |
| 254 | |
| 255 TEST_F(AppListViewTestDesktop, DisplayLandscape) { | |
| 256 EXPECT_TRUE(test_context_->is_landscape()); | |
| 257 EXPECT_NO_FATAL_FAILURE(test_context_->RunDisplayTest()); | |
| 258 } | |
| 259 | |
| 260 } // namespace test | |
| 261 } // namespace app_list | |
| OLD | NEW |