Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: chrome/browser/chromeos/settings/device_oauth2_token_service_factory_unittest.cc

Issue 39443002: settings: Add async system salt retrieval logic in DeviceOAuth2TokenServiceFactory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add unit test Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h "
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "chromeos/cryptohome/system_salt_getter.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace chromeos {
16 namespace {
17
18 // Copies the token service and increments the counter.
19 void CopyTokenServiceAndCount(
20 chromeos::DeviceOAuth2TokenService** out_token_service,
21 int* counter,
22 chromeos::DeviceOAuth2TokenService* in_token_service) {
23 *out_token_service = in_token_service;
24 ++(*counter);
25 }
26
27 // Sets up and tears down DeviceOAuth2TokenServiceFactory and its
28 // dependencies.
29 class ScopedDeviceOAuth2TokenServiceFactorySetUp {
30 public:
31 ScopedDeviceOAuth2TokenServiceFactorySetUp() {
32 DBusThreadManager::Initialize();
33 SystemSaltGetter::Initialize();
34 DeviceOAuth2TokenServiceFactory::Initialize();
35 }
36
37 ~ScopedDeviceOAuth2TokenServiceFactorySetUp() {
38 DeviceOAuth2TokenServiceFactory::Shutdown();
39 SystemSaltGetter::Shutdown();
40 DBusThreadManager::Shutdown();
41 }
42 };
43
44 } // namespace
45
46 class DeviceOAuth2TokenServiceFactoryTest : public testing::Test {
47 protected:
48 content::TestBrowserThreadBundle thread_bundle_;
49 };
50
51 // Test a case where Get() is called before the factory is initialized.
52 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_Uninitialized) {
53 DeviceOAuth2TokenService* token_service = NULL;
54 int counter = 0;
55 DeviceOAuth2TokenServiceFactory::Get(
56 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter));
57 // The callback will be run asynchronously.
58 EXPECT_EQ(0, counter);
59 EXPECT_FALSE(token_service);
60
61 // This lets the factory to run callback with NULL.
62 base::RunLoop().RunUntilIdle();
63 EXPECT_EQ(1, counter);
64 EXPECT_FALSE(token_service);
65 }
66
67 // Test a case where Get() is called from only one caller.
68 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_Simple) {
69 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup;
70
71 DeviceOAuth2TokenService* token_service = NULL;
72 int counter = 0;
73 DeviceOAuth2TokenServiceFactory::Get(
74 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter));
75 // The callback will be run asynchronously.
76 EXPECT_EQ(0, counter);
77 EXPECT_FALSE(token_service);
78
79 // This lets FakeCryptohomeClient return the system salt.
80 base::RunLoop().RunUntilIdle();
81 EXPECT_EQ(1, counter);
82 EXPECT_TRUE(token_service);
83 }
84
85 // Test a case where Get() is called from multiple callers, before the token
86 // service is ready, and confirm that the callback of every caller is run.
87 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_MultipleCallers) {
88 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup;
89
90 DeviceOAuth2TokenService* token_service1 = NULL;
91 DeviceOAuth2TokenService* token_service2 = NULL;
92 DeviceOAuth2TokenService* token_service3 = NULL;
93 int counter = 0;
94 DeviceOAuth2TokenServiceFactory::Get(
95 base::Bind(&CopyTokenServiceAndCount, &token_service1, &counter));
96 DeviceOAuth2TokenServiceFactory::Get(
97 base::Bind(&CopyTokenServiceAndCount, &token_service2, &counter));
98 DeviceOAuth2TokenServiceFactory::Get(
99 base::Bind(&CopyTokenServiceAndCount, &token_service3, &counter));
100 // The token service will be returned asynchronously.
101 EXPECT_EQ(0, counter);
102 EXPECT_FALSE(token_service1);
103 EXPECT_FALSE(token_service2);
104 EXPECT_FALSE(token_service3);
105
106 // This lets FakeCryptohomeClient return the system salt.
107 base::RunLoop().RunUntilIdle();
108 EXPECT_EQ(3, counter);
109 EXPECT_TRUE(token_service1);
110 EXPECT_TRUE(token_service2);
111 EXPECT_TRUE(token_service3);
112
113 // Make sure that token_service1,2,3 are the same one.
114 EXPECT_EQ(token_service1, token_service2);
115 EXPECT_EQ(token_service1, token_service3);
116 }
117
118 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698