Switch OmahaEvent's error_code to ActionExitCode.

Also, emit the errorcode attribute only for non-success events.
Added explicit unit tests for OmahaEvent.

BUG=560
TEST=unit tests, gmerged on device, forced update, looked at logs.

Review URL: http://codereview.chromium.org/3035007
diff --git a/omaha_request_action.cc b/omaha_request_action.cc
index e801f40..97c1ea9 100644
--- a/omaha_request_action.cc
+++ b/omaha_request_action.cc
@@ -69,10 +69,15 @@
         "        <o:ping active=\"0\"></o:ping>\n"
         "        <o:updatecheck></o:updatecheck>\n");
   } else {
+    // The error code is an optional attribute so append it only if
+    // the result is not success.
+    string error_code;
+    if (event->result != OmahaEvent::kResultSuccess) {
+      error_code = StringPrintf(" errorcode=\"%d\"", event->error_code);
+    }
     body = StringPrintf(
-        "        <o:event eventtype=\"%d\" eventresult=\"%d\" "
-        "errorcode=\"%d\"></o:event>\n",
-        event->type, event->result, event->error_code);
+        "        <o:event eventtype=\"%d\" eventresult=\"%d\"%s></o:event>\n",
+        event->type, event->result, error_code.c_str());
   }
   return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" "
diff --git a/omaha_request_action.h b/omaha_request_action.h
index 5564e73..b487a3b 100644
--- a/omaha_request_action.h
+++ b/omaha_request_action.h
@@ -67,15 +67,19 @@
   OmahaEvent()
       : type(kTypeUnknown),
         result(kResultError),
-        error_code(0) {}
-  OmahaEvent(Type in_type, Result in_result, int in_error_code)
+        error_code(kActionCodeError) {}
+  explicit OmahaEvent(Type in_type)
+      : type(in_type),
+        result(kResultSuccess),
+        error_code(kActionCodeSuccess) {}
+  OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code)
       : type(in_type),
         result(in_result),
         error_code(in_error_code) {}
 
   Type type;
   Result result;
-  int error_code;
+  ActionExitCode error_code;
 };
 
 class NoneType;
diff --git a/omaha_request_action_unittest.cc b/omaha_request_action_unittest.cc
index 05ec1b8..a4ecb29 100755
--- a/omaha_request_action_unittest.cc
+++ b/omaha_request_action_unittest.cc
@@ -597,6 +597,66 @@
   EXPECT_EQ(post_str.find("o:event"), string::npos);
 }
 
+TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
+  vector<char> post_data;
+  OmahaRequestParams params("machine_id",
+                            "user_id",
+                            OmahaRequestParams::kOsPlatform,
+                            OmahaRequestParams::kOsVersion,
+                            "service_pack",
+                            "x86-generic",
+                            OmahaRequestParams::kAppId,
+                            "0.1.0.0",
+                            "en-US",
+                            "unittest_track",
+                            false,  // delta okay
+                            "http://url");
+  TestEvent(params,
+            new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
+            "invalid xml>",
+            &post_data);
+  // convert post_data to string
+  string post_str(&post_data[0], post_data.size());
+  string expected_event = StringPrintf(
+      "        <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
+      OmahaEvent::kTypeUpdateDownloadStarted,
+      OmahaEvent::kResultSuccess);
+  EXPECT_NE(post_str.find(expected_event), string::npos);
+  EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
+}
+
+TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
+  vector<char> post_data;
+  OmahaRequestParams params("machine_id",
+                            "user_id",
+                            OmahaRequestParams::kOsPlatform,
+                            OmahaRequestParams::kOsVersion,
+                            "service_pack",
+                            "x86-generic",
+                            OmahaRequestParams::kAppId,
+                            "0.1.0.0",
+                            "en-US",
+                            "unittest_track",
+                            false,  // delta okay
+                            "http://url");
+  TestEvent(params,
+            new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
+                           OmahaEvent::kResultError,
+                           kActionCodeError),
+            "invalid xml>",
+            &post_data);
+  // convert post_data to string
+  string post_str(&post_data[0], post_data.size());
+  string expected_event = StringPrintf(
+      "        <o:event eventtype=\"%d\" eventresult=\"%d\" "
+      "errorcode=\"%d\"></o:event>\n",
+      OmahaEvent::kTypeDownloadComplete,
+      OmahaEvent::kResultError,
+      kActionCodeError);
+  EXPECT_NE(post_str.find(expected_event), string::npos);
+  EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
+}
+
 TEST(OmahaRequestActionTest, FormatEventOutputTest) {
   vector<char> post_data;
   OmahaRequestParams params("machine_id",
@@ -614,7 +674,7 @@
   TestEvent(params,
             new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
                            OmahaEvent::kResultError,
-                           5),
+                           kActionCodeError),
             "invalid xml>",
             &post_data);
   // convert post_data to string
@@ -624,7 +684,7 @@
       "errorcode=\"%d\"></o:event>\n",
       OmahaEvent::kTypeDownloadComplete,
       OmahaEvent::kResultError,
-      5);
+      kActionCodeError);
   EXPECT_NE(post_str.find(expected_event), string::npos);
   EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
 }
@@ -653,9 +713,7 @@
 
   OmahaRequestAction event_action(
       params,
-      new OmahaEvent(OmahaEvent::kTypeInstallComplete,
-                     OmahaEvent::kResultError,
-                     0),
+      new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
       new MockHttpFetcher(http_response.data(),
                           http_response.size()));
   EXPECT_TRUE(event_action.IsEvent());
@@ -691,4 +749,23 @@
   }
 }
 
+TEST(OmahaRequestActionTest, OmahaEventTest) {
+  OmahaEvent default_event;
+  EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
+  EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
+  EXPECT_EQ(kActionCodeError, default_event.error_code);
+
+  OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
+  EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
+  EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
+  EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
+
+  OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
+                         OmahaEvent::kResultError,
+                         kActionCodeError);
+  EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
+  EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
+  EXPECT_EQ(kActionCodeError, error_event.error_code);
+}
+
 }  // namespace chromeos_update_engine
diff --git a/update_attempter.cc b/update_attempter.cc
index b5d9644..f4b284f 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -116,18 +116,14 @@
   shared_ptr<OmahaRequestAction> download_started_action(
       new OmahaRequestAction(omaha_request_params_,
                              new OmahaEvent(
-                                 OmahaEvent::kTypeUpdateDownloadStarted,
-                                 OmahaEvent::kResultSuccess,
-                                 0),
+                                 OmahaEvent::kTypeUpdateDownloadStarted),
                              new LibcurlHttpFetcher));
   shared_ptr<DownloadAction> download_action(
       new DownloadAction(new LibcurlHttpFetcher));
   shared_ptr<OmahaRequestAction> download_finished_action(
       new OmahaRequestAction(omaha_request_params_,
                              new OmahaEvent(
-                                 OmahaEvent::kTypeUpdateDownloadFinished,
-                                 OmahaEvent::kResultSuccess,
-                                 0),
+                                 OmahaEvent::kTypeUpdateDownloadFinished),
                              new LibcurlHttpFetcher));
   shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
       new PostinstallRunnerAction(true));
@@ -137,9 +133,7 @@
       new PostinstallRunnerAction(false));
   shared_ptr<OmahaRequestAction> update_complete_action(
       new OmahaRequestAction(omaha_request_params_,
-                             new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
-                                            OmahaEvent::kResultSuccess,
-                                            0),
+                             new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
                              new LibcurlHttpFetcher));
 
   download_action->set_delegate(this);