OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 file_thread_(BrowserThread::FILE, &loop_) { | 42 file_thread_(BrowserThread::FILE, &loop_) { |
43 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); | 43 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); |
44 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); | 44 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); |
45 fetcher_->StartFetching(); | 45 fetcher_->StartFetching(); |
46 } | 46 } |
47 | 47 |
48 virtual void TearDown() { | 48 virtual void TearDown() { |
49 loop_.RunAllPending(); | 49 loop_.RunAllPending(); |
50 } | 50 } |
51 | 51 |
52 void SimulateSuccessfulLoginAndRunPending() { | 52 void SimulateSuccessfulLoginAndRunPending(const std::string& username) { |
53 loop_.RunAllPending(); | 53 loop_.RunAllPending(); |
54 SimulateSuccessfulLogin(); | 54 SimulateSuccessfulLogin(fetcher_.get(), username); |
55 loop_.RunAllPending(); | 55 loop_.RunAllPending(); |
56 } | 56 } |
57 | 57 |
58 DeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { | 58 DeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { |
59 backend_.reset(new MockDeviceManagementBackend()); | 59 backend_.reset(new MockDeviceManagementBackend()); |
60 return new DeviceTokenFetcher( | 60 return new DeviceTokenFetcher( |
61 backend_.get(), | 61 backend_.get(), |
62 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); | 62 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); |
63 } | 63 } |
64 | 64 |
65 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, | 65 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, |
66 FilePath* path) { | 66 FilePath* path) { |
67 fetcher->GetDeviceTokenPath(path); | 67 fetcher->GetDeviceTokenPath(path); |
68 } | 68 } |
69 | 69 |
70 MessageLoop loop_; | 70 MessageLoop loop_; |
71 scoped_ptr<MockDeviceManagementBackend> backend_; | 71 scoped_ptr<MockDeviceManagementBackend> backend_; |
72 ScopedTempDir temp_user_data_dir_; | 72 ScopedTempDir temp_user_data_dir_; |
73 scoped_refptr<DeviceTokenFetcher> fetcher_; | 73 scoped_refptr<DeviceTokenFetcher> fetcher_; |
74 | 74 |
75 private: | 75 private: |
76 BrowserThread ui_thread_; | 76 BrowserThread ui_thread_; |
77 BrowserThread file_thread_; | 77 BrowserThread file_thread_; |
78 }; | 78 }; |
79 | 79 |
80 TEST_F(DeviceTokenFetcherTest, IsPending) { | 80 TEST_F(DeviceTokenFetcherTest, IsPending) { |
81 ASSERT_TRUE(fetcher_->IsTokenPending()); | 81 ASSERT_TRUE(fetcher_->IsTokenPending()); |
82 backend_->AllShouldSucceed(); | 82 backend_->AllShouldSucceed(); |
83 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 83 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
84 SimulateSuccessfulLoginAndRunPending(); | 84 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
85 ASSERT_FALSE(fetcher_->IsTokenPending()); | 85 ASSERT_FALSE(fetcher_->IsTokenPending()); |
86 } | 86 } |
87 | 87 |
88 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { | 88 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { |
89 backend_->AllShouldSucceed(); | 89 backend_->AllShouldSucceed(); |
90 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 90 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
91 SimulateSuccessfulLoginAndRunPending(); | 91 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
92 ASSERT_FALSE(fetcher_->IsTokenPending()); | 92 ASSERT_FALSE(fetcher_->IsTokenPending()); |
93 ASSERT_TRUE(fetcher_->IsTokenValid()); | 93 ASSERT_TRUE(fetcher_->IsTokenValid()); |
94 const std::string token(fetcher_->GetDeviceToken()); | 94 const std::string token(fetcher_->GetDeviceToken()); |
95 EXPECT_NE("", token); | 95 EXPECT_NE("", token); |
96 } | 96 } |
97 | 97 |
98 TEST_F(DeviceTokenFetcherTest, SimpleFetchDoubleLogin) { | 98 TEST_F(DeviceTokenFetcherTest, SimpleFetchDoubleLogin) { |
99 backend_->AllShouldSucceed(); | 99 backend_->AllShouldSucceed(); |
100 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 100 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
101 SimulateSuccessfulLoginAndRunPending(); | 101 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
102 ASSERT_FALSE(fetcher_->IsTokenPending()); | 102 ASSERT_FALSE(fetcher_->IsTokenPending()); |
103 const std::string token(fetcher_->GetDeviceToken()); | 103 const std::string token(fetcher_->GetDeviceToken()); |
104 EXPECT_NE("", token); | 104 EXPECT_NE("", token); |
105 | 105 |
106 SimulateSuccessfulLoginAndRunPending(); | 106 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
107 ASSERT_FALSE(fetcher_->IsTokenPending()); | 107 ASSERT_FALSE(fetcher_->IsTokenPending()); |
108 const std::string token2(fetcher_->GetDeviceToken()); | 108 const std::string token2(fetcher_->GetDeviceToken()); |
109 EXPECT_NE("", token2); | 109 EXPECT_NE("", token2); |
110 EXPECT_EQ(token, token2); | 110 EXPECT_EQ(token, token2); |
111 } | 111 } |
112 | 112 |
113 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { | 113 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { |
114 NotificationRegistrar registrar; | 114 NotificationRegistrar registrar; |
115 MockTokenAvailableObserver observer; | 115 MockTokenAvailableObserver observer; |
116 registrar.Add(&observer, | 116 registrar.Add(&observer, |
117 NotificationType::DEVICE_TOKEN_AVAILABLE, | 117 NotificationType::DEVICE_TOKEN_AVAILABLE, |
118 NotificationService::AllSources()); | 118 NotificationService::AllSources()); |
119 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); | 119 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); |
120 backend_->AllShouldSucceed(); | 120 backend_->AllShouldSucceed(); |
121 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 121 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
122 SimulateSuccessfulLoginAndRunPending(); | 122 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
123 ASSERT_FALSE(fetcher_->IsTokenPending()); | 123 ASSERT_FALSE(fetcher_->IsTokenPending()); |
124 const std::string token(fetcher_->GetDeviceToken()); | 124 const std::string token(fetcher_->GetDeviceToken()); |
125 EXPECT_NE("", token); | 125 EXPECT_NE("", token); |
126 Mock::VerifyAndClearExpectations(&observer); | 126 Mock::VerifyAndClearExpectations(&observer); |
127 | 127 |
128 // Swap out the fetchers, including copying the device management token on | 128 // Swap out the fetchers, including copying the device management token on |
129 // disk to where the new fetcher expects it. | 129 // disk to where the new fetcher expects it. |
130 fetcher_ = NewTestFetcher( | 130 fetcher_ = NewTestFetcher( |
131 temp_user_data_dir_.path()); | 131 temp_user_data_dir_.path()); |
132 fetcher_->StartFetching(); | 132 fetcher_->StartFetching(); |
133 ASSERT_TRUE(fetcher_->IsTokenPending()); | 133 ASSERT_TRUE(fetcher_->IsTokenPending()); |
134 loop_.RunAllPending(); | 134 loop_.RunAllPending(); |
135 ASSERT_FALSE(fetcher_->IsTokenPending()); | 135 ASSERT_FALSE(fetcher_->IsTokenPending()); |
136 const std::string token2(fetcher_->GetDeviceToken()); | 136 const std::string token2(fetcher_->GetDeviceToken()); |
137 EXPECT_NE("", token2); | 137 EXPECT_NE("", token2); |
138 EXPECT_EQ(token, token2); | 138 EXPECT_EQ(token, token2); |
139 } | 139 } |
140 | 140 |
141 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { | 141 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { |
142 backend_->AllShouldFail(); | 142 backend_->AllShouldFail(); |
143 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 143 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
144 SimulateSuccessfulLoginAndRunPending(); | 144 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
145 ASSERT_FALSE(fetcher_->IsTokenPending()); | 145 ASSERT_FALSE(fetcher_->IsTokenPending()); |
146 const std::string token(fetcher_->GetDeviceToken()); | 146 const std::string token(fetcher_->GetDeviceToken()); |
147 EXPECT_EQ("", token); | 147 EXPECT_EQ("", token); |
148 } | 148 } |
149 | 149 |
| 150 TEST_F(DeviceTokenFetcherTest, FetchBetweenTokenNotifyAndLoginNotify) { |
| 151 backend_->AllShouldSucceed(); |
| 152 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(0); |
| 153 SimulateSuccessfulLoginAndRunPending("___@gmail.com"); |
| 154 ASSERT_TRUE(fetcher_->IsTokenPending()); |
| 155 ASSERT_FALSE(fetcher_->IsTokenValid()); |
| 156 } |
| 157 |
| 158 TEST_F(DeviceTokenFetcherTest, FetchWithNonDasherUsername) { |
| 159 backend_->AllShouldSucceed(); |
| 160 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(0); |
| 161 loop_.RunAllPending(); |
| 162 SimulateTokenAvailable(); // Token is available but user name is not. |
| 163 loop_.RunAllPending(); |
| 164 ASSERT_TRUE(fetcher_->IsTokenPending()); |
| 165 ASSERT_FALSE(fetcher_->IsTokenValid()); |
| 166 } |
| 167 |
150 } // namespace policy | 168 } // namespace policy |
OLD | NEW |