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

Side by Side Diff: chrome/browser/geolocation/access_token_store_browsertest.cc

Issue 2129313002: Geolocation cleanup: corrects uses of content::AccessTokenStore* and net::URLRequestContextGetter* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: wez@ second round of comments, with a minor rebase Created 4 years, 5 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h" 6 #include "base/bind_helpers.h"
7 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/geolocation/chrome_access_token_store.h" 9 #include "chrome/browser/geolocation/chrome_access_token_store.h"
10 #include "chrome/test/base/in_process_browser_test.h" 10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "content/public/browser/access_token_store.h" 11 #include "content/public/browser/access_token_store.h"
12 #include "content/public/test/test_browser_thread.h" 12 #include "content/public/test/test_browser_thread.h"
13 #include "content/public/test/test_utils.h" 13 #include "content/public/test/test_utils.h"
14 14
15 using content::AccessTokenStore; 15 using content::AccessTokenStore;
16 using content::BrowserThread; 16 using content::BrowserThread;
17 17
18 namespace { 18 namespace {
19 19
20 // The token store factory implementation expects to be used from any well-known 20 // The token store factory implementation expects to be used from any well-known
21 // chrome thread other than UI. We could use any arbitrary thread; IO is 21 // chrome thread other than UI. We could use any arbitrary thread; IO is
22 // a good choice as this is the expected usage. 22 // a good choice as this is the expected usage.
23 const BrowserThread::ID kExpectedClientThreadId = BrowserThread::IO; 23 const BrowserThread::ID kExpectedClientThreadId = BrowserThread::IO;
24 const char* kRefServerUrl1 = "https://test.domain.example/foo?id=bar.bar"; 24 const char* kRefServerUrl1 = "https://test.domain.example/foo?id=bar.bar";
25 const char* kRefServerUrl2 = "http://another.domain.example/foo?id=bar.bar#2"; 25 const char* kRefServerUrl2 = "http://another.domain.example/foo?id=bar.bar#2";
26 const char* kOldDefaultNetworkProviderUrl = "https://www.google.com/loc/json"; 26 const char* kOldDefaultNetworkProviderUrl = "https://www.google.com/loc/json";
27 27
28 class GeolocationAccessTokenStoreTest 28 class GeolocationAccessTokenStoreTest : public InProcessBrowserTest {
29 : public InProcessBrowserTest {
30 public: 29 public:
31 GeolocationAccessTokenStoreTest() 30 GeolocationAccessTokenStoreTest()
32 : token_to_expect_(NULL), token_to_set_(NULL) {} 31 : token_to_expect_(nullptr), token_to_set_(nullptr) {}
33 32
34 void DoTestStepAndWaitForResults( 33 void DoTestStepAndWaitForResults(
35 const char* ref_url, const base::string16* token_to_expect, 34 const char* ref_url, const base::string16* token_to_expect,
36 const base::string16* token_to_set); 35 const base::string16* token_to_set);
37 36
38 void OnAccessTokenStoresLoaded( 37 void OnAccessTokenStoresLoaded(
39 AccessTokenStore::AccessTokenMap access_token_map, 38 AccessTokenStore::AccessTokenMap access_token_map,
40 net::URLRequestContextGetter* context_getter); 39 const scoped_refptr<net::URLRequestContextGetter>& context_getter);
41 40
42 scoped_refptr<AccessTokenStore> token_store_; 41 scoped_refptr<AccessTokenStore> token_store_;
43 GURL ref_url_; 42 GURL ref_url_;
44 const base::string16* token_to_expect_; 43 const base::string16* token_to_expect_;
45 const base::string16* token_to_set_; 44 const base::string16* token_to_set_;
46 }; 45 };
47 46
48 void StartTestStepFromClientThread( 47 void StartTestStepFromClientThread(
49 scoped_refptr<AccessTokenStore>* store, 48 scoped_refptr<AccessTokenStore> store,
Wez 2016/07/18 17:50:53 Now that this isn't a ptr or reference, you're not
mcasas 2016/07/18 21:48:52 Interestingly, the test still works. Reverted.
Wez 2016/07/18 22:49:18 It should still say if (!*store), I think. Lookin
mcasas 2016/07/19 01:18:18 Done.
50 const AccessTokenStore::LoadAccessTokensCallback& callback) { 49 const AccessTokenStore::LoadAccessTokensCallback& callback) {
51 ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)); 50 ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId));
52 if (store->get() == NULL) 51 if (!store)
53 (*store) = new ChromeAccessTokenStore(); 52 store = new ChromeAccessTokenStore();
54 (*store)->LoadAccessTokens(callback); 53 store->LoadAccessTokens(callback);
55 } 54 }
56 55
57 struct TokenLoadClientForTest { 56 struct TokenLoadClientForTest {
58 void NotReachedCallback(AccessTokenStore::AccessTokenMap /*tokens*/, 57 void NotReachedCallback(AccessTokenStore::AccessTokenMap /*tokens*/,
59 net::URLRequestContextGetter* /*context_getter*/) { 58 net::URLRequestContextGetter* /*context_getter*/) {
60 NOTREACHED() << "This request should have been canceled before callback"; 59 NOTREACHED() << "This request should have been canceled before callback";
61 } 60 }
62 }; 61 };
63 62
64 void GeolocationAccessTokenStoreTest::DoTestStepAndWaitForResults( 63 void GeolocationAccessTokenStoreTest::DoTestStepAndWaitForResults(
65 const char* ref_url, const base::string16* token_to_expect, 64 const char* ref_url, const base::string16* token_to_expect,
66 const base::string16* token_to_set) { 65 const base::string16* token_to_set) {
67 ref_url_ = GURL(ref_url); 66 ref_url_ = GURL(ref_url);
68 token_to_expect_ = token_to_expect; 67 token_to_expect_ = token_to_expect;
69 token_to_set_ = token_to_set; 68 token_to_set_ = token_to_set;
70 69
71 BrowserThread::PostTask( 70 BrowserThread::PostTask(
72 kExpectedClientThreadId, FROM_HERE, 71 kExpectedClientThreadId, FROM_HERE,
73 base::Bind( 72 base::Bind(
74 &StartTestStepFromClientThread, 73 &StartTestStepFromClientThread,
75 &token_store_, 74 token_store_,
76 base::Bind( 75 base::Bind(
77 &GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded, 76 &GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded,
78 base::Unretained(this)))); 77 base::Unretained(this))));
79 content::RunMessageLoop(); 78 content::RunMessageLoop();
80 } 79 }
81 80
82 void GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded( 81 void GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded(
83 AccessTokenStore::AccessTokenMap access_token_map, 82 AccessTokenStore::AccessTokenMap access_token_map,
84 net::URLRequestContextGetter* context_getter) { 83 const scoped_refptr<net::URLRequestContextGetter>& context_getter) {
85 ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)) 84 ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId))
86 << "Callback from token factory should be from the same thread as the " 85 << "Callback from token factory should be from the same thread as the "
87 "LoadAccessTokenStores request was made on"; 86 "LoadAccessTokenStores request was made on";
88 DCHECK(context_getter); 87 DCHECK(context_getter);
89 AccessTokenStore::AccessTokenMap::const_iterator item = 88 AccessTokenStore::AccessTokenMap::const_iterator item =
90 access_token_map.find(ref_url_); 89 access_token_map.find(ref_url_);
91 if (!token_to_expect_) { 90 if (!token_to_expect_) {
92 EXPECT_TRUE(item == access_token_map.end()); 91 EXPECT_TRUE(item == access_token_map.end());
93 } else { 92 } else {
94 EXPECT_FALSE(item == access_token_map.end()); 93 EXPECT_FALSE(item == access_token_map.end());
95 EXPECT_EQ(*token_to_expect_, item->second); 94 EXPECT_EQ(*token_to_expect_, item->second);
96 } 95 }
97 96
98 if (token_to_set_) { 97 if (token_to_set_) {
99 scoped_refptr<AccessTokenStore> store(new ChromeAccessTokenStore()); 98 scoped_refptr<AccessTokenStore> store(new ChromeAccessTokenStore());
100 store->SaveAccessToken(ref_url_, *token_to_set_); 99 store->SaveAccessToken(ref_url_, *token_to_set_);
101 } 100 }
102 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 101 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
103 base::MessageLoop::QuitWhenIdleClosure()); 102 base::MessageLoop::QuitWhenIdleClosure());
104 } 103 }
105 104
106 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, SetAcrossInstances) { 105 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, SetAcrossInstances) {
107 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); 106 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*(");
108 const base::string16 ref_token2 = 107 const base::string16 ref_token2 =
109 base::ASCIIToUTF16("\1\2\3\4\5\6\7\10\11\12=023"); 108 base::ASCIIToUTF16("\1\2\3\4\5\6\7\10\11\12=023");
110 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); 109 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
111 110
112 DoTestStepAndWaitForResults(kRefServerUrl1, NULL, &ref_token1); 111 DoTestStepAndWaitForResults(kRefServerUrl1, nullptr, &ref_token1);
113 // Check it was set, and change to new value. 112 // Check it was set, and change to new value.
114 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, &ref_token2); 113 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, &ref_token2);
115 // And change back. 114 // And change back.
116 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token2, &ref_token1); 115 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token2, &ref_token1);
117 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, NULL); 116 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, nullptr);
118 117
119 // Set a second server URL 118 // Set a second server URL
120 DoTestStepAndWaitForResults(kRefServerUrl2, NULL, &ref_token2); 119 DoTestStepAndWaitForResults(kRefServerUrl2, nullptr, &ref_token2);
121 DoTestStepAndWaitForResults(kRefServerUrl2, &ref_token2, NULL); 120 DoTestStepAndWaitForResults(kRefServerUrl2, &ref_token2, nullptr);
122 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, NULL); 121 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, nullptr);
123 } 122 }
124 123
125 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, OldUrlRemoval) { 124 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, OldUrlRemoval) {
126 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); 125 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*(");
127 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); 126 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
128 127
129 // Set a token for the old default network provider url. 128 // Set a token for the old default network provider url.
130 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, 129 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl,
131 NULL, &ref_token1); 130 nullptr, &ref_token1);
132 // Check that the token related to the old default network provider url 131 // Check that the token related to the old default network provider url
133 // was deleted. 132 // was deleted.
134 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, 133 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl,
135 NULL, NULL); 134 nullptr, nullptr);
136 } 135 }
137 136
138 } // namespace 137 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698