|
| 1 | +// Copyright 2017 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/app_list/app_list_syncable_service.h" |
| 6 | +#include "base/files/scoped_temp_dir.h" |
| 7 | +#include "chrome/browser/extensions/extension_service.h" |
| 8 | +#include "chrome/browser/profiles/profile_manager.h" |
| 9 | +#include "chrome/browser/ui/app_list/app_list_test_util.h" |
| 10 | +#include "chrome/test/base/testing_browser_process.h" |
| 11 | +#include "chrome/test/base/testing_profile.h" |
| 12 | +#include "components/crx_file/id_util.h" |
| 13 | +#include "extensions/browser/extension_system.h" |
| 14 | +#include "extensions/common/constants.h" |
| 15 | +#include "ui/app_list/app_list_item.h" |
| 16 | +#include "ui/app_list/app_list_model.h" |
| 17 | + |
| 18 | +namespace { |
| 19 | + |
| 20 | +scoped_refptr<extensions::Extension> MakeApp( |
| 21 | + const std::string& name, |
| 22 | + const std::string& id, |
| 23 | + extensions::Extension::InitFromValueFlags flags) { |
| 24 | + std::string err; |
| 25 | + base::DictionaryValue value; |
| 26 | + value.SetString("name", name); |
| 27 | + value.SetString("version", "0.0"); |
| 28 | + value.SetString("app.launch.web_url", "http://google.com"); |
| 29 | + scoped_refptr<extensions::Extension> app = extensions::Extension::Create( |
| 30 | + base::FilePath(), extensions::Manifest::INTERNAL, value, flags, id, &err); |
| 31 | + EXPECT_EQ(err, ""); |
| 32 | + return app; |
| 33 | +} |
| 34 | + |
| 35 | +// Creates next by natural sort ordering application id. Application id has to |
| 36 | +// have 32 chars each in range 'a' to 'p' inclusively. |
| 37 | +std::string CreateNextAppId(const std::string& app_id) { |
| 38 | + DCHECK(crx_file::id_util::IdIsValid(app_id)); |
| 39 | + std::string next_app_id = app_id; |
| 40 | + size_t index = next_app_id.length() - 1; |
| 41 | + while (index > 0 && next_app_id[index] == 'p') |
| 42 | + next_app_id[index--] = 'a'; |
| 43 | + DCHECK(next_app_id[index] != 'p'); |
| 44 | + next_app_id[index]++; |
| 45 | + DCHECK(crx_file::id_util::IdIsValid(next_app_id)); |
| 46 | + return next_app_id; |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace |
| 50 | + |
| 51 | +class AppListSyncableServiceTest : public AppListTestBase { |
| 52 | + public: |
| 53 | + AppListSyncableServiceTest() = default; |
| 54 | + ~AppListSyncableServiceTest() override = default; |
| 55 | + |
| 56 | + void SetUp() override { |
| 57 | + AppListTestBase::SetUp(); |
| 58 | + |
| 59 | + // Make sure we have a Profile Manager. |
| 60 | + DCHECK(temp_dir_.CreateUniqueTempDir()); |
| 61 | + TestingBrowserProcess::GetGlobal()->SetProfileManager( |
| 62 | + new ProfileManagerWithoutInit(temp_dir_.GetPath())); |
| 63 | + |
| 64 | + extensions::ExtensionSystem* extension_system = |
| 65 | + extensions::ExtensionSystem::Get(profile_.get()); |
| 66 | + DCHECK(extension_system); |
| 67 | + app_list_syncable_service_.reset( |
| 68 | + new app_list::AppListSyncableService(profile_.get(), extension_system)); |
| 69 | + } |
| 70 | + |
| 71 | + void TearDown() override { app_list_syncable_service_.reset(); } |
| 72 | + |
| 73 | + app_list::AppListModel* model() { |
| 74 | + return app_list_syncable_service_->GetModel(); |
| 75 | + } |
| 76 | + |
| 77 | + private: |
| 78 | + base::ScopedTempDir temp_dir_; |
| 79 | + std::unique_ptr<app_list::AppListSyncableService> app_list_syncable_service_; |
| 80 | + |
| 81 | + DISALLOW_COPY_AND_ASSIGN(AppListSyncableServiceTest); |
| 82 | +}; |
| 83 | + |
| 84 | +TEST_F(AppListSyncableServiceTest, OEMFolderForConflictingPos) { |
| 85 | + // Create a "web store" app. |
| 86 | + const std::string web_store_app_id(extensions::kWebStoreAppId); |
| 87 | + scoped_refptr<extensions::Extension> store = |
| 88 | + MakeApp("webstore", web_store_app_id, |
| 89 | + extensions::Extension::WAS_INSTALLED_BY_DEFAULT); |
| 90 | + service_->AddExtension(store.get()); |
| 91 | + |
| 92 | + // Create some app. Note its id should be greater than web store app id in |
| 93 | + // order to move app in case of conflicting pos after web store app. |
| 94 | + const std::string some_app_id = CreateNextAppId(extensions::kWebStoreAppId); |
| 95 | + scoped_refptr<extensions::Extension> some_app = |
| 96 | + MakeApp("some_app", some_app_id, |
| 97 | + extensions ::Extension::WAS_INSTALLED_BY_DEFAULT); |
| 98 | + service_->AddExtension(some_app.get()); |
| 99 | + |
| 100 | + app_list::AppListItem* web_store_item = model()->FindItem(web_store_app_id); |
| 101 | + ASSERT_TRUE(web_store_item); |
| 102 | + app_list::AppListItem* some_app_item = model()->FindItem(some_app_id); |
| 103 | + ASSERT_TRUE(some_app_item); |
| 104 | + |
| 105 | + // Simulate position conflict. |
| 106 | + model()->SetItemPosition(web_store_item, some_app_item->position()); |
| 107 | + |
| 108 | + // Install an OEM app. It must be placed by default after web store app but in |
| 109 | + // case of app of the same position should be shifted next. |
| 110 | + const std::string oem_app_id = CreateNextAppId(some_app_id); |
| 111 | + scoped_refptr<extensions::Extension> oem_app = MakeApp( |
| 112 | + "oem_app", oem_app_id, extensions::Extension::WAS_INSTALLED_BY_OEM); |
| 113 | + service_->AddExtension(oem_app.get()); |
| 114 | + |
| 115 | + size_t web_store_app_index; |
| 116 | + size_t some_app_index; |
| 117 | + size_t oem_app_index; |
| 118 | + size_t oem_folder_index; |
| 119 | + EXPECT_TRUE(model()->top_level_item_list()->FindItemIndex( |
| 120 | + web_store_app_id, &web_store_app_index)); |
| 121 | + EXPECT_TRUE(model()->top_level_item_list()->FindItemIndex(some_app_id, |
| 122 | + &some_app_index)); |
| 123 | + // OEM item is not top level element. |
| 124 | + EXPECT_FALSE(model()->top_level_item_list()->FindItemIndex(oem_app_id, |
| 125 | + &oem_app_index)); |
| 126 | + // But OEM folder is. |
| 127 | + EXPECT_TRUE(model()->top_level_item_list()->FindItemIndex( |
| 128 | + app_list::AppListSyncableService::kOemFolderId, &oem_folder_index)); |
| 129 | + |
| 130 | + // Ensure right item sequence. |
| 131 | + EXPECT_EQ(some_app_index, web_store_app_index + 1); |
| 132 | + EXPECT_EQ(oem_folder_index, web_store_app_index + 2); |
| 133 | +} |
0 commit comments