[modular] Automatically start running a newly started story in StoryProvider.

Updates relevant integration tests to expect the stories to start
automatically.

Also updates the TestSessionShell (part of the modular
test harness) to not automatically start stories, since that's done
anyway now (note that this change leaves the OnUpdate() function there in case
it's needed in the future for something like recording story state
changes in tests).

TEST=fx run-test modular_tests

MF-271 #done

Change-Id: I0fb8a9def8b9a385fa3420e21193ae0685040a46
diff --git a/peridot/bin/modular_test_harness/test_session_shell.cc b/peridot/bin/modular_test_harness/test_session_shell.cc
index dc624d3..188d7dd 100644
--- a/peridot/bin/modular_test_harness/test_session_shell.cc
+++ b/peridot/bin/modular_test_harness/test_session_shell.cc
@@ -17,10 +17,7 @@
 
 namespace {
 
-// Implementation of a minimal session shell used for testing purposes. This
-// session shell listens for new stories and starts them if they are in a
-// stopped state. Note that stopping a running story will cause it to start up
-// again.
+// Implementation of a minimal session shell used for testing purposes.
 class TestSessionShellApp : public modular::ViewApp,
                             public fuchsia::modular::StoryProviderWatcher,
                             public fuchsia::modular::SessionShell {
@@ -70,14 +67,7 @@
   void OnChange(
       fuchsia::modular::StoryInfo story_info,
       fuchsia::modular::StoryState story_state,
-      fuchsia::modular::StoryVisibilityState story_visibility_state) override {
-    if (story_state == fuchsia::modular::StoryState::STOPPED) {
-      fuchsia::modular::StoryControllerPtr story_controller;
-      story_provider_->GetController(story_info.id,
-                                     story_controller.NewRequest());
-      story_controller->RequestStart();
-    }
-  }
+      fuchsia::modular::StoryVisibilityState story_visibility_state) override {}
 
   // |fuchsia::modular::StoryProviderWatcher|
   void OnDelete(std::string story_id) override {}
diff --git a/peridot/bin/sessionmgr/story_runner/story_provider_impl.cc b/peridot/bin/sessionmgr/story_runner/story_provider_impl.cc
index 1b10f6b..709bd7f 100644
--- a/peridot/bin/sessionmgr/story_runner/story_provider_impl.cc
+++ b/peridot/bin/sessionmgr/story_runner/story_provider_impl.cc
@@ -641,7 +641,8 @@
   // If we have a StoryRuntimeContainer for this story id, update our cached
   // StoryData and get runtime state available from it.
   //
-  // Otherwise, use defaults for an unloaded story.
+  // Otherwise, use defaults for an unloaded story and send a request for the
+  // story to start running (stories should start running by default).
   fuchsia::modular::StoryState runtime_state =
       fuchsia::modular::StoryState::STOPPED;
   fuchsia::modular::StoryVisibilityState visibility_state =
@@ -651,6 +652,10 @@
     runtime_state = i->second.model_observer->model().runtime_state();
     visibility_state = i->second.model_observer->model().visibility_state();
     i->second.current_data = CloneOptional(story_data);
+  } else {
+    fuchsia::modular::StoryControllerPtr story_controller;
+    GetController(story_id, story_controller.NewRequest());
+    story_controller->RequestStart();
   }
   NotifyStoryWatchers(&story_data, runtime_state, visibility_state);
 }
diff --git a/peridot/tests/session_shell/session_shell_test_session_shell.cc b/peridot/tests/session_shell/session_shell_test_session_shell.cc
index 64b6467..e2ab71b 100644
--- a/peridot/tests/session_shell/session_shell_test_session_shell.cc
+++ b/peridot/tests/session_shell/session_shell_test_session_shell.cc
@@ -294,14 +294,13 @@
                                       fuchsia::modular::StoryState state) {
       story1_get_controller_.Pass();
       story_info_ = std::move(story_info);
-      TestStory1_Run();
+      TestStory1_CheckRunning();
     });
   }
 
   TestPoint story1_run_{"Story1 Run"};
-  void TestStory1_Run() {
+  void TestStory1_CheckRunning() {
     // Start and show the new story.
-    story_controller_->RequestStart();
     story1_run_.Pass();
     TestStory1_Stop();
   }
@@ -369,25 +368,13 @@
             story2_get_modules_.Pass();
           }
 
-          TestStory2_Run();
+          TestStory2_CheckRunning();
         });
   }
 
-  TestPoint story2_state_before_run_{"Story2 State before Run"};
   TestPoint story2_state_after_run_{"Story2 State after Run"};
 
