Fix behavior of bootstrap manager after monitoring timeout was reached

StartMonitoring resets monitor_until_ member always.
ContinueMonitoring resets monitor_until_ only for online state as we
don't have timeout in this state.

Removed check for last_configured_ssid.empty() from
UpdateConnectionState because we should shutdown bootstrapping if device
is connected using other interface.

BUG:25463084

Change-Id: I0afd943f4a3ca797b65a51236103ea3d345828d2
Reviewed-on: https://weave-review.googlesource.com/1473
Reviewed-by: Paul Westbrook <pwestbro@google.com>
diff --git a/src/weave_unittest.cc b/src/weave_unittest.cc
index 6ff3f6d..fb00cc9 100644
--- a/src/weave_unittest.cc
+++ b/src/weave_unittest.cc
@@ -21,13 +21,14 @@
 #include "src/bind_lambda.h"
 
 using testing::_;
+using testing::AtLeast;
 using testing::AtMost;
 using testing::HasSubstr;
+using testing::InSequence;
 using testing::Invoke;
 using testing::InvokeWithoutArgs;
 using testing::MatchesRegex;
 using testing::Mock;
-using testing::AtLeast;
 using testing::Return;
 using testing::ReturnRefOfCopy;
 using testing::StartsWith;
@@ -266,10 +267,13 @@
 
   void NotifyNetworkChanged(provider::Network::State state,
                             base::TimeDelta delay) {
-    EXPECT_CALL(network_, GetConnectionState()).WillRepeatedly(Return(state));
-    for (const auto& cb : network_callbacks_) {
-      task_runner_.PostDelayedTask(FROM_HERE, cb, delay);
-    }
+    auto task = [this, state] {
+      EXPECT_CALL(network_, GetConnectionState()).WillRepeatedly(Return(state));
+      for (const auto& cb : network_callbacks_)
+        cb.Run();
+    };
+
+    task_runner_.PostDelayedTask(FROM_HERE, base::Bind(task), delay);
   }
 
   std::map<std::string, provider::HttpServer::RequestHandlerCallback>
@@ -460,4 +464,67 @@
   StartDevice();
 }
 
+TEST_F(WeaveWiFiSetupTest, OfflineLongTimeWithNoSsid) {
+  EXPECT_CALL(network_, GetConnectionState())
+      .WillRepeatedly(Return(Network::State::kOffline));
+  NotifyNetworkChanged(provider::Network::State::kOnline,
+                       base::TimeDelta::FromHours(15));
+
+  {
+    InSequence s;
+    auto time_stamp = task_runner_.GetClock()->Now();
+
+    EXPECT_CALL(wifi_, StartAccessPoint(MatchesRegex("TEST_NAME.*prv")))
+        .WillOnce(InvokeWithoutArgs([this, &time_stamp]() {
+          EXPECT_LE(task_runner_.GetClock()->Now() - time_stamp,
+                    base::TimeDelta::FromMinutes(1));
+          time_stamp = task_runner_.GetClock()->Now();
+        }));
+
+    EXPECT_CALL(wifi_, StopAccessPoint())
+        .WillOnce(InvokeWithoutArgs([this, &time_stamp]() {
+          EXPECT_GT(task_runner_.GetClock()->Now() - time_stamp,
+                    base::TimeDelta::FromMinutes(5));
+          time_stamp = task_runner_.GetClock()->Now();
+          task_runner_.Break();
+        }));
+  }
+
+  StartDevice();
+}
+
+TEST_F(WeaveWiFiSetupTest, OfflineLongTimeWithSsid) {
+  EXPECT_CALL(config_store_, LoadSettings())
+      .WillRepeatedly(Return(R"({"last_configured_ssid": "TEST_ssid"})"));
+  EXPECT_CALL(network_, GetConnectionState())
+      .WillRepeatedly(Return(Network::State::kOffline));
+  NotifyNetworkChanged(provider::Network::State::kOnline,
+                       base::TimeDelta::FromHours(15));
+
+  {
+    InSequence s;
+    auto time_stamp = task_runner_.GetClock()->Now();
+    for (size_t i = 0; i < 10; ++i) {
+      EXPECT_CALL(wifi_, StartAccessPoint(MatchesRegex("TEST_NAME.*prv")))
+          .WillOnce(InvokeWithoutArgs([this, &time_stamp]() {
+            EXPECT_GT(task_runner_.GetClock()->Now() - time_stamp,
+                      base::TimeDelta::FromMinutes(1));
+            time_stamp = task_runner_.GetClock()->Now();
+          }));
+
+      EXPECT_CALL(wifi_, StopAccessPoint())
+          .WillOnce(InvokeWithoutArgs([this, &time_stamp]() {
+            EXPECT_GT(task_runner_.GetClock()->Now() - time_stamp,
+                      base::TimeDelta::FromMinutes(5));
+            time_stamp = task_runner_.GetClock()->Now();
+          }));
+    }
+
+    EXPECT_CALL(wifi_, StartAccessPoint(MatchesRegex("TEST_NAME.*prv")))
+        .WillOnce(InvokeWithoutArgs([this]() { task_runner_.Break(); }));
+  }
+
+  StartDevice();
+}
+
 }  // namespace weave