Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / media_picker / desktop_media_picker_controller_unittest.mm
blob7c8ecb643e4af8e7848f9efa174c48d909340c21
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.
5 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.h"
7 #include "base/bind.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/media/desktop_media_list_observer.h"
11 #include "chrome/browser/media/fake_desktop_media_list.h"
12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest_mac.h"
16 @interface DesktopMediaPickerController (ExposedForTesting)
17 - (IKImageBrowserView*)sourceBrowser;
18 - (NSButton*)shareButton;
19 - (NSArray*)items;
20 @end
22 @implementation DesktopMediaPickerController (ExposedForTesting)
23 - (IKImageBrowserView*)sourceBrowser {
24   return sourceBrowser_;
27 - (NSButton*)shareButton {
28   return shareButton_;
31 - (NSButton*)cancelButton {
32   return cancelButton_;
35 - (NSArray*)items {
36   return items_;
38 @end
40 class DesktopMediaPickerControllerTest : public CocoaTest {
41  public:
42   DesktopMediaPickerControllerTest()
43       : callback_called_(false), media_list_(NULL) {
44   }
46   void SetUp() override {
47     CocoaTest::SetUp();
49     media_list_ = new FakeDesktopMediaList();
51     DesktopMediaPicker::DoneCallback callback =
52         base::Bind(&DesktopMediaPickerControllerTest::OnResult,
53                    base::Unretained(this));
55     controller_.reset(
56         [[DesktopMediaPickerController alloc]
57             initWithMediaList:scoped_ptr<DesktopMediaList>(media_list_)
58                        parent:nil
59                      callback:callback
60                       appName:base::ASCIIToUTF16("Screenshare Test")
61                    targetName:base::ASCIIToUTF16("https://foo.com")]);
62   }
64   void TearDown() override {
65     controller_.reset();
66     CocoaTest::TearDown();
67   }
69   bool WaitForCallback() {
70     if (!callback_called_) {
71       base::RunLoop().RunUntilIdle();
72     }
73     return callback_called_;
74   }
76  protected:
77   void OnResult(content::DesktopMediaID source) {
78     EXPECT_FALSE(callback_called_);
79     callback_called_ = true;
80     source_reported_ = source;
81   }
83   content::TestBrowserThreadBundle thread_bundle_;
84   bool callback_called_;
85   content::DesktopMediaID source_reported_;
86   FakeDesktopMediaList* media_list_;
87   base::scoped_nsobject<DesktopMediaPickerController> controller_;
90 TEST_F(DesktopMediaPickerControllerTest, ShowAndDismiss) {
91   [controller_ showWindow:nil];
93   media_list_->AddSource(0);
94   media_list_->AddSource(1);
95   media_list_->SetSourceThumbnail(1);
97   NSArray* items = [controller_ items];
98   EXPECT_EQ(2U, [items count]);
99   EXPECT_NSEQ(@"0", [[items objectAtIndex:0] imageTitle]);
100   EXPECT_EQ(nil, [[items objectAtIndex:0] imageRepresentation]);
101   EXPECT_NSEQ(@"1", [[items objectAtIndex:1] imageTitle]);
102   EXPECT_TRUE([[items objectAtIndex:1] imageRepresentation] != nil);
105 TEST_F(DesktopMediaPickerControllerTest, ClickShare) {
106   [controller_ showWindow:nil];
108   media_list_->AddSource(0);
109   media_list_->SetSourceThumbnail(0);
110   media_list_->AddSource(1);
111   media_list_->SetSourceThumbnail(1);
113   EXPECT_EQ(2U, [[controller_ items] count]);
114   EXPECT_FALSE([[controller_ shareButton] isEnabled]);
116   NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:1];
117   [[controller_ sourceBrowser] setSelectionIndexes:indexSet
118                               byExtendingSelection:NO];
119   EXPECT_TRUE([[controller_ shareButton] isEnabled]);
121   [[controller_ shareButton] performClick:nil];
122   EXPECT_TRUE(WaitForCallback());
123   EXPECT_EQ(media_list_->GetSource(1).id, source_reported_);
126 TEST_F(DesktopMediaPickerControllerTest, ClickCancel) {
127   [controller_ showWindow:nil];
129   media_list_->AddSource(0);
130   media_list_->SetSourceThumbnail(0);
131   media_list_->AddSource(1);
132   media_list_->SetSourceThumbnail(1);
134   [[controller_ cancelButton] performClick:nil];
135   EXPECT_TRUE(WaitForCallback());
136   EXPECT_EQ(content::DesktopMediaID(), source_reported_);
139 TEST_F(DesktopMediaPickerControllerTest, CloseWindow) {
140   [controller_ showWindow:nil];
142   media_list_->AddSource(0);
143   media_list_->SetSourceThumbnail(0);
144   media_list_->AddSource(1);
145   media_list_->SetSourceThumbnail(1);
147   [controller_ close];
148   EXPECT_TRUE(WaitForCallback());
149   EXPECT_EQ(content::DesktopMediaID(), source_reported_);
152 TEST_F(DesktopMediaPickerControllerTest, UpdateThumbnail) {
153   [controller_ showWindow:nil];
155   media_list_->AddSource(0);
156   media_list_->SetSourceThumbnail(0);
157   media_list_->AddSource(1);
158   media_list_->SetSourceThumbnail(1);
160   NSArray* items = [controller_ items];
161   EXPECT_EQ(2U, [items count]);
162   NSUInteger version = [[items objectAtIndex:0] imageVersion];
164   media_list_->SetSourceThumbnail(0);
165   EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]);
168 TEST_F(DesktopMediaPickerControllerTest, UpdateName) {
169   [controller_ showWindow:nil];
171   media_list_->AddSource(0);
172   media_list_->SetSourceThumbnail(0);
173   media_list_->AddSource(1);
174   media_list_->SetSourceThumbnail(1);
176   NSArray* items = [controller_ items];
177   EXPECT_EQ(2U, [items count]);
178   NSUInteger version = [[items objectAtIndex:0] imageVersion];
180   media_list_->SetSourceThumbnail(0);
181   EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]);
184 TEST_F(DesktopMediaPickerControllerTest, RemoveSource) {
185   [controller_ showWindow:nil];
187   media_list_->AddSource(0);
188   media_list_->AddSource(1);
189   media_list_->AddSource(2);
190   media_list_->SetSourceName(1, base::ASCIIToUTF16("foo"));
192   NSArray* items = [controller_ items];
193   EXPECT_EQ(3U, [items count]);
194   EXPECT_NSEQ(@"foo", [[items objectAtIndex:1] imageTitle]);
197 TEST_F(DesktopMediaPickerControllerTest, MoveSource) {
198   [controller_ showWindow:nil];
200   media_list_->AddSource(0);
201   media_list_->AddSource(1);
202   media_list_->SetSourceName(1, base::ASCIIToUTF16("foo"));
203   NSArray* items = [controller_ items];
204   EXPECT_NSEQ(@"foo", [[items objectAtIndex:1] imageTitle]);
206   media_list_->MoveSource(1, 0);
207   EXPECT_NSEQ(@"foo", [[items objectAtIndex:0] imageTitle]);
209   media_list_->MoveSource(0, 1);
210   EXPECT_NSEQ(@"foo", [[items objectAtIndex:1] imageTitle]);