| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008-2010 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/base/constants.h" | |
| 17 #include "omaha/base/path.h" | |
| 18 #include "omaha/base/reg_key.h" | |
| 19 #include "omaha/base/synchronized.h" | |
| 20 #include "omaha/common/const_goopdate.h" | |
| 21 #include "omaha/core/system_monitor.h" | |
| 22 #include "omaha/testing/unit_test.h" | |
| 23 | |
| 24 namespace omaha { | |
| 25 | |
| 26 class SystemMonitorTest | |
| 27 : public testing::Test, | |
| 28 public SystemMonitorObserver { | |
| 29 protected: | |
| 30 virtual void SetUp() { | |
| 31 RegKey::DeleteKey(kRegistryHiveOverrideRoot); | |
| 32 OverrideRegistryHives(kRegistryHiveOverrideRoot); | |
| 33 gate_.reset(new Gate); | |
| 34 } | |
| 35 | |
| 36 virtual void TearDown() { | |
| 37 gate_.reset(); | |
| 38 RestoreRegistryHives(); | |
| 39 RegKey::DeleteKey(kRegistryHiveOverrideRoot); | |
| 40 } | |
| 41 | |
| 42 // SystemMonitorObserver interface. | |
| 43 virtual void LastCheckedDeleted() { | |
| 44 gate_->Open(); | |
| 45 } | |
| 46 | |
| 47 virtual void NoRegisteredClients() { | |
| 48 gate_->Open(); | |
| 49 } | |
| 50 | |
| 51 void MonitorLastCheckedTest(bool is_machine); | |
| 52 void MonitorClientsTest(bool is_machine); | |
| 53 | |
| 54 scoped_ptr<Gate> gate_; | |
| 55 }; | |
| 56 | |
| 57 void SystemMonitorTest::MonitorLastCheckedTest(bool is_machine) { | |
| 58 const TCHAR* key_name = is_machine ? MACHINE_REG_UPDATE : USER_REG_UPDATE; | |
| 59 DWORD last_checked_value(1); | |
| 60 ASSERT_HRESULT_SUCCEEDED(RegKey::SetValue(key_name, | |
| 61 kRegValueLastChecked, | |
| 62 last_checked_value)); | |
| 63 SystemMonitor system_monitor(is_machine); | |
| 64 system_monitor.set_observer(this); | |
| 65 ASSERT_HRESULT_SUCCEEDED(system_monitor.Initialize(true)); | |
| 66 | |
| 67 // Trigger the callback first time. | |
| 68 EXPECT_HRESULT_SUCCEEDED(RegKey::DeleteValue(key_name, | |
| 69 kRegValueLastChecked)); | |
| 70 EXPECT_TRUE(gate_->Wait(1000)); | |
| 71 EXPECT_FALSE(RegKey::HasValue(key_name, kRegValueLastChecked)); | |
| 72 | |
| 73 // Trigger the callback second time. | |
| 74 last_checked_value = 2; | |
| 75 EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(key_name, | |
| 76 kRegValueLastChecked, | |
| 77 last_checked_value)); | |
| 78 | |
| 79 // It takes a while for the registry monitor to detect the changes. There are | |
| 80 // two changes here: setting and deleting the values. | |
| 81 ::Sleep(50); | |
| 82 EXPECT_TRUE(gate_->Close()); | |
| 83 EXPECT_HRESULT_SUCCEEDED(RegKey::DeleteValue(key_name, | |
| 84 kRegValueLastChecked)); | |
| 85 EXPECT_TRUE(gate_->Wait(1000)); | |
| 86 EXPECT_FALSE(RegKey::HasValue(key_name, kRegValueLastChecked)); | |
| 87 } | |
| 88 | |
| 89 void SystemMonitorTest::MonitorClientsTest(bool is_machine) { | |
| 90 const TCHAR* key_name = is_machine ? MACHINE_REG_CLIENTS : USER_REG_CLIENTS; | |
| 91 const TCHAR guid[] = _T("{4AAF2315-B7C8-4633-A1BA-884EFAB755F7}"); | |
| 92 | |
| 93 CString app_guid = ConcatenatePath(key_name, guid); | |
| 94 CString omaha_guid = ConcatenatePath(key_name, kGoogleUpdateAppId); | |
| 95 const TCHAR* keys_to_create[] = { app_guid, omaha_guid }; | |
| 96 EXPECT_HRESULT_SUCCEEDED(RegKey::CreateKeys(keys_to_create, | |
| 97 arraysize(keys_to_create))); | |
| 98 | |
| 99 SystemMonitor system_monitor(is_machine); | |
| 100 system_monitor.set_observer(this); | |
| 101 EXPECT_HRESULT_SUCCEEDED(system_monitor.Initialize(true)); | |
| 102 | |
| 103 EXPECT_HRESULT_SUCCEEDED(RegKey::DeleteKey(keys_to_create[0], true)); | |
| 104 EXPECT_TRUE(gate_->Wait(1000)); | |
| 105 } | |
| 106 | |
| 107 TEST_F(SystemMonitorTest, SystemMonitor) { | |
| 108 SystemMonitor system_monitor(false); | |
| 109 ASSERT_HRESULT_SUCCEEDED(system_monitor.Initialize(false)); | |
| 110 EXPECT_EQ(0, system_monitor.SendMessage(WM_POWERBROADCAST, 0, 0)); | |
| 111 EXPECT_EQ(TRUE, system_monitor.SendMessage(WM_QUERYENDSESSION, 0, 0)); | |
| 112 EXPECT_EQ(0, system_monitor.SendMessage(WM_ENDSESSION, 0, 0)); | |
| 113 EXPECT_EQ(0, system_monitor.SendMessage(WM_WTSSESSION_CHANGE, 0, 0)); | |
| 114 } | |
| 115 | |
| 116 // Tests the callback gets called when the "LastChecked" is deleted and | |
| 117 // the value is not recreated automatically by the monitor. | |
| 118 TEST_F(SystemMonitorTest, DeleteLastChecked_User) { | |
| 119 MonitorLastCheckedTest(false); | |
| 120 } | |
| 121 | |
| 122 TEST_F(SystemMonitorTest, DeleteLastChecked_Machine) { | |
| 123 MonitorLastCheckedTest(true); | |
| 124 } | |
| 125 | |
| 126 TEST_F(SystemMonitorTest, MonitorClients_User) { | |
| 127 MonitorClientsTest(false); | |
| 128 } | |
| 129 | |
| 130 TEST_F(SystemMonitorTest, MonitorClients_Machine) { | |
| 131 MonitorClientsTest(true); | |
| 132 } | |
| 133 | |
| 134 } // namespace omaha | |
| 135 | |
| OLD | NEW |