-  void TestStory2_Run() {
-    story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
-                                      fuchsia::modular::StoryState state) {
-      if (state == fuchsia::modular::StoryState::STOPPED) {
-        story2_state_before_run_.Pass();
-      }
-    });
-
-    // Start and show the new story *while* the GetInfo() call above is in
-    // flight.
-    story_controller_->RequestStart();
-
+  void TestStory2_CheckRunning() {
     story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
                                       fuchsia::modular::StoryState state) {
       if (state == fuchsia::modular::StoryState::RUNNING) {
@@ -474,15 +461,13 @@
               FXL_LOG(INFO) << item.id;
             }
           }
-          TestStory3_Run();
+          TestStory3_CheckRunning();
         });
   }
 
   TestPoint story3_run_{"Story3 Run"};
 
-  void TestStory3_Run() {
-    story_controller_->RequestStart();
-
+  void TestStory3_CheckRunning() {
     story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
                                       fuchsia::modular::StoryState state) {
       if (state == fuchsia::modular::StoryState::RUNNING) {
@@ -527,7 +512,7 @@
 
   // Test Case Story4:
   //
-  // Create a story and start it with RequestStart() rather than Start().
+  // Create a story; it should start running automatically.
   //
   // Verify the view is received through SessionShell.AttachView().
   //
@@ -550,33 +535,22 @@
     commands.push_back(std::move(command));
 
     story_puppet_master_->Enqueue(std::move(commands));
-    story_puppet_master_->Execute(
-        [this](fuchsia::modular::ExecuteResult result) {
-          story4_create_.Pass();
-          TestStory4_Run();
-        });
-  }
-
-  TestPoint story4_state_before_run_{"Story4 State before Run"};
-  TestPoint story4_state_after_run_{"Story4 State after Run"};
-  TestPoint story4_attach_view_{"Story4 attach View"};
-
-  void TestStory4_Run() {
-    story_provider()->GetController("story4", story_controller_.NewRequest());
-    story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
-                                      fuchsia::modular::StoryState state) {
-      story_info_ = std::move(info);
-      if (state == fuchsia::modular::StoryState::STOPPED) {
-        story4_state_before_run_.Pass();
-      }
-    });
-
-    // Start and show the new story using RequestStart().
-    story_controller_->RequestStart();
 
     session_shell_impl()->set_on_attach_view(
         [this](ViewId) { story4_attach_view_.Pass(); });
 
+    story_puppet_master_->Execute(
+        [this](fuchsia::modular::ExecuteResult result) {
+          story4_create_.Pass();
+          TestStory4_CheckRunning();
+        });
+  }
+
+  TestPoint story4_state_after_run_{"Story4 State after Run"};
+  TestPoint story4_attach_view_{"Story4 attach View"};
+
+  void TestStory4_CheckRunning() {
+    story_provider()->GetController("story4", story_controller_.NewRequest());
     story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
                                       fuchsia::modular::StoryState state) {
       if (state == fuchsia::modular::StoryState::RUNNING) {
@@ -625,7 +599,7 @@
 
   // Test Case Story5:
   //
-  // Create a story and start it with RequestStart() rather than Start().
+  // Create a story; it should start running automatically.
   //
   // Verify that, when the story is stopped, a request for
   // SessionShell.DetachView() is received, and if the request is not answered,
@@ -647,35 +621,23 @@
     commands.push_back(std::move(command));
 
     story_puppet_master_->Enqueue(std::move(commands));
-    story_puppet_master_->Execute(
-        [this](fuchsia::modular::ExecuteResult result) {
-          story5_create_.Pass();
-          TestStory5_Run();
-        });
-  }
-
-  TestPoint story5_state_before_run_{"Story5 State before Run"};
-  TestPoint story5_state_after_run_{"Story5 State after Run"};
-
-  TestPoint story5_attach_view_{"Story5 attach View"};
-
-  void TestStory5_Run() {
-    story_provider()->GetController("story5", story_controller_.NewRequest());
-
-    story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
-                                      fuchsia::modular::StoryState state) {
-      story_info_ = std::move(info);
-      if (state == fuchsia::modular::StoryState::STOPPED) {
-        story5_state_before_run_.Pass();
-      }
-    });
-
-    // Start and show the new story using RequestStart().
-    story_controller_->RequestStart();
 
     session_shell_impl()->set_on_attach_view(
         [this](ViewId) { story5_attach_view_.Pass(); });
 
+    story_puppet_master_->Execute(
+        [this](fuchsia::modular::ExecuteResult result) {
+          story5_create_.Pass();
+          TestStory5_CheckRunning();
+        });
+  }
+
+  TestPoint story5_state_after_run_{"Story5 State after Run"};
+
+  TestPoint story5_attach_view_{"Story5 attach View"};
+
+  void TestStory5_CheckRunning() {
+    story_provider()->GetController("story5", story_controller_.NewRequest());
     story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
                                       fuchsia::modular::StoryState state) {
       if (state == fuchsia::modular::StoryState::RUNNING) {
@@ -727,7 +689,7 @@
 
   // Test Case Story6:
   //
-  // Create a story and start it with RequestStart() rather than Start().
+  // Create a story; it should start running automatically.
   //
   // Verify that, when the story is NOT stopped when the SessionShell is stopped
   // (such as at Logout) NO request for SessionShell.DetachView() is received.
@@ -748,35 +710,21 @@
     commands.push_back(std::move(command));
 
     story_puppet_master_->Enqueue(std::move(commands));
+    session_shell_impl()->set_on_attach_view(
+        [this](ViewId) { story6_attach_view_.Pass(); });
     story_puppet_master_->Execute(
         [this](fuchsia::modular::ExecuteResult result) {
           story6_create_.Pass();
-          TestStory6_Run();
+          TestStory6_CheckRunning();
         });
   }
 
-  TestPoint story6_state_before_run_{"Story6 State before Run"};
   TestPoint story6_state_after_run_{"Story6 State after Run"};
 
   TestPoint story6_attach_view_{"Story6 attach View"};
 
-  void TestStory6_Run() {
+  void TestStory6_CheckRunning() {
     story_provider()->GetController("story6", story_controller_.NewRequest());
-
-    story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
-                                      fuchsia::modular::StoryState state) {
-      story_info_ = std::move(info);
-      if (state == fuchsia::modular::StoryState::STOPPED) {
-        story6_state_before_run_.Pass();
-      }
-    });
-
-    // Start and show the new story using RequestStart().
-    story_controller_->RequestStart();
-
-    session_shell_impl()->set_on_attach_view(
-        [this](ViewId) { story6_attach_view_.Pass(); });
-
     story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
                                       fuchsia::modular::StoryState state) {
       if (state == fuchsia::modular::StoryState::RUNNING) {
diff --git a/peridot/tests/story_shell_factory/story_shell_factory_test_session_shell.cc b/peridot/tests/story_shell_factory/story_shell_factory_test_session_shell.cc
index c5fa67b..91487768 100644
--- a/peridot/tests/story_shell_factory/story_shell_factory_test_session_shell.cc
+++ b/peridot/tests/story_shell_factory/story_shell_factory_test_session_shell.cc
@@ -55,7 +55,7 @@
  private:
   // TestStoryShellAttachDetach
   //
-  // Create a story and start.
+  // Create a story, which should start running automatically.
   //
   // Verify the story shell is attached through StoryShellFactory.AttachStory()
   //
@@ -78,32 +78,6 @@
     commands.push_back(std::move(command));
 
     story_puppet_master_->Enqueue(std::move(commands));
-    story_puppet_master_->Execute(
-        [this](fuchsia::modular::ExecuteResult result) {
-          story_shell_attach_detach_create_.Pass();
-          TestStoryShellAttachDetach_RunStory();
-        });
-  }
-
-  TestPoint story_shell_attach_detach_state_before_run_{
-      "TestStoryShellAttachDetach: State before Run"};
-  TestPoint story_shell_attach_detach_state_after_run_{
-      "TestStoryShellAttachDetach: State after Run"};
-  TestPoint story_shell_attach_detach_attach_story_{
-      "TestStoryShellAttachDetach: AttachStory"};
-
-  void TestStoryShellAttachDetach_RunStory() {
-    story_provider()->GetController("story1", story_controller_.NewRequest());
-    story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
-                                      fuchsia::modular::StoryState state) {
-      story_info_ = std::move(info);
-      if (state == fuchsia::modular::StoryState::STOPPED) {
-        story_shell_attach_detach_state_before_run_.Pass();
-      }
-    });
-
-    // Start and show the new story using RequestStart().
-    story_controller_->RequestStart();
 
     story_shell_factory_impl_.set_on_attach_story(
         [this](std::string,
@@ -113,6 +87,20 @@
           story_shell_impl_.GetHandler()(std::move(request));
         });
 
+    story_puppet_master_->Execute(
+        [this](fuchsia::modular::ExecuteResult result) {
+          story_shell_attach_detach_create_.Pass();
+          TestStoryShellAttachDetach_RunStory();
+        });
+  }
+
+  TestPoint story_shell_attach_detach_state_after_run_{
+      "TestStoryShellAttachDetach: State after Run"};
+  TestPoint story_shell_attach_detach_attach_story_{
+      "TestStoryShellAttachDetach: AttachStory"};
+
+  void TestStoryShellAttachDetach_RunStory() {
+    story_provider()->GetController("story1", story_controller_.NewRequest());
     story_controller_->GetInfo([this](fuchsia::modular::StoryInfo info,
                                       fuchsia::modular::StoryState state) {
       if (state == fuchsia::modular::StoryState::RUNNING) {