blob: 8c063dfd5cf22d0bdac9021fed09b07657b00ea3 [file] [log] [blame]
Wyatt Allen60ea95f2016-06-03 09:35:07 -07001<!DOCTYPE html>
2<!--
3Copyright (C) 2016 The Android Open Source Project
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16-->
17
18<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
19<title>gr-settings-view</title>
20
21<script src="../../../bower_components/webcomponentsjs/webcomponents.min.js"></script>
22<script src="../../../bower_components/web-component-tester/browser.js"></script>
23
24<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
25<link rel="import" href="gr-settings-view.html">
26
27<test-fixture id="basic">
28 <template>
29 <gr-settings-view></gr-settings-view>
30 </template>
31</test-fixture>
32
Wyatt Allen2dc5c362016-06-13 21:05:45 -070033<test-fixture id="blank">
34 <template>
35 <div></div>
36 </template>
37</test-fixture>
38
Wyatt Allen60ea95f2016-06-03 09:35:07 -070039<script>
40 suite('gr-settings-view tests', function() {
41 var element;
42 var account;
43 var preferences;
Wyatt Allen649fa8a2016-06-13 21:31:26 -070044 var diffPreferences;
Wyatt Allen60ea95f2016-06-03 09:35:07 -070045
Wyatt Allend14895b2016-06-06 12:37:24 -070046 function valueOf(title, fieldsetid) {
47 var sections = element.$[fieldsetid].querySelectorAll('section');
48 var titleEl;
49 for (var i = 0; i < sections.length; i++) {
50 titleEl = sections[i].querySelector('.title');
51 if (titleEl.textContent === title) {
52 return sections[i].querySelector('.value');
53 }
54 }
55 }
56
57 // Because deepEqual isn't behaving in Safari.
58 function assertMenusEqual(actual, expected) {
59 assert.equal(actual.length, expected.length);
60 for (var i = 0; i < actual.length; i++) {
61 assert.equal(actual[i].name, expected[i].name);
62 assert.equal(actual[i].url, expected[i].url);
63 }
64 }
65
Wyatt Allen60ea95f2016-06-03 09:35:07 -070066 setup(function(done) {
67 account = {
68 _account_id: 123,
69 name: 'user name',
70 email: 'user@email',
71 username: 'user username',
72 registered: '2000-01-01 00:00:00.000000000',
73 };
Wyatt Allend14895b2016-06-06 12:37:24 -070074 preferences = {
75 changes_per_page: 25,
76 date_format: 'UK',
77 time_format: 'HHMM_12',
78 diff_view: 'UNIFIED_DIFF',
79 email_strategy: 'ENABLED',
80
81 my: [
82 {url: '/first/url', name: 'first name', target: '_blank'},
83 {url: '/second/url', name: 'second name', target: '_blank'},
84 ],
85 };
Wyatt Allen649fa8a2016-06-13 21:31:26 -070086 diffPreferences = {
87 context: 10,
88 tab_size: 8,
89 line_length: 100,
90 cursor_blink_rate: 0,
91 intraline_difference: true,
92 show_line_endings: true,
93 show_tabs: true,
94 show_whitespace_errors: true,
95 syntax_highlighting: true,
96 auto_hide_diff_table_header: true,
97 theme: 'DEFAULT',
98 ignore_whitespace: 'IGNORE_NONE'
99 };
Wyatt Allenbd472172016-06-09 17:44:51 -0700100 watchedProjects = [];
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700101
102 stub('gr-rest-api-interface', {
103 getLoggedIn: function() { return Promise.resolve(true); },
104 getAccount: function() { return Promise.resolve(account); },
105 getPreferences: function() { return Promise.resolve(preferences); },
Wyatt Allen649fa8a2016-06-13 21:31:26 -0700106 getDiffPreferences: function() {
107 return Promise.resolve(diffPreferences);
108 },
Wyatt Allenbd472172016-06-09 17:44:51 -0700109 getWatchedProjects: function() {
110 return Promise.resolve(watchedProjects);
111 },
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700112 });
113 element = fixture('basic');
114
115 // Allow the element to render.
116 element.async(done, 1);
117 });
118
Wyatt Allend14895b2016-06-06 12:37:24 -0700119 test('account info render', function() {
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700120 assert.isFalse(element._loading);
121
Wyatt Allend14895b2016-06-06 12:37:24 -0700122 assert.equal(valueOf('ID', 'profile').textContent, account._account_id);
123 assert.equal(valueOf('Name', 'profile').textContent, account.name);
124 assert.equal(valueOf('Email', 'profile').textContent, account.email);
125 assert.equal(valueOf('Username', 'profile').textContent,
126 account.username);
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700127 assert.equal(element._computeRegistered(element.account.registered),
128 'Sat, 01 Jan 2000 00:00:00 GMT');
129 });
Wyatt Allend14895b2016-06-06 12:37:24 -0700130
Wyatt Allen2dc5c362016-06-13 21:05:45 -0700131 test('calls the title-change event', function() {
132 var titleChangedStub = sinon.stub();
133
134 // Create a new view.
135 var newElement = document.createElement('gr-settings-view');
136 newElement.addEventListener('title-change', titleChangedStub);
137
138 // Attach it to the fixture.
139 var blank = fixture('blank');
140 blank.appendChild(newElement);
141
Wyatt Allen649fa8a2016-06-13 21:31:26 -0700142 Polymer.dom.flush();
143
Wyatt Allen2dc5c362016-06-13 21:05:45 -0700144 assert.isTrue(titleChangedStub.called);
145 assert.equal(titleChangedStub.getCall(0).args[0].detail.title,
146 'Settings');
147 });
148
Wyatt Allend14895b2016-06-06 12:37:24 -0700149 test('user preferences', function(done) {
150 // Rendered with the expected preferences selected.
Wyatt Allenbd472172016-06-09 17:44:51 -0700151 assert.equal(valueOf('Changes Per Page', 'preferences')
152 .firstElementChild.bindValue, preferences.changes_per_page);
Wyatt Allend14895b2016-06-06 12:37:24 -0700153 assert.equal(valueOf('Date/Time Format', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700154 .firstElementChild.bindValue, preferences.date_format);
Wyatt Allend14895b2016-06-06 12:37:24 -0700155 assert.equal(valueOf('Date/Time Format', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700156 .lastElementChild.bindValue, preferences.time_format);
Wyatt Allend14895b2016-06-06 12:37:24 -0700157 assert.equal(valueOf('Email Notifications', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700158 .firstElementChild.bindValue, preferences.email_strategy);
Wyatt Allend14895b2016-06-06 12:37:24 -0700159 assert.equal(valueOf('Diff View', 'preferences')
Wyatt Allenbd472172016-06-09 17:44:51 -0700160 .firstElementChild.bindValue, preferences.diff_view);
Wyatt Allend14895b2016-06-06 12:37:24 -0700161
162 assert.isFalse(element._prefsChanged);
163 assert.isFalse(element._menuChanged);
164
165 // Change the diff view element.
166 var diffSelect = valueOf('Diff View', 'preferences').firstElementChild;
Wyatt Allenbd472172016-06-09 17:44:51 -0700167 diffSelect.bindValue = 'SIDE_BY_SIDE';
Wyatt Allend14895b2016-06-06 12:37:24 -0700168 diffSelect.fire('change');
169
170 assert.isTrue(element._prefsChanged);
171 assert.isFalse(element._menuChanged);
172
173 stub('gr-rest-api-interface', {
174 savePreferences: function(prefs) {
175 assert.equal(prefs.diff_view, 'SIDE_BY_SIDE');
176 assertMenusEqual(prefs.my, preferences.my);
177 return Promise.resolve();
178 }
179 });
180
181 // Save the change.
182 element._handleSavePreferences().then(function() {
183 assert.isFalse(element._prefsChanged);
184 assert.isFalse(element._menuChanged);
185 done();
186 });
187 });
188
Wyatt Allen649fa8a2016-06-13 21:31:26 -0700189 test('diff preferences', function(done) {
190 // Rendered with the expected preferences selected.
191 assert.equal(valueOf('Context', 'diffPreferences')
192 .firstElementChild.bindValue, diffPreferences.context);
193 assert.equal(valueOf('Columns', 'diffPreferences')
194 .firstElementChild.bindValue, diffPreferences.line_length);
195 assert.equal(valueOf('Tab Width', 'diffPreferences')
196 .firstElementChild.bindValue, diffPreferences.tab_size);
197 assert.equal(valueOf('Show Tabs', 'diffPreferences')
198 .firstElementChild.checked, diffPreferences.show_tabs);
199
200 assert.isFalse(element._diffPrefsChanged);
201
202 var showTabsCheckbox = valueOf('Show Tabs', 'diffPreferences')
203 .firstElementChild;
204 showTabsCheckbox.checked = false;
205 element._handleShowTabsChanged();
206
207 assert.isTrue(element._diffPrefsChanged);
208
209 stub('gr-rest-api-interface', {
210 saveDiffPreferences: function(prefs) {
211 assert.equal(prefs.show_tabs, false);
212 return Promise.resolve();
213 }
214 });
215
216 // Save the change.
217 element._handleSaveDiffPreferences().then(function() {
218 assert.isFalse(element._diffPrefsChanged);
219 done();
220 });
221 });
222
Wyatt Allend14895b2016-06-06 12:37:24 -0700223 test('menu', function(done) {
224 assert.isFalse(element._menuChanged);
225 assert.isFalse(element._prefsChanged);
226
227 assertMenusEqual(element._localMenu, preferences.my);
228
229 var menu = element.$.menu.firstElementChild;
230 var tableRows = Polymer.dom(menu.root).querySelectorAll('tbody tr');
231 assert.equal(tableRows.length, preferences.my.length);
232
233 // Add a menu item:
234 element.splice('_localMenu', 1, 0, {name: 'foo', url: 'bar', target: ''});
235 Polymer.dom.flush();
236
237 tableRows = Polymer.dom(menu.root).querySelectorAll('tbody tr');
238 assert.equal(tableRows.length, preferences.my.length + 1);
239
240 assert.isTrue(element._menuChanged);
241 assert.isFalse(element._prefsChanged);
242
243 stub('gr-rest-api-interface', {
244 savePreferences: function(prefs) {
245 assertMenusEqual(prefs.my, element._localMenu);
246 return Promise.resolve();
247 }
248 });
249
250 element._handleSaveMenu().then(function() {
251 assert.isFalse(element._menuChanged);
252 assert.isFalse(element._prefsChanged);
253 assertMenusEqual(element.prefs.my, element._localMenu);
254 done();
255 });
256 });
Wyatt Allen60ea95f2016-06-03 09:35:07 -0700257 });
258</script>