OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Unit tests for GoogleChromeDistribution class. | 5 // Unit tests for GoogleChromeDistribution class. |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
| 9 #include "base/registry.h" |
9 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| 11 #include "base/file_util.h" |
10 #include "chrome/common/json_value_serializer.h" | 12 #include "chrome/common/json_value_serializer.h" |
11 #include "chrome/installer/util/browser_distribution.h" | 13 #include "chrome/installer/util/browser_distribution.h" |
12 #include "chrome/installer/util/google_update_constants.h" | 14 #include "chrome/installer/util/google_update_constants.h" |
13 #include "chrome/installer/util/google_chrome_distribution.h" | 15 #include "chrome/installer/util/google_chrome_distribution.h" |
| 16 #include "chrome/installer/util/work_item_list.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
15 | 18 |
16 TEST(GoogleChromeDistributionTest, TestExtractUninstallMetrics) { | 19 namespace { |
| 20 class GoogleChromeDistributionTest : public testing::Test { |
| 21 protected: |
| 22 virtual void SetUp() { |
| 23 // Currently no setup required. |
| 24 } |
| 25 |
| 26 virtual void TearDown() { |
| 27 // Currently no tear down required. |
| 28 } |
| 29 |
| 30 // Creates "ap" key with the value given as parameter. Also adds work |
| 31 // items to work_item_list given so that they can be rolled back later. |
| 32 bool CreateApKey(WorkItemList* work_item_list, const std::wstring& value) { |
| 33 HKEY reg_root = HKEY_CURRENT_USER; |
| 34 std::wstring reg_key = GetApKeyPath(); |
| 35 work_item_list->AddCreateRegKeyWorkItem(reg_root, reg_key); |
| 36 work_item_list->AddSetRegValueWorkItem(reg_root, reg_key, |
| 37 google_update::kRegApField, value.c_str(), true); |
| 38 if (!work_item_list->Do()) { |
| 39 work_item_list->Rollback(); |
| 40 return false; |
| 41 } |
| 42 return true; |
| 43 } |
| 44 |
| 45 // Returns the key path of "ap" key Google\Update\ClientState\<chrome-guid> |
| 46 std::wstring GetApKeyPath() { |
| 47 std::wstring reg_key(google_update::kRegPathClientState); |
| 48 reg_key.append(L"\\"); |
| 49 |
| 50 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 51 reg_key.append(dist->GetAppGuid()); |
| 52 return reg_key; |
| 53 } |
| 54 |
| 55 // Utility method to read "ap" key value |
| 56 std::wstring ReadApKeyValue() { |
| 57 RegKey key; |
| 58 std::wstring ap_key_value; |
| 59 std::wstring reg_key = GetApKeyPath(); |
| 60 if (key.Open(HKEY_CURRENT_USER, reg_key.c_str(), KEY_ALL_ACCESS) && |
| 61 key.ReadValue(google_update::kRegApField, &ap_key_value)) { |
| 62 return ap_key_value; |
| 63 } |
| 64 return std::wstring(); |
| 65 } |
| 66 }; |
| 67 } // namespace |
| 68 |
| 69 #if defined(GOOGLE_CHROME_BUILD) |
| 70 TEST_F(GoogleChromeDistributionTest, GetNewGoogleUpdateApKeyTest) { |
| 71 GoogleChromeDistribution* dist = static_cast<GoogleChromeDistribution*>( |
| 72 BrowserDistribution::GetDistribution()); |
| 73 installer_util::InstallStatus s = installer_util::FIRST_INSTALL_SUCCESS; |
| 74 installer_util::InstallStatus f = installer_util::INSTALL_FAILED; |
| 75 |
| 76 // Incremental Installer that worked. |
| 77 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, s, L""), L""); |
| 78 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, s, L"1.1"), L"1.1"); |
| 79 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, s, L"1.1-dev"), L"1.1-dev"); |
| 80 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, s, L"-full"), L""); |
| 81 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, s, L"1.1-full"), L"1.1"); |
| 82 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, s, L"1.1-dev-full"), |
| 83 L"1.1-dev"); |
| 84 |
| 85 // Incremental Installer that failed. |
| 86 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, f, L""), L"-full"); |
| 87 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, f, L"1.1"), L"1.1-full"); |
| 88 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, f, L"1.1-dev"), |
| 89 L"1.1-dev-full"); |
| 90 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, f, L"-full"), L"-full"); |
| 91 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, f, L"1.1-full"), L"1.1-full"); |
| 92 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(true, f, L"1.1-dev-full"), |
| 93 L"1.1-dev-full"); |
| 94 |
| 95 // Full Installer that worked. |
| 96 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, s, L""), L""); |
| 97 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, s, L"1.1"), L"1.1"); |
| 98 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, s, L"1.1-dev"), L"1.1-dev"); |
| 99 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, s, L"-full"), L""); |
| 100 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, s, L"1.1-full"), L"1.1"); |
| 101 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, s, L"1.1-dev-full"), |
| 102 L"1.1-dev"); |
| 103 |
| 104 // Full Installer that failed. |
| 105 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, f, L""), L""); |
| 106 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, f, L"1.1"), L"1.1"); |
| 107 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, f, L"1.1-dev"), L"1.1-dev"); |
| 108 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, f, L"-full"), L""); |
| 109 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, f, L"1.1-full"), L"1.1"); |
| 110 EXPECT_EQ(dist->GetNewGoogleUpdateApKey(false, f, L"1.1-dev-full"), |
| 111 L"1.1-dev"); |
| 112 } |
| 113 |
| 114 TEST_F(GoogleChromeDistributionTest, UpdateDiffInstallStatusTest) { |
| 115 // Get Google Chrome distribution |
| 116 GoogleChromeDistribution* dist = static_cast<GoogleChromeDistribution*>( |
| 117 BrowserDistribution::GetDistribution()); |
| 118 |
| 119 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList()); |
| 120 // Test incremental install failure |
| 121 if (!CreateApKey(work_item_list.get(), L"")) |
| 122 FAIL() << "Failed to create ap key."; |
| 123 dist->UpdateDiffInstallStatus(false, true, installer_util::INSTALL_FAILED); |
| 124 EXPECT_STREQ(ReadApKeyValue().c_str(), L"-full"); |
| 125 work_item_list->Rollback(); |
| 126 |
| 127 work_item_list.reset(WorkItem::CreateWorkItemList()); |
| 128 // Test incremental install success |
| 129 if (!CreateApKey(work_item_list.get(), L"")) |
| 130 FAIL() << "Failed to create ap key."; |
| 131 dist->UpdateDiffInstallStatus(false, true, |
| 132 installer_util::FIRST_INSTALL_SUCCESS); |
| 133 EXPECT_STREQ(ReadApKeyValue().c_str(), L""); |
| 134 work_item_list->Rollback(); |
| 135 |
| 136 work_item_list.reset(WorkItem::CreateWorkItemList()); |
| 137 // Test full install failure |
| 138 if (!CreateApKey(work_item_list.get(), L"-full")) |
| 139 FAIL() << "Failed to create ap key."; |
| 140 dist->UpdateDiffInstallStatus(false, false, installer_util::INSTALL_FAILED); |
| 141 EXPECT_STREQ(ReadApKeyValue().c_str(), L""); |
| 142 work_item_list->Rollback(); |
| 143 |
| 144 work_item_list.reset(WorkItem::CreateWorkItemList()); |
| 145 // Test full install success |
| 146 if (!CreateApKey(work_item_list.get(), L"-full")) |
| 147 FAIL() << "Failed to create ap key."; |
| 148 dist->UpdateDiffInstallStatus(false, false, |
| 149 installer_util::FIRST_INSTALL_SUCCESS); |
| 150 EXPECT_STREQ(ReadApKeyValue().c_str(), L""); |
| 151 work_item_list->Rollback(); |
| 152 |
| 153 work_item_list.reset(WorkItem::CreateWorkItemList()); |
| 154 // Test the case of when "ap" key doesnt exist at all |
| 155 std::wstring ap_key_value = ReadApKeyValue(); |
| 156 std::wstring reg_key = GetApKeyPath(); |
| 157 HKEY reg_root = HKEY_CURRENT_USER; |
| 158 bool ap_key_deleted = false; |
| 159 RegKey key; |
| 160 if (!key.Open(HKEY_CURRENT_USER, reg_key.c_str(), KEY_ALL_ACCESS)) { |
| 161 work_item_list->AddCreateRegKeyWorkItem(reg_root, reg_key); |
| 162 if (!work_item_list->Do()) |
| 163 FAIL() << "Failed to create ClientState key."; |
| 164 } else if (key.DeleteValue(google_update::kRegApField)) { |
| 165 ap_key_deleted = true; |
| 166 } |
| 167 // try differential installer |
| 168 dist->UpdateDiffInstallStatus(false, true, installer_util::INSTALL_FAILED); |
| 169 EXPECT_STREQ(ReadApKeyValue().c_str(), L"-full"); |
| 170 // try full installer now |
| 171 dist->UpdateDiffInstallStatus(false, false, installer_util::INSTALL_FAILED); |
| 172 EXPECT_STREQ(ReadApKeyValue().c_str(), L""); |
| 173 // Now cleanup to leave the system in unchanged state. |
| 174 // - Diff installer creates an ap key if it didnt exist, so delete this ap key |
| 175 // - If we created any reg key path for ap, roll it back |
| 176 // - Finally restore the original value of ap key. |
| 177 key.Open(HKEY_CURRENT_USER, reg_key.c_str(), KEY_ALL_ACCESS); |
| 178 key.DeleteValue(google_update::kRegApField); |
| 179 work_item_list->Rollback(); |
| 180 if (ap_key_deleted) { |
| 181 work_item_list.reset(WorkItem::CreateWorkItemList()); |
| 182 if (!CreateApKey(work_item_list.get(), ap_key_value)) |
| 183 FAIL() << "Failed to restore ap key."; |
| 184 } |
| 185 } |
| 186 |
| 187 TEST_F(GoogleChromeDistributionTest, TestExtractUninstallMetrics) { |
17 // A make-believe JSON preferences file. | 188 // A make-believe JSON preferences file. |
18 std::string pref_string( | 189 std::string pref_string( |
19 "{ \n" | 190 "{ \n" |
20 " \"foo\": \"bar\",\n" | 191 " \"foo\": \"bar\",\n" |
21 " \"uninstall_metrics\": { \n" | 192 " \"uninstall_metrics\": { \n" |
22 " \"last_launch_time_sec\": \"1235341118\"," | 193 " \"last_launch_time_sec\": \"1235341118\"," |
23 " \"last_observed_running_time_sec\": \"1235341183\"," | 194 " \"last_observed_running_time_sec\": \"1235341183\"," |
24 " \"launch_count\": \"11\"," | 195 " \"launch_count\": \"11\"," |
25 " \"page_load_count\": \"68\"," | 196 " \"page_load_count\": \"68\"," |
26 " \"uptime_sec\": \"809\"," | 197 " \"uptime_sec\": \"809\"," |
(...skipping 23 matching lines...) Expand all Loading... |
50 ASSERT_TRUE(root.get()); | 221 ASSERT_TRUE(root.get()); |
51 | 222 |
52 std::wstring uninstall_metrics_string; | 223 std::wstring uninstall_metrics_string; |
53 GoogleChromeDistribution* dist = static_cast<GoogleChromeDistribution*>( | 224 GoogleChromeDistribution* dist = static_cast<GoogleChromeDistribution*>( |
54 BrowserDistribution::GetDistribution()); | 225 BrowserDistribution::GetDistribution()); |
55 EXPECT_TRUE( | 226 EXPECT_TRUE( |
56 dist->ExtractUninstallMetrics(*static_cast<DictionaryValue*>(root.get()), | 227 dist->ExtractUninstallMetrics(*static_cast<DictionaryValue*>(root.get()), |
57 &uninstall_metrics_string)); | 228 &uninstall_metrics_string)); |
58 EXPECT_EQ(expected_url_string, uninstall_metrics_string); | 229 EXPECT_EQ(expected_url_string, uninstall_metrics_string); |
59 } | 230 } |
| 231 #endif |
OLD | NEW |