| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * @fileoverview Tests the local NTP. | 7 * @fileoverview Tests the local NTP. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Shortcut for document.getElementById. | 12 * Shortcut for document.getElementById. |
| 13 * @param {string} id of the element. | 13 * @param {string} id of the element. |
| 14 * @return {HTMLElement} with the id. | 14 * @return {HTMLElement} with the id. |
| 15 */ | 15 */ |
| 16 function $(id) { | 16 function $(id) { |
| 17 return document.getElementById(id); | 17 return document.getElementById(id); |
| 18 } | 18 } |
| 19 | 19 |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Sets up for the next test case. Recreates the default local NTP DOM. | 22 * Sets up for the next test case. Recreates the default local NTP DOM. |
| 23 */ | 23 */ |
| 24 function setUp() { | 24 function setUp() { |
| 25 // First, clear up the DOM and state left over from any previous test case. |
| 25 document.body.innerHTML = ''; | 26 document.body.innerHTML = ''; |
| 27 // The NTP stores some state such as fakebox focus in the body's classList. |
| 28 document.body.classList = ''; |
| 29 |
| 26 document.body.appendChild($('local-ntp-body').content.cloneNode(true)); | 30 document.body.appendChild($('local-ntp-body').content.cloneNode(true)); |
| 27 } | 31 } |
| 28 | 32 |
| 29 | |
| 30 /** | 33 /** |
| 31 * Cleans up after test execution. | |
| 32 */ | |
| 33 function tearDown() { | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Aborts a test if a condition is not met. | 34 * Aborts a test if a condition is not met. |
| 38 * @param {boolean} condition The condition that must be true. | 35 * @param {boolean} condition The condition that must be true. |
| 39 * @param {string=} opt_message A message to log if the condition is not met. | 36 * @param {string=} opt_message A message to log if the condition is not met. |
| 40 */ | 37 */ |
| 41 function assert(condition, opt_message) { | 38 function assert(condition, opt_message) { |
| 42 if (!condition) | 39 if (!condition) |
| 43 throw new Error(opt_message || 'Assertion failed'); | 40 throw new Error(opt_message || 'Assertion failed'); |
| 44 } | 41 } |
| 45 | 42 |
| 46 /** | 43 /** |
| 47 * Runs all tests. | 44 * Runs all simple tests, i.e. those that don't require interaction from the |
| 45 * native side. |
| 48 * @return {boolean} True if all tests pass and false otherwise. | 46 * @return {boolean} True if all tests pass and false otherwise. |
| 49 */ | 47 */ |
| 50 function runTests() { | 48 function runSimpleTests() { |
| 51 var pass = true; | 49 var pass = true; |
| 52 for (var testName in window) { | 50 for (var testName in window) { |
| 53 if (/^test.+/.test(testName) && typeof window[testName] == 'function') { | 51 if (/^test.+/.test(testName) && typeof window[testName] == 'function') { |
| 54 try { | 52 try { |
| 55 setUp(); | 53 setUp(); |
| 56 window[testName].call(window); | 54 window[testName].call(window); |
| 57 tearDown(); | |
| 58 } catch (err) { | 55 } catch (err) { |
| 59 window.console.log(testName + ' ' + err); | 56 window.console.log(testName + ' ' + err); |
| 60 pass = false; | 57 pass = false; |
| 61 } | 58 } |
| 62 } | 59 } |
| 63 } | 60 } |
| 64 return pass; | 61 return pass; |
| 65 } | 62 } |
| 66 | 63 |
| 67 | 64 |
| 68 /** | 65 /** |
| 66 * Creates and initializes a LocalNTP object. |
| 67 * @param {boolean} isGooglePage Whether to make it a Google-branded NTP. |
| 68 */ |
| 69 function initLocalNTP(isGooglePage) { |
| 70 configData.isGooglePage = isGooglePage; |
| 71 var localNTP = LocalNTP(); |
| 72 localNTP.init(); |
| 73 } |
| 74 |
| 75 |
| 76 /** |
| 69 * Checks whether a given HTMLElement exists and is visible. | 77 * Checks whether a given HTMLElement exists and is visible. |
| 70 * @param {HTMLElement|undefined} elem An HTMLElement. | 78 * @param {HTMLElement|undefined} elem An HTMLElement. |
| 71 * @return {boolean} True if the element exists and is visible. | 79 * @return {boolean} True if the element exists and is visible. |
| 72 */ | 80 */ |
| 73 function elementIsVisible(elem) { | 81 function elementIsVisible(elem) { |
| 74 return elem && elem.offsetWidth > 0 && elem.offsetHeight > 0; | 82 return elem && elem.offsetWidth > 0 && elem.offsetHeight > 0 && |
| 83 window.getComputedStyle(elem).visibility != 'hidden'; |
| 75 } | 84 } |
| 76 | 85 |
| 77 | 86 |
| 87 // ******************************* SIMPLE TESTS ******************************* |
| 88 // These are run by runSimpleTests above. |
| 89 |
| 90 |
| 78 /** | 91 /** |
| 79 * Tests that Google NTPs show a fakebox and logo. | 92 * Tests that Google NTPs show a fakebox and logo. |
| 80 */ | 93 */ |
| 81 function testShowsFakeboxAndLogoIfGoogle() { | 94 function testShowsFakeboxAndLogoIfGoogle() { |
| 82 var localNTP = LocalNTP(); | 95 initLocalNTP(/*isGooglePage=*/true); |
| 83 configData.isGooglePage = true; | |
| 84 localNTP.init(); | |
| 85 assert(elementIsVisible($('fakebox'))); | 96 assert(elementIsVisible($('fakebox'))); |
| 86 assert(elementIsVisible($('logo'))); | 97 assert(elementIsVisible($('logo'))); |
| 87 } | 98 } |
| 88 | 99 |
| 89 | 100 |
| 90 /** | 101 /** |
| 91 * Tests that non-Google NTPs do not show a fakebox. | 102 * Tests that non-Google NTPs do not show a fakebox. |
| 92 */ | 103 */ |
| 93 function testDoesNotShowFakeboxIfNotGoogle() { | 104 function testDoesNotShowFakeboxIfNotGoogle() { |
| 94 var localNTP = LocalNTP(); | 105 initLocalNTP(/*isGooglePage=*/false); |
| 95 configData.isGooglePage = false; | |
| 96 localNTP.init(); | |
| 97 assert(!elementIsVisible($('fakebox'))); | 106 assert(!elementIsVisible($('fakebox'))); |
| 98 assert(!elementIsVisible($('logo'))); | 107 assert(!elementIsVisible($('logo'))); |
| 99 } | 108 } |
| 109 |
| 110 |
| 111 |
| 112 // ****************************** ADVANCED TESTS ****************************** |
| 113 // Advanced tests are controlled from the native side. The helpers here are |
| 114 // called from native code to set up the page and to check results. |
| 115 |
| 116 function setupAdvancedTest() { |
| 117 setUp(); |
| 118 initLocalNTP(/*isGooglePage=*/true); |
| 119 |
| 120 assert(elementIsVisible($('fakebox'))); |
| 121 |
| 122 return true; |
| 123 } |
| 124 |
| 125 function getFakeboxPositionX() { |
| 126 assert(elementIsVisible($('fakebox'))); |
| 127 var rect = $('fakebox').getBoundingClientRect(); |
| 128 return rect.left; |
| 129 } |
| 130 |
| 131 function getFakeboxPositionY() { |
| 132 assert(elementIsVisible($('fakebox'))); |
| 133 var rect = $('fakebox').getBoundingClientRect(); |
| 134 return rect.top; |
| 135 } |
| 136 |
| 137 function fakeboxIsVisible() { |
| 138 return elementIsVisible($('fakebox')); |
| 139 } |
| 140 |
| 141 function fakeboxIsFocused() { |
| 142 return fakeboxIsVisible() && |
| 143 document.body.classList.contains('fakebox-focused'); |
| 144 } |
| OLD | NEW |