Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / off_the_record_profile_impl_unittest.cc
1 // Copyright (c) 2012 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 "chrome/browser/profiles/off_the_record_profile_impl.h"
6
7 #include "base/prefs/pref_registry_simple.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/net/ssl_config_service_manager.h"
12 #include "chrome/browser/prefs/browser_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/browser_with_test_window_test.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_io_thread_state.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "chrome/test/base/testing_profile_manager.h"
20 #include "components/keyed_service/content/browser_context_dependency_manager.h"
21 #include "content/public/browser/host_zoom_map.h"
22 #include "net/dns/mock_host_resolver.h"
23
24 using content::HostZoomMap;
25
26 namespace {
27
28 class TestingProfileWithHostZoomMap : public TestingProfile {
29  public:
30   TestingProfileWithHostZoomMap() {
31     zoom_subscription_ =
32         HostZoomMap::GetForBrowserContext(this)->AddZoomLevelChangedCallback(
33             base::Bind(&TestingProfileWithHostZoomMap::OnZoomLevelChanged,
34                         base::Unretained(this)));
35   }
36
37   virtual ~TestingProfileWithHostZoomMap() {}
38
39   virtual Profile* GetOffTheRecordProfile() OVERRIDE {
40     if (!off_the_record_profile_)
41       off_the_record_profile_.reset(CreateOffTheRecordProfile());
42     return off_the_record_profile_.get();
43   }
44
45   virtual PrefService* GetOffTheRecordPrefs() OVERRIDE {
46     return GetPrefs();
47   }
48
49  private:
50   void OnZoomLevelChanged(const HostZoomMap::ZoomLevelChange& change) {
51
52     if (change.mode != HostZoomMap::ZOOM_CHANGED_FOR_HOST)
53       return;
54
55     HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this);
56
57     double level = change.zoom_level;
58     DictionaryPrefUpdate update(prefs_.get(), prefs::kPerHostZoomLevels);
59     base::DictionaryValue* host_zoom_dictionary = update.Get();
60     if (level == host_zoom_map->GetDefaultZoomLevel()) {
61       host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL);
62     } else {
63       host_zoom_dictionary->SetWithoutPathExpansion(
64           change.host, base::Value::CreateDoubleValue(level));
65     }
66   }
67
68   scoped_ptr<Profile> off_the_record_profile_;
69   scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
70
71   scoped_ptr<HostZoomMap::Subscription> zoom_subscription_;
72
73   DISALLOW_COPY_AND_ASSIGN(TestingProfileWithHostZoomMap);
74 };
75
76 }  // namespace
77
78 // We need to have a BrowserProcess in g_browser_process variable, since
79 // OffTheRecordProfileImpl ctor uses it in
80 // ProfileIOData::InitializeProfileParams.
81 class OffTheRecordProfileImplTest : public BrowserWithTestWindowTest {
82  protected:
83   OffTheRecordProfileImplTest() {}
84
85   virtual ~OffTheRecordProfileImplTest() {}
86
87   virtual void SetUp() OVERRIDE {
88     profile_manager_.reset(new TestingProfileManager(browser_process()));
89     ASSERT_TRUE(profile_manager_->SetUp());
90
91     testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
92     testing_io_thread_state_->io_thread_state()->globals()->host_resolver.reset(
93         new net::MockHostResolver());
94
95     BrowserWithTestWindowTest::SetUp();
96   }
97
98   virtual void TearDown() OVERRIDE {
99     DestroyBrowserAndProfile();
100     BrowserWithTestWindowTest::TearDown();
101
102     testing_io_thread_state_.reset();
103
104     profile_manager_.reset();
105   }
106
107  private:
108   TestingBrowserProcess* browser_process() {
109     return TestingBrowserProcess::GetGlobal();
110   }
111
112   scoped_ptr<TestingProfileManager> profile_manager_;
113   scoped_ptr<chrome::TestingIOThreadState> testing_io_thread_state_;
114
115   DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileImplTest);
116 };
117
118 // Test four things:
119 //  1. Host zoom maps of parent profile and child profile are different.
120 //  2. Child host zoom map inherites zoom level at construction.
121 //  3. Change of zoom level doesn't propagate from child to parent.
122 //  4. Change of zoom level propagate from parent to child.
123 TEST_F(OffTheRecordProfileImplTest, GetHostZoomMap) {
124   // Constants for test case.
125   std::string const host("example.com");
126   double const zoom_level_25 = 2.5;
127   double const zoom_level_30 = 3.0;
128   double const zoom_level_40 = 4.0;
129
130   // Prepare parent profile.
131   scoped_ptr<Profile> parent_profile(new TestingProfileWithHostZoomMap);
132   ASSERT_TRUE(parent_profile.get());
133   ASSERT_TRUE(parent_profile->GetPrefs());
134   ASSERT_TRUE(parent_profile->GetOffTheRecordPrefs());
135
136   // Prepare parent host zoom map.
137   HostZoomMap* parent_zoom_map =
138       HostZoomMap::GetForBrowserContext(parent_profile.get());
139   ASSERT_TRUE(parent_zoom_map);
140
141   parent_zoom_map->SetZoomLevelForHost(host, zoom_level_25);
142   ASSERT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
143       zoom_level_25);
144
145   // TODO(yosin) We need to wait ProfileImpl::Observe done for
146   // OnZoomLevelChanged.
147
148   // Prepare child profile as off the record profile.
149   scoped_ptr<OffTheRecordProfileImpl> child_profile(
150       new OffTheRecordProfileImpl(parent_profile.get()));
151   child_profile->InitIoData();
152   child_profile->InitHostZoomMap();
153
154   BrowserContextDependencyManager::GetInstance()->
155       CreateBrowserContextServicesForTest(child_profile.get());
156
157   // Prepare child host zoom map.
158   HostZoomMap* child_zoom_map =
159       HostZoomMap::GetForBrowserContext(child_profile.get());
160   ASSERT_TRUE(child_zoom_map);
161
162   // Verity.
163   EXPECT_NE(parent_zoom_map, child_zoom_map);
164
165   EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
166             child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
167                 "Child must inherit from parent.";
168
169   child_zoom_map->SetZoomLevelForHost(host, zoom_level_30);
170   ASSERT_EQ(
171       child_zoom_map->GetZoomLevelForHostAndScheme("http", host),
172       zoom_level_30);
173
174   EXPECT_NE(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
175             child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
176                 "Child change must not propagate to parent.";
177
178   parent_zoom_map->SetZoomLevelForHost(host, zoom_level_40);
179   ASSERT_EQ(
180       parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
181       zoom_level_40);
182
183   EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
184             child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
185                 "Parent change should propagate to child.";
186   base::RunLoop().RunUntilIdle();
187 }