Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Unified Diff: chrome/browser/extensions/api/preferences_private/preferences_private_apitest.cc

Issue 118413003: Add hook for extension to check sync status. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: NULL check' Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/preferences_private/preferences_private_apitest.cc
diff --git a/chrome/browser/extensions/api/preferences_private/preferences_private_apitest.cc b/chrome/browser/extensions/api/preferences_private/preferences_private_apitest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..28edadb153c13bf8e66f92190a85496c971114b1
--- /dev/null
+++ b/chrome/browser/extensions/api/preferences_private/preferences_private_apitest.cc
@@ -0,0 +1,124 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/basictypes.h"
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/message_loop/message_loop.h"
+#include "chrome/browser/extensions/extension_apitest.h"
+#include "chrome/browser/extensions/extension_test_message_listener.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
+#include "chrome/browser/sync/profile_sync_service.h"
+#include "chrome/browser/sync/profile_sync_service_factory.h"
+#include "content/public/browser/browser_context.h"
+
+namespace {
+
+class FakeProfileSyncService : public ProfileSyncService {
+ public:
+ explicit FakeProfileSyncService(Profile* profile)
+ : ProfileSyncService(
+ NULL,
+ profile,
+ NULL,
+ ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
+ ProfileSyncService::MANUAL_START),
+ sync_initialized_(true),
+ initialized_state_violation_(false) {}
+
+ virtual ~FakeProfileSyncService() {}
+
+ static BrowserContextKeyedService* BuildFakeProfileSyncService(
+ content::BrowserContext* context) {
+ return new FakeProfileSyncService(static_cast<Profile*>(context));
+ }
+
+ void set_sync_initialized(bool sync_initialized) {
+ sync_initialized_ = sync_initialized;
+ }
+
+ bool initialized_state_violation() { return initialized_state_violation_; }
+
+ // ProfileSyncService:
+ virtual bool sync_initialized() const OVERRIDE {
+ return sync_initialized_;
+ }
+
+ virtual void AddObserver(
+ ProfileSyncServiceBase::Observer* observer) OVERRIDE {
+ if (sync_initialized_)
+ initialized_state_violation_ = true;
+ // Set sync initialized state to true so the function will run after
+ // OnStateChanged is called.
+ sync_initialized_ = true;
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged,
+ base::Unretained(observer)));
+ }
+
+ virtual syncer::ModelTypeSet GetEncryptedDataTypes() const OVERRIDE {
+ if (!sync_initialized_)
+ initialized_state_violation_ = true;
+ syncer::ModelTypeSet type_set;
+ type_set.Put(syncer::AUTOFILL);
+ return type_set;
+ }
+
+ virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE {
+ if (!sync_initialized_)
+ initialized_state_violation_ = true;
+ syncer::ModelTypeSet preferred_types =
+ syncer::UserSelectableTypes();
+ preferred_types.Remove(syncer::TYPED_URLS);
+ return preferred_types;
+ }
+
+ private:
+ bool sync_initialized_;
+ // Set to true if a function is called when sync_initialized is in an
+ // unexpected state.
+ mutable bool initialized_state_violation_;
+
+ DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
+};
+
+class PreferencesPrivateApiTest : public ExtensionApiTest {
+ public:
+ PreferencesPrivateApiTest() : service_(NULL) {}
+ virtual ~PreferencesPrivateApiTest() {}
+
+ virtual void SetUpOnMainThread() OVERRIDE {
+ ExtensionApiTest::SetUpOnMainThread();
+ service_ = static_cast<FakeProfileSyncService*>(
+ ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
+ profile(), &FakeProfileSyncService::BuildFakeProfileSyncService));
+ browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs());
+ sync_prefs.SetKeepEverythingSynced(false);
+ }
+
+ FakeProfileSyncService* service() { return service_; }
+
+ private:
+ FakeProfileSyncService* service_;
+ DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest);
+};
+
+IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
+ GetSyncCategoriesWithoutPassphraseSynchronous) {
+ ASSERT_TRUE(RunComponentExtensionTest("preferences_private")) << message_;
+ EXPECT_FALSE(service()->initialized_state_violation());
+}
+
+// Verifies that we wait for the sync service to be ready before checking
+// encryption status.
+IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
+ GetSyncCategoriesWithoutPassphraseAsynchronous) {
+ service()->set_sync_initialized(false);
+ ASSERT_TRUE(RunComponentExtensionTest("preferences_private")) << message_;
+ EXPECT_FALSE(service()->initialized_state_violation());
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698