summaryrefslogtreecommitdiffstats
path: root/chrome/browser/protector/session_startup_change_unittest.cc
blob: 38de32ccd0fdcc9d634abab10a1e5f3eb055f5f3 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/protector/base_setting_change.h"
#include "chrome/browser/ui/startup/startup_tab.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace protector {

namespace {

const char kStartupUrl1[] = "http://google.com";
const char kStartupUrl2[] = "http://example.com";
const char kStartupUrl3[] = "http://example.org";
const char kStartupHost1[] = "google.com";
const char kStartupHost2[] = "example.com";
const char kStartupHost3[] = "example.org";

const BaseSettingChange::DisplayName kNoDisplayName(
    BaseSettingChange::kDefaultNamePriority, string16());

}  // namespace

class SessionStartupChangeTest : public testing::Test {
 public:
  SessionStartupChangeTest()
      : initial_startup_pref_(SessionStartupPref::DEFAULT) {
  }

  virtual void SetUp() OVERRIDE {
    // Ensure initial session startup pref.
    SessionStartupPref::SetStartupPref(&profile_, initial_startup_pref_);
  }

 protected:
  TestingProfile profile_;
  SessionStartupPref initial_startup_pref_;
  StartupTabs empty_pinned_tabs_;
};

TEST_F(SessionStartupChangeTest, InitAndApply) {
  // Create a change and apply it.
  SessionStartupPref backup_startup_pref(SessionStartupPref::LAST);
  scoped_ptr<BaseSettingChange> change(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  // Setting is initially reverted to backup.
  EXPECT_EQ(SessionStartupPref::LAST,
            SessionStartupPref::GetStartupPref(&profile_).type);
  change->Apply(NULL);  // |browser| is unused.
  // New setting active now.
  EXPECT_EQ(SessionStartupPref::DEFAULT,
            SessionStartupPref::GetStartupPref(&profile_).type);
}

TEST_F(SessionStartupChangeTest, InitAndDiscard) {
  // Create a change and discard it.
  SessionStartupPref backup_startup_pref(SessionStartupPref::LAST);
  scoped_ptr<BaseSettingChange> change(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  // Setting is initially reverted to backup.
  EXPECT_EQ(SessionStartupPref::LAST,
            SessionStartupPref::GetStartupPref(&profile_).type);
  change->Discard(NULL);  // |browser| is unused.
  // Nothing changed by Discard.
  EXPECT_EQ(SessionStartupPref::LAST,
            SessionStartupPref::GetStartupPref(&profile_).type);
}

TEST_F(SessionStartupChangeTest, ApplyButtonCaptions) {
  // Apply button captions for "Open NTP" and "Open specific URLs" cases.
  string16 open_ntp_caption =
      l10n_util::GetStringUTF16(IDS_CHANGE_STARTUP_SETTINGS_NTP);
  string16 open_url1_etc_caption =
      l10n_util::GetStringFUTF16(IDS_CHANGE_STARTUP_SETTINGS_URLS,
                                 UTF8ToUTF16(GURL(kStartupUrl1).host()));
  string16 open_url2_etc_caption =
      l10n_util::GetStringFUTF16(IDS_CHANGE_STARTUP_SETTINGS_URLS,
                                 UTF8ToUTF16(GURL(kStartupUrl2).host()));
  string16 open_url3_etc_caption =
      l10n_util::GetStringFUTF16(IDS_CHANGE_STARTUP_SETTINGS_URLS,
                                 UTF8ToUTF16(GURL(kStartupUrl3).host()));

  // Open NTP.
  initial_startup_pref_.type = SessionStartupPref::DEFAULT;
  SessionStartupPref backup_startup_pref(SessionStartupPref::DEFAULT);
  scoped_ptr<BaseSettingChange> change(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_ntp_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(), change->GetNewSettingURL());
  EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName());

  // Pinned tabs count as startup URLs as well.
  StartupTabs new_pinned_tabs;
  StartupTab pinned_tab;
  pinned_tab.url = GURL(kStartupUrl3);
  new_pinned_tabs.push_back(pinned_tab);
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, new_pinned_tabs,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_url3_etc_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(kStartupUrl3), change->GetNewSettingURL());
  EXPECT_EQ(UTF8ToUTF16(kStartupHost3), change->GetApplyDisplayName().second);

  // "Open URLs" with no URLs is the same as "Open NTP".
  initial_startup_pref_.type = SessionStartupPref::URLS;
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_ntp_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(), change->GetNewSettingURL());
  EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName());

  // Single URL.
  initial_startup_pref_.urls.push_back(GURL(kStartupUrl1));
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL());
  EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second);

  // Multiple URLs: name of the first one used.
  initial_startup_pref_.urls.push_back(GURL(kStartupUrl2));
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL());
  EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second);

  // Pinned tabs go after the startup URLs.
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, new_pinned_tabs,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL());
  EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second);

  // Multiple URLs, the first present in both backup and new settings.
  backup_startup_pref.type = SessionStartupPref::URLS;
  backup_startup_pref.urls.push_back(GURL(kStartupUrl1));
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_url2_etc_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(kStartupUrl2), change->GetNewSettingURL());
  EXPECT_EQ(UTF8ToUTF16(kStartupHost2), change->GetApplyDisplayName().second);

  // Multiple URLs, all present in both backup and new settings: the first one
  // is displayed in that case.
  backup_startup_pref.urls.push_back(GURL(kStartupUrl2));
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL());
  EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second);

  // Multiple URLs, all present in both backup and new settings, new pinned tabs
  // added: the first pinned tab URL is used.
  change.reset(
      CreateSessionStartupChange(initial_startup_pref_, new_pinned_tabs,
                                 backup_startup_pref, empty_pinned_tabs_));
  ASSERT_TRUE(change->Init(&profile_));
  EXPECT_EQ(open_url3_etc_caption, change->GetApplyButtonText());
  EXPECT_EQ(GURL(kStartupUrl3), change->GetNewSettingURL());
  EXPECT_EQ(UTF8ToUTF16(kStartupHost3), change->GetApplyDisplayName().second);
}

}  // namespace protector