MD Settings: Fix and re-enable settings-main tests.

 - Updated cases where the code had rotten (for example functions
   that used to return synchronously now return a Promise).
 - Updated test logic where Polymer.dom.flush() was not sufficient
   anymore, to use a MutationObserer (for detecting visibility changes).
 - Re-enable tests an all platforms, for non-debug builds. Debug builds
   remain disabled.

The tests still use the real chrome.settingsPrivate API, which makes it
likely that they could flake (timeout). Will address that if it happens.

BUG=667882
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2863213002
Cr-Commit-Position: refs/heads/master@{#470123}
diff --git a/chrome/browser/resources/settings/settings_main/settings_main.html b/chrome/browser/resources/settings/settings_main/settings_main.html
index 9eb0f6a..7132c20 100644
--- a/chrome/browser/resources/settings/settings_main/settings_main.html
+++ b/chrome/browser/resources/settings/settings_main/settings_main.html
@@ -8,6 +8,7 @@
 <link rel="import" href="../about_page/about_page.html">
 <link rel="import" href="../basic_page/basic_page.html">
 <link rel="import" href="../i18n_setup.html">
+<link rel="import" href="../prefs/prefs.html">
 <link rel="import" href="../route.html">
 <link rel="import" href="../settings_shared_css.html">
 <link rel="import" href="../settings_vars_css.html">
diff --git a/chrome/test/data/webui/settings/cr_settings_browsertest.js b/chrome/test/data/webui/settings/cr_settings_browsertest.js
index 0768c5b..d919eb7 100644
--- a/chrome/test/data/webui/settings/cr_settings_browsertest.js
+++ b/chrome/test/data/webui/settings/cr_settings_browsertest.js
@@ -251,7 +251,6 @@
   __proto__: CrSettingsBrowserTest.prototype,
 
   /** @override */
-  //browsePreload: 'chrome://md-settings/settings_main/settings_main.html',
   browsePreload: 'chrome://md-settings/people_page/lock_screen.html',
 
   /** @override */
@@ -1428,11 +1427,10 @@
 
 // Times out on Windows Tests (dbg). See https://crbug.com/651296.
 // Times out / crashes on chromium.linux/Linux Tests (dbg) crbug.com/667882
-GEN('#if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_LINUX)' +
-    ' || defined(OS_MACOSX)');
-GEN('#define MAYBE_MainPage_All DISABLED_All');
+GEN('#if !defined(NDEBUG)')
+GEN('#define MAYBE_MainPage_All DISABLED_MainPage_All');
 GEN('#else');
-GEN('#define MAYBE_MainPage_All All');
+GEN('#define MAYBE_MainPage_All MainPage_All');
 GEN('#endif');
 
 /**
@@ -1451,6 +1449,7 @@
   /** @override */
   extraLibraries: CrSettingsBrowserTest.prototype.extraLibraries.concat([
     'test_browser_proxy.js',
+    'test_util.js',
     'settings_main_test.js',
   ]),
 };
diff --git a/chrome/test/data/webui/settings/settings_main_test.js b/chrome/test/data/webui/settings/settings_main_test.js
index 7d5b281..af1b160 100644
--- a/chrome/test/data/webui/settings/settings_main_test.js
+++ b/chrome/test/data/webui/settings/settings_main_test.js
@@ -169,15 +169,20 @@
        * Asserts the visibility of the basic and advanced pages.
        * @param {string} Expected 'display' value for the basic page.
        * @param {string} Expected 'display' value for the advanced page.
+       * @return {!Promise}
        */
       function assertPageVisibility(expectedBasic, expectedAdvanced) {
         Polymer.dom.flush();
         var page = settingsMain.$$('settings-basic-page');
         assertEquals(
-            expectedBasic, page.$$('#basicPage').style.display);
-        assertEquals(
-            expectedAdvanced,
-            page.$$('#advancedPageTemplate').get().style.display);
+            expectedBasic, getComputedStyle(page.$$('#basicPage')).display);
+
+        return page.$$('#advancedPageTemplate').get().then(
+            function(advancedPage) {
+              assertEquals(
+                  expectedAdvanced,
+                  getComputedStyle(advancedPage).display);
+        });
       }
 
       // TODO(michaelpg): It would be better not to drill into
@@ -199,7 +204,7 @@
           searchManager.setMatchesFound(false);
           return settingsMain.searchContents('');
         }).then(function() {
-          assertPageVisibility(expectedBasic, expectedAdvanced);
+          return assertPageVisibility(expectedBasic, expectedAdvanced);
         });
       }
 
@@ -207,7 +212,7 @@
         // Simulating searching while the advanced page is collapsed.
         settingsMain.currentRouteChanged(settings.Route.BASIC);
         Polymer.dom.flush();
-        return assertPageVisibilityAfterSearch('', 'none');
+        return assertPageVisibilityAfterSearch('block', 'none');
       });
 
       // Ensure that clearing the search results restores both "basic" and
@@ -216,7 +221,7 @@
       test('exiting search mode, advanced expanded', function() {
         settings.navigateTo(settings.Route.SITE_SETTINGS);
         Polymer.dom.flush();
-        return assertPageVisibilityAfterSearch('', '');
+        return assertPageVisibilityAfterSearch('block', 'block');
       });
 
       // Ensure that searching, then entering a subpage, then going back
@@ -235,7 +240,7 @@
 
           // Simulate clicking the left arrow to go back to the search results.
           settings.navigateTo(settings.Route.BASIC);
-          assertPageVisibility('', '');
+          return assertPageVisibility('block', 'block');
         });
       });
 
@@ -244,14 +249,25 @@
         settings.navigateTo(settings.Route.PRIVACY);
         Polymer.dom.flush();
 
-        var advancedToggle =
-            getToggleContainer().querySelector('#advancedToggle');
-        assertTrue(!!advancedToggle);
+        var basicPage = settingsMain.$$('settings-basic-page');
+        var advancedPage = null;
+        return basicPage.$$('#advancedPageTemplate').get().then(
+            function(advanced) {
+              advancedPage = advanced;
+              return assertPageVisibility('block', 'block');
+            }).then(function() {
+              var whenHidden = test_util.whenAttributeIs(
+                  advancedPage, 'hidden', true);
 
-        MockInteractions.tap(advancedToggle);
-        Polymer.dom.flush();
+              var advancedToggle =
+                  getToggleContainer().querySelector('#advancedToggle');
+              assertTrue(!!advancedToggle);
+              MockInteractions.tap(advancedToggle);
 
-        assertPageVisibility('', 'none');
+              return whenHidden;
+            }).then(function() {
+              return assertPageVisibility('block', 'none');
+            });
       });
 
       test('navigating to a basic page does not collapse advanced', function() {
@@ -263,7 +279,7 @@
         settings.navigateTo(settings.Route.PEOPLE);
         Polymer.dom.flush();
 
-        assertPageVisibility('', '');
+        return assertPageVisibility('block', 'block');
       });
     });
   }
diff --git a/chrome/test/data/webui/settings/test_util.js b/chrome/test/data/webui/settings/test_util.js
index d6cf3f7..4ed7650 100644
--- a/chrome/test/data/webui/settings/test_util.js
+++ b/chrome/test/data/webui/settings/test_util.js
@@ -12,7 +12,12 @@
    * @return {!Promise}
    */
   function whenAttributeIs(target, attributeName, attributeValue) {
-    function isDone() { return target[attributeName] === attributeValue; }
+    function isDone() {
+      // TODO(dpapad): Following line should check for an attribute, not a
+      // property, meaning target.getAttribute(attributeName). Fix this and
+      // update callers to pass an attribute value instead.
+      return target[attributeName] === attributeValue;
+    }
 
     return isDone() ? Promise.resolve() : new Promise(function(resolve) {
       new MutationObserver(function(mutations, observer) {