| 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 test_started_(false), | |
| 54 sync_initialized_(true), | |
| 55 initialized_state_violation_(false) {} | |
| 56 | |
| 57 ~FakeProfileSyncService() override {} | |
| 58 | |
| 59 static scoped_ptr<KeyedService> BuildFakeProfileSyncService( | |
| 60 content::BrowserContext* context) { | |
| 61 return make_scoped_ptr( | |
| 62 new FakeProfileSyncService(static_cast<Profile*>(context))); | |
| 63 } | |
| 64 | |
| 65 void set_test_started(bool test_started) { | |
| 66 test_started_ = test_started; | |
| 67 } | |
| 68 | |
| 69 void set_sync_initialized(bool sync_initialized) { | |
| 70 sync_initialized_ = sync_initialized; | |
| 71 } | |
| 72 | |
| 73 bool initialized_state_violation() { return initialized_state_violation_; } | |
| 74 | |
| 75 // ProfileSyncService: | |
| 76 bool IsSyncActive() const override { return sync_initialized_; } | |
| 77 | |
| 78 void AddObserver(sync_driver::SyncServiceObserver* observer) override { | |
| 79 // Ignore other SyncServiceObsevers which we don't care about. | |
| 80 if (!test_started_) | |
| 81 return; | |
| 82 | |
| 83 if (sync_initialized_) | |
| 84 initialized_state_violation_ = true; | |
| 85 // Set sync initialized state to true so the function will run after | |
| 86 // OnStateChanged is called. | |
| 87 sync_initialized_ = true; | |
| 88 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 89 FROM_HERE, base::Bind(&sync_driver::SyncServiceObserver::OnStateChanged, | |
| 90 base::Unretained(observer))); | |
| 91 } | |
| 92 | |
| 93 syncer::ModelTypeSet GetEncryptedDataTypes() const override { | |
| 94 if (!sync_initialized_) | |
| 95 initialized_state_violation_ = true; | |
| 96 syncer::ModelTypeSet type_set; | |
| 97 type_set.Put(syncer::AUTOFILL); | |
| 98 return type_set; | |
| 99 } | |
| 100 | |
| 101 syncer::ModelTypeSet GetPreferredDataTypes() const override { | |
| 102 if (!sync_initialized_) | |
| 103 initialized_state_violation_ = true; | |
| 104 syncer::ModelTypeSet preferred_types = | |
| 105 syncer::UserSelectableTypes(); | |
| 106 preferred_types.Remove(syncer::TYPED_URLS); | |
| 107 return preferred_types; | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 bool test_started_; | |
| 112 bool sync_initialized_; | |
| 113 // Set to true if a function is called when sync_initialized is in an | |
| 114 // unexpected state. | |
| 115 mutable bool initialized_state_violation_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService); | |
| 118 }; | |
| 119 | |
| 120 class PreferencesPrivateApiTest : public ExtensionApiTest { | |
| 121 public: | |
| 122 PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {} | |
| 123 ~PreferencesPrivateApiTest() override {} | |
| 124 | |
| 125 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 126 #if defined(OS_CHROMEOS) | |
| 127 command_line->AppendSwitch( | |
| 128 chromeos::switches::kIgnoreUserProfileMappingForTests); | |
| 129 #endif | |
| 130 } | |
| 131 | |
| 132 void SetUpOnMainThread() override { | |
| 133 ExtensionApiTest::SetUpOnMainThread(); | |
| 134 | |
| 135 base::FilePath path; | |
| 136 PathService::Get(chrome::DIR_USER_DATA, &path); | |
| 137 path = path.AppendASCII("test_profile"); | |
| 138 if (!base::PathExists(path)) | |
| 139 CHECK(base::CreateDirectory(path)); | |
| 140 | |
| 141 Profile* profile = | |
| 142 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); | |
| 143 sync_driver::SyncPrefs sync_prefs(profile->GetPrefs()); | |
| 144 sync_prefs.SetKeepEverythingSynced(false); | |
| 145 | |
| 146 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 147 profile_manager->RegisterTestingProfile(profile, true, false); | |
| 148 | |
| 149 service_ = static_cast<FakeProfileSyncService*>( | |
| 150 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 151 profile, &FakeProfileSyncService::BuildFakeProfileSyncService)); | |
| 152 | |
| 153 browser_ = new Browser(Browser::CreateParams(profile)); | |
| 154 | |
| 155 service_->set_test_started(true); | |
| 156 } | |
| 157 | |
| 158 // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the | |
| 159 // results returned are the expected ones. | |
| 160 void TestGetSyncCategoriesWithoutPassphraseFunction(); | |
| 161 | |
| 162 protected: | |
| 163 Browser* browser_; | |
| 164 FakeProfileSyncService* service_; | |
| 165 | |
| 166 private: | |
| 167 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest); | |
| 168 }; | |
| 169 | |
| 170 void | |
| 171 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() { | |
| 172 scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction> | |
| 173 function( | |
| 174 new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction); | |
| 175 ASSERT_TRUE(extension_function_test_utils::RunFunction( | |
| 176 function.get(), "[]", browser_, extension_function_test_utils::NONE)); | |
| 177 EXPECT_FALSE(service_->initialized_state_violation()); | |
| 178 | |
| 179 const base::ListValue* result = function->GetResultList(); | |
| 180 EXPECT_EQ(1u, result->GetSize()); | |
| 181 | |
| 182 const base::ListValue* categories = NULL; | |
| 183 ASSERT_TRUE(result->GetList(0, &categories)); | |
| 184 EXPECT_NE(categories->end(), | |
| 185 categories->Find(base::StringValue(bookmarks::kBookmarksFileName))); | |
| 186 EXPECT_NE(categories->end(), | |
| 187 categories->Find(base::StringValue(chrome::kPreferencesFilename))); | |
| 188 EXPECT_EQ(categories->end(), | |
| 189 categories->Find(base::StringValue("Autofill"))) << | |
| 190 "Encrypted categories should not be present"; | |
| 191 EXPECT_EQ(categories->end(), | |
| 192 categories->Find(base::StringValue("Typed URLs"))) << | |
| 193 "Unsynced categories should not be present"; | |
| 194 } | |
| 195 | |
| 196 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest, | |
| 197 GetSyncCategoriesWithoutPassphrase) { | |
| 198 TestGetSyncCategoriesWithoutPassphraseFunction(); | |
| 199 } | |
| 200 | |
| 201 // Verifies that we wait for the sync service to be ready before checking | |
| 202 // encryption status. | |
| 203 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest, | |
| 204 GetSyncCategoriesWithoutPassphraseAsynchronous) { | |
| 205 service_->set_sync_initialized(false); | |
| 206 TestGetSyncCategoriesWithoutPassphraseFunction(); | |
| 207 } | |
| 208 | |
| 209 } // namespace | |
| OLD | NEW |