Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / preferences_private / preferences_private_apitest.cc
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/basictypes.h"
6 #include "base/bind.h"
7 #include "base/bind_helpers.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/path_service.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/api/preferences_private/preferences_private_api.h"
16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/extensions/extension_function_test_utils.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
21 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/test/base/testing_profile.h"
29 #include "components/bookmarks/common/bookmark_constants.h"
30 #include "components/sync_driver/sync_prefs.h"
31 #include "content/public/browser/browser_context.h"
32 #include "extensions/test/extension_test_message_listener.h"
33
34 #if defined(OS_CHROMEOS)
35 #include "chromeos/chromeos_switches.h"
36 #endif
37
38 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction;
39
40 namespace {
41
42 class FakeProfileSyncService : public ProfileSyncService {
43  public:
44   explicit FakeProfileSyncService(Profile* profile)
45       : ProfileSyncService(
46             scoped_ptr<ProfileSyncComponentsFactory>(
47                 new ProfileSyncComponentsFactoryMock()),
48             profile,
49             make_scoped_ptr<SupervisedUserSigninManagerWrapper>(NULL),
50             ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
51             browser_sync::MANUAL_START),
52         sync_initialized_(true),
53         initialized_state_violation_(false) {}
54
55   ~FakeProfileSyncService() override {}
56
57   static KeyedService* BuildFakeProfileSyncService(
58       content::BrowserContext* context) {
59     return new FakeProfileSyncService(static_cast<Profile*>(context));
60   }
61
62   void set_sync_initialized(bool sync_initialized) {
63     sync_initialized_ = sync_initialized;
64   }
65
66   bool initialized_state_violation() { return initialized_state_violation_; }
67
68   // ProfileSyncService:
69   bool SyncActive() const override { return sync_initialized_; }
70
71   void AddObserver(ProfileSyncServiceBase::Observer* observer) override {
72     if (sync_initialized_)
73       initialized_state_violation_ = true;
74     // Set sync initialized state to true so the function will run after
75     // OnStateChanged is called.
76     sync_initialized_ = true;
77     base::MessageLoop::current()->PostTask(
78         FROM_HERE,
79         base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged,
80                    base::Unretained(observer)));
81   }
82
83   syncer::ModelTypeSet GetEncryptedDataTypes() const override {
84     if (!sync_initialized_)
85       initialized_state_violation_ = true;
86     syncer::ModelTypeSet type_set;
87     type_set.Put(syncer::AUTOFILL);
88     return type_set;
89   }
90
91   syncer::ModelTypeSet GetPreferredDataTypes() const override {
92     if (!sync_initialized_)
93       initialized_state_violation_ = true;
94     syncer::ModelTypeSet preferred_types =
95         syncer::UserSelectableTypes();
96     preferred_types.Remove(syncer::TYPED_URLS);
97     return preferred_types;
98   }
99
100  private:
101   bool sync_initialized_;
102   // Set to true if a function is called when sync_initialized is in an
103   // unexpected state.
104   mutable bool initialized_state_violation_;
105
106   DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
107 };
108
109 class PreferencesPrivateApiTest : public ExtensionApiTest {
110  public:
111   PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {}
112   ~PreferencesPrivateApiTest() override {}
113
114   void SetUpCommandLine(CommandLine* command_line) override {
115 #if defined(OS_CHROMEOS)
116     command_line->AppendSwitch(
117         chromeos::switches::kIgnoreUserProfileMappingForTests);
118 #endif
119   }
120
121   void SetUpOnMainThread() override {
122     ExtensionApiTest::SetUpOnMainThread();
123
124     base::FilePath path;
125     PathService::Get(chrome::DIR_USER_DATA, &path);
126     path = path.AppendASCII("test_profile");
127     if (!base::PathExists(path))
128       CHECK(base::CreateDirectory(path));
129
130     Profile* profile =
131         Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
132     sync_driver::SyncPrefs sync_prefs(profile->GetPrefs());
133     sync_prefs.SetKeepEverythingSynced(false);
134
135     ProfileManager* profile_manager = g_browser_process->profile_manager();
136     profile_manager->RegisterTestingProfile(profile, true, false);
137
138     service_ = static_cast<FakeProfileSyncService*>(
139         ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
140         profile, &FakeProfileSyncService::BuildFakeProfileSyncService));
141
142     browser_ = new Browser(Browser::CreateParams(
143         profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
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