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