diff options
author | aousterh@chromium.org <aousterh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-04 17:09:37 +0000 |
---|---|---|
committer | aousterh@chromium.org <aousterh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-04 17:09:37 +0000 |
commit | 5530b08ab5af83b54c73dff9b03d3300595b0525 (patch) | |
tree | d7df72ce6de3bae64bd67b0f18264557c9bbb0d1 | |
parent | b07117e22c71b6d3f359988cf4440ce3f830f2b9 (diff) | |
download | chromium_src-5530b08ab5af83b54c73dff9b03d3300595b0525.zip chromium_src-5530b08ab5af83b54c73dff9b03d3300595b0525.tar.gz chromium_src-5530b08ab5af83b54c73dff9b03d3300595b0525.tar.bz2 |
Device Orientation Cleanup
Makes instance variables in Orientation private. Removes a constructor with
many parameters and adds setters and getters.
BUG=none
TEST=content_unittests:DeviceOrientationProviderTest, browser_tests:DeviceOrientationBrowserTest.BasicTest
Review URL: https://chromiumcodereview.appspot.com/10689039
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@145472 0039d316-1c4b-4281-b951-d872f2087c98
7 files changed, 203 insertions, 123 deletions
diff --git a/content/browser/device_orientation/accelerometer_mac.cc b/content/browser/device_orientation/accelerometer_mac.cc index d47f2dc..bdc4278 100644 --- a/content/browser/device_orientation/accelerometer_mac.cc +++ b/content/browser/device_orientation/accelerometer_mac.cc @@ -63,32 +63,27 @@ bool AccelerometerMac::GetOrientation(Orientation* orientation) { // const double kRad2deg = 180.0 / M_PI; - orientation->alpha_ = 0.0; - orientation->beta_ = kRad2deg * atan2(-axis_value[1], axis_value[2]); - orientation->gamma_ = kRad2deg * asin(axis_value[0]); - orientation->absolute_ = false; + orientation->set_beta(kRad2deg * atan2(-axis_value[1], axis_value[2])); + orientation->set_gamma(kRad2deg * asin(axis_value[0])); + // TODO(aousterh): should absolute_ be set to false here? + // See crbug.com/136010. // Make sure that the interval boundaries comply with the specification. At // this point, beta is [-180, 180] and gamma is [-90, 90], but the spec has // the upper bound open on both. - if (orientation->beta_ == 180.0) { - orientation->beta_ = -180.0; // -180 == 180 (upside-down) + if (orientation->beta() == 180.0) { + orientation->set_beta(-180.0); // -180 == 180 (upside-down) } - if (orientation->gamma_ == 90.0) { + if (orientation->gamma() == 90.0) { static double just_less_than_90 = nextafter(90, 0); - orientation->gamma_ = just_less_than_90; + orientation->set_gamma(just_less_than_90); } // At this point, DCHECKing is paranoia. Never hurts. - DCHECK_GE(orientation->beta_, -180.0); - DCHECK_LT(orientation->beta_, 180.0); - DCHECK_GE(orientation->gamma_, -90.0); - DCHECK_LT(orientation->gamma_, 90.0); - - orientation->can_provide_alpha_ = false; - orientation->can_provide_beta_ = true; - orientation->can_provide_gamma_ = true; - orientation->can_provide_absolute_ = false; + DCHECK_GE(orientation->beta(), -180.0); + DCHECK_LT(orientation->beta(), 180.0); + DCHECK_GE(orientation->gamma(), -90.0); + DCHECK_LT(orientation->gamma(), 90.0); return true; } diff --git a/content/browser/device_orientation/data_fetcher_impl_android.cc b/content/browser/device_orientation/data_fetcher_impl_android.cc index 6f54192..0354635 100644 --- a/content/browser/device_orientation/data_fetcher_impl_android.cc +++ b/content/browser/device_orientation/data_fetcher_impl_android.cc @@ -70,8 +70,13 @@ bool DataFetcherImplAndroid::GetOrientation(Orientation* orientation) { void DataFetcherImplAndroid::GotOrientation( JNIEnv*, jobject, double alpha, double beta, double gamma) { base::AutoLock autolock(next_orientation_lock_); - next_orientation_.reset( - new Orientation(true, alpha, true, beta, true, gamma, true, true)); + + Orientation* orientation = new Orientation(); + orientation->set_alpha(alpha); + orientation->set_beta(beta); + orientation->set_gamma(gamma); + orientation->set_absolute(true); + next_orientation_.reset(orientation); } bool DataFetcherImplAndroid::Start(int rate_in_milliseconds) { diff --git a/content/browser/device_orientation/device_orientation_browsertest.cc b/content/browser/device_orientation/device_orientation_browsertest.cc index 9459ef9..346d5920 100644 --- a/content/browser/device_orientation/device_orientation_browsertest.cc +++ b/content/browser/device_orientation/device_orientation_browsertest.cc @@ -54,8 +54,12 @@ class DeviceOrientationBrowserTest : public InProcessBrowserTest { // crbug.com/113952 IN_PROC_BROWSER_TEST_F(DeviceOrientationBrowserTest, BasicTest) { - const Orientation kTestOrientation(true, 1, true, 2, true, 3, true, true); - scoped_refptr<MockProvider> provider(new MockProvider(kTestOrientation)); + Orientation test_orientation; + test_orientation.set_alpha(1); + test_orientation.set_beta(2); + test_orientation.set_gamma(3); + test_orientation.set_absolute(true); + scoped_refptr<MockProvider> provider(new MockProvider(test_orientation)); Provider::SetInstanceForTests(provider.get()); // The test page will register an event handler for orientation events, diff --git a/content/browser/device_orientation/orientation.h b/content/browser/device_orientation/orientation.h index 023780f..f4679b2 100644 --- a/content/browser/device_orientation/orientation.h +++ b/content/browser/device_orientation/orientation.h @@ -14,38 +14,69 @@ class Orientation { // can_provide_{alpha,beta,gamma,absolute} is true if data can be provided // for that variable. - Orientation(bool can_provide_alpha, double alpha, - bool can_provide_beta, double beta, - bool can_provide_gamma, double gamma, - bool can_provide_absolute, bool absolute) - : alpha_(alpha), - beta_(beta), - gamma_(gamma), - absolute_(absolute), - can_provide_alpha_(can_provide_alpha), - can_provide_beta_(can_provide_beta), - can_provide_gamma_(can_provide_gamma), - can_provide_absolute_(can_provide_absolute) { - } - Orientation() - : alpha_(0), - beta_(0), - gamma_(0), - absolute_(false), - can_provide_alpha_(false), + : can_provide_alpha_(false), can_provide_beta_(false), can_provide_gamma_(false), can_provide_absolute_(false) { } + Orientation(const Orientation& orientation) + : alpha_(orientation.alpha()), + beta_(orientation.beta()), + gamma_(orientation.gamma()), + absolute_(orientation.absolute()), + can_provide_alpha_(orientation.can_provide_alpha()), + can_provide_beta_(orientation.can_provide_beta()), + can_provide_gamma_(orientation.can_provide_gamma()), + can_provide_absolute_(orientation.can_provide_absolute()) { + } + void operator=(const Orientation& source) { + alpha_ = source.alpha(); + beta_ = source.beta(); + gamma_ = source.gamma(); + absolute_ = source.absolute(); + can_provide_alpha_ = source.can_provide_alpha(); + can_provide_beta_ = source.can_provide_beta(); + can_provide_gamma_ = source.can_provide_gamma(); + can_provide_absolute_ = source.can_provide_absolute(); + } static Orientation Empty() { return Orientation(); } - bool IsEmpty() const { + bool is_empty() const { return !can_provide_alpha_ && !can_provide_beta_ && !can_provide_gamma_ && !can_provide_absolute_; } + void set_alpha(double alpha) { + can_provide_alpha_ = true; + alpha_ = alpha; + } + bool can_provide_alpha() const { return can_provide_alpha_; } + double alpha() const { return alpha_; } + + void set_beta(double beta) { + can_provide_beta_ = true; + beta_ = beta; + } + bool can_provide_beta() const { return can_provide_beta_; } + double beta() const { return beta_; } + + void set_gamma(double gamma) { + can_provide_gamma_ = true; + gamma_ = gamma; + } + bool can_provide_gamma() const { return can_provide_gamma_; } + double gamma() const { return gamma_; } + + void set_absolute(bool absolute) { + can_provide_absolute_ = true; + absolute_ = absolute; + } + bool can_provide_absolute() const { return can_provide_absolute_; } + bool absolute() const { return absolute_; } + + private: double alpha_; double beta_; double gamma_; diff --git a/content/browser/device_orientation/orientation_message_filter.cc b/content/browser/device_orientation/orientation_message_filter.cc index 254f48c..11b1e06a 100644 --- a/content/browser/device_orientation/orientation_message_filter.cc +++ b/content/browser/device_orientation/orientation_message_filter.cc @@ -61,14 +61,14 @@ OrientationMessageFilter::ObserverDelegate::~ObserverDelegate() { void OrientationMessageFilter::ObserverDelegate::OnOrientationUpdate( const Orientation& orientation) { DeviceOrientationMsg_Updated_Params params; - params.can_provide_alpha = orientation.can_provide_alpha_; - params.alpha = orientation.alpha_; - params.can_provide_beta = orientation.can_provide_beta_; - params.beta = orientation.beta_; - params.can_provide_gamma = orientation.can_provide_gamma_; - params.gamma = orientation.gamma_; - params.can_provide_absolute = orientation.can_provide_absolute_; - params.absolute = orientation.absolute_; + params.can_provide_alpha = orientation.can_provide_alpha(); + params.alpha = orientation.alpha(); + params.can_provide_beta = orientation.can_provide_beta(); + params.beta = orientation.beta(); + params.can_provide_gamma = orientation.can_provide_gamma(); + params.gamma = orientation.gamma(); + params.can_provide_absolute = orientation.can_provide_absolute(); + params.absolute = orientation.absolute(); sender_->Send(new DeviceOrientationMsg_Updated(render_view_id_, params)); } diff --git a/content/browser/device_orientation/provider_impl.cc b/content/browser/device_orientation/provider_impl.cc index b27421c..fab78a1 100644 --- a/content/browser/device_orientation/provider_impl.cc +++ b/content/browser/device_orientation/provider_impl.cc @@ -83,7 +83,7 @@ void ProviderImpl::DoInitializePollingThread( last_orientation_ = orientation; // Notify observers. - if (!orientation.IsEmpty()) + if (!orientation.is_empty()) ScheduleDoNotify(orientation); // Start polling. @@ -115,7 +115,7 @@ void ProviderImpl::DoNotify(const Orientation& orientation) { for (Iterator i = observers_.begin(), e = observers_.end(); i != e; ++i) (*i)->OnOrientationUpdate(orientation); - if (orientation.IsEmpty()) { + if (orientation.is_empty()) { // Notify observers about failure to provide data exactly once. observers_.clear(); Stop(); @@ -140,7 +140,7 @@ void ProviderImpl::DoPoll() { return; } - if (!orientation.IsEmpty() && + if (!orientation.is_empty() && SignificantlyDifferent(orientation, last_orientation_)) { last_orientation_ = orientation; ScheduleDoNotify(orientation); @@ -180,20 +180,20 @@ bool IsElementSignificantlyDifferent(bool can_provide_element1, // observers should be notified of the new orientation. bool ProviderImpl::SignificantlyDifferent(const Orientation& o1, const Orientation& o2) { - return IsElementSignificantlyDifferent(o1.can_provide_alpha_, - o2.can_provide_alpha_, - o1.alpha_, - o2.alpha_) || - IsElementSignificantlyDifferent(o1.can_provide_beta_, - o2.can_provide_beta_, - o1.beta_, - o2.beta_) || - IsElementSignificantlyDifferent(o1.can_provide_gamma_, - o2.can_provide_gamma_, - o1.gamma_, - o2.gamma_) || - (o1.can_provide_absolute_ != o2.can_provide_absolute_ - || o1.absolute_ != o2.absolute_); + return IsElementSignificantlyDifferent(o1.can_provide_alpha(), + o2.can_provide_alpha(), + o1.alpha(), + o2.alpha()) || + IsElementSignificantlyDifferent(o1.can_provide_beta(), + o2.can_provide_beta(), + o1.beta(), + o2.beta()) || + IsElementSignificantlyDifferent(o1.can_provide_gamma(), + o2.can_provide_gamma(), + o1.gamma(), + o2.gamma()) || + (o1.can_provide_absolute() != o2.can_provide_absolute() || + o1.absolute() != o2.absolute()); } base::TimeDelta ProviderImpl::SamplingInterval() const { diff --git a/content/browser/device_orientation/provider_unittest.cc b/content/browser/device_orientation/provider_unittest.cc index 17d3d43..be49dfd 100644 --- a/content/browser/device_orientation/provider_unittest.cc +++ b/content/browser/device_orientation/provider_unittest.cc @@ -31,19 +31,19 @@ class UpdateChecker : public Provider::Observer { Orientation expected = expectations_queue_.front(); expectations_queue_.pop(); - EXPECT_EQ(expected.can_provide_alpha_, orientation.can_provide_alpha_); - EXPECT_EQ(expected.can_provide_beta_, orientation.can_provide_beta_); - EXPECT_EQ(expected.can_provide_gamma_, orientation.can_provide_gamma_); - EXPECT_EQ(expected.can_provide_absolute_, - orientation.can_provide_absolute_); - if (expected.can_provide_alpha_) - EXPECT_EQ(expected.alpha_, orientation.alpha_); - if (expected.can_provide_beta_) - EXPECT_EQ(expected.beta_, orientation.beta_); - if (expected.can_provide_gamma_) - EXPECT_EQ(expected.gamma_, orientation.gamma_); - if (expected.can_provide_absolute_) - EXPECT_EQ(expected.absolute_, orientation.absolute_); + EXPECT_EQ(expected.can_provide_alpha(), orientation.can_provide_alpha()); + EXPECT_EQ(expected.can_provide_beta(), orientation.can_provide_beta()); + EXPECT_EQ(expected.can_provide_gamma(), orientation.can_provide_gamma()); + EXPECT_EQ(expected.can_provide_absolute(), + orientation.can_provide_absolute()); + if (expected.can_provide_alpha()) + EXPECT_EQ(expected.alpha(), orientation.alpha()); + if (expected.can_provide_beta()) + EXPECT_EQ(expected.beta(), orientation.beta()); + if (expected.can_provide_gamma()) + EXPECT_EQ(expected.gamma(), orientation.gamma()); + if (expected.can_provide_absolute()) + EXPECT_EQ(expected.absolute(), orientation.absolute()); --(*expectations_count_ptr_); @@ -211,11 +211,15 @@ TEST_F(DeviceOrientationProviderTest, BasicPushTest) { scoped_refptr<MockOrientationFactory> orientation_factory( new MockOrientationFactory()); Init(MockOrientationFactory::CreateDataFetcher); - const Orientation kTestOrientation(true, 1, true, 2, true, 3, true, true); + Orientation test_orientation; + test_orientation.set_alpha(1); + test_orientation.set_beta(2); + test_orientation.set_gamma(3); + test_orientation.set_absolute(true); scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); - checker->AddExpectation(kTestOrientation); - orientation_factory->SetOrientation(kTestOrientation); + checker->AddExpectation(test_orientation); + orientation_factory->SetOrientation(test_orientation); provider_->AddObserver(checker.get()); MessageLoop::current()->Run(); @@ -226,10 +230,23 @@ TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { scoped_refptr<MockOrientationFactory> orientation_factory( new MockOrientationFactory()); Init(MockOrientationFactory::CreateDataFetcher); - const Orientation kTestOrientations[] = { - Orientation(true, 1, true, 2, true, 3, true, true), - Orientation(true, 4, true, 5, true, 6, true, false), - Orientation(true, 7, true, 8, true, 9, false, true)}; + + Orientation test_orientations[] = {Orientation(), Orientation(), + Orientation()}; + test_orientations[0].set_alpha(1); + test_orientations[0].set_beta(2); + test_orientations[0].set_gamma(3); + test_orientations[0].set_absolute(true); + + test_orientations[1].set_alpha(4); + test_orientations[1].set_beta(5); + test_orientations[1].set_gamma(6); + test_orientations[1].set_absolute(false); + + test_orientations[2].set_alpha(7); + test_orientations[2].set_beta(8); + test_orientations[2].set_gamma(9); + // can't provide absolute scoped_ptr<UpdateChecker> checker_a( new UpdateChecker(&pending_expectations_)); @@ -238,23 +255,23 @@ TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { scoped_ptr<UpdateChecker> checker_c( new UpdateChecker(&pending_expectations_)); - checker_a->AddExpectation(kTestOrientations[0]); - orientation_factory->SetOrientation(kTestOrientations[0]); + checker_a->AddExpectation(test_orientations[0]); + orientation_factory->SetOrientation(test_orientations[0]); provider_->AddObserver(checker_a.get()); MessageLoop::current()->Run(); - checker_a->AddExpectation(kTestOrientations[1]); - checker_b->AddExpectation(kTestOrientations[0]); - checker_b->AddExpectation(kTestOrientations[1]); - orientation_factory->SetOrientation(kTestOrientations[1]); + checker_a->AddExpectation(test_orientations[1]); + checker_b->AddExpectation(test_orientations[0]); + checker_b->AddExpectation(test_orientations[1]); + orientation_factory->SetOrientation(test_orientations[1]); provider_->AddObserver(checker_b.get()); MessageLoop::current()->Run(); provider_->RemoveObserver(checker_a.get()); - checker_b->AddExpectation(kTestOrientations[2]); - checker_c->AddExpectation(kTestOrientations[1]); - checker_c->AddExpectation(kTestOrientations[2]); - orientation_factory->SetOrientation(kTestOrientations[2]); + checker_b->AddExpectation(test_orientations[2]); + checker_c->AddExpectation(test_orientations[1]); + checker_c->AddExpectation(test_orientations[2]); + orientation_factory->SetOrientation(test_orientations[2]); provider_->AddObserver(checker_c.get()); MessageLoop::current()->Run(); @@ -273,17 +290,26 @@ TEST_F(DeviceOrientationProviderTest, MAYBE_ObserverNotRemoved) { scoped_refptr<MockOrientationFactory> orientation_factory( new MockOrientationFactory()); Init(MockOrientationFactory::CreateDataFetcher); - const Orientation kTestOrientation(true, 1, true, 2, true, 3, true, true); - const Orientation kTestOrientation2(true, 4, true, 5, true, 6, true, false); + Orientation test_orientation; + test_orientation.set_alpha(1); + test_orientation.set_beta(2); + test_orientation.set_gamma(3); + test_orientation.set_absolute(true); + + Orientation test_orientation2; + test_orientation2.set_alpha(4); + test_orientation2.set_beta(5); + test_orientation2.set_gamma(6); + test_orientation2.set_absolute(false); scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); - checker->AddExpectation(kTestOrientation); - orientation_factory->SetOrientation(kTestOrientation); + checker->AddExpectation(test_orientation); + orientation_factory->SetOrientation(test_orientation); provider_->AddObserver(checker.get()); MessageLoop::current()->Run(); - checker->AddExpectation(kTestOrientation2); - orientation_factory->SetOrientation(kTestOrientation2); + checker->AddExpectation(test_orientation2); + orientation_factory->SetOrientation(test_orientation2); MessageLoop::current()->Run(); // Note that checker is not removed. This should not be a problem. @@ -299,15 +325,19 @@ TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { scoped_refptr<MockOrientationFactory> orientation_factory( new MockOrientationFactory()); Init(MockOrientationFactory::CreateDataFetcher); - const Orientation kTestOrientation(true, 1, true, 2, true, 3, true, true); + Orientation test_orientation; + test_orientation.set_alpha(1); + test_orientation.set_beta(2); + test_orientation.set_gamma(3); + test_orientation.set_absolute(true); scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( &pending_expectations_)); scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( &pending_expectations_)); - orientation_factory->SetOrientation(kTestOrientation); - checker_a->AddExpectation(kTestOrientation); + orientation_factory->SetOrientation(test_orientation); + checker_a->AddExpectation(test_orientation); provider_->AddObserver(checker_a.get()); MessageLoop::current()->Run(); @@ -327,23 +357,33 @@ TEST_F(DeviceOrientationProviderTest, StartStopStart) { scoped_refptr<MockOrientationFactory> orientation_factory( new MockOrientationFactory()); Init(MockOrientationFactory::CreateDataFetcher); - const Orientation kTestOrientation(true, 1, true, 2, true, 3, true, true); - const Orientation kTestOrientation2(true, 4, true, 5, true, 6, true, false); + + Orientation test_orientation; + test_orientation.set_alpha(1); + test_orientation.set_beta(2); + test_orientation.set_gamma(3); + test_orientation.set_absolute(true); + + Orientation test_orientation2; + test_orientation2.set_alpha(4); + test_orientation2.set_beta(5); + test_orientation2.set_gamma(6); + test_orientation2.set_absolute(false); scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( &pending_expectations_)); scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( &pending_expectations_)); - checker_a->AddExpectation(kTestOrientation); - orientation_factory->SetOrientation(kTestOrientation); + checker_a->AddExpectation(test_orientation); + orientation_factory->SetOrientation(test_orientation); provider_->AddObserver(checker_a.get()); MessageLoop::current()->Run(); provider_->RemoveObserver(checker_a.get()); // This stops the Provider. - checker_b->AddExpectation(kTestOrientation2); - orientation_factory->SetOrientation(kTestOrientation2); + checker_b->AddExpectation(test_orientation2); + orientation_factory->SetOrientation(test_orientation2); provider_->AddObserver(checker_b.get()); MessageLoop::current()->Run(); @@ -361,18 +401,23 @@ TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) { const double kSignificantDifference = 30; const double kAlpha = 4, kBeta = 5, kGamma = 6; - const Orientation first_orientation(true, kAlpha, true, kBeta, true, kGamma, - true, true); - - const Orientation second_orientation(true, kAlpha + kInsignificantDifference, - true, kBeta + kInsignificantDifference, - true, kGamma + kInsignificantDifference, - true, false); - - const Orientation third_orientation(true, kAlpha + kSignificantDifference, - true, kBeta + kSignificantDifference, - true, kGamma + kSignificantDifference, - false, true); + Orientation first_orientation; + first_orientation.set_alpha(kAlpha); + first_orientation.set_beta(kBeta); + first_orientation.set_gamma(kGamma); + first_orientation.set_absolute(true); + + Orientation second_orientation; + second_orientation.set_alpha(kAlpha + kInsignificantDifference); + second_orientation.set_beta(kBeta + kInsignificantDifference); + second_orientation.set_gamma(kGamma + kInsignificantDifference); + second_orientation.set_absolute(false); + + Orientation third_orientation; + third_orientation.set_alpha(kAlpha + kSignificantDifference); + third_orientation.set_beta(kBeta + kSignificantDifference); + third_orientation.set_gamma(kGamma + kSignificantDifference); + // can't provide absolute scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( &pending_expectations_)); |