OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/browser_watcher/exit_funnel_win.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/process/process_handle.h" | |
11 #include "base/strings/string16.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "base/test/test_reg_util_win.h" | |
15 #include "base/threading/platform_thread.h" | |
16 #include "base/time/time.h" | |
17 #include "base/win/registry.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace browser_watcher { | |
21 | |
22 namespace { | |
23 | |
24 const wchar_t kRegistryPath[] = L"Software\\ExitFunnelWinTest"; | |
25 | |
26 class ExitFunnelWinTest : public testing::Test { | |
27 public: | |
28 typedef testing::Test Super; | |
29 typedef std::map<base::string16, int64> EventMap; | |
30 | |
31 virtual void SetUp() override { | |
32 Super::SetUp(); | |
33 | |
34 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); | |
35 } | |
36 | |
37 base::string16 GetEventSubkey() { | |
38 // There should be a single subkey named after this process' pid. | |
39 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, kRegistryPath); | |
40 EXPECT_EQ(1, it.SubkeyCount()); | |
41 | |
42 unsigned pid = 0; | |
43 base::StringToUint(it.Name(), &pid); | |
44 EXPECT_EQ(base::GetCurrentProcId(), pid); | |
45 | |
46 return it.Name(); | |
47 } | |
48 | |
49 EventMap ReadEventsFromSubkey(const base::string16& subkey_name) { | |
50 base::string16 key_name( | |
51 base::StringPrintf(L"%ls\\%ls", kRegistryPath, subkey_name.c_str())); | |
erikwright (departed)
2014/12/11 15:00:46
It really seems like there should be a single impl
Sigurður Ásgeirsson
2014/12/12 16:21:40
The behavior is subtly different, and in any case
| |
52 | |
53 base::win::RegKey key(HKEY_CURRENT_USER, key_name.c_str(), KEY_READ); | |
54 EXPECT_TRUE(key.Valid()); | |
55 EXPECT_EQ(2, key.GetValueCount()); | |
56 | |
57 EventMap events; | |
58 for (size_t i = 0; i < key.GetValueCount(); ++i) { | |
59 base::string16 name; | |
60 EXPECT_EQ(key.GetValueNameAt(i, &name), ERROR_SUCCESS); | |
61 int64 value = 0; | |
62 EXPECT_EQ(key.ReadInt64(name.c_str(), &value), ERROR_SUCCESS); | |
63 | |
64 events[name] = value; | |
65 } | |
66 | |
67 return events; | |
68 } | |
69 | |
70 EventMap ReadEvents() { | |
71 base::string16 name = GetEventSubkey(); | |
72 return ReadEventsFromSubkey(name); | |
73 } | |
74 | |
75 protected: | |
76 registry_util::RegistryOverrideManager override_manager_; | |
77 }; | |
78 | |
79 } // namespace | |
80 | |
81 TEST_F(ExitFunnelWinTest, RecordSingleEvent) { | |
82 // Record a couple of events. | |
83 ASSERT_TRUE(ExitFunnel::RecordSingleEvent(kRegistryPath, L"One")); | |
84 ASSERT_TRUE(ExitFunnel::RecordSingleEvent(kRegistryPath, L"Two")); | |
85 | |
86 EventMap events = ReadEvents(); | |
87 | |
88 ASSERT_EQ(events.size(), 2); | |
89 ASSERT_TRUE(events.find(L"One") != events.end()); | |
90 ASSERT_TRUE(events.find(L"Two") != events.end()); | |
91 } | |
92 | |
93 TEST_F(ExitFunnelWinTest, RecordsEventTimes) { | |
94 ExitFunnel funnel; | |
95 | |
96 ASSERT_TRUE(funnel.Init(kRegistryPath, base::GetCurrentProcessHandle())); | |
97 ASSERT_TRUE(funnel.RecordEvent(L"One")); | |
98 | |
99 // Sleep for half a second to space the events in time. | |
100 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500)); | |
101 | |
102 ASSERT_TRUE(funnel.RecordEvent(L"Two")); | |
103 | |
104 EventMap events = ReadEvents(); | |
105 ASSERT_EQ(events.size(), 2); | |
106 | |
107 base::TimeDelta one = base::TimeDelta::FromInternalValue(events[L"One"]); | |
108 base::TimeDelta two = base::TimeDelta::FromInternalValue(events[L"Two"]); | |
109 | |
110 // Sleep is not accurate, it may over or under sleep. To minimize flakes, | |
111 // this test only compares relative ordering of the events. | |
112 ASSERT_LT(one, two); | |
113 } | |
114 | |
115 } // namespace browser_watcher | |
OLD | NEW |