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