Chromad: Fix kerberos files tests

Modify KerberosFilesChangeWaiter to be more robust callbacks called
several times

BUG=chromium:865206

Change-Id: I0c5f22fb26871607257d3b29ad3bd88d5b505c7c
Reviewed-on: https://chromium-review.googlesource.com/1145074
Commit-Queue: Alexander Alekseev <alemate@chromium.org>
Reviewed-by: Alexander Alekseev <alemate@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576941}
diff --git a/chrome/browser/chromeos/login/existing_user_controller_browsertest.cc b/chrome/browser/chromeos/login/existing_user_controller_browsertest.cc
index 34976ed..f553a5a 100644
--- a/chrome/browser/chromeos/login/existing_user_controller_browsertest.cc
+++ b/chrome/browser/chromeos/login/existing_user_controller_browsertest.cc
@@ -3,6 +3,7 @@
 // found in the LICENSE file.
 
 #include <string>
+#include <unordered_set>
 #include <vector>
 
 #include "base/barrier_closure.h"
@@ -138,16 +139,6 @@
   // If |files_must_exist| is true and a file already exists the class does not
   // wait when it changes.
   explicit KerberosFilesChangeWaiter(bool files_must_exist) {
-    barrier_closure_ = base::BarrierClosure(2, loop_.QuitClosure());
-
-    watch_callback_ = base::BindRepeating(
-        [](const base::RepeatingClosure& barrier_closure,
-           const base::FilePath& path, bool error) -> void {
-          EXPECT_FALSE(error);
-          barrier_closure.Run();
-        },
-        barrier_closure_);
-
     config_watcher_ = std::make_unique<base::FilePathWatcher>();
     MaybeStartWatch(&config_watcher_,
                     base::FilePath(GetKerberosConfigFileName()),
@@ -157,6 +148,8 @@
     MaybeStartWatch(&creds_watcher_,
                     base::FilePath(GetKerberosCredentialsCacheFileName()),
                     files_must_exist);
+    if (watching_paths_.empty())
+      loop_.Quit();
   }
 
   // Should be called once.
@@ -170,19 +163,31 @@
   void MaybeStartWatch(std::unique_ptr<base::FilePathWatcher>* watcher,
                        const base::FilePath& path,
                        bool files_must_exist) {
-    (*watcher)->Watch(path, false /* recursive */, watch_callback_);
+    (*watcher)->Watch(
+        path, false /* recursive */,
+        base::BindRepeating(&KerberosFilesChangeWaiter::WatchCallback,
+                            weak_factory_.GetWeakPtr()));
     if (!files_must_exist && base::PathExists(path)) {
-      watch_callback_.Run(path, false /* error */);
+      // File was written so stop the watch.
       watcher->reset();
+      return;
     }
+    watching_paths_.insert(path.value());
   }
-  base::RunLoop loop_;
-  base::RepeatingClosure barrier_closure_;
 
-  base::RepeatingCallback<void(const base::FilePath& path, bool error)>
-      watch_callback_;
+  void WatchCallback(const base::FilePath& path, bool error) {
+    EXPECT_FALSE(error);
+    watching_paths_.erase(path.value());
+    if (watching_paths_.empty())
+      loop_.Quit();
+  }
+
+  base::RunLoop loop_;
+  std::unordered_set<std::string> watching_paths_;
   std::unique_ptr<base::FilePathWatcher> config_watcher_;
   std::unique_ptr<base::FilePathWatcher> creds_watcher_;
+
+  base::WeakPtrFactory<KerberosFilesChangeWaiter> weak_factory_{this};
 };
 
 }  // namespace
@@ -977,7 +982,7 @@
 // Kerberos files.
 // Disabled due to flakiness, see https://crbug.com/865206.
 IN_PROC_BROWSER_TEST_F(ExistingUserControllerActiveDirectoryTest,
-                       DISABLED_PolicyChangeTriggersFileUpdate) {
+                       PolicyChangeTriggersFileUpdate) {
   LoginAdOnline();
 
   ApplyPolicyAndWaitFilesChanged(false /* enable_dns_cname_lookup */);
@@ -990,9 +995,8 @@
 // Tests if user Kerberos files changed D-Bus signal triggers updating user
 // Kerberos files.
 // Disabled due to flakiness, see https://crbug.com/865206.
-IN_PROC_BROWSER_TEST_F(
-    ExistingUserControllerActiveDirectoryTest,
-    DISABLED_UserKerberosFilesChangedSignalTriggersFileUpdate) {
+IN_PROC_BROWSER_TEST_F(ExistingUserControllerActiveDirectoryTest,
+                       UserKerberosFilesChangedSignalTriggersFileUpdate) {
   LoginAdOnline();
   KerberosFilesChangeWaiter files_change_waiter(true /* files_must_exist */);
   fake_authpolicy_client()->SetUserKerberosFiles("new_kerberos_creds",