| 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 "base/bind.h" | |
| 6 #include "base/bind_helpers.h" | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/thread_task_runner_handle.h" | |
| 16 #include "base/values.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "chrome/browser/browser_process.h" | |
| 19 #include "chrome/browser/extensions/api/preferences_private/preferences_private_
api.h" | |
| 20 #include "chrome/browser/extensions/extension_apitest.h" | |
| 21 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | |
| 23 #include "chrome/browser/profiles/profile_manager.h" | |
| 24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 25 #include "chrome/browser/sync/chrome_sync_client.h" | |
| 26 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 27 #include "chrome/browser/sync/profile_sync_test_util.h" | |
| 28 #include "chrome/browser/ui/browser.h" | |
| 29 #include "chrome/common/channel_info.h" | |
| 30 #include "chrome/common/chrome_constants.h" | |
| 31 #include "chrome/common/chrome_paths.h" | |
| 32 #include "chrome/test/base/testing_profile.h" | |
| 33 #include "components/bookmarks/common/bookmark_constants.h" | |
| 34 #include "components/browser_sync/browser/profile_sync_service.h" | |
| 35 #include "components/sync_driver/signin_manager_wrapper.h" | |
| 36 #include "components/sync_driver/sync_api_component_factory_mock.h" | |
| 37 #include "components/sync_driver/sync_prefs.h" | |
| 38 #include "content/public/browser/browser_context.h" | |
| 39 #include "extensions/test/extension_test_message_listener.h" | |
| 40 | |
| 41 #if defined(OS_CHROMEOS) | |
| 42 #include "chromeos/chromeos_switches.h" | |
| 43 #endif | |
| 44 | |
| 45 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction; | |
| 46 | |
| 47 namespace { | |
| 48 | |
| 49 class FakeProfileSyncService : public ProfileSyncService { | |
| 50 public: | |
| 51 explicit FakeProfileSyncService(Profile* profile) | |
| 52 : ProfileSyncService(CreateProfileSyncServiceParamsForTest(profile)), | |
| 53 sync_initialized_(true), | |
| 54 initialized_state_violation_(false) {} | |
| 55 | |
| 56 ~FakeProfileSyncService() override {} | |
| 57 | |
| 58 static scoped_ptr<KeyedService> BuildFakeProfileSyncService( | |
| 59 content::BrowserContext* context) { | |
| 60 return make_scoped_ptr( | |
| 61 new FakeProfileSyncService(static_cast<Profile*>(context))); | |
| 62 } | |
| 63 | |
| 64 void set_sync_initialized(bool sync_initialized) { | |
| 65 sync_initialized_ = sync_initialized; | |
| 66 } | |
| 67 | |
| 68 bool initialized_state_violation() { return initialized_state_violation_; } | |
| 69 | |
| 70 // ProfileSyncService: | |
| 71 bool IsSyncActive() const override { return sync_initialized_; } | |
| 72 | |
| 73 void AddObserver(sync_driver::SyncServiceObserver* observer) override { | |
| 74 if (sync_initialized_) | |
| 75 initialized_state_violation_ = true; | |
| 76 // Set sync initialized state to true so the function will run after | |
| 77 // OnStateChanged is called. | |
| 78 sync_initialized_ = true; | |
| 79 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 80 FROM_HERE, base::Bind(&sync_driver::SyncServiceObserver::OnStateChanged, | |
| 81 base::Unretained(observer))); | |
| 82 } | |
| 83 | |
| 84 syncer::ModelTypeSet GetEncryptedDataTypes() const override { | |
| 85 if (!sync_initialized_) | |
| 86 initialized_state_violation_ = true; | |
| 87 syncer::ModelTypeSet type_set; | |
| 88 type_set.Put(syncer::AUTOFILL); | |
| 89 return type_set; | |
| 90 } | |
| 91 | |
| 92 syncer::ModelTypeSet GetPreferredDataTypes() const override { | |
| 93 if (!sync_initialized_) | |
| 94 initialized_state_violation_ = true; | |
| 95 syncer::ModelTypeSet preferred_types = | |
| 96 syncer::UserSelectableTypes(); | |
| 97 preferred_types.Remove(syncer::TYPED_URLS); | |
| 98 return preferred_types; | |
| 99 } | |
| 100 | |
| 101 private: | |
| 102 bool sync_initialized_; | |
| 103 // Set to true if a function is called when sync_initialized is in an | |
| 104 // unexpected state. | |
| 105 mutable bool initialized_state_violation_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService); | |
| 108 }; | |
| 109 | |
| 110 class PreferencesPrivateApiTest : public ExtensionApiTest { | |
| 111 public: | |
| 112 PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {} | |
| 113 ~PreferencesPrivateApiTest() override {} | |
| 114 | |
| 115 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 116 #if defined(OS_CHROMEOS) | |
| 117 command_line->AppendSwitch( | |
| 118 chromeos::switches::kIgnoreUserProfileMappingForTests); | |
| 119 #endif | |
| 120 } | |
| 121 | |
| 122 void SetUpOnMainThread() override { | |
| 123 ExtensionApiTest::SetUpOnMainThread(); | |
| 124 | |
| 125 base::FilePath path; | |
| 126 PathService::Get(chrome::DIR_USER_DATA, &path); | |
| 127 path = path.AppendASCII("test_profile"); | |
| 128 if (!base::PathExists(path)) | |
| 129 CHECK(base::CreateDirectory(path)); | |
| 130 | |
| 131 Profile* profile = | |
| 132 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); | |
| 133 sync_driver::SyncPrefs sync_prefs(profile->GetPrefs()); | |
| 134 sync_prefs.SetKeepEverythingSynced(false); | |
| 135 | |
| 136 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 137 profile_manager->RegisterTestingProfile(profile, true, false); | |
| 138 | |
| 139 service_ = static_cast<FakeProfileSyncService*>( | |
| 140 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 141 profile, &FakeProfileSyncService::BuildFakeProfileSyncService)); | |
| 142 | |
| 143 browser_ = new Browser(Browser::CreateParams(profile)); | |
| 144 } | |
| 145 | |
| 146 // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the | |
| 147 // results returned are the expected ones. | |
| 148 void TestGetSyncCategoriesWithoutPassphraseFunction(); | |
| 149 | |
| 150 protected: | |
| 151 Browser* browser_; | |
| 152 FakeProfileSyncService* service_; | |
| 153 | |
| 154 private: | |
| 155 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest); | |
| 156 }; | |
| 157 | |
| 158 void | |
| 159 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() { | |
| 160 scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction> | |
| 161 function( | |
| 162 new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction); | |
| 163 ASSERT_TRUE(extension_function_test_utils::RunFunction( | |
| 164 function.get(), "[]", browser_, extension_function_test_utils::NONE)); | |
| 165 EXPECT_FALSE(service_->initialized_state_violation()); | |
| 166 | |
| 167 const base::ListValue* result = function->GetResultList(); | |
| 168 EXPECT_EQ(1u, result->GetSize()); | |
| 169 | |
| 170 const base::ListValue* categories = NULL; | |
| 171 ASSERT_TRUE(result->GetList(0, &categories)); | |
| 172 EXPECT_NE(categories->end(), | |
| 173 categories->Find(base::StringValue(bookmarks::kBookmarksFileName))); | |
| 174 EXPECT_NE(categories->end(), | |
| 175 categories->Find(base::StringValue(chrome::kPreferencesFilename))); | |
| 176 EXPECT_EQ(categories->end(), | |
| 177 categories->Find(base::StringValue("Autofill"))) << | |
| 178 "Encrypted categories should not be present"; | |
| 179 EXPECT_EQ(categories->end(), | |
| 180 categories->Find(base::StringValue("Typed URLs"))) << | |
| 181 "Unsynced categories should not be present"; | |
| 182 } | |
| 183 | |
| 184 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest, | |
| 185 GetSyncCategoriesWithoutPassphrase) { | |
| 186 TestGetSyncCategoriesWithoutPassphraseFunction(); | |
| 187 } | |
| 188 | |
| 189 // Verifies that we wait for the sync service to be ready before checking | |
| 190 // encryption status. | |
| 191 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest, | |
| 192 GetSyncCategoriesWithoutPassphraseAsynchronous) { | |
| 193 service_->set_sync_initialized(false); | |
| 194 TestGetSyncCategoriesWithoutPassphraseFunction(); | |
| 195 } | |
| 196 | |
| 197 } // namespace | |
| OLD | NEW |