summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/prefs/prefs_tab_helper_unittest.cc
blob: 168bbb0ed5a2df9aa65d2db901210198271c0f24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2011 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 "chrome/common/pref_names.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/ui/prefs/prefs_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
#include "content/browser/tab_contents/test_tab_contents.h"
#include "content/test/test_browser_thread.h"

using content::BrowserThread;

class TestPrefsTabHelper : public PrefsTabHelper {
 public:
  explicit TestPrefsTabHelper(TabContents* tab_contents)
      : PrefsTabHelper(tab_contents),
        was_update_web_preferences_called_(false) {
  }
  virtual ~TestPrefsTabHelper() { }

  virtual void UpdateWebPreferences() OVERRIDE {
    was_update_web_preferences_called_ = true;
    PrefsTabHelper::UpdateWebPreferences();
  }

  void NotifyRenderViewCreated() {
    RenderViewCreated(NULL);
  }

  bool was_update_web_preferences_called() {
    return was_update_web_preferences_called_;
  }

 private:
  bool was_update_web_preferences_called_;
};

class PrefsTabHelperTest : public TabContentsWrapperTestHarness {
 public:
  PrefsTabHelperTest()
      : TabContentsWrapperTestHarness(),
        ui_thread_(BrowserThread::UI, &message_loop_) {}

  virtual ~PrefsTabHelperTest() {}

  TabContentsWrapper* contents_wrapper2() {
    return contents_wrapper2_.get();
  }

  void SetContents2(TestTabContents* contents) {
    contents_wrapper2_.reset(
        contents ? new TabContentsWrapper(contents) : NULL);
  }

 protected:
  virtual void SetUp() OVERRIDE {
    TabContentsWrapperTestHarness::SetUp();
    SetContents2(CreateTestTabContents());
  }

  virtual void TearDown() OVERRIDE {
    contents_wrapper2_.reset();
    TabContentsWrapperTestHarness::TearDown();
  }

 private:
  content::TestBrowserThread ui_thread_;
  scoped_ptr<TabContentsWrapper> contents_wrapper2_;

  DISALLOW_COPY_AND_ASSIGN(PrefsTabHelperTest);
};

TEST_F(PrefsTabHelperTest, PerTabJavaScriptEnabled) {
  const char* key = prefs::kWebKitJavascriptEnabled;
  PrefService* prefs1 = contents_wrapper()->prefs_tab_helper()->per_tab_prefs();
  PrefService* prefs2 =
      contents_wrapper2()->prefs_tab_helper()->per_tab_prefs();
  const bool initial_value = prefs1->GetBoolean(key);
  EXPECT_EQ(initial_value, prefs2->GetBoolean(key));

  prefs1->SetBoolean(key, !initial_value);
  EXPECT_EQ(!initial_value, prefs1->GetBoolean(key));
  EXPECT_EQ(initial_value, prefs2->GetBoolean(key));

  prefs1->SetBoolean(key, initial_value);
  EXPECT_EQ(initial_value, prefs1->GetBoolean(key));
  EXPECT_EQ(initial_value, prefs2->GetBoolean(key));

  prefs2->SetBoolean(key, !initial_value);
  EXPECT_EQ(initial_value, prefs1->GetBoolean(key));
  EXPECT_EQ(!initial_value, prefs2->GetBoolean(key));
}

TEST_F(PrefsTabHelperTest, OverridePrefsOnViewCreation) {
  TestPrefsTabHelper* test_prefs_helper =
      new TestPrefsTabHelper(contents_wrapper()->tab_contents());
  contents_wrapper()->prefs_tab_helper_.reset(test_prefs_helper);
  EXPECT_FALSE(test_prefs_helper->was_update_web_preferences_called());
  test_prefs_helper->NotifyRenderViewCreated();
  EXPECT_TRUE(test_prefs_helper->was_update_web_preferences_called());
}