1// Copyright (c) 2012 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 <string>
6
7#include "base/bind.h"
8#include "base/message_loop/message_loop.h"
9#include "base/sys_info.h"
10#include "sync/util/get_session_name.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13#if defined(OS_CHROMEOS)
14#include "base/command_line.h"
15#include "chromeos/chromeos_switches.h"
16#endif  // OS_CHROMEOS
17
18namespace syncer {
19
20namespace {
21
22class GetSessionNameTest : public ::testing::Test {
23 public:
24  void SetSessionNameAndQuit(const std::string& session_name) {
25    session_name_ = session_name;
26    message_loop_.Quit();
27  }
28
29 protected:
30  base::MessageLoop message_loop_;
31  std::string session_name_;
32};
33
34// Call GetSessionNameSynchronouslyForTesting and make sure its return
35// value looks sane.
36TEST_F(GetSessionNameTest, GetSessionNameSynchronously) {
37  const std::string& session_name = GetSessionNameSynchronouslyForTesting();
38  EXPECT_FALSE(session_name.empty());
39}
40
41#if defined(OS_CHROMEOS)
42
43// Call GetSessionNameSynchronouslyForTesting on ChromeOS where the board type
44// is "lumpy-signed-mp-v2keys" and make sure the return value is "Chromebook".
45TEST_F(GetSessionNameTest, GetSessionNameSynchronouslyChromebook) {
46  const char* kLsbRelease = "CHROMEOS_RELEASE_BOARD=lumpy-signed-mp-v2keys\n";
47  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
48  const std::string& session_name = GetSessionNameSynchronouslyForTesting();
49  EXPECT_EQ("Chromebook", session_name);
50}
51
52// Call GetSessionNameSynchronouslyForTesting on ChromeOS where the board type
53// is "stumpy-signed-mp-v2keys" and make sure the return value is "Chromebox".
54TEST_F(GetSessionNameTest, GetSessionNameSynchronouslyChromebox) {
55  const char* kLsbRelease = "CHROMEOS_RELEASE_BOARD=stumpy-signed-mp-v2keys\n";
56  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
57  const std::string& session_name = GetSessionNameSynchronouslyForTesting();
58  EXPECT_EQ("Chromebox", session_name);
59}
60
61#endif  // OS_CHROMEOS
62
63// Calls GetSessionName and runs the message loop until it comes back
64// with a session name.  Makes sure the returned session name is equal
65// to the return value of GetSessionNameSynchronouslyForTesting().
66TEST_F(GetSessionNameTest, GetSessionName) {
67  GetSessionName(message_loop_.message_loop_proxy(),
68                 base::Bind(&GetSessionNameTest::SetSessionNameAndQuit,
69                            base::Unretained(this)));
70  message_loop_.Run();
71  EXPECT_EQ(session_name_, GetSessionNameSynchronouslyForTesting());
72}
73
74}  // namespace
75
76}  // namespace syncer
77