| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 #include "components/browser_watcher/exit_funnel_win.h" | 5 #include "components/browser_watcher/exit_funnel_win.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 void SetUp() override { | 31 void SetUp() override { |
| 32 Super::SetUp(); | 32 Super::SetUp(); |
| 33 | 33 |
| 34 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); | 34 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); |
| 35 } | 35 } |
| 36 | 36 |
| 37 base::string16 GetEventSubkey() { | 37 base::string16 GetEventSubkey() { |
| 38 // There should be a single subkey named after this process' pid. | 38 // There should be a single subkey named after this process' pid. |
| 39 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, kRegistryPath); | 39 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, kRegistryPath); |
| 40 EXPECT_EQ(1, it.SubkeyCount()); | 40 EXPECT_EQ(1u, it.SubkeyCount()); |
| 41 | 41 |
| 42 unsigned pid = 0; | 42 unsigned pid = 0; |
| 43 base::StringToUint(it.Name(), &pid); | 43 base::StringToUint(it.Name(), &pid); |
| 44 EXPECT_EQ(base::GetCurrentProcId(), pid); | 44 EXPECT_EQ(base::GetCurrentProcId(), pid); |
| 45 | 45 |
| 46 return it.Name(); | 46 return it.Name(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // TODO(siggi): Reuse the reading code from the metrics provider. | 49 // TODO(siggi): Reuse the reading code from the metrics provider. |
| 50 EventMap ReadEventsFromSubkey(const base::string16& subkey_name) { | 50 EventMap ReadEventsFromSubkey(const base::string16& subkey_name) { |
| 51 base::string16 key_name( | 51 base::string16 key_name( |
| 52 base::StringPrintf(L"%ls\\%ls", kRegistryPath, subkey_name.c_str())); | 52 base::StringPrintf(L"%ls\\%ls", kRegistryPath, subkey_name.c_str())); |
| 53 | 53 |
| 54 base::win::RegKey key(HKEY_CURRENT_USER, key_name.c_str(), KEY_READ); | 54 base::win::RegKey key(HKEY_CURRENT_USER, key_name.c_str(), KEY_READ); |
| 55 EXPECT_TRUE(key.Valid()); | 55 EXPECT_TRUE(key.Valid()); |
| 56 EXPECT_EQ(2, key.GetValueCount()); | 56 EXPECT_EQ(2u, key.GetValueCount()); |
| 57 | 57 |
| 58 EventMap events; | 58 EventMap events; |
| 59 for (size_t i = 0; i < key.GetValueCount(); ++i) { | 59 for (size_t i = 0; i < key.GetValueCount(); ++i) { |
| 60 base::string16 name; | 60 base::string16 name; |
| 61 EXPECT_EQ(key.GetValueNameAt(i, &name), ERROR_SUCCESS); | 61 EXPECT_EQ(key.GetValueNameAt(i, &name), ERROR_SUCCESS); |
| 62 int64 value = 0; | 62 int64 value = 0; |
| 63 EXPECT_EQ(key.ReadInt64(name.c_str(), &value), ERROR_SUCCESS); | 63 EXPECT_EQ(key.ReadInt64(name.c_str(), &value), ERROR_SUCCESS); |
| 64 | 64 |
| 65 events[name] = value; | 65 events[name] = value; |
| 66 } | 66 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 TEST_F(ExitFunnelWinTest, RecordSingleEvent) { | 82 TEST_F(ExitFunnelWinTest, RecordSingleEvent) { |
| 83 // Record a couple of events. | 83 // Record a couple of events. |
| 84 ASSERT_TRUE(ExitFunnel::RecordSingleEvent(kRegistryPath, L"One")); | 84 ASSERT_TRUE(ExitFunnel::RecordSingleEvent(kRegistryPath, L"One")); |
| 85 ASSERT_TRUE(ExitFunnel::RecordSingleEvent(kRegistryPath, L"Two")); | 85 ASSERT_TRUE(ExitFunnel::RecordSingleEvent(kRegistryPath, L"Two")); |
| 86 | 86 |
| 87 EventMap events = ReadEvents(); | 87 EventMap events = ReadEvents(); |
| 88 | 88 |
| 89 ASSERT_EQ(events.size(), 2); | 89 ASSERT_EQ(2u, events.size()); |
| 90 ASSERT_TRUE(events.find(L"One") != events.end()); | 90 ASSERT_TRUE(events.find(L"One") != events.end()); |
| 91 ASSERT_TRUE(events.find(L"Two") != events.end()); | 91 ASSERT_TRUE(events.find(L"Two") != events.end()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 TEST_F(ExitFunnelWinTest, RecordsEventTimes) { | 94 TEST_F(ExitFunnelWinTest, RecordsEventTimes) { |
| 95 ExitFunnel funnel; | 95 ExitFunnel funnel; |
| 96 | 96 |
| 97 ASSERT_TRUE(funnel.Init(kRegistryPath, base::GetCurrentProcessHandle())); | 97 ASSERT_TRUE(funnel.Init(kRegistryPath, base::GetCurrentProcessHandle())); |
| 98 ASSERT_TRUE(funnel.RecordEvent(L"One")); | 98 ASSERT_TRUE(funnel.RecordEvent(L"One")); |
| 99 | 99 |
| 100 // Sleep for half a second to space the events in time. | 100 // Sleep for half a second to space the events in time. |
| 101 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); | 101 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); |
| 102 | 102 |
| 103 ASSERT_TRUE(funnel.RecordEvent(L"Two")); | 103 ASSERT_TRUE(funnel.RecordEvent(L"Two")); |
| 104 | 104 |
| 105 EventMap events = ReadEvents(); | 105 EventMap events = ReadEvents(); |
| 106 ASSERT_EQ(events.size(), 2); | 106 ASSERT_EQ(2u, events.size()); |
| 107 | 107 |
| 108 base::TimeDelta one = base::TimeDelta::FromInternalValue(events[L"One"]); | 108 base::TimeDelta one = base::TimeDelta::FromInternalValue(events[L"One"]); |
| 109 base::TimeDelta two = base::TimeDelta::FromInternalValue(events[L"Two"]); | 109 base::TimeDelta two = base::TimeDelta::FromInternalValue(events[L"Two"]); |
| 110 | 110 |
| 111 // Sleep is not accurate, it may over or under sleep. To minimize flakes, | 111 // Sleep is not accurate, it may over or under sleep. To minimize flakes, |
| 112 // this test only compares relative ordering of the events. | 112 // this test only compares relative ordering of the events. |
| 113 ASSERT_LT(one, two); | 113 ASSERT_LT(one, two); |
| 114 } | 114 } |
| 115 | 115 |
| 116 } // namespace browser_watcher | 116 } // namespace browser_watcher |
| OLD | NEW |