OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/basictypes.h" |
| 6 #include "base/bind.h" |
| 7 #include "base/bind_helpers.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 13 #include "chrome/browser/sync/profile_sync_service.h" |
| 14 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 15 #include "content/public/browser/browser_context.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class FakeProfileSyncService : public ProfileSyncService { |
| 20 public: |
| 21 explicit FakeProfileSyncService(Profile* profile) |
| 22 : ProfileSyncService( |
| 23 NULL, |
| 24 profile, |
| 25 NULL, |
| 26 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
| 27 ProfileSyncService::MANUAL_START), |
| 28 sync_initialized_(true), |
| 29 initialized_state_violation_(false) {} |
| 30 |
| 31 virtual ~FakeProfileSyncService() {} |
| 32 |
| 33 static BrowserContextKeyedService* BuildFakeProfileSyncService( |
| 34 content::BrowserContext* context) { |
| 35 return new FakeProfileSyncService(static_cast<Profile*>(context)); |
| 36 } |
| 37 |
| 38 void set_sync_initialized(bool sync_initialized) { |
| 39 sync_initialized_ = sync_initialized; |
| 40 } |
| 41 |
| 42 bool initialized_state_violation() { return initialized_state_violation_; } |
| 43 |
| 44 // ProfileSyncService: |
| 45 virtual bool sync_initialized() const OVERRIDE { |
| 46 return sync_initialized_; |
| 47 } |
| 48 |
| 49 virtual void AddObserver( |
| 50 ProfileSyncServiceBase::Observer* observer) OVERRIDE { |
| 51 if (sync_initialized_) |
| 52 initialized_state_violation_ = true; |
| 53 // Set sync initialized state to true so the function will run after |
| 54 // OnStateChanged is called. |
| 55 sync_initialized_ = true; |
| 56 base::MessageLoop::current()->PostTask( |
| 57 FROM_HERE, |
| 58 base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged, |
| 59 base::Unretained(observer))); |
| 60 } |
| 61 |
| 62 virtual syncer::ModelTypeSet GetEncryptedDataTypes() const OVERRIDE { |
| 63 if (!sync_initialized_) |
| 64 initialized_state_violation_ = true; |
| 65 syncer::ModelTypeSet type_set; |
| 66 type_set.Put(syncer::AUTOFILL); |
| 67 return type_set; |
| 68 } |
| 69 |
| 70 virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE { |
| 71 if (!sync_initialized_) |
| 72 initialized_state_violation_ = true; |
| 73 syncer::ModelTypeSet preferred_types = |
| 74 syncer::UserSelectableTypes(); |
| 75 preferred_types.Remove(syncer::TYPED_URLS); |
| 76 return preferred_types; |
| 77 } |
| 78 |
| 79 private: |
| 80 bool sync_initialized_; |
| 81 // Set to true if a function is called when sync_initialized is in an |
| 82 // unexpected state. |
| 83 mutable bool initialized_state_violation_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService); |
| 86 }; |
| 87 |
| 88 class PreferencesPrivateApiTest : public ExtensionApiTest { |
| 89 public: |
| 90 PreferencesPrivateApiTest() : service_(NULL) {} |
| 91 virtual ~PreferencesPrivateApiTest() {} |
| 92 |
| 93 virtual void SetUpOnMainThread() OVERRIDE { |
| 94 ExtensionApiTest::SetUpOnMainThread(); |
| 95 service_ = static_cast<FakeProfileSyncService*>( |
| 96 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 97 profile(), &FakeProfileSyncService::BuildFakeProfileSyncService)); |
| 98 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs()); |
| 99 sync_prefs.SetKeepEverythingSynced(false); |
| 100 } |
| 101 |
| 102 FakeProfileSyncService* service() { return service_; } |
| 103 |
| 104 private: |
| 105 FakeProfileSyncService* service_; |
| 106 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest); |
| 107 }; |
| 108 |
| 109 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest, |
| 110 GetSyncCategoriesWithoutPassphraseSynchronous) { |
| 111 ASSERT_TRUE(RunComponentExtensionTest("preferences_private")) << message_; |
| 112 EXPECT_FALSE(service()->initialized_state_violation()); |
| 113 } |
| 114 |
| 115 // Verifies that we wait for the sync service to be ready before checking |
| 116 // encryption status. |
| 117 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest, |
| 118 GetSyncCategoriesWithoutPassphraseAsynchronous) { |
| 119 service()->set_sync_initialized(false); |
| 120 ASSERT_TRUE(RunComponentExtensionTest("preferences_private")) << message_; |
| 121 EXPECT_FALSE(service()->initialized_state_violation()); |
| 122 } |
| 123 |
| 124 } // namespace |
OLD | NEW |