OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 <algorithm> |
| 6 #include <map> |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_util.h" |
| 15 #include "base/task_runner_util.h" |
| 16 #include "chrome/browser/ui/app_list/app_list_test_util.h" |
| 17 #include "chrome/browser/ui/app_list/arc/arc_app_icon.h" |
| 18 #include "chrome/browser/ui/app_list/arc/arc_app_item.h" |
| 19 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| 20 #include "chrome/browser/ui/app_list/arc/arc_app_model_builder.h" |
| 21 #include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h" |
| 22 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/arc/arc_bridge_service.h" |
| 24 #include "components/arc/test/fake_arc_bridge_service.h" |
| 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "ui/app_list/app_list_model.h" |
| 28 #include "ui/gfx/image/image_skia.h" |
| 29 |
| 30 namespace { |
| 31 |
| 32 std::string GetAppId(const arc::AppInfo& app_info) { |
| 33 return ArcAppListPrefs::GetAppId(app_info.package, app_info.activity); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 class ArcAppModelBuilderTest : public AppListTestBase { |
| 39 public: |
| 40 ArcAppModelBuilderTest() {} |
| 41 ~ArcAppModelBuilderTest() override { |
| 42 // Release profile file in order to keep right sequence. |
| 43 profile_.reset(); |
| 44 } |
| 45 |
| 46 void SetUp() override { |
| 47 AppListTestBase::SetUp(); |
| 48 |
| 49 // Make sure we have enough data for test. |
| 50 for (int i = 0; i < 3; ++i) { |
| 51 arc::AppInfo app; |
| 52 char buffer[16]; |
| 53 base::snprintf(buffer, arraysize(buffer), "Fake App %d", i); |
| 54 app.name = buffer; |
| 55 base::snprintf(buffer, arraysize(buffer), "fake.app.%d", i); |
| 56 app.package = buffer; |
| 57 base::snprintf(buffer, arraysize(buffer), "fake.app.%d.activity", i); |
| 58 app.activity = buffer; |
| 59 fake_apps_.push_back(app); |
| 60 } |
| 61 |
| 62 bridge_service_.reset(new arc::FakeArcBridgeService()); |
| 63 |
| 64 // Check initial conditions. |
| 65 EXPECT_EQ(bridge_service_.get(), arc::ArcBridgeService::Get()); |
| 66 EXPECT_EQ(true, !arc::ArcBridgeService::Get()->available()); |
| 67 EXPECT_EQ(arc::ArcBridgeService::State::STOPPED, |
| 68 arc::ArcBridgeService::Get()->state()); |
| 69 |
| 70 CreateBuilder(); |
| 71 |
| 72 // At this point we should have ArcAppListPrefs as observer of service. |
| 73 EXPECT_EQ( |
| 74 true, |
| 75 bridge_service()->HasObserver(ArcAppListPrefs::Get(profile_.get()))); |
| 76 } |
| 77 |
| 78 void TearDown() override { ResetBuilder(); } |
| 79 |
| 80 protected: |
| 81 // Creates a new builder, destroying any existing one. |
| 82 void CreateBuilder() { |
| 83 ResetBuilder(); // Destroy any existing builder in the correct order. |
| 84 |
| 85 model_.reset(new app_list::AppListModel); |
| 86 controller_.reset(new test::TestAppListControllerDelegate); |
| 87 builder_.reset(new ArcAppModelBuilder(controller_.get())); |
| 88 builder_->InitializeWithProfile(profile_.get(), model_.get()); |
| 89 } |
| 90 |
| 91 void ResetBuilder() { |
| 92 builder_.reset(); |
| 93 controller_.reset(); |
| 94 model_.reset(); |
| 95 } |
| 96 |
| 97 size_t GetArcItemCount() const { |
| 98 size_t arc_count = 0; |
| 99 const size_t count = model_->top_level_item_list()->item_count(); |
| 100 for (size_t i = 0; i < count; ++i) { |
| 101 app_list::AppListItem* item = model_->top_level_item_list()->item_at(i); |
| 102 if (item->GetItemType() == ArcAppItem::kItemType) |
| 103 ++arc_count; |
| 104 } |
| 105 return arc_count; |
| 106 } |
| 107 |
| 108 ArcAppItem* GetArcItem(size_t index) const { |
| 109 size_t arc_count = 0; |
| 110 const size_t count = model_->top_level_item_list()->item_count(); |
| 111 ArcAppItem* arc_item = nullptr; |
| 112 for (size_t i = 0; i < count; ++i) { |
| 113 app_list::AppListItem* item = model_->top_level_item_list()->item_at(i); |
| 114 if (item->GetItemType() == ArcAppItem::kItemType) { |
| 115 if (arc_count++ == index) { |
| 116 arc_item = reinterpret_cast<ArcAppItem*>(item); |
| 117 break; |
| 118 } |
| 119 } |
| 120 } |
| 121 EXPECT_NE(nullptr, arc_item); |
| 122 return arc_item; |
| 123 } |
| 124 |
| 125 ArcAppItem* FindArcItem(const std::string& id) const { |
| 126 const size_t count = GetArcItemCount(); |
| 127 for (size_t i = 0; i < count; ++i) { |
| 128 ArcAppItem* item = GetArcItem(i); |
| 129 if (item && item->id() == id) |
| 130 return item; |
| 131 } |
| 132 return nullptr; |
| 133 } |
| 134 |
| 135 // Validate that prefs and model have right content. |
| 136 void ValidateHaveApps(const std::vector<arc::AppInfo> apps) { |
| 137 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get()); |
| 138 const std::vector<std::string> ids = prefs->GetAppIds(); |
| 139 ASSERT_EQ(apps.size(), ids.size()); |
| 140 ASSERT_EQ(apps.size(), GetArcItemCount()); |
| 141 // In principle, order of items is not defined. |
| 142 for (auto& app : apps) { |
| 143 const std::string id = GetAppId(app); |
| 144 EXPECT_NE(std::find(ids.begin(), ids.end(), id), ids.end()); |
| 145 scoped_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(id); |
| 146 ASSERT_NE(nullptr, app_info.get()); |
| 147 EXPECT_EQ(app.name, app_info->name); |
| 148 EXPECT_EQ(app.package, app_info->package); |
| 149 EXPECT_EQ(app.activity, app_info->activity); |
| 150 |
| 151 const ArcAppItem* app_item = FindArcItem(id); |
| 152 ASSERT_NE(nullptr, app_item); |
| 153 EXPECT_EQ(app.name, app_item->GetDisplayName()); |
| 154 } |
| 155 } |
| 156 |
| 157 // Validate that requested apps have required ready state and other apps have |
| 158 // opposite state. |
| 159 void ValidateAppReadyState(const std::vector<arc::AppInfo> apps, bool ready) { |
| 160 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get()); |
| 161 ASSERT_NE(nullptr, prefs); |
| 162 |
| 163 std::vector<std::string> ids = prefs->GetAppIds(); |
| 164 EXPECT_EQ(ids.size(), GetArcItemCount()); |
| 165 |
| 166 // Process requested apps. |
| 167 for (auto& app : apps) { |
| 168 const std::string id = GetAppId(app); |
| 169 std::vector<std::string>::iterator it_id = std::find(ids.begin(), |
| 170 ids.end(), |
| 171 id); |
| 172 ASSERT_NE(it_id, ids.end()); |
| 173 ids.erase(it_id); |
| 174 |
| 175 scoped_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(id); |
| 176 ASSERT_NE(nullptr, app_info.get()); |
| 177 EXPECT_EQ(ready, app_info->ready); |
| 178 const ArcAppItem* app_item = FindArcItem(id); |
| 179 ASSERT_NE(nullptr, app_item); |
| 180 EXPECT_EQ(ready, app_item->ready()); |
| 181 } |
| 182 |
| 183 // Process the rest of the apps. |
| 184 for (auto& id : ids) { |
| 185 scoped_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(id); |
| 186 ASSERT_NE(nullptr, app_info.get()); |
| 187 EXPECT_NE(ready, app_info->ready); |
| 188 const ArcAppItem* app_item = FindArcItem(id); |
| 189 ASSERT_NE(nullptr, app_item); |
| 190 EXPECT_NE(ready, app_item->ready()); |
| 191 } |
| 192 |
| 193 } |
| 194 |
| 195 arc::FakeArcBridgeService* bridge_service() { return bridge_service_.get(); } |
| 196 |
| 197 const std::vector<arc::AppInfo>& fake_apps() const { return fake_apps_; } |
| 198 |
| 199 private: |
| 200 scoped_ptr<app_list::AppListModel> model_; |
| 201 scoped_ptr<test::TestAppListControllerDelegate> controller_; |
| 202 scoped_ptr<ArcAppModelBuilder> builder_; |
| 203 scoped_ptr<arc::FakeArcBridgeService> bridge_service_; |
| 204 std::vector<arc::AppInfo> fake_apps_; |
| 205 |
| 206 DISALLOW_COPY_AND_ASSIGN(ArcAppModelBuilderTest); |
| 207 }; |
| 208 |
| 209 TEST_F(ArcAppModelBuilderTest, RefreshAllOnReady) { |
| 210 EXPECT_EQ(0, bridge_service()->refresh_app_list_count()); |
| 211 bridge_service()->SetReady(); |
| 212 EXPECT_EQ(1, bridge_service()->refresh_app_list_count()); |
| 213 } |
| 214 |
| 215 TEST_F(ArcAppModelBuilderTest, RefreshAllFillsContent) { |
| 216 ValidateHaveApps(std::vector<arc::AppInfo>()); |
| 217 bridge_service()->SetReady(); |
| 218 bridge_service()->SendRefreshAppList(fake_apps()); |
| 219 ValidateHaveApps(fake_apps()); |
| 220 } |
| 221 |
| 222 TEST_F(ArcAppModelBuilderTest, MultipleRefreshAll) { |
| 223 ValidateHaveApps(std::vector<arc::AppInfo>()); |
| 224 bridge_service()->SetReady(); |
| 225 // Send info about all fake apps except last. |
| 226 std::vector<arc::AppInfo> apps1(fake_apps().begin(), fake_apps().end() - 1); |
| 227 bridge_service()->SendRefreshAppList(apps1); |
| 228 // At this point all apps (except last) should exist and be ready. |
| 229 ValidateHaveApps(apps1); |
| 230 ValidateAppReadyState(apps1, true); |
| 231 |
| 232 // Send info about all fake apps except first. |
| 233 std::vector<arc::AppInfo> apps2(fake_apps().begin() + 1, fake_apps().end()); |
| 234 bridge_service()->SendRefreshAppList(apps2); |
| 235 // At this point all apps should exist but first one should be non-ready. |
| 236 ValidateHaveApps(fake_apps()); |
| 237 ValidateAppReadyState(apps2, true); |
| 238 |
| 239 // Send info about all fake apps. |
| 240 bridge_service()->SendRefreshAppList(fake_apps()); |
| 241 // At this point all apps should exist and be ready. |
| 242 ValidateHaveApps(fake_apps()); |
| 243 ValidateAppReadyState(fake_apps(), true); |
| 244 |
| 245 // Send info no app available. |
| 246 bridge_service()->SendRefreshAppList(std::vector<arc::AppInfo>()); |
| 247 // At this point all apps should exist and be non-ready. |
| 248 ValidateHaveApps(fake_apps()); |
| 249 ValidateAppReadyState(fake_apps(), false); |
| 250 } |
| 251 |
| 252 TEST_F(ArcAppModelBuilderTest, StopServiceDisablesApps) { |
| 253 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get()); |
| 254 ASSERT_NE(nullptr, prefs); |
| 255 |
| 256 bridge_service()->SetReady(); |
| 257 EXPECT_EQ(static_cast<size_t>(0), GetArcItemCount()); |
| 258 EXPECT_EQ(static_cast<size_t>(0), prefs->GetAppIds().size()); |
| 259 |
| 260 bridge_service()->SendRefreshAppList(fake_apps()); |
| 261 std::vector<std::string> ids = prefs->GetAppIds(); |
| 262 EXPECT_EQ(fake_apps().size(), ids.size()); |
| 263 ValidateAppReadyState(fake_apps(), true); |
| 264 |
| 265 // Stopping service does not delete items. It makes them non-ready. |
| 266 bridge_service()->SetStopped(); |
| 267 // Ids should be the same. |
| 268 EXPECT_EQ(ids, prefs->GetAppIds()); |
| 269 ValidateAppReadyState(fake_apps(), false); |
| 270 } |
| 271 |
| 272 TEST_F(ArcAppModelBuilderTest, LaunchApps) { |
| 273 bridge_service()->SetReady(); |
| 274 bridge_service()->SendRefreshAppList(fake_apps()); |
| 275 |
| 276 // Simulate item activate. |
| 277 const arc::AppInfo& app_first = fake_apps()[0]; |
| 278 const arc::AppInfo& app_last = fake_apps()[0]; |
| 279 ArcAppItem* item_first = FindArcItem(GetAppId(app_first)); |
| 280 ArcAppItem* item_last = FindArcItem(GetAppId(app_last)); |
| 281 ASSERT_NE(nullptr, item_first); |
| 282 ASSERT_NE(nullptr, item_last); |
| 283 item_first->Activate(0); |
| 284 item_last->Activate(0); |
| 285 item_first->Activate(0); |
| 286 |
| 287 const ScopedVector<arc::FakeArcBridgeService::Request>& launch_requests = |
| 288 bridge_service()->launch_requests(); |
| 289 EXPECT_EQ(static_cast<size_t>(3), launch_requests.size()); |
| 290 EXPECT_EQ(true, launch_requests[0]->IsForApp(app_first)); |
| 291 EXPECT_EQ(true, launch_requests[1]->IsForApp(app_last)); |
| 292 EXPECT_EQ(true, launch_requests[2]->IsForApp(app_first)); |
| 293 } |
| 294 |
| 295 TEST_F(ArcAppModelBuilderTest, RequestIcons) { |
| 296 // Make sure we are on UI thread. |
| 297 ASSERT_EQ(true, |
| 298 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 299 |
| 300 bridge_service()->SetReady(); |
| 301 bridge_service()->SendRefreshAppList(fake_apps()); |
| 302 |
| 303 // Validate that no icon exists at the beginning and request icon for |
| 304 // each supported scale factor. This will start asynchronous loading. |
| 305 uint32_t expected_mask = 0; |
| 306 const std::vector<ui::ScaleFactor>& scale_factors = |
| 307 ui::GetSupportedScaleFactors(); |
| 308 for (auto& scale_factor : scale_factors) { |
| 309 expected_mask |= 1 << scale_factor; |
| 310 for (auto& app : fake_apps()) { |
| 311 ArcAppItem* app_item = FindArcItem(GetAppId(app)); |
| 312 ASSERT_NE(nullptr, app_item); |
| 313 const float scale = ui::GetScaleForScaleFactor(scale_factor); |
| 314 app_item->icon().GetRepresentation(scale); |
| 315 } |
| 316 } |
| 317 |
| 318 // Process pending tasks. |
| 319 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 320 base::RunLoop().RunUntilIdle(); |
| 321 |
| 322 // At this moment we should receive all requests for icon loading. |
| 323 const ScopedVector<arc::FakeArcBridgeService::IconRequest>& icon_requests = |
| 324 bridge_service()->icon_requests(); |
| 325 EXPECT_EQ(scale_factors.size() * fake_apps().size(), icon_requests.size()); |
| 326 std::map<std::string, uint32_t> app_masks; |
| 327 for (size_t i = 0; i < icon_requests.size(); ++i) { |
| 328 const arc::FakeArcBridgeService::IconRequest* icon_request = |
| 329 icon_requests[i]; |
| 330 const std::string id = ArcAppListPrefs::GetAppId(icon_request->package(), |
| 331 icon_request->activity()); |
| 332 // Make sure no double requests. |
| 333 EXPECT_NE(app_masks[id], |
| 334 app_masks[id] | (1 << icon_request->scale_factor())); |
| 335 app_masks[id] |= (1 << icon_request->scale_factor()); |
| 336 } |
| 337 |
| 338 // Validate that we have a request for each icon for each supported scale |
| 339 // factor. |
| 340 EXPECT_EQ(fake_apps().size(), app_masks.size()); |
| 341 for (auto& app : fake_apps()) { |
| 342 const std::string id = GetAppId(app); |
| 343 ASSERT_NE(app_masks.find(id), app_masks.end()); |
| 344 EXPECT_EQ(app_masks[id], expected_mask); |
| 345 } |
| 346 } |
| 347 |
| 348 TEST_F(ArcAppModelBuilderTest, InstallIcon) { |
| 349 // Make sure we are on UI thread. |
| 350 ASSERT_EQ(true, |
| 351 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 352 |
| 353 |
| 354 bridge_service()->SetReady(); |
| 355 bridge_service()->SendRefreshAppList(std::vector<arc::AppInfo>( |
| 356 fake_apps().begin(), fake_apps().begin() + 1)); |
| 357 const arc::AppInfo& app = fake_apps()[0]; |
| 358 |
| 359 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get()); |
| 360 ASSERT_NE(nullptr, prefs); |
| 361 |
| 362 const ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactors()[0]; |
| 363 const float scale = ui::GetScaleForScaleFactor(scale_factor); |
| 364 const base::FilePath icon_path = prefs->GetIconPath(GetAppId(app), |
| 365 scale_factor); |
| 366 EXPECT_EQ(true, !base::PathExists(icon_path)); |
| 367 |
| 368 const ArcAppItem* app_item = FindArcItem(GetAppId(app)); |
| 369 EXPECT_NE(nullptr, app_item); |
| 370 // This initiates async loading. |
| 371 app_item->icon().GetRepresentation(scale); |
| 372 |
| 373 // Process pending tasks. |
| 374 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 375 base::RunLoop().RunUntilIdle(); |
| 376 |
| 377 // Validating decoded content does not fit well for unit tests. |
| 378 ArcAppIcon::DisableDecodingForTesting(); |
| 379 |
| 380 // Now send generated icon for the app. |
| 381 std::string png_data; |
| 382 EXPECT_EQ(true, bridge_service()->GenerateAndSendIcon( |
| 383 app, |
| 384 static_cast<arc::ScaleFactor>(scale_factor), |
| 385 &png_data)); |
| 386 |
| 387 // Process pending tasks. |
| 388 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 389 base::RunLoop().RunUntilIdle(); |
| 390 |
| 391 // Validate that icons are installed, have right content and icon is |
| 392 // refreshed for ARC app item. |
| 393 EXPECT_EQ(true, base::PathExists(icon_path)); |
| 394 |
| 395 std::string icon_data; |
| 396 // Read the file from disk and compare with reference data. |
| 397 EXPECT_EQ(true, base::ReadFileToString(icon_path, &icon_data)); |
| 398 ASSERT_EQ(icon_data, png_data); |
| 399 } |
OLD | NEW